|
1 |
| -libFuzzer Integration |
2 |
| ---------------------- |
3 |
| - |
4 |
| -Swift compiler comes with a built-in `libFuzzer` integration. |
5 |
| -In order to use it on a file `myfile.swift`, we define an entry point fuzzing function |
6 |
| -with a `@_cdecl("LLVMFuzzerTestOneInput")` annotation: |
| 1 | +# libFuzzer Integration |
7 | 2 |
|
| 3 | +Custom builds of the Swift toolchain (including development snapshots) |
| 4 | +have a built-in `libFuzzer` integration. In order to use it on a file |
| 5 | +`myfile.swift`, define an entry point fuzzing function with a |
| 6 | +`@_cdecl("LLVMFuzzerTestOneInput")` annotation: |
8 | 7 |
|
9 | 8 | ```swift
|
10 |
| -@_cdecl("LLVMFuzzerTestOneInput") public func fuzzMe(Data: UnsafePointer<CChar>, Size: CInt) -> CInt{ |
11 |
| - // Test our code using provided Data. |
12 |
| - } |
| 9 | +@_cdecl("LLVMFuzzerTestOneInput") |
| 10 | +public func test(_ start: UnsafeRawPointer, _ count: Int) -> CInt { |
| 11 | + let bytes = UnsafeRawBufferPointer(start: start, count: count) |
| 12 | + // TODO: Test the code using the provided bytes. |
| 13 | + return 0 |
13 | 14 | }
|
14 | 15 | ```
|
15 | 16 |
|
16 |
| -To compile it, we use `-sanitize=fuzzer` flag to link `libFuzzer` |
17 |
| -and enable coverage annotation, and `-parse-as-library` flag not to insert |
18 |
| -the `main` symbol, such that the fuzzer entry point can be used: |
| 17 | +To compile it, use the `-sanitize=fuzzer` flag to link `libFuzzer` |
| 18 | +and enable code coverage information; and the `-parse-as-library` flag |
| 19 | +to omit the `main` symbol, so that the fuzzer entry point can be used: |
19 | 20 |
|
20 | 21 | ```bash
|
21 | 22 | % swiftc -sanitize=fuzzer -parse-as-library myfile.swift
|
22 | 23 | ```
|
23 | 24 |
|
24 |
| -`libFuzzer` can be also combined with other sanitizers: |
| 25 | +`libFuzzer` can be combined with other sanitizers: |
25 | 26 |
|
26 | 27 | ```bash
|
27 | 28 | % swiftc -sanitize=fuzzer,address -parse-as-library myfile.swift
|
28 | 29 | ```
|
29 | 30 |
|
30 |
| -Finally, we launch the fuzzing process: |
| 31 | +Finally, launch the fuzzing process: |
31 | 32 |
|
32 | 33 | ```bash
|
33 |
| -% ./a.out |
| 34 | +% ./myfile |
34 | 35 | ```
|
35 | 36 |
|
36 |
| -Refer to the official `libFuzzer` documentation at http://llvm.org/docs/LibFuzzer.html |
37 |
| -for the description of flags the resulting binary has. |
| 37 | +Refer to the official `libFuzzer` documentation at |
| 38 | +<https://llvm.org/docs/LibFuzzer.html#options> |
| 39 | +for a description of the fuzzer's command line options. |
0 commit comments