Skip to content

Commit cc42980

Browse files
author
Alexey Samsonov
committed
UBSan: enable proper linking with UBsan runtime on Darwin. Turn on building ubsan on OS X in 'make' build system. Patch by Jean-Daniel Dupas.
llvm-svn: 168168
1 parent 77d3574 commit cc42980

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

clang/lib/Driver/ToolChains.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,29 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
317317

318318
SanitizerArgs Sanitize(getDriver(), Args);
319319

320+
// Add Ubsan runtime library, if required.
321+
if (Sanitize.needsUbsanRt()) {
322+
if (Args.hasArg(options::OPT_dynamiclib) ||
323+
Args.hasArg(options::OPT_bundle)) {
324+
// Assume the binary will provide the Ubsan runtime.
325+
} else if (isTargetIPhoneOS()) {
326+
getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
327+
<< "-fsanitize=undefined";
328+
} else {
329+
AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a");
330+
331+
// The Ubsan runtime library requires C++.
332+
AddCXXStdlibLibArgs(Args, CmdArgs);
333+
}
334+
}
335+
320336
// Add ASAN runtime library, if required. Dynamic libraries and bundles
321337
// should not be linked with the runtime library.
322338
if (Sanitize.needsAsanRt()) {
323339
if (Args.hasArg(options::OPT_dynamiclib) ||
324-
Args.hasArg(options::OPT_bundle)) return;
325-
if (isTargetIPhoneOS()) {
340+
Args.hasArg(options::OPT_bundle)) {
341+
// Assume the binary will provide the ASan runtime.
342+
} else if (isTargetIPhoneOS()) {
326343
getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
327344
<< "-fsanitize=address";
328345
} else {

clang/lib/Driver/Tools.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4710,10 +4710,11 @@ void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
47104710
Args.AddAllArgs(CmdArgs, options::OPT_L);
47114711

47124712
SanitizerArgs Sanitize(getToolChain().getDriver(), Args);
4713-
// If we're building a dynamic lib with -fsanitize=address, unresolved
4714-
// symbols may appear. Mark all of them as dynamic_lookup.
4715-
// Linking executables is handled in lib/Driver/ToolChains.cpp.
4716-
if (Sanitize.needsAsanRt()) {
4713+
// If we're building a dynamic lib with -fsanitize=address, or
4714+
// -fsanitize=undefined, unresolved symbols may appear. Mark all
4715+
// of them as dynamic_lookup. Linking executables is handled in
4716+
// lib/Driver/ToolChains.cpp.
4717+
if (Sanitize.needsAsanRt() || Sanitize.needsUbsanRt()) {
47174718
if (Args.hasArg(options::OPT_dynamiclib) ||
47184719
Args.hasArg(options::OPT_bundle)) {
47194720
CmdArgs.push_back("-undefined");

clang/runtime/compiler-rt/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ RuntimeDirs += darwin
7676
RuntimeLibrary.darwin.Configs := \
7777
eprintf.a 10.4.a osx.a ios.a cc_kext.a cc_kext_ios5.a \
7878
asan_osx.a asan_osx_dynamic.dylib \
79-
profile_osx.a profile_ios.a
79+
profile_osx.a profile_ios.a \
80+
ubsan_osx.a
8081
endif
8182

8283
# On Linux, include a library which has all the runtime functions.

0 commit comments

Comments
 (0)