Skip to content

Commit eaf6c6c

Browse files
[ATfL] Enable sanitizers for testing (#566)
[ATfL] Enable sanitizers for testing ATfL. Scripts implements 2-stage pipeline: first clang is built using arm-toolchain sources. Then this clang is used to compile ATfL sanitizer build. The script creates a build of the toolchain in the 'build_clang_with_sanitizer' directory, inside the repository tree and tests it. --------- Co-authored-by: Kiran Chandramohan <[email protected]>
1 parent b9ed71f commit eaf6c6c

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) 2025, Arm Limited and affiliates.
4+
# Part of the Arm Toolchain project, under the Apache License v2.0 with LLVM Exceptions.
5+
# See https://llvm.org/LICENSE.txt for license information.
6+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
8+
# A bash script to build the Arm Toolchain for Linux, with Address (ASan) and Undefined Behaviour Sanitizer (UBSan) enabled.
9+
10+
# Script implements 2-stage pipeline: first clang is built using arm-toolchain sources.
11+
# Then this clang is used to compile ATfL sanitizer build.
12+
#
13+
# The script creates a build of the toolchain in the 'build_clang_with_sanitizer' directory, inside
14+
# the repository tree.
15+
16+
set -vex
17+
18+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
19+
REPO_ROOT=$( git -C "${SCRIPT_DIR}" rev-parse --show-toplevel )
20+
21+
echo "==> Stage 1: Building clang (unsanitized)"
22+
23+
mkdir -p "${REPO_ROOT}"/build_llvm
24+
cd "${REPO_ROOT}"/build_llvm
25+
26+
cmake -G Ninja ../llvm \
27+
-DCMAKE_BUILD_TYPE=Release \
28+
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
29+
-DLLVM_LIT_ARGS="-v" \
30+
-DCMAKE_INSTALL_PREFIX=../stage1.install \
31+
-DCMAKE_C_COMPILER=clang \
32+
-DCMAKE_CXX_COMPILER=clang++ \
33+
-DLLVM_TARGETS_TO_BUILD=AArch64 \
34+
-DLLVM_ENABLE_PROJECTS="clang;llvm" \
35+
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt"
36+
37+
ninja
38+
39+
echo "==> Stage 2: Building libraries (sanitized)"
40+
41+
mkdir -p "${REPO_ROOT}"/build_libcxx
42+
cd "${REPO_ROOT}"/build_libcxx
43+
44+
cmake -G Ninja ../runtimes \
45+
-DLLVM_TARGETS_TO_BUILD=AArch64 \
46+
-DLLVM_USE_SANITIZER="Address;Undefined" \
47+
-DLLVM_ENABLE_ASSERTIONS=True \
48+
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" \
49+
-DCMAKE_C_COMPILER="${REPO_ROOT}/build_llvm/bin/clang" \
50+
-DCMAKE_CXX_COMPILER="${REPO_ROOT}/build_llvm/bin/clang++" \
51+
-DLIBCXX_ENABLE_TIME_ZONE_DATABASE=off
52+
53+
ninja
54+
55+
echo "==> Stage 3: Building clang (sanitized)"
56+
57+
mkdir -p "${REPO_ROOT}"/build_clang_with_sanitizer
58+
cd "${REPO_ROOT}"/build_clang_with_sanitizer
59+
60+
export LD_LIBRARY_PATH="${REPO_ROOT}"/build_libcxx/lib:$LD_LIBRARY_PATH
61+
62+
# Flag to disable LeakSanitizer (memory leaks detection) in AddressSanitizer.
63+
export ASAN_OPTIONS=detect_leaks=0
64+
65+
# Have chosen Address Sanitizer and Undefined Sanitizer to build and test.
66+
# These sanitizers are most commonly used and relatively easy to set-up.
67+
# These sanitizers will help to detect runtime issues related to use after free,
68+
# overflow (Heap buffer, stack buffer) etc.
69+
# We have explicitly disabled memory leak detection, since the observed issues are
70+
# unrelated to product under test.
71+
cmake -G Ninja ../llvm \
72+
-DCMAKE_BUILD_TYPE=Release \
73+
-DLLVM_USE_SANITIZER="Address;Undefined" \
74+
-DLLVM_ENABLE_ASSERTIONS=True \
75+
-DCMAKE_C_COMPILER="${REPO_ROOT}/build_llvm/bin/clang" \
76+
-DCMAKE_CXX_COMPILER="${REPO_ROOT}/build_llvm/bin/clang++" \
77+
-DLLVM_LIT_ARGS="--ignore-fail --xunit-xml-output=lit_results.junit.xml \
78+
--param=env='ASAN_OPTIONS=detect_leaks=0' \
79+
--filter-out='(time\.zone|tzdb|time\.clock|orc|activation-options|quarantine_size|mmap_write_exec|assert\.cpp|asan_preload_test|thread_local_quarantine_size|leak|tzdb|assert\.cpp|use_globals|closed-fds)'" \
80+
-DCMAKE_INSTALL_PREFIX=../stage3.install \
81+
-DLLVM_TARGETS_TO_BUILD=AArch64 \
82+
-DLLVM_ENABLE_PROJECTS="clang;llvm" \
83+
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" \
84+
-DLIBCXX_ENABLE_TIME_ZONE_DATABASE=off
85+
86+
ninja
87+
echo "==> Stage 3: Completed building clang (sanitized)"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) 2025, Arm Limited and affiliates.
4+
# Part of the Arm Toolchain project, under the Apache License v2.0 with LLVM Exceptions.
5+
# See https://llvm.org/LICENSE.txt for license information.
6+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
8+
# A bash script to run the tests from the Arm Toolchain for Linux, when sanitizers
9+
# are enabled.
10+
11+
# The script assumes a successful build of the toolchain exists in the 'build_clang_with_sanitizer'
12+
# directory inside the repository tree.
13+
14+
# Script does not exit, when the command in the script exits with a non-zero status.
15+
# Also, print the line of the script being executed.
16+
set -vx
17+
18+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
19+
REPO_ROOT=$( git -C "${SCRIPT_DIR}" rev-parse --show-toplevel )
20+
21+
cd "${REPO_ROOT}"/build_clang_with_sanitizer || exit
22+
23+
# Flag to disable memory leaks detection of LeakSanitizer.
24+
export ASAN_OPTIONS=detect_leaks=0
25+
26+
# If a test fails, lit will ordinarily return a non-zero result,
27+
# which prevents further testing. Setting the --ignore-fail option
28+
# will cause testing to continue, so that CI systems can get a
29+
# full set of results.
30+
# Upstream clang and LLVM tests do not generate the junit xml results file by default.
31+
# Additionally setting the --xunit-xml-output option store the
32+
# results.
33+
export LIT_ARGS="--ignore-fail --xunit-xml-output=lit_results.junit.xml --param=env='ASAN_OPTIONS=detect_leaks=0'"
34+
35+
# Skip the flaky tests
36+
export LIT_ARGS="${LIT_ARGS} --filter-out 'time.zone|tzdb|time.clock|orc|activation-options|quarantine_size|mmap_write_exec|log-path_test|assert.cpp|initialization-nobug|use_globals|closed-fds'"
37+
38+
ninja -v check-llvm
39+
echo "check-llvm exit code: $?"
40+
41+
ninja -v check-clang
42+
echo "check-clang exit code: $?"
43+
44+
ninja -v check-cxx
45+
echo "check-cxx exit code: $?"
46+
47+
ninja -v check-cxxabi
48+
echo "check-cxxabi exit code: $?"
49+
50+
ninja -v check-compiler-rt
51+
echo "check-compiler-rt exit code: $?"
52+
53+
ninja -v check-unwind
54+
echo "check-unwind exit code: $?"

0 commit comments

Comments
 (0)