add fuzzing script

This commit is contained in:
Brendan Hansknecht 2024-07-26 18:07:08 -07:00
parent 67241e8f6b
commit 383259e55f
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
3 changed files with 54 additions and 8 deletions

View file

@ -1,10 +1,3 @@
/// Sort Fuzzer!
/// To fuzz: On linux, first install afl++.
/// Then build this with:
/// zig build-lib -static -fcompiler-rt -flto -fPIC src/fuzz_sort.zig
/// afl-clang-lto -o fuzz libfuzz_sort.a
/// Finally, run with afl
/// afl-fuzz -i input -o output -- ./fuzz
const std = @import("std");
const sort = @import("sort.zig");
@ -19,6 +12,8 @@ comptime {
@export(cMain, .{ .name = "main", .linkage = .Strong });
}
const DEBUG = false;
var allocator: std.mem.Allocator = undefined;
pub fn fuzz_main() !void {
@ -36,9 +31,17 @@ pub fn fuzz_main() !void {
const size = data.len / @sizeOf(i64);
const arr_ptr: [*]i64 = @alignCast(@ptrCast(data.ptr));
if (DEBUG) {
std.debug.print("Input: [{d}]{d}\n", .{ size, arr_ptr[0..size] });
}
sort.quadsort(@ptrCast(arr_ptr), size, &test_i64_compare, null, false, &test_i64_inc_n, @sizeOf(i64), @alignOf(i64), &test_i64_copy);
std.debug.assert(std.sort.isSorted(i64, arr_ptr[0..size], {}, std.sort.asc(i64)));
const sorted = std.sort.isSorted(i64, arr_ptr[0..size], {}, std.sort.asc(i64));
if (DEBUG) {
std.debug.print("Output: [{d}]{d}\nSorted: {}\n", .{ size, arr_ptr[0..size], sorted });
}
std.debug.assert(sorted);
}
const Opaque = ?[*]u8;