Skip to content

Commit d3cc043

Browse files
rahul-malikGeorge Karpenkov
authored andcommitted
Add Undefined Behavior sanitizer to Swift Driver (swiftlang#18553)
This change allows the swift driver to link the ubsan runtime if `-sanitize=undefined` is specified. This is useful for sanitizing linked Objective-C code.
1 parent 6462473 commit d3cc043

22 files changed

+56
-1
lines changed

include/swift/Basic/Sanitizers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ namespace swift {
1919
enum class SanitizerKind : unsigned {
2020
Address = 1 << 1,
2121
Thread = 1 << 2,
22-
Fuzzer = 1 << 3
22+
Fuzzer = 1 << 3,
23+
Undefined = 1 << 4
2324
};
2425

2526
} // end namespace swift

lib/Driver/DarwinToolChains.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
360360
if (context.OI.SelectedSanitizers & SanitizerKind::Thread)
361361
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "tsan", *this);
362362

363+
if (context.OI.SelectedSanitizers & SanitizerKind::Undefined)
364+
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "ubsan", *this);
365+
363366
// Only link in libFuzzer for executables.
364367
if (job.getKind() == LinkKind::Executable &&
365368
(context.OI.SelectedSanitizers & SanitizerKind::Fuzzer))

lib/Driver/UnixToolChains.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,13 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
301301
if (context.OI.SelectedSanitizers & SanitizerKind::Thread)
302302
addLinkSanitizerLibArgsForLinux(context.Args, Arguments, "tsan", *this);
303303

304+
if (context.OI.SelectedSanitizers & SanitizerKind::Undefined)
305+
addLinkSanitizerLibArgsForLinux(context.Args, Arguments, "ubsan", *this);
306+
304307
if (context.OI.SelectedSanitizers & SanitizerKind::Fuzzer)
305308
addLinkRuntimeLib(context.Args, Arguments,
306309
sanitizerRuntimeLibName("fuzzer"));
310+
307311
}
308312
}
309313

lib/Driver/WindowsToolChains.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ toolchains::Windows::constructInvocation(const LinkJobAction &job,
147147
if (context.OI.SelectedSanitizers & SanitizerKind::Address)
148148
addLinkRuntimeLib(context.Args, Arguments,
149149
sanitizerRuntimeLibName("asan"));
150+
151+
if (context.OI.SelectedSanitizers & SanitizerKind::Undefined)
152+
addLinkRuntimeLib(context.Args, Arguments,
153+
sanitizerRuntimeLibName("ubsan"));
150154
}
151155

152156
if (context.Args.hasArg(options::OPT_profile_generate)) {

lib/Option/SanitizerOptions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ static StringRef toStringRef(const SanitizerKind kind) {
3737
return "thread";
3838
case SanitizerKind::Fuzzer:
3939
return "fuzzer";
40+
case SanitizerKind::Undefined:
41+
return "undefined";
4042
}
4143
llvm_unreachable("Unsupported sanitizer");
4244
}
@@ -49,6 +51,8 @@ static const char* toFileName(const SanitizerKind kind) {
4951
return "tsan";
5052
case SanitizerKind::Fuzzer:
5153
return "fuzzer";
54+
case SanitizerKind::Undefined:
55+
return "ubsan";
5256
}
5357
llvm_unreachable("Unsupported sanitizer");
5458
}
@@ -134,6 +138,7 @@ OptionSet<SanitizerKind> swift::parseSanitizerArgValues(
134138
.Case("address", SanitizerKind::Address)
135139
.Case("thread", SanitizerKind::Thread)
136140
.Case("fuzzer", SanitizerKind::Fuzzer)
141+
.Case("undefined", SanitizerKind::Undefined)
137142
.Default(None);
138143
bool isShared = kind && *kind != SanitizerKind::Fuzzer;
139144
if (!kind) {

test/Driver/Inputs/fake-resource-dir/lib/swift/clang/lib/darwin/libclang_rt.ubsan_ios.a

Whitespace-only changes.

test/Driver/Inputs/fake-resource-dir/lib/swift/clang/lib/darwin/libclang_rt.ubsan_ios_dynamic.dylib

Whitespace-only changes.

test/Driver/Inputs/fake-resource-dir/lib/swift/clang/lib/darwin/libclang_rt.ubsan_iossim.a

Whitespace-only changes.

test/Driver/Inputs/fake-resource-dir/lib/swift/clang/lib/darwin/libclang_rt.ubsan_iossim_dynamic.dylib

Whitespace-only changes.

test/Driver/Inputs/fake-resource-dir/lib/swift/clang/lib/darwin/libclang_rt.ubsan_osx.a

Whitespace-only changes.

0 commit comments

Comments
 (0)