Skip to content

Commit a8b0ecd

Browse files
[libc] enable stack protectors and frame pointers on default (#86288)
1 parent 7de82ca commit a8b0ecd

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ function(_get_common_compile_options output_var flags)
6060
if (LIBC_CC_SUPPORTS_PATTERN_INIT)
6161
list(APPEND compile_options "-ftrivial-auto-var-init=pattern")
6262
endif()
63+
if (LIBC_CONF_KEEP_FRAME_POINTER)
64+
list(APPEND compile_options "-fno-omit-frame-pointer")
65+
if (LIBC_TARGET_ARCHITECTURE_IS_X86)
66+
list(APPEND compile_options "-mno-omit-leaf-frame-pointer")
67+
endif()
68+
endif()
69+
if (LIBC_CONF_ENABLE_STACK_PROTECTOR)
70+
list(APPEND compile_options "-fstack-protector-strong")
71+
endif()
6372
list(APPEND compile_options "-Wall")
6473
list(APPEND compile_options "-Wextra")
6574
# -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.

libc/config/config.json

+10
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,15 @@
3030
"value": false,
3131
"doc": "Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled."
3232
}
33+
},
34+
"codegen": {
35+
"LIBC_CONF_KEEP_FRAME_POINTER": {
36+
"value": true,
37+
"doc": "Keep frame pointer in functions for better debugging experience."
38+
},
39+
"LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR": {
40+
"value": true,
41+
"doc": "Enable -fstack-protector-strong to defend against stack smashing attack."
42+
}
3343
}
3444
}

libc/docs/configure.rst

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ See the main ``config/config.json``, and the platform and architecture specific
2525
overrides in ``config/<platform>/config.json`` and ``config/<platform>/<arch>/config.json,``
2626
to learn about the defaults for your platform and target.
2727

28+
* **"codegen" options**
29+
- ``LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR``: Enable -fstack-protector-strong to defend against stack smashing attack.
30+
- ``LIBC_CONF_KEEP_FRAME_POINTER``: Keep frame pointer in functions for better debugging experience.
2831
* **"printf" options**
2932
- ``LIBC_CONF_PRINTF_DISABLE_FIXED_POINT``: Disable printing fixed point values in printf and friends.
3033
- ``LIBC_CONF_PRINTF_DISABLE_FLOAT``: Disable printing floating point values in printf and friends.

utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl

+8-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def libc_function(
7878
its deps.
7979
**kwargs: Other attributes relevant for a cc_library. For example, deps.
8080
"""
81-
8281
# We use the explicit equals pattern here because append and += mutate the
8382
# original list, where this creates a new list and stores it in deps.
8483
copts = copts or []
@@ -87,7 +86,15 @@ def libc_function(
8786
"-fno-builtin",
8887
"-fno-lax-vector-conversions",
8988
"-ftrivial-auto-var-init=pattern",
89+
"-fno-omit-frame-pointer",
90+
"-fstack-protector-strong",
9091
]
92+
# x86 targets have -mno-omit-leaf-frame-pointer.
93+
platform_copts = selects.with_or({
94+
PLATFORM_CPU_X86_64: ["-mno-omit-leaf-frame-pointer"],
95+
"//conditions:default": []
96+
})
97+
copts = copts + platform_copts
9198

9299
# We compile the code twice, the first target is suffixed with ".__internal__" and contains the
93100
# C++ functions in the "LIBC_NAMESPACE" namespace. This allows us to test the function in the

0 commit comments

Comments
 (0)