|
| 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)" |
0 commit comments