Skip to content

Commit 56adf76

Browse files
authored
Add EXECUTORCH_USE_SANITIZER CMake option for ASAN+UBSAN support (#17114)
Add a first-class CMake option to enable AddressSanitizer and UndefinedBehaviorSanitizer for catching memory bugs and undefined behavior in C++ tests. Previously, sanitizers were enabled via a CXXFLAGS hack in utils.sh that only applied to the initial build, but the test build in run_oss_cpp_tests.sh would rebuild from scratch without sanitizers. This meant C++ tests (including runtime, portable kernels, and XNNPACK tests) were not actually running with sanitizer coverage. All unittest in pull mode ("unittest / linux" and "unittest /macos") will be compiling and running with sanitizer on from now on. The new design: - Adds EXECUTORCH_USE_SANITIZER as a proper CMake option - CI scripts export EXECUTORCH_USE_SANITIZER=ON for Debug builds - Both utils.sh and run_oss_cpp_tests.sh check the env var and pass the CMake flag, ensuring consistent sanitizer coverage - CMakeLists.txt applies -fsanitize=address,undefined flags globally - Verified CMake configuration shows "Sanitizers enabled: address, undefined" - Verified compile_commands.json contains -fsanitize=address,undefined - Built and ran runtime_core_test with sanitizers, confirmed ASAN/UBSAN symbols present in binary (nm shows ___asan_* and ___ubsan_* symbols) - Verified Debug mode enables sanitizers, Release mode does not - All shell scripts pass syntax check (bash -n)
1 parent 3b3c9d4 commit 56adf76

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

.ci/scripts/unittest-linux.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ if [[ "$BUILD_TOOL" == "cmake" ]]; then
2020
# Setup swiftshader and Vulkan SDK which are required to build the Vulkan delegate
2121
source .ci/scripts/setup-vulkan-linux-deps.sh
2222

23+
# Enable sanitizers for Debug builds
24+
if [[ "$BUILD_MODE" == "Debug" ]]; then
25+
export EXECUTORCH_USE_SANITIZER=ON
26+
fi
27+
2328
# We need the runner to test the built library.
2429
PYTHON_EXECUTABLE=python \
2530
CMAKE_ARGS="-DEXECUTORCH_BUILD_EXTENSION_EVALUE_UTIL=ON -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON -DEXECUTORCH_BUILD_TESTS=ON" \

.ci/scripts/unittest-macos.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export TMP_DIR=$(mktemp -d)
1919
export PATH="${TMP_DIR}:$PATH"
2020
trap 'rm -rfv ${TMP_DIR}' EXIT
2121

22+
# Enable sanitizers for Debug builds
23+
if [[ "$BUILD_MODE" == "Debug" ]]; then
24+
export EXECUTORCH_USE_SANITIZER=ON
25+
fi
26+
2227
# Setup MacOS dependencies as there is no Docker support on MacOS atm
2328
# We need the runner to test the built library.
2429
PYTHON_EXECUTABLE=python \

.ci/scripts/utils.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,18 @@ build_executorch_runner_cmake() {
164164
clean_executorch_install_folders
165165
mkdir "${CMAKE_OUTPUT_DIR}"
166166

167-
if [[ $1 == "Debug" ]]; then
168-
CXXFLAGS="-fsanitize=address,undefined"
169-
else
170-
CXXFLAGS=""
167+
local build_type="${1:-Release}"
168+
local sanitizer_flag=""
169+
170+
if [[ "${EXECUTORCH_USE_SANITIZER:-OFF}" == "ON" ]]; then
171+
sanitizer_flag="-DEXECUTORCH_USE_SANITIZER=ON"
171172
fi
172-
CXXFLAGS="$CXXFLAGS" retry cmake \
173+
174+
retry cmake \
173175
-DPYTHON_EXECUTABLE="${PYTHON_EXECUTABLE}" \
174-
-DCMAKE_BUILD_TYPE="${1:-Release}" \
176+
-DCMAKE_BUILD_TYPE="${build_type}" \
177+
${sanitizer_flag} \
178+
${CMAKE_ARGS:-} \
175179
-B${CMAKE_OUTPUT_DIR} .
176180

177181
if [ "$(uname)" == "Darwin" ]; then

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ if(NOT CMAKE_BUILD_TYPE)
7474
endif()
7575
announce_configured_options(CMAKE_BUILD_TYPE)
7676

77+
# Sanitizer support (ASAN + UBSAN)
78+
if(EXECUTORCH_USE_SANITIZER)
79+
if(MSVC)
80+
message(WARNING "Sanitizers are not fully supported on MSVC, skipping")
81+
else()
82+
set(EXECUTORCH_SANITIZER_FLAGS
83+
"-fsanitize=address,undefined -fno-omit-frame-pointer"
84+
)
85+
# Suppress deprecation warnings on macOS (third-party code like flatcc uses
86+
# deprecated sprintf)
87+
if(APPLE)
88+
set(EXECUTORCH_SANITIZER_FLAGS
89+
"${EXECUTORCH_SANITIZER_FLAGS} -Wno-deprecated-declarations"
90+
)
91+
endif()
92+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}")
93+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}")
94+
set(CMAKE_EXE_LINKER_FLAGS
95+
"${CMAKE_EXE_LINKER_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}"
96+
)
97+
set(CMAKE_SHARED_LINKER_FLAGS
98+
"${CMAKE_SHARED_LINKER_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}"
99+
)
100+
message(STATUS "Sanitizers enabled: address, undefined")
101+
endif()
102+
endif()
103+
announce_configured_options(EXECUTORCH_USE_SANITIZER)
104+
77105
if(NOT PYTHON_EXECUTABLE)
78106
resolve_python_executable()
79107
endif()

test/run_oss_cpp_tests.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ build_executorch() {
3232
if [ -x "$(command -v glslc)" ]; then
3333
BUILD_VULKAN="ON"
3434
fi
35+
36+
SANITIZER_FLAG=""
37+
if [[ "${EXECUTORCH_USE_SANITIZER:-OFF}" == "ON" ]]; then
38+
SANITIZER_FLAG="-DEXECUTORCH_USE_SANITIZER=ON"
39+
fi
40+
3541
cmake . \
3642
-DCMAKE_INSTALL_PREFIX=cmake-out \
3743
-DEXECUTORCH_USE_CPP_CODE_COVERAGE=ON \
44+
${SANITIZER_FLAG} \
3845
-DEXECUTORCH_BUILD_KERNELS_LLM=ON \
3946
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
4047
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \

tools/cmake/preset/default.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ define_overridable_option(
5555
EXECUTORCH_OPTIMIZE_SIZE
5656
"Build executorch runtime optimizing for binary size" BOOL OFF
5757
)
58+
define_overridable_option(
59+
EXECUTORCH_USE_SANITIZER
60+
"Build with AddressSanitizer and UndefinedBehaviorSanitizer enabled" BOOL OFF
61+
)
5862
define_overridable_option(
5963
EXECUTORCH_BUILD_ARM_BAREMETAL
6064
"Build the Arm Baremetal flow for Cortex-M and Ethos-U" BOOL OFF

0 commit comments

Comments
 (0)