Skip to content

Commit

Permalink
build: fix asan build (PROOF-914) (#180)
Browse files Browse the repository at this point in the history
* tweak

* build lld

* set up lld

* build compiler-rt

* fix asan

* fix asan

* rework asan options

* update build

* clean up

* drop cruft

* reformat
  • Loading branch information
rnburn authored Sep 25, 2024
1 parent 540f4bf commit 135519f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
18 changes: 14 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-check-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions nix/clang.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -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++"
Expand Down Expand Up @@ -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\""
Expand Down
12 changes: 6 additions & 6 deletions nix/compiler_rt.patch
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sxt/field51/base/byte_conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 135519f

Please sign in to comment.