From a44477b1f4b5c462474c12144d5f914d04a674cb Mon Sep 17 00:00:00 2001 From: usama hameed Date: Wed, 11 Jan 2023 15:16:30 -0800 Subject: [PATCH] [CompilerRT] Remove ubsan static runtime on Apple This patch removes the static ubsan runtime on Apple devices. The motivation is to reduce the toolchain size. rdar://102061519 Differential Revision: https://reviews.llvm.org/D141550 --- .../clang/Basic/DiagnosticDriverKinds.td | 2 ++ clang/lib/Driver/ToolChains/Darwin.cpp | 13 +++++++-- clang/test/Driver/sanitizer-ld.c | 3 +- compiler-rt/lib/ubsan/CMakeLists.txt | 28 ++++++++++--------- compiler-rt/test/ubsan/CMakeLists.txt | 1 - 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 08be6966f686..6f046cdb7952 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -215,6 +215,8 @@ def err_drv_malformed_sanitizer_coverage_allowlist : Error< "malformed sanitizer coverage allowlist: '%0'">; def err_drv_malformed_sanitizer_coverage_ignorelist : Error< "malformed sanitizer coverage ignorelist: '%0'">; +def err_drv_unsupported_static_ubsan_darwin : Error< + "static UndefinedBehaviorSanitizer runtime is not supported on darwin">; def err_drv_duplicate_config : Error< "no more than one option '--config' is allowed">; def err_drv_cannot_open_config_file : Error< diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp index a161355ccbc4..9f95c962ee9a 100644 --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -1425,15 +1425,22 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, } const SanitizerArgs &Sanitize = getSanitizerArgs(Args); + + if (!Sanitize.needsSharedRt() && Sanitize.needsUbsanRt()) { + getDriver().Diag(diag::err_drv_unsupported_static_ubsan_darwin); + return; + } + if (Sanitize.needsAsanRt()) AddLinkSanitizerLibArgs(Args, CmdArgs, "asan"); if (Sanitize.needsLsanRt()) AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan"); - if (Sanitize.needsUbsanRt()) + if (Sanitize.needsUbsanRt()) { + assert(Sanitize.needsSharedRt() && "Static sanitizer runtimes not supported"); AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitize.requiresMinimalRuntime() ? "ubsan_minimal" - : "ubsan", - Sanitize.needsSharedRt()); + : "ubsan"); + } if (Sanitize.needsTsanRt()) AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan"); if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) { diff --git a/clang/test/Driver/sanitizer-ld.c b/clang/test/Driver/sanitizer-ld.c index e58a6e51547f..6b85abfd81c5 100644 --- a/clang/test/Driver/sanitizer-ld.c +++ b/clang/test/Driver/sanitizer-ld.c @@ -423,8 +423,7 @@ // RUN: --target=x86_64-apple-darwin -fuse-ld=ld -static-libsan \ // RUN: --sysroot=%S/Inputs/basic_linux_tree \ // RUN: | FileCheck --check-prefix=CHECK-UBSAN-STATIC-DARWIN %s -// CHECK-UBSAN-STATIC-DARWIN: "{{.*}}ld{{(.exe)?}}" -// CHECK-UBSAN-STATIC-DARWIN: "{{.*}}libclang_rt.ubsan_osx.a" +// CHECK-UBSAN-STATIC-DARWIN: {{.*}}error: static UndefinedBehaviorSanitizer runtime is not supported on darwin // RUN: %clang -fsanitize=address,undefined -### %s 2>&1 \ // RUN: --target=i386-unknown-linux -fuse-ld=ld \ diff --git a/compiler-rt/lib/ubsan/CMakeLists.txt b/compiler-rt/lib/ubsan/CMakeLists.txt index 2c7e0f596ff5..520a024fbede 100644 --- a/compiler-rt/lib/ubsan/CMakeLists.txt +++ b/compiler-rt/lib/ubsan/CMakeLists.txt @@ -114,19 +114,21 @@ if(APPLE) LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS} PARENT_TARGET ubsan) - add_compiler_rt_runtime(clang_rt.ubsan - STATIC - OS ${UBSAN_SUPPORTED_OS} - ARCHS ${UBSAN_SUPPORTED_ARCH} - OBJECT_LIBS RTUbsan - RTUbsan_standalone - RTSanitizerCommonNoHooks - RTSanitizerCommonLibcNoHooks - RTSanitizerCommonCoverage - RTSanitizerCommonSymbolizerNoHooks - RTInterception - LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS} - PARENT_TARGET ubsan) + if (NOT APPLE) + add_compiler_rt_runtime(clang_rt.ubsan + STATIC + OS ${UBSAN_SUPPORTED_OS} + ARCHS ${UBSAN_SUPPORTED_ARCH} + OBJECT_LIBS RTUbsan + RTUbsan_standalone + RTSanitizerCommonNoHooks + RTSanitizerCommonLibcNoHooks + RTSanitizerCommonCoverage + RTSanitizerCommonSymbolizerNoHooks + RTInterception + LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS} + PARENT_TARGET ubsan) + endif() endif() else() diff --git a/compiler-rt/test/ubsan/CMakeLists.txt b/compiler-rt/test/ubsan/CMakeLists.txt index 749446320ff3..d14aa149d978 100644 --- a/compiler-rt/test/ubsan/CMakeLists.txt +++ b/compiler-rt/test/ubsan/CMakeLists.txt @@ -101,7 +101,6 @@ if(APPLE) set(UBSAN_TEST_TARGET_ARCH ${arch}) get_test_cc_for_arch(${arch} UBSAN_TEST_TARGET_CC UBSAN_TEST_TARGET_CFLAGS) set(UBSAN_TEST_TARGET_CFLAGS "${UBSAN_TEST_TARGET_CFLAGS} -lc++abi") - add_ubsan_testsuites("StandaloneStatic" ubsan ${arch}) endforeach() # Device and simulator test suites.