Skip to content

Commit cf41ab2

Browse files
Fuzzing: Add a rationale for the current build style
I myself was confused about the current build setup of the fuzzing targets while setting up clusterfuzzlite, so added some comments.
1 parent cefa44e commit cf41ab2

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

FuzzTesting/Package.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,25 @@ import PackageDescription
55
let package = Package(
66
name: "FuzzTesting",
77
products: [
8+
// Discussion: Why we build libraries instead of executables linking libFuzzer?
9+
//
10+
// First, libclang_rt.fuzzer.a defines the main function for the fuzzing process
11+
// and object files given by the user are expected not to have a "main" function
12+
// to avoid conflicts.
13+
// Fortunately, SwiftPM asks the compiler frontend to define the main entrypoint as
14+
// `<module_name>_main` for testing executable targets (`-entry-point-function-name`)
15+
// so object files of `executableTarget` targets are capable of being linked with
16+
// libclang_rt.fuzzer.a.
17+
// However, at link-time, SwiftPM asks the linker to rename the `<module_name>_main`
18+
// symbol back to `main` for the final executable (`--defsym main=<module_name>_main`)
19+
// and gold linker respects the renamed "main" symbol rather than the one defined in
20+
// libclang_rt.fuzzer.a, so the final executable does not start the fuzzing process.
21+
//
22+
// Instead of relying on the SwiftPM's linking process, we build libraries defining
23+
// fuzzing target functions and manually link them with fuzzing runtime libraries.
824
.library(name: "FuzzTranslator", type: .static, targets: ["FuzzTranslator"]),
925
.library(name: "FuzzExecute", type: .static, targets: ["FuzzExecute"]),
26+
// FuzzDifferential is not a libFuzzer-based target, so we build it as an executable.
1027
.executable(name: "FuzzDifferential", targets: ["FuzzDifferential"]),
1128
],
1229
dependencies: [

FuzzTesting/fuzz.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def build(args, runner: CommandRunner):
101101
], check=True)
102102

103103
print('Building fuzzer executable')
104+
# See "Discussion" in Package.swift for why we need to manually link
105+
# the library product.
104106
output = executable_path(args.target_name)
105107
runner.run([
106108
'swiftc', f'./.build/debug/lib{args.target_name}.a', '-g',

0 commit comments

Comments
 (0)