Skip to content

Commit 07cd5a4

Browse files
DanBlackwellbripeticca
authored andcommitted
Add support for a --sanitize=fuzzer flag (swiftlang#8729)
### Motivation: I hope that this should fix swiftlang#8731, and make it easier to build with libFuzzer. ### Modifications: Add support for a `-sanitize=fuzzer` option in `swift-build`, that sets the correct flags (including `-parse-as-library`) to build a binary for fuzzing. Add an additional test to make sure that these flags get set.
1 parent 0349b3b commit 07cd5a4

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ public final class SwiftModuleBuildDescription {
198198
/// True if this module needs to be parsed as a library based on the target type and the configuration
199199
/// of the source code
200200
var needsToBeParsedAsLibrary: Bool {
201+
if buildParameters.sanitizers.sanitizers.contains(.fuzzer) {
202+
return true
203+
}
204+
201205
switch self.target.type {
202206
case .library, .test:
203207
return true

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ public final class SwiftCommandState {
869869
flags: ["entry-point-function-name"],
870870
toolchain: toolchain,
871871
fileSystem: self.fileSystem
872-
),
872+
) && !options.build.sanitizers.contains(.fuzzer),
873873
enableParseableModuleInterfaces: self.options.build.shouldEnableParseableModuleInterfaces,
874874
explicitTargetDependencyImportCheckingMode: self.options.build.explicitTargetDependencyImportCheck
875875
.modeParameter,

Sources/PackageModel/Sanitizers.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum Sanitizer: String, Encodable, CaseIterable {
1616
case thread
1717
case undefined
1818
case scudo
19+
case fuzzer
1920

2021
/// Return an established short name for a sanitizer, e.g. "asan".
2122
public var shortName: String {
@@ -24,6 +25,7 @@ public enum Sanitizer: String, Encodable, CaseIterable {
2425
case .thread: return "tsan"
2526
case .undefined: return "ubsan"
2627
case .scudo: return "scudo"
28+
case .fuzzer: return "fuzzer"
2729
}
2830
}
2931
}

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6617,6 +6617,10 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
66176617
try await self.sanitizerTest(.scudo, expectedName: "scudo")
66186618
}
66196619

6620+
func testFuzzerSanitizer() async throws {
6621+
try await self.sanitizerTest(.fuzzer, expectedName: "fuzzer")
6622+
}
6623+
66206624
func testSnippets() async throws {
66216625
let fs: FileSystem = InMemoryFileSystem(
66226626
emptyFiles:
@@ -6724,6 +6728,14 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
67246728
let clib = try result.moduleBuildDescription(for: "clib").clang().basicArguments(isCXX: false)
67256729
XCTAssertMatch(clib, ["-fsanitize=\(expectedName)"])
67266730

6731+
if sanitizer == .fuzzer {
6732+
XCTAssertMatch(exe, ["-parse-as-library"])
6733+
XCTAssertNoMatch(exe, ["-Xlinker", "alias", "_main"])
6734+
XCTAssertMatch(lib, ["-parse-as-library"])
6735+
XCTAssertNoMatch(lib, ["-Xlinker", "alias", "_main"])
6736+
XCTAssertNoMatch(clib, ["-parse-as-library"])
6737+
}
6738+
67276739
XCTAssertMatch(try result.buildProduct(for: "exe").linkArguments(), ["-sanitize=\(expectedName)"])
67286740
}
67296741

0 commit comments

Comments
 (0)