pub const fuzz = struct Returns if error.SkipZigTest was indicated
pub extern fn fuzzer_init(cache_dir_path: Slice) voidpub extern fn fuzzer_coverage() Coveragefuzzer_init must be called first.
pub extern fn fuzzer_unslide_address(addr: usize) usizepub extern fn fuzzer_main(n_tests: u32, seed: u32, limit_kind: LimitKind, amount_or_instance: u64) voidPerforms all the fuzzing work and selects tests to run
fuzzer_init must be called first.
pub extern fn runner_test_run(i: u32) voidpub extern fn runner_test_name(i: u32) Slicepub extern fn runner_start_input_poller() voidpub extern fn runner_stop_input_poller() voidpub extern fn runner_futex_wait(*const u32, expected: u32) boolReturns if cancelation has been indicated.
pub extern fn runner_futex_wake(*const u32, waiters: u32) voidpub extern fn runner_broadcast_input(test_i: u32, bytes: Slice) voidpub extern fn fuzzer_receive_input(test_i: u32, bytes: Slice) boolfuzzer_main must be called first.
Called concurrently with fuzzer_main. Returns if cancelation has been indicated.
pub extern fn fuzzer_set_test(test_one: TestOne) voidMust be called from inside a test function
pub extern fn fuzzer_new_input(bytes: Slice) voidMust be called from inside a test function where fuzzer_set_test has been called first.
pub extern fn fuzzer_start_test() voidMust be called from inside a test function where fuzzer_set_test has been called first.
pub extern fn fuzzer_int(uid: Uid, weights: Weights) u64pub extern fn fuzzer_eos(uid: Uid, weights: Weights) boolpub extern fn fuzzer_bytes(uid: Uid, out: MutSlice, weights: Weights) voidpub extern fn fuzzer_slice(uid: Uid, buf: MutSlice, len_weights: Weights, byte_weights: Weights) u32141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437pub const fuzz = struct {
/// Returns if `error.SkipZigTest` was indicated
pub const TestOne = *const fn () callconv(.c) bool;
/// A unique value to identify the related requests across runs
pub const Uid = packed struct(u32) {
kind: enum(u1) { int, bytes },
hash: u31,
pub const hashmap_ctx = struct {
pub fn hash(_: @This(), u: Uid) u32 {
// We can ignore `kind` since `hash` should be unique regardless
return u.hash;
}
pub fn eql(_: @This(), a: Uid, b: Uid, _: usize) bool {
return a == b;
}
};
};
pub extern fn fuzzer_init(cache_dir_path: Slice) void;
/// `fuzzer_init` must be called first.
pub extern fn fuzzer_coverage() Coverage;
pub extern fn fuzzer_unslide_address(addr: usize) usize;
/// Performs all the fuzzing work and selects tests to run
///
/// `fuzzer_init` must be called first.
pub extern fn fuzzer_main(
n_tests: u32,
seed: u32,
limit_kind: LimitKind,
amount_or_instance: u64,
) void;
pub extern fn runner_test_run(i: u32) void;
pub extern fn runner_test_name(i: u32) Slice;
// Since the runner owns the `std.zig.Server` instance, it also controls the
// concurrent Io instance so reads can be canceled. As such, the fuzzer has
// to call into the runner for any zig server / concurrent operation.
pub extern fn runner_start_input_poller() void;
pub extern fn runner_stop_input_poller() void;
/// Returns if cancelation has been indicated.
pub extern fn runner_futex_wait(*const u32, expected: u32) bool;
pub extern fn runner_futex_wake(*const u32, waiters: u32) void;
pub extern fn runner_broadcast_input(test_i: u32, bytes: Slice) void;
/// `fuzzer_main` must be called first.
///
/// Called concurrently with `fuzzer_main`. Returns if cancelation has been indicated.
pub extern fn fuzzer_receive_input(test_i: u32, bytes: Slice) bool;
/// Must be called from inside a test function
pub extern fn fuzzer_set_test(test_one: TestOne) void;
/// Must be called from inside a test function where `fuzzer_set_test` has been called first.
pub extern fn fuzzer_new_input(bytes: Slice) void;
/// Must be called from inside a test function where `fuzzer_set_test` has been called first.
pub extern fn fuzzer_start_test() void;
pub extern fn fuzzer_int(uid: Uid, weights: Weights) u64;
pub extern fn fuzzer_eos(uid: Uid, weights: Weights) bool;
pub extern fn fuzzer_bytes(uid: Uid, out: MutSlice, weights: Weights) void;
pub extern fn fuzzer_slice(
uid: Uid,
buf: MutSlice,
len_weights: Weights,
byte_weights: Weights,
) u32;
pub const Slice = extern struct {
ptr: [*]const u8,
len: usize,
pub fn toSlice(s: Slice) []const u8 {
return s.ptr[0..s.len];
}
pub fn fromSlice(s: []const u8) Slice {
return .{ .ptr = s.ptr, .len = s.len };
}
};
pub const MutSlice = extern struct {
ptr: [*]u8,
len: usize,
pub fn toSlice(s: MutSlice) []u8 {
return s.ptr[0..s.len];
}
pub fn fromSlice(s: []u8) MutSlice {
return .{ .ptr = s.ptr, .len = s.len };
}
};
pub const Weights = extern struct {
ptr: [*]const Weight,
len: usize,
pub fn toSlice(s: Weights) []const Weight {
return s.ptr[0..s.len];
}
pub fn fromSlice(s: []const Weight) Weights {
return .{ .ptr = s.ptr, .len = s.len };
}
};
/// Increases the probability of values being selected by the fuzzer.
///
/// `weight` applies to each value in the range (i.e. not evenly across
/// the range) and must be nonzero.
///
/// In a set of weights, the total weight must not exceed 2^64 and be
/// nonzero.
pub const Weight = extern struct {
/// Inclusive
min: u64,
/// Inclusive
max: u64,
weight: u64,
/// `inline` to propogate comptimeness
inline fn intFromValue(x: anytype) u64 {
const T = @TypeOf(x);
return switch (@typeInfo(T)) {
.comptime_int => x,
.bool => @intFromBool(x),
.@"enum" => @backingInt(x),
else => @as(@Int(.unsigned, @bitSizeOf(T)), @bitCast(x)),
.int => |i| x: {
comptime {
if (i.signedness == .signed) {
@compileError("type does not have a continous range: " ++ @typeName(T));
}
// Reject types that don't have a fixed bitsize (esp. usize)
// since they are not gauraunteed to fit in a u64 across targets.
//
// std.mem.indexOfScalar is not used to avoid backward branches
// and preserve the eval branch quota.
if (T == usize or T == c_char or T == c_ushort or
T == c_uint or T == c_ulong or T == c_ulonglong)
{
@compileError("type does not have a fixed bitsize: " ++ @typeName(T));
}
}
break :x x;
},
.comptime_float,
.float,
=> @compileError("type does not have a continous range: " ++ @typeName(T)),
.pointer => @compileError("type does not have a fixed bitsize: " ++ @typeName(T)),
};
}
/// `inline` to propogate comptimeness
pub inline fn value(T: type, x: T, weight: u64) Weight {
return .{ .min = intFromValue(x), .max = intFromValue(x), .weight = weight };
}
/// `inline` to propogate comptimeness
pub inline fn rangeAtMost(T: type, at_least: T, at_most: T, weight: u64) Weight {
std.debug.assert(intFromValue(at_least) <= intFromValue(at_most));
return .{
.min = intFromValue(at_least),
.max = intFromValue(at_most),
.weight = weight,
};
}
/// `inline` to propogate comptimeness
pub inline fn rangeLessThan(T: type, at_least: T, less_than: T, weight: u64) Weight {
std.debug.assert(intFromValue(at_least) < intFromValue(less_than));
return .{
.min = intFromValue(at_least),
.max = intFromValue(less_than) - 1,
.weight = weight,
};
}
};
pub const LimitKind = enum(u8) { forever, iterations };
/// libfuzzer uses this and its usize is the one that counts. To match the ABI,
/// make the ints be the size of the target used with libfuzzer.
///
/// Trailing:
/// * 1 bit per pc_addr, usize elements
/// * pc_addr: usize for each pcs_len
pub const SeenPcsHeader = extern struct {
n_runs: usize,
unique_runs: usize,
pcs_len: usize,
/// Used for comptime assertions. Provides a mechanism for strategically
/// causing compile errors.
pub const trailing = .{
.pc_bits_usize,
.pc_addr,
};
pub fn headerEnd(header: *const SeenPcsHeader) []const usize {
const ptr: [*]align(@alignOf(usize)) const u8 = @ptrCast(header);
const header_end_ptr: [*]const usize = @ptrCast(ptr + @sizeOf(SeenPcsHeader));
const pcs_len = header.pcs_len;
return header_end_ptr[0 .. pcs_len + seenElemsLen(pcs_len)];
}
pub fn seenBits(header: *const SeenPcsHeader) []const usize {
return header.headerEnd()[0..seenElemsLen(header.pcs_len)];
}
pub fn seenElemsLen(pcs_len: usize) usize {
return (pcs_len + @bitSizeOf(usize) - 1) / @bitSizeOf(usize);
}
pub fn pcAddrs(header: *const SeenPcsHeader) []const usize {
const pcs_len = header.pcs_len;
return header.headerEnd()[seenElemsLen(pcs_len)..][0..pcs_len];
}
};
/// Fields are little-endian
pub const MmapInputHeader = extern struct {
pc_digest: u64 align(4), // aligned so header does not have padding
instance_id: u32,
test_i: u32,
len: u32,
};
/// WebSocket server->client.
///
/// Sent once, when fuzzing starts, to indicate the available coverage data.
///
/// Trailing:
/// * std.debug.Coverage.String for each directories_len
/// * std.debug.Coverage.File for each files_len
/// * std.debug.Coverage.SourceLocation for each source_locations_len
/// * u8 for each string_bytes_len
pub const SourceIndexHeader = extern struct {
tag: ToClientTag = .fuzz_source_index,
_: [3]u8 = @splat(0),
directories_len: u32,
files_len: u32,
source_locations_len: u32,
string_bytes_len: u32,
/// When, according to the server, fuzzing started.
start_timestamp: i64 align(4),
start_n_runs: u64 align(4),
};
/// WebSocket server->client.
///
/// Sent whenever the set of covered source locations is updated.
///
/// Trailing:
/// * one bit per source_locations_len, contained in u64 elements
pub const CoverageUpdateHeader = extern struct {
tag: ToClientTag = .fuzz_coverage_update,
_: [7]u8 = @splat(0),
n_runs: u64,
unique_runs: u64,
pub const trailing = .{
.pc_bits_usize,
};
};
/// WebSocket server->client.
///
/// Sent whenever the set of entry points is updated.
///
/// Trailing:
/// * one u32 index of source_locations per locsLen()
pub const EntryPointHeader = extern struct {
tag: ToClientTag = .fuzz_entry_points,
locs_len_raw: [3]u8,
pub fn locsLen(hdr: EntryPointHeader) u24 {
return @bitCast(hdr.locs_len_raw);
}
pub fn init(locs_len: u24) EntryPointHeader {
return .{ .locs_len_raw = @bitCast(locs_len) };
}
};
/// Sent by lib/fuzzer to test_runner to obtain information about the
/// active memory mapped input file and cumulative stats about previous
/// fuzzing runs.
pub const Coverage = extern struct {
id: u64,
runs: u64,
unique: u64,
seen: u64,
};
}
ABI bits specifically relating to the fuzzer interface.