Skip to content

Commit 498f3c3

Browse files
committed
[UBSan] Use shared library for UBSan on OS X (Clang part).
Summary: UBSan is now used in the same way as ASan, and is supported on OSX and on iOS simulator. At the moment ASan and UBSan can't be used together due to PR21112, but I hope to resolve it soon by embedding UBSan into ASan. Test Plan: regression test suite. Reviewers: zaks.anna, kubabrecka Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D8471 llvm-svn: 233035
1 parent acf28be commit 498f3c3

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
lines changed

clang/lib/Driver/ToolChains.cpp

+29-34
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,22 @@ void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
324324
}
325325
}
326326

327+
void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args,
328+
ArgStringList &CmdArgs,
329+
StringRef Sanitizer) const {
330+
if (!Args.hasArg(options::OPT_dynamiclib) &&
331+
!Args.hasArg(options::OPT_bundle)) {
332+
// Sanitizer runtime libraries requires C++.
333+
AddCXXStdlibLibArgs(Args, CmdArgs);
334+
}
335+
assert(isTargetMacOS() || isTargetIOSSimulator());
336+
StringRef OS = isTargetMacOS() ? "osx" : "iossim";
337+
AddLinkRuntimeLib(Args, CmdArgs, (Twine("libclang_rt.") + Sanitizer + "_" +
338+
OS + "_dynamic.dylib").str(),
339+
/*AlwaysLink*/ true, /*IsEmbedded*/ false,
340+
/*AddRPath*/ true);
341+
}
342+
327343
void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
328344
ArgStringList &CmdArgs) const {
329345
// Darwin only supports the compiler-rt based runtime libraries.
@@ -368,47 +384,26 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
368384

369385
const SanitizerArgs &Sanitize = getSanitizerArgs();
370386

371-
// Add Ubsan runtime library, if required.
372-
if (Sanitize.needsUbsanRt()) {
373-
// FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
374-
if (isTargetIOSBased()) {
387+
if (Sanitize.needsAsanRt()) {
388+
if (!isTargetMacOS() && !isTargetIOSSimulator()) {
389+
// FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
375390
getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
376-
<< "-fsanitize=undefined";
391+
<< "-fsanitize=address";
377392
} else {
378-
assert(isTargetMacOS() && "unexpected non OS X target");
379-
AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
380-
381-
// The Ubsan runtime library requires C++.
382-
AddCXXStdlibLibArgs(Args, CmdArgs);
393+
AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
383394
}
384395
}
385396

386-
// Add ASAN runtime library, if required. Dynamic libraries and bundles
387-
// should not be linked with the runtime library.
388-
if (Sanitize.needsAsanRt()) {
389-
// FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
390-
if (isTargetIPhoneOS()) {
397+
if (Sanitize.needsUbsanRt()) {
398+
if (!isTargetMacOS() && !isTargetIOSSimulator()) {
399+
// FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
391400
getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
392-
<< "-fsanitize=address";
401+
<< "-fsanitize=undefined";
393402
} else {
394-
if (!Args.hasArg(options::OPT_dynamiclib) &&
395-
!Args.hasArg(options::OPT_bundle)) {
396-
// The ASAN runtime library requires C++.
397-
AddCXXStdlibLibArgs(Args, CmdArgs);
398-
}
399-
if (isTargetMacOS()) {
400-
AddLinkRuntimeLib(Args, CmdArgs,
401-
"libclang_rt.asan_osx_dynamic.dylib",
402-
/*AlwaysLink*/ true, /*IsEmbedded*/ false,
403-
/*AddRPath*/ true);
404-
} else {
405-
if (isTargetIOSSimulator()) {
406-
AddLinkRuntimeLib(Args, CmdArgs,
407-
"libclang_rt.asan_iossim_dynamic.dylib",
408-
/*AlwaysLink*/ true, /*IsEmbedded*/ false,
409-
/*AddRPath*/ true);
410-
}
411-
}
403+
AddLinkSanitizerLibArgs(Args, CmdArgs, "ubsan");
404+
// Add explicit dependcy on -lc++abi, as -lc++ doesn't re-export
405+
// all RTTI-related symbols that UBSan uses.
406+
CmdArgs.push_back("-lc++abi");
412407
}
413408
}
414409

clang/lib/Driver/ToolChains.h

+5
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
494494
AddLinkARCArgs(const llvm::opt::ArgList &Args,
495495
llvm::opt::ArgStringList &CmdArgs) const override;
496496
/// }
497+
498+
private:
499+
void AddLinkSanitizerLibArgs(const llvm::opt::ArgList &Args,
500+
llvm::opt::ArgStringList &CmdArgs,
501+
StringRef Sanitizer) const;
497502
};
498503

499504
class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {

clang/runtime/compiler-rt/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ RuntimeLibrary.darwin.Configs := \
7979
eprintf.a 10.4.a osx.a cc_kext.a \
8080
asan_osx_dynamic.dylib \
8181
profile_osx.a \
82-
ubsan_osx.a
82+
ubsan_osx_dynamic.dylib
8383

8484
IOS_SDK := $(shell xcrun --show-sdk-path -sdk iphoneos 2> /dev/null)
8585
IOSSIM_SDK := $(shell xcrun --show-sdk-path -sdk iphonesimulator 2> /dev/null)
@@ -93,7 +93,8 @@ RuntimeLibrary.darwin.Configs += cc_kext_ios5.a
9393
endif
9494

9595
ifneq ($(IOSSIM_SDK),)
96-
RuntimeLibrary.darwin.Configs += asan_iossim_dynamic.dylib
96+
RuntimeLibrary.darwin.Configs += asan_iossim_dynamic.dylib \
97+
ubsan_iossim_dynamic.dylib
9798
endif
9899

99100
RuntimeLibrary.macho_embedded.Configs := \

clang/test/Driver/darwin-sanitizer-ld.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
// RUN: | FileCheck --check-prefix=CHECK-UBSAN %s
3636

3737
// CHECK-UBSAN: "{{.*}}ld{{(.exe)?}}"
38-
// CHECK-UBSAN: libclang_rt.ubsan_osx.a"
3938
// CHECK-UBSAN: stdc++
39+
// CHECK-UBSAN: libclang_rt.ubsan_osx_dynamic.dylib"
40+
// CHECK-UBSAN: "-rpath" "@executable_path"
41+
// CHECK-UBSAN: "-rpath" "{{.*}}lib{{.*}}darwin"
4042

4143
// RUN: %clang -no-canonical-prefixes -### -target x86_64-darwin \
4244
// RUN: -fsanitize=bounds -fsanitize-undefined-trap-on-error \
@@ -52,12 +54,14 @@
5254

5355
// CHECK-DYN-UBSAN: "{{.*}}ld{{(.exe)?}}"
5456
// CHECK-DYN-UBSAN: "-dylib"
55-
// CHECK-DYN-UBSAN: libclang_rt.ubsan_osx.a
57+
// CHECK-DYN-UBSAN: libclang_rt.ubsan_osx_dynamic.dylib"
58+
// CHECK-DYN-UBSAN: "-rpath" "@executable_path"
59+
// CHECK-DYN-UBSAN: "-rpath" "{{.*}}lib{{.*}}darwin"
5660

5761
// RUN: %clang -no-canonical-prefixes -### -target x86_64-darwin \
5862
// RUN: -fsanitize=bounds -fsanitize-undefined-trap-on-error \
5963
// RUN: %s -o %t.so -fPIC -shared 2>&1 \
6064
// RUN: | FileCheck --check-prefix=CHECK-DYN-BOUNDS %s
6165

6266
// CHECK-DYN-BOUNDS: "{{.*}}ld{{(.exe)?}}"
63-
// CHECK-DYN-BOUNDS-NOT: libclang_rt.ubsan_osx.a
67+
// CHECK-DYN-BOUNDS-NOT: ubsan_osx

0 commit comments

Comments
 (0)