Skip to content

Commit fd66913

Browse files
Add WASI as a supported target for sanitizers
We are adding support for sanitizers on WASI targets. This commit adds driver support by unblocking hard-coded checks and forwarding sanitizer flags to clang linker driver.
1 parent f7b8007 commit fd66913

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

Sources/SwiftDriver/Driver/Driver.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2751,7 +2751,7 @@ extension Driver {
27512751
}
27522752

27532753
// Check that we're one of the known supported targets for sanitizers.
2754-
if !(targetTriple.isWindows || targetTriple.isDarwin || targetTriple.os == .linux) {
2754+
if !(targetTriple.isWindows || targetTriple.isDarwin || targetTriple.os == .linux || targetTriple.os == .wasi) {
27552755
diagnosticEngine.emit(
27562756
.error_unsupported_opt_for_target(
27572757
arg: "-sanitize=",

Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,12 @@ extension WebAssemblyToolchain {
165165

166166
// Delegate to Clang for sanitizers. It will figure out the correct linker
167167
// options.
168-
guard sanitizers.isEmpty else {
169-
throw Error.sanitizersUnsupportedForTarget(targetTriple.triple)
168+
if linkerOutputType == .executable && !sanitizers.isEmpty {
169+
let sanitizerNames = sanitizers
170+
.map { $0.rawValue }
171+
.sorted() // Sort so we get a stable, testable order
172+
.joined(separator: ",")
173+
commandLine.appendFlag("-fsanitize=\(sanitizerNames)")
170174
}
171175

172176
if parsedOptions.hasArgument(.profileGenerate) {

Sources/SwiftDriver/Toolchains/WebAssemblyToolchain.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ public final class WebAssemblyToolchain: Toolchain {
146146
targetTriple: Triple,
147147
isShared: Bool
148148
) throws -> String {
149-
throw Error.sanitizersUnsupportedForTarget(targetTriple.triple)
149+
switch sanitizer {
150+
case .address:
151+
return "libclang_rt.\(sanitizer.libraryName)-\(targetTriple.archName).a"
152+
default:
153+
throw Error.sanitizersUnsupportedForTarget(targetTriple.triple)
154+
}
150155
}
151156

152157
public func platformSpecificInterpreterEnvironmentVariables(env: [String : String],

Tests/SwiftDriverTests/SwiftDriverTests.swift

+41
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,47 @@ final class SwiftDriverTests: XCTestCase {
28772877
XCTAssertTrue(linkCmd.contains(.flag("-fsanitize=address")))
28782878
}
28792879
#endif
2880+
2881+
func checkWASITarget(target: String, clangOSDir: String) throws {
2882+
try withTemporaryDirectory { resourceDir in
2883+
var env = ProcessEnv.vars
2884+
env["SWIFT_DRIVER_SWIFT_AUTOLINK_EXTRACT_EXEC"] = "/garbage/swift-autolink-extract"
2885+
2886+
let asanRuntimeLibPath = resourceDir.appending(components: [
2887+
"clang", "lib", clangOSDir, "libclang_rt.asan-wasm32.a"
2888+
])
2889+
try localFileSystem.writeFileContents(asanRuntimeLibPath) {
2890+
$0.send("garbage")
2891+
}
2892+
try localFileSystem.writeFileContents(resourceDir.appending(components: "wasi", "static-executable-args.lnk")) {
2893+
$0.send("garbage")
2894+
}
2895+
2896+
var driver = try Driver(
2897+
args: commonArgs + [
2898+
"-target", target, "-sanitize=address",
2899+
"-resource-dir", resourceDir.pathString
2900+
],
2901+
env: env
2902+
)
2903+
let plannedJobs = try driver.planBuild()
2904+
2905+
XCTAssertEqual(plannedJobs.count, 4)
2906+
2907+
let compileJob = plannedJobs[0]
2908+
let compileCmd = compileJob.commandLine
2909+
XCTAssertTrue(compileCmd.contains(.flag("-sanitize=address")))
2910+
2911+
let linkJob = plannedJobs[3]
2912+
let linkCmd = linkJob.commandLine
2913+
XCTAssertTrue(linkCmd.contains(.flag("-fsanitize=address")))
2914+
}
2915+
}
2916+
do {
2917+
try checkWASITarget(target: "wasm32-unknown-wasi", clangOSDir: "wasi")
2918+
try checkWASITarget(target: "wasm32-unknown-wasip1", clangOSDir: "wasip1")
2919+
try checkWASITarget(target: "wasm32-unknown-wasip1-threads", clangOSDir: "wasip1")
2920+
}
28802921
}
28812922

28822923
func testSanitizerCoverageArgs() throws {

0 commit comments

Comments
 (0)