Skip to content

Commit

Permalink
Add Tokenizer fuzz test
Browse files Browse the repository at this point in the history
To test this, I reverted `aed02e737f7850026d736f46683d254b64a0c1af` and the fuzz test found
the crash within approximately 0.2 seconds.
  • Loading branch information
ehaas authored Aug 1, 2024
1 parent 6f27119 commit 8ab72a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/aro/Tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,28 @@ test "C23 keywords" {
}, .c23);
}

test "Tokenizer fuzz test" {
var comp = Compilation.init(std.testing.allocator);
defer comp.deinit();

const input_bytes = std.testing.fuzzInput(.{});
if (input_bytes.len == 0) return;

const source = try comp.addSourceFromBuffer("fuzz.c", input_bytes);

var tokenizer: Tokenizer = .{
.buf = source.buf,
.source = source.id,
.langopts = comp.langopts,
};
while (true) {
const prev_index = tokenizer.index;
const tok = tokenizer.next();
if (tok.id == .eof) break;
try std.testing.expect(prev_index < tokenizer.index); // ensure that the tokenizer always makes progress
}
}

fn expectTokensExtra(contents: []const u8, expected_tokens: []const Token.Id, standard: ?LangOpts.Standard) !void {
var comp = Compilation.init(std.testing.allocator);
defer comp.deinit();
Expand Down
7 changes: 6 additions & 1 deletion test/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ If a test case is currently broken, it should be commented and a `TESTS_SKIPPED`
// <something broken here>
```
---
## Running fuzz tests
## Running fuzz tests using AFLplusplus
Fuzz testing requires [AFLplusplus](https://github.com/AFLplusplus/AFLplusplus). Run `zig build fuzz` to build the fuzz target,
then `afl-fuzz -i test/cases -o test/fuzz-output -- ./zig-out/bin/arofuzz`

Expand All @@ -109,3 +109,8 @@ docker run --rm -it -v $PWD:/arocc -w /arocc --mount type=tmpfs,destination=/ram
zig build fuzz # This might take a while
afl-fuzz -i test/cases -o test/fuzz-output -- ./zig-out/bin/arofuzz
```
---
## Running fuzz tests via Zig build system
```sh-session
zig build test-unit --fuzz
```

0 comments on commit 8ab72a4

Please sign in to comment.