Skip to content

Update docs and tests for LLVMFuzzerTestOneInput #31802

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
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
36 changes: 19 additions & 17 deletions docs/libFuzzerIntegration.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
libFuzzer Integration
---------------------

Swift compiler comes with a built-in `libFuzzer` integration.
In order to use it on a file `myfile.swift`, we define an entry point fuzzing function
with a `@_cdecl("LLVMFuzzerTestOneInput")` annotation:
# libFuzzer Integration

Custom builds of the Swift toolchain (including development snapshots)
have a built-in `libFuzzer` integration. In order to use it on a file
`myfile.swift`, define an entry point fuzzing function with a
`@_cdecl("LLVMFuzzerTestOneInput")` annotation:

```swift
@_cdecl("LLVMFuzzerTestOneInput") public func fuzzMe(Data: UnsafePointer<CChar>, Size: CInt) -> CInt{
// Test our code using provided Data.
}
@_cdecl("LLVMFuzzerTestOneInput")
public func test(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
let bytes = UnsafeRawBufferPointer(start: start, count: count)
// TODO: Test the code using the provided bytes.
return 0
}
```

To compile it, we use `-sanitize=fuzzer` flag to link `libFuzzer`
and enable coverage annotation, and `-parse-as-library` flag not to insert
the `main` symbol, such that the fuzzer entry point can be used:
To compile it, use the `-sanitize=fuzzer` flag to link `libFuzzer`
and enable code coverage information; and the `-parse-as-library` flag
to omit the `main` symbol, so that the fuzzer entry point can be used:

```bash
% swiftc -sanitize=fuzzer -parse-as-library myfile.swift
```

`libFuzzer` can be also combined with other sanitizers:
`libFuzzer` can be combined with other sanitizers:

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

Finally, we launch the fuzzing process:
Finally, launch the fuzzing process:

```bash
% ./a.out
% ./myfile
```

Refer to the official `libFuzzer` documentation at http://llvm.org/docs/LibFuzzer.html
for the description of flags the resulting binary has.
Refer to the official `libFuzzer` documentation at
<https://llvm.org/docs/LibFuzzer.html#options>
for a description of the fuzzer's command line options.
5 changes: 3 additions & 2 deletions test/Driver/fuzzer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// LIBFUZZER_OSX: libclang_rt.fuzzer
// LIBFUZZER_LINUX: -fsanitize=address,fuzzer

@_cdecl("LLVMFuzzerTestOneInput") public func fuzzOneInput(Data: UnsafePointer<CChar>, Size: CLong) -> CInt {
return 0;
@_cdecl("LLVMFuzzerTestOneInput")
public func test(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
return 0
}
28 changes: 0 additions & 28 deletions test/Fuzzing/fuzzer_test.swift

This file was deleted.

34 changes: 0 additions & 34 deletions test/Fuzzing/fuzzer_test_simpler.swift

This file was deleted.

28 changes: 28 additions & 0 deletions validation-test/Sanitizers/fuzzer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %target-build-swift -parse-as-library -sanitize=fuzzer %s -o %t
// RUN: not %t -only_ascii=1 -max_len=3 | %FileCheck %s
// REQUIRES: CPU=x86_64
// REQUIRES: executable_test
// REQUIRES: fuzzer_runtime
// XFAIL: OS=ios
// XFAIL: OS=tvos
// XFAIL: OS=watchos
// CHECK: Crash!

#if canImport(Darwin)
import Darwin.C
#elseif canImport(Glibc)
import Glibc
#elseif canImport(MSVCRT)
import MSVCRT
#endif

@_cdecl("LLVMFuzzerTestOneInput")
public func test(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
let bytes = UnsafeRawBufferPointer(start: start, count: count)
if bytes.starts(with: "ABC".utf8) {
print("Crash!")
fflush(stdout)
exit(EXIT_FAILURE)
}
return 0
}