Skip to content

Add support for a --sanitize=fuzzer flag #8729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public final class SwiftModuleBuildDescription {
/// True if this module needs to be parsed as a library based on the target type and the configuration
/// of the source code
var needsToBeParsedAsLibrary: Bool {
if buildParameters.sanitizers.sanitizers.contains(.fuzzer) {
return true
}

switch self.target.type {
case .library, .test:
return true
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreCommands/SwiftCommandState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ public final class SwiftCommandState {
flags: ["entry-point-function-name"],
toolchain: toolchain,
fileSystem: self.fileSystem
),
) && !options.build.sanitizers.contains(.fuzzer),
enableParseableModuleInterfaces: self.options.build.shouldEnableParseableModuleInterfaces,
explicitTargetDependencyImportCheckingMode: self.options.build.explicitTargetDependencyImportCheck
.modeParameter,
Expand Down
2 changes: 2 additions & 0 deletions Sources/PackageModel/Sanitizers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum Sanitizer: String, Encodable, CaseIterable {
case thread
case undefined
case scudo
case fuzzer

/// Return an established short name for a sanitizer, e.g. "asan".
public var shortName: String {
Expand All @@ -24,6 +25,7 @@ public enum Sanitizer: String, Encodable, CaseIterable {
case .thread: return "tsan"
case .undefined: return "ubsan"
case .scudo: return "scudo"
case .fuzzer: return "fuzzer"
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6656,6 +6656,10 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
try await self.sanitizerTest(.scudo, expectedName: "scudo")
}

func testFuzzerSanitizer() async throws {
try await self.sanitizerTest(.fuzzer, expectedName: "fuzzer")
}

func testSnippets() async throws {
let fs: FileSystem = InMemoryFileSystem(
emptyFiles:
Expand Down Expand Up @@ -6763,6 +6767,14 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
let clib = try result.moduleBuildDescription(for: "clib").clang().basicArguments(isCXX: false)
XCTAssertMatch(clib, ["-fsanitize=\(expectedName)"])

if sanitizer == .fuzzer {
XCTAssertMatch(exe, ["-parse-as-library"])
XCTAssertNoMatch(exe, ["-Xlinker", "alias", "_main"])
XCTAssertMatch(lib, ["-parse-as-library"])
XCTAssertNoMatch(lib, ["-Xlinker", "alias", "_main"])
XCTAssertNoMatch(clib, ["-parse-as-library"])
}

XCTAssertMatch(try result.buildProduct(for: "exe").linkArguments(), ["-sanitize=\(expectedName)"])
}

Expand Down