From 135519f03f3955a8823bcd1776a04839995c344b Mon Sep 17 00:00:00 2001 From: Ryan Burn Date: Wed, 25 Sep 2024 09:10:08 -0700 Subject: [PATCH] build: fix asan build (PROOF-914) (#180) * tweak * build lld * set up lld * build compiler-rt * fix asan * fix asan * rework asan options * update build * clean up * drop cruft * reformat --- .bazelrc | 18 ++++++++++++++---- .github/workflows/test-check-lint.yml | 2 +- nix/clang.nix | 24 +++++++++++++++++++++--- nix/compiler_rt.patch | 12 ++++++------ sxt/field51/base/byte_conversion.cc | 2 +- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/.bazelrc b/.bazelrc index 48107a27..18f84b84 100644 --- a/.bazelrc +++ b/.bazelrc @@ -30,19 +30,29 @@ build --config=clang # https://stackoverflow.com/a/57733619 build:asan --strip=never build:asan --copt -DADDRESS_SANITIZER -build:asan --copt -O1 -build:asan --copt -g build:asan --copt -fno-omit-frame-pointer build:asan --copt -fsanitize=address + +build:asan --@rules_cuda//cuda:host_copts=-DADDRESS_SANITIZER +build:asan --@rules_cuda//cuda:host_copts=-D_LIBCPP_HAS_NO_ASAN +build:asan --@rules_cuda//cuda:host_copts=-fno-omit-frame-pointer +build:asan --@rules_cuda//cuda:host_copts=-fsanitize=address +build:asan --@rules_cuda//cuda:copts=-DADDRESS_SANITIZER +build:asan --@rules_cuda//cuda:copts=-D_LIBCPP_HAS_NO_ASAN +build:asan --@rules_cuda//cuda:copts=-fno-omit-frame-pointer +build:asan --@rules_cuda//cuda:copts=-fsanitize=address + build:asan --linkopt -fsanitize=address +build:asan --linkopt -fuse-ld=lld +build:asan --linkopt -rtlib=compiler-rt # work around for https://github.com/google/sanitizers/issues/856 build:asan --linkopt -no-pie # For asan to work with cuda, we need to add this option # See https://github.com/google/sanitizers/issues/629#issuecomment-161357276 -test:asan --action_env=ASAN_OPTIONS=protect_shadow_gap=0 -run:asan --action_env=ASAN_OPTIONS=protect_shadow_gap=0 +run:asan --action_env=ASAN_OPTIONS=protect_shadow_gap=0:detect_container_overflow=0 +test:asan --action_env=ASAN_OPTIONS=protect_shadow_gap=0:detect_container_overflow=0 # Hack to add suppressions for libcuda # See https://github.com/bazelbuild/bazel/issues/3216 diff --git a/.github/workflows/test-check-lint.yml b/.github/workflows/test-check-lint.yml index 49b4b027..f58cd677 100644 --- a/.github/workflows/test-check-lint.yml +++ b/.github/workflows/test-check-lint.yml @@ -81,7 +81,7 @@ jobs: uses: actions/checkout@v3 - name: Run cpp-asan tests - run: TEST_TMPDIR=$HOME/.bazel_test_asan nix develop --command bazel test --config=asan //... + run: TEST_TMPDIR=$HOME/.bazel_test_asan nix develop --command bazel test --config=asan //sxt/... test-rust: name: Rust Sys Crate diff --git a/nix/clang.nix b/nix/clang.nix index ddcc4c76..58e54911 100644 --- a/nix/clang.nix +++ b/nix/clang.nix @@ -9,8 +9,8 @@ stdenvNoCC.mkDerivation { src = pkgs.fetchFromGitHub { owner = "llvm"; repo = "llvm-project"; - rev = "aa91d90"; - hash = "sha256-+UGCC3OEwGpAz1/ZPKaemZvP4Do7+POqZfduP78WtRc="; + rev = "bde2357"; + hash = "sha256-eWqHYSEHl+YAx3qeabsCY0OKHySEsjNqvbEcYrMucKU="; }; nativeBuildInputs = [ cmake @@ -21,6 +21,7 @@ stdenvNoCC.mkDerivation { ]; buildInputs = [ gcc + zlib ]; NIX_LDFLAGS = "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} -L${gcc.libc}/lib"; CFLAGS = "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} -B${gcc.libc}/lib"; @@ -45,7 +46,8 @@ stdenvNoCC.mkDerivation { "-DLLVM_TARGETS_TO_BUILD=\"host;NVPTX\"" "-DLLVM_BUILTIN_TARGETS=\"x86_64-unknown-linux-gnu\"" "-DLLVM_RUNTIME_TARGETS=\"x86_64-unknown-linux-gnu\"" - "-DLLVM_ENABLE_PROJECTS=\"clang;clang-tools-extra\"" + "-DLLVM_ENABLE_PROJECTS=\"clang;lld;clang-tools-extra\"" + "-DLLVM_ENABLE_ZLIB=ON" # clang "-DCLANG_DEFAULT_CXX_STDLIB=libc++" @@ -73,8 +75,24 @@ stdenvNoCC.mkDerivation { # compiler-rt "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_CXX_LIBRARY=libcxx" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_USE_LIBCXX=ON" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_STATIC_CXX_LIBRARY=ON" + "-DRUNTIMES_x86_64-unknown-linux-gnu_SANITIZER_USE_STATIC_CXX_ABI=ON" "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_USE_LLVM_UNWINDER=ON" + "-DRUNTIMES_x86_64-unknown-linux-gnu_SANITIZER_USE_STATIC_LLVM_UNWINDER=ON" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_BUILTINS=ON" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_SANITIZERS_TO_BUILD=\"asan;msan\"" "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_LIBFUZZER=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_XRAY=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_XRAY_NO_PREINIT=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_PROFILE=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_CTX_PROFILE=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_MEMPROF=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_ORC=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_BUILD_GWP_ASAN=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=OFF" + "-DRUNTIMES_x86_64-unknown-linux-gnu_COMPILER_RT_INCLUDE_TESTS=OFF" "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_INSTALL_PREFIX=\"$out\"" diff --git a/nix/compiler_rt.patch b/nix/compiler_rt.patch index c8ee9702..e8ffb463 100644 --- a/nix/compiler_rt.patch +++ b/nix/compiler_rt.patch @@ -1,8 +1,8 @@ diff --git a/compiler-rt/lib/asan/CMakeLists.txt b/compiler-rt/lib/asan/CMakeLists.txt -index 463ea233b37a..98293e1f66c9 100644 +index fb3d74283a61..13534ddd8547 100644 --- a/compiler-rt/lib/asan/CMakeLists.txt +++ b/compiler-rt/lib/asan/CMakeLists.txt -@@ -185,22 +185,22 @@ if(APPLE) +@@ -209,22 +209,22 @@ if(APPLE) add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS) add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS) @@ -41,10 +41,10 @@ index 463ea233b37a..98293e1f66c9 100644 add_compiler_rt_runtime(clang_rt.asan_static STATIC -@@ -293,25 +293,25 @@ else() - SanitizerCommonWeakInterception) +@@ -308,25 +308,25 @@ else() endif() + set(ASAN_DYNAMIC_WEAK_INTERCEPTION) - add_compiler_rt_runtime(clang_rt.asan - SHARED - ARCHS ${arch} @@ -187,7 +187,7 @@ index dc700cec9bec..112d8d77c3ed 100644 if(COMPILER_RT_INCLUDE_TESTS) add_subdirectory(tests) diff --git a/compiler-rt/lib/ubsan/CMakeLists.txt b/compiler-rt/lib/ubsan/CMakeLists.txt -index db0b33f1276e..1b7d8324b205 100644 +index 5d45a53d02db..3de1500dcf77 100644 --- a/compiler-rt/lib/ubsan/CMakeLists.txt +++ b/compiler-rt/lib/ubsan/CMakeLists.txt @@ -104,19 +104,19 @@ if(APPLE) @@ -223,7 +223,7 @@ index db0b33f1276e..1b7d8324b205 100644 if (NOT APPLE) add_compiler_rt_runtime(clang_rt.ubsan -@@ -246,23 +246,23 @@ else() +@@ -225,23 +225,23 @@ else() APPEND PROPERTY OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.ubsan_standalone-dynamic-${arch}.vers) diff --git a/sxt/field51/base/byte_conversion.cc b/sxt/field51/base/byte_conversion.cc index 157953bd..8a5c5faa 100644 --- a/sxt/field51/base/byte_conversion.cc +++ b/sxt/field51/base/byte_conversion.cc @@ -37,7 +37,7 @@ void from_bytes(uint64_t h[5], const uint8_t s[32]) noexcept { const uint64_t mask = 0x7ffffffffffffULL; uint64_t h0, h1, h2, h3, h4; - h0 = (basbt::load64_le(s))&mask; + h0 = (basbt::load64_le(s)) & mask; h1 = (basbt::load64_le(s + 6) >> 3) & mask; h2 = (basbt::load64_le(s + 12) >> 6) & mask; h3 = (basbt::load64_le(s + 19) >> 1) & mask;