Skip to content

Commit 329dbb5

Browse files
committed
Update docs and tests for LLVMFuzzerTestOneInput
1 parent 4e7569e commit 329dbb5

File tree

5 files changed

+50
-81
lines changed

5 files changed

+50
-81
lines changed

docs/libFuzzerIntegration.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
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
72

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:
87

98
```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
1314
}
1415
```
1516

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:
1920

2021
```bash
2122
% swiftc -sanitize=fuzzer -parse-as-library myfile.swift
2223
```
2324

24-
`libFuzzer` can be also combined with other sanitizers:
25+
`libFuzzer` can be combined with other sanitizers:
2526

2627
```bash
2728
% swiftc -sanitize=fuzzer,address -parse-as-library myfile.swift
2829
```
2930

30-
Finally, we launch the fuzzing process:
31+
Finally, launch the fuzzing process:
3132

3233
```bash
33-
% ./a.out
34+
% ./myfile
3435
```
3536

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.

test/Driver/fuzzer.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// LIBFUZZER_OSX: libclang_rt.fuzzer
77
// LIBFUZZER_LINUX: -fsanitize=address,fuzzer
88

9-
@_cdecl("LLVMFuzzerTestOneInput") public func fuzzOneInput(Data: UnsafePointer<CChar>, Size: CLong) -> CInt {
10-
return 0;
9+
@_cdecl("LLVMFuzzerTestOneInput")
10+
public func test(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
11+
return 0
1112
}

test/Fuzzing/fuzzer_test.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/Fuzzing/fuzzer_test_simpler.swift

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %target-build-swift -parse-as-library -sanitize=fuzzer %s -o %t
2+
// RUN: not %t -only_ascii=1 -max_len=3 | %FileCheck %s
3+
// REQUIRES: CPU=x86_64
4+
// REQUIRES: executable_test
5+
// REQUIRES: fuzzer_runtime
6+
// XFAIL: OS=ios
7+
// XFAIL: OS=tvos
8+
// XFAIL: OS=watchos
9+
// CHECK: Crash!
10+
11+
#if canImport(Darwin)
12+
import Darwin.C
13+
#elseif canImport(Glibc)
14+
import Glibc
15+
#elseif canImport(MSVCRT)
16+
import MSVCRT
17+
#endif
18+
19+
@_cdecl("LLVMFuzzerTestOneInput")
20+
public func test(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
21+
let bytes = UnsafeRawBufferPointer(start: start, count: count)
22+
if bytes.starts(with: "ABC".utf8) {
23+
print("Crash!")
24+
fflush(stdout)
25+
exit(EXIT_FAILURE)
26+
}
27+
return 0
28+
}

0 commit comments

Comments
 (0)