Skip to content

Commit 1d5c16d

Browse files
[libc] default enable -ftrivial-auto-var-init=pattern (llvm#78776)
Usage of uninitialized memory is a top memory safety issue in C++ codebases. Help mitigate this somewhat by default initialize stack allocations to a pattern (0xAA repeating). Clang has received optimizations to sink these into control flow paths that access such values to minimize the overhead of these added initializations. If there's a measurable slowdown, we can add -ftrivial-auto-var-init-max-size=<N> for some value N bytes if we have any large stack allocations, or add attribute uninitialized to any variable declarations. Unsupported until GCC 12.1 / Clang 8. Increases file size of libc.a from a full build by +8.79Ki (+0.2%).
1 parent b83b8d3 commit 1d5c16d

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
5757
endforeach()
5858

5959
message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
60+
61+
### Compiler Feature Detection ###
62+
63+
# clang-8+, gcc-12+
64+
check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function(_get_common_compile_options output_var flags)
4141
list(APPEND compile_options "-fno-unwind-tables")
4242
list(APPEND compile_options "-fno-asynchronous-unwind-tables")
4343
list(APPEND compile_options "-fno-rtti")
44+
if (LIBC_CC_SUPPORTS_PATTERN_INIT)
45+
list(APPEND compile_options "-ftrivial-auto-var-init=pattern")
46+
endif()
4447
list(APPEND compile_options "-Wall")
4548
list(APPEND compile_options "-Wextra")
4649
# -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ def libc_function(
8181
# We use the explicit equals pattern here because append and += mutate the
8282
# original list, where this creates a new list and stores it in deps.
8383
copts = copts or []
84-
copts = copts + ["-O3", "-fno-builtin", "-fno-lax-vector-conversions"]
84+
copts = copts + [
85+
"-O3",
86+
"-fno-builtin",
87+
"-fno-lax-vector-conversions",
88+
"-ftrivial-auto-var-init=pattern"
89+
]
8590

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

0 commit comments

Comments
 (0)