Skip to content

Commit 3374ff8

Browse files
authored
Arm backend: Add support for ubsan in executor_runner (#15894)
A lightweight Undefinedsanitizer runtime tailored for the ExecuTorch bare metal examples. The goal is to provide basic memory safety diagnostics while keeping the runtime self-contained. Signed-off-by: per.held@arm.com
1 parent 6e9fb80 commit 3374ff8

File tree

6 files changed

+538
-5
lines changed

6 files changed

+538
-5
lines changed

backends/arm/scripts/build_executorch.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# LICENSE file in the root directory of this source tree.
66

77
# Optional parameter:
8-
# --build_type= "Release" | "Debug" | "RelWithDebInfo"
8+
# --build_type= "Release" | "Debug" | "RelWithDebInfo" | "UndefinedSanitizer"
99
# --etdump build with devtools-etdump support
1010

1111
set -eu
@@ -28,7 +28,7 @@ help() {
2828
echo "Usage: $(basename $0) [options]"
2929
echo "Options:"
3030
echo " --et_build_root=<FOLDER> Build output root folder to use, defaults to ${et_build_root}"
31-
echo " --build_type=<TYPE> Build with Release, Debug or RelWithDebInfo, default is ${build_type}"
31+
echo " --build_type=<TYPE> Build with Release, Debug, RelWithDebInfo or UndefinedSanitizer, default is ${build_type}"
3232
echo " --devtools Build Devtools libs"
3333
echo " --etdump Adds Devtools etdump support to track timing, etdump area will be base64 encoded in the log"
3434
echo " --toolchain=<TOOLCHAIN> Toolchain can be specified (e.g. bare metal as arm-none-eabi-gcc or zephyr as arm-zephyr-eabi-gcc Default: ${toolchain}"
@@ -78,7 +78,7 @@ cd "${et_root_dir}"
7878

7979
# Build
8080
cmake -DCMAKE_TOOLCHAIN_FILE=${toolchain_cmake} \
81-
-DCMAKE_BUILD_TYPE=Release \
81+
-DCMAKE_BUILD_TYPE=${build_type} \
8282
-DEXECUTORCH_BUILD_DEVTOOLS=$build_devtools \
8383
-DEXECUTORCH_BUILD_ARM_ETDUMP=$build_with_etdump \
8484
--preset arm-baremetal -B${et_build_dir}

backends/arm/test/test_arm_baremetal.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,14 @@ test_memory_allocation() {
394394
echo "${TEST_SUITE_NAME}: PASS"
395395
}
396396

397+
test_undefinedbehavior_sanitizer() {
398+
echo "${TEST_SUITE_NAME}: Test ethos-u executor_runner with UBSAN"
399+
400+
mkdir -p arm_test/test_run
401+
# Ethos-U85
402+
echo "${TEST_SUITE_NAME}: Test target Ethos-U85"
403+
examples/arm/run.sh --et_build_root=arm_test/test_run --target=ethos-u85-128 --model_name=examples/arm/example_modules/add.py --build_type=UndefinedSanitizer
404+
echo "${TEST_SUITE_NAME}: PASS"
405+
}
406+
397407
${TEST_SUITE}

examples/arm/executor_runner/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,29 @@ endif()
326326

327327
# Need whole-archive to ensure C++ ctor's are called - this may be wasteful for
328328
# bin size as we link in a number of other symbols
329-
target_link_libraries(arm_executor_runner ${arm_executor_runner_link})
329+
target_link_libraries(arm_executor_runner PUBLIC ${arm_executor_runner_link})
330330

331331
target_link_options(
332332
arm_executor_runner PUBLIC LINKER:-Map=arm_executor_runner.map
333333
)
334334

335+
# Sanitizers
336+
if(CMAKE_BUILD_TYPE MATCHES "UndefinedSanitizer")
337+
set(_et_runner_ubsan_flag -fsanitize=undefined)
338+
target_compile_options(arm_executor_runner PRIVATE ${_et_runner_ubsan_flag})
339+
target_link_options(arm_executor_runner PRIVATE ${_et_runner_ubsan_flag})
340+
if(NOT TARGET executorch_ubsan)
341+
add_subdirectory(
342+
${ET_DIR_PATH}/examples/arm/ubsan
343+
${CMAKE_CURRENT_BINARY_DIR}/ubsan_runtime
344+
)
345+
endif()
346+
target_link_directories(
347+
arm_executor_runner PRIVATE $<TARGET_FILE_DIR:executorch_ubsan>
348+
)
349+
target_link_libraries(arm_executor_runner PRIVATE executorch_ubsan)
350+
endif()
351+
335352
# ET headers and generated headers includes
336353
target_include_directories(
337354
arm_executor_runner

examples/arm/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function help() {
6161
echo " --output=<FOLDER> Target build output folder Default: ${output_folder}"
6262
echo " --bundleio Create Bundled pte using Devtools BundelIO with Input/RefOutput included"
6363
echo " --etdump Adds Devtools etdump support to track timing, etdump area will be base64 encoded in the log"
64-
echo " --build_type=<TYPE> Build with Release, Debug or RelWithDebInfo, default is ${build_type}"
64+
echo " --build_type=<TYPE> Build with Release, Debug, RelWithDebInfo or UndefinedSanitizer, default is ${build_type}"
6565
echo " --extra_build_flags=<FLAGS> Extra flags to pass to cmake like -DET_ARM_BAREMETAL_METHOD_ALLOCATOR_POOL_SIZE=60000 Default: none "
6666
echo " --build_only Only build, don't run"
6767
echo " --toolchain=<TOOLCHAIN> Ethos-U: Toolchain can be specified (e.g. bare metal as arm-none-eabi-gcc or zephyr as arm-zephyr-eabi-gcc Default: ${toolchain}"

examples/arm/ubsan/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2025 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
add_library(executorch_ubsan STATIC ubsan_runtime.c)
7+
8+
target_compile_features(executorch_ubsan PRIVATE c_std_11)
9+
10+
target_compile_options(executorch_ubsan PRIVATE -fno-sanitize=undefined)
11+
12+
set_target_properties(executorch_ubsan PROPERTIES OUTPUT_NAME "ubsan")
13+
14+
install(
15+
TARGETS executorch_ubsan
16+
EXPORT ExecuTorchTargets
17+
ARCHIVE DESTINATION lib
18+
)

0 commit comments

Comments
 (0)