-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.zig
More file actions
585 lines (520 loc) · 21.7 KB
/
Copy pathtest_runner.zig
File metadata and controls
585 lines (520 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Custom test runner: identical to the standard Zig 0.16 test runner,
//! except the `log` function does NOT filter messages by `testing.log_level`.
//! This ensures `std.log.debug` output is always visible during tests.
//!
//! Based on: compiler/test_runner.zig (Zig 0.16)
const builtin = @import("builtin");
const std = @import("std");
const Io = std.Io;
const testing = std.testing;
const assert = std.debug.assert;
const panic = std.debug.panic;
const fuzz_abi = std.Build.abi.fuzz;
pub const std_options: std.Options = .{
.logFn = log,
};
var log_err_count: usize = 0;
var fba: std.heap.FixedBufferAllocator = .init(&fba_buffer);
var fba_buffer: [8192]u8 = undefined;
var stdin_buffer: [4096]u8 = undefined;
var stdout_buffer: [4096]u8 = undefined;
var stdin_reader: Io.File.Reader = undefined;
var stdout_writer: Io.File.Writer = undefined;
const runner_threaded_io: Io = Io.Threaded.global_single_threaded.io();
const need_simple = switch (builtin.zig_backend) {
.stage2_aarch64,
.stage2_powerpc,
.stage2_riscv64,
=> true,
else => false,
};
pub fn main(init: std.process.Init.Minimal) void {
@disableInstrumentation();
if (builtin.cpu.arch.isSpirV()) {
return;
}
if (need_simple) {
return mainSimple() catch |err| panic("test failure: {t}", .{err});
}
const args = init.args.toSlice(fba.allocator()) catch |err|
panic("unable to parse command line args: {t}", .{err});
var listen = false;
var opt_cache_dir: ?[]const u8 = null;
for (args[1..]) |arg| {
if (std.mem.eql(u8, arg, "--listen=-")) {
listen = true;
} else if (std.mem.startsWith(u8, arg, "--seed=")) {
testing.random_seed = std.fmt.parseUnsigned(u32, arg["--seed=".len..], 0) catch
@panic("unable to parse --seed command line argument");
} else if (std.mem.startsWith(u8, arg, "--cache-dir")) {
opt_cache_dir = arg["--cache-dir=".len..];
} else {
panic("unrecognized command line argument: {s}", .{arg});
}
}
if (builtin.fuzz) {
const cache_dir = opt_cache_dir orelse @panic("missing --cache-dir=[path] argument");
fuzz_abi.fuzzer_init(.fromSlice(cache_dir));
}
if (listen) {
return mainServer(init) catch |err| panic("internal test runner failure: {t}", .{err});
} else {
return mainTerminal(init);
}
}
fn mainServer(init: std.process.Init.Minimal) !void {
@disableInstrumentation();
stdin_reader = .initStreaming(.stdin(), runner_threaded_io, &stdin_buffer);
stdout_writer = .initStreaming(.stdout(), runner_threaded_io, &stdout_buffer);
var server = try std.zig.Server.init(.{
.in = &stdin_reader.interface,
.out = &stdout_writer.interface,
.zig_version = builtin.zig_version_string,
});
while (true) {
const hdr = try server.receiveMessage();
switch (hdr.tag) {
.exit => {
return std.process.exit(0);
},
.query_test_metadata => {
testing.allocator_instance = .{};
defer if (testing.allocator_instance.deinit() == .leak) {
@panic("internal test runner memory leak");
};
var string_bytes: std.ArrayList(u8) = .empty;
defer string_bytes.deinit(testing.allocator);
try string_bytes.append(testing.allocator, 0);
const test_fns = builtin.test_functions;
const names = try testing.allocator.alloc(u32, test_fns.len);
defer testing.allocator.free(names);
const expected_panic_msgs = try testing.allocator.alloc(u32, test_fns.len);
defer testing.allocator.free(expected_panic_msgs);
for (test_fns, names, expected_panic_msgs) |test_fn, *name, *expected_panic_msg| {
name.* = @intCast(string_bytes.items.len);
try string_bytes.ensureUnusedCapacity(testing.allocator, test_fn.name.len + 1);
string_bytes.appendSliceAssumeCapacity(test_fn.name);
string_bytes.appendAssumeCapacity(0);
expected_panic_msg.* = 0;
}
try server.serveTestMetadata(.{
.names = names,
.expected_panic_msgs = expected_panic_msgs,
.string_bytes = string_bytes.items,
});
},
.run_test => {
testing.environ = init.environ;
testing.allocator_instance = .{};
testing.io_instance = .init(testing.allocator, .{
.argv0 = .init(init.args),
.environ = init.environ,
});
log_err_count = 0;
const index = try server.receiveBody_u32();
const test_fn = builtin.test_functions[index];
is_fuzz_test = false;
try server.serveStringMessage(.test_started, &.{});
const TestResults = std.zig.Server.Message.TestResults;
const status: TestResults.Status = if (test_fn.func()) |v| s: {
v;
break :s .pass;
} else |err| switch (err) {
error.SkipZigTest => .skip,
else => s: {
if (@errorReturnTrace()) |trace| {
std.debug.dumpErrorReturnTrace(trace);
}
break :s .fail;
},
};
testing.io_instance.deinit();
const leak_count = testing.allocator_instance.detectLeaks();
testing.allocator_instance.deinitWithoutLeakChecks();
try server.serveTestResults(.{
.index = index,
.flags = .{
.status = status,
.fuzz = is_fuzz_test,
.log_err_count = std.math.lossyCast(
@FieldType(TestResults.Flags, "log_err_count"),
log_err_count,
),
.leak_count = std.math.lossyCast(
@FieldType(TestResults.Flags, "leak_count"),
leak_count,
),
},
});
},
.start_fuzzing => {
if (!builtin.fuzz) unreachable;
var gpa_instance: std.heap.DebugAllocator(.{}) = .init;
defer if (gpa_instance.deinit() == .leak) {
@panic("internal test runner memory leak");
};
const gpa = gpa_instance.allocator();
var io_instance: Io.Threaded = .init(gpa, .{
.argv0 = .init(init.args),
.environ = init.environ,
});
defer io_instance.deinit();
const io = io_instance.io();
const mode: fuzz_abi.LimitKind = @enumFromInt(try server.receiveBody_u8());
const amount_or_instance = try server.receiveBody_u64();
const main_instance = mode == .iterations or amount_or_instance == 0;
if (main_instance) {
const coverage = fuzz_abi.fuzzer_coverage();
try server.serveCoverageIdMessage(
coverage.id,
coverage.runs,
coverage.unique,
coverage.seen,
);
}
const n_tests: u32 = try server.receiveBody_u32();
const test_indexes = try gpa.alloc(u32, n_tests);
defer gpa.free(test_indexes);
fuzz_runner = .{
.indexes = test_indexes,
.server = &server,
.gpa = gpa,
.io = io,
.input_poller = undefined,
};
{
var large_name_buf: std.ArrayList(u8) = .empty;
defer large_name_buf.deinit(gpa);
for (test_indexes) |*i| {
const name_len = try server.receiveBody_u32();
const name = if (name_len <= server.in.buffer.len)
try server.in.take(name_len)
else large_name: {
try large_name_buf.resize(gpa, name_len);
try server.in.readSliceAll(large_name_buf.items);
break :large_name large_name_buf.items;
};
for (0.., builtin.test_functions) |test_i, test_fn| {
if (std.mem.eql(u8, name, test_fn.name)) {
i.* = @intCast(test_i);
break;
}
} else {
panic("fuzz test {s} no longer exists", .{name});
}
if (main_instance) {
const relocated_entry_addr = @intFromPtr(builtin.test_functions[i.*].func);
const entry_addr = fuzz_abi.fuzzer_unslide_address(relocated_entry_addr);
try server.serveU64Message(.fuzz_start_addr, entry_addr);
}
}
}
fuzz_abi.fuzzer_main(n_tests, testing.random_seed, mode, amount_or_instance);
assert(mode != .forever);
std.process.exit(0);
},
else => {
std.debug.print("unsupported message: {x}\n", .{@intFromEnum(hdr.tag)});
std.process.exit(1);
},
}
}
}
fn mainTerminal(init: std.process.Init.Minimal) void {
@disableInstrumentation();
if (builtin.fuzz) @panic("fuzz test requires server");
const test_fn_list = builtin.test_functions;
var ok_count: usize = 0;
var skip_count: usize = 0;
var fail_count: usize = 0;
var fuzz_count: usize = 0;
const root_node = if (builtin.fuzz) std.Progress.Node.none else std.Progress.start(runner_threaded_io, .{
.root_name = "Test",
.estimated_total_items = test_fn_list.len,
});
const have_tty = Io.File.stderr().isTty(runner_threaded_io) catch unreachable;
var leaks: usize = 0;
for (test_fn_list, 0..) |test_fn, i| {
testing.allocator_instance = .{};
testing.io_instance = .init(testing.allocator, .{
.argv0 = .init(init.args),
.environ = init.environ,
});
defer {
testing.io_instance.deinit();
if (testing.allocator_instance.deinit() == .leak) leaks += 1;
}
// NOTE: changed from standard test runner — do NOT reset testing.log_level
// to .warn here, so std.log.debug messages are always visible.
testing.environ = init.environ;
const test_node = root_node.start(test_fn.name, 0);
if (!have_tty) {
std.debug.print("{d}/{d} {s}...", .{ i + 1, test_fn_list.len, test_fn.name });
}
is_fuzz_test = false;
if (test_fn.func()) |_| {
ok_count += 1;
test_node.end();
if (!have_tty) std.debug.print("OK\n", .{});
} else |err| switch (err) {
error.SkipZigTest => {
skip_count += 1;
if (have_tty) {
std.debug.print("{d}/{d} {s}...SKIP\n", .{ i + 1, test_fn_list.len, test_fn.name });
} else {
std.debug.print("SKIP\n", .{});
}
test_node.end();
},
else => {
fail_count += 1;
if (have_tty) {
std.debug.print("{d}/{d} {s}...FAIL ({t})\n", .{
i + 1, test_fn_list.len, test_fn.name, err,
});
} else {
std.debug.print("FAIL ({t})\n", .{err});
}
if (@errorReturnTrace()) |trace| {
std.debug.dumpErrorReturnTrace(trace);
}
test_node.end();
},
}
fuzz_count += @intFromBool(is_fuzz_test);
}
root_node.end();
if (ok_count == test_fn_list.len) {
std.debug.print("All {d} tests passed.\n", .{ok_count});
} else {
std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count });
}
if (log_err_count != 0) {
std.debug.print("{d} errors were logged.\n", .{log_err_count});
}
if (leaks != 0) {
std.debug.print("{d} tests leaked memory.\n", .{leaks});
}
if (fuzz_count != 0) {
std.debug.print("{d} fuzz tests found.\n", .{fuzz_count});
}
if (leaks != 0 or log_err_count != 0 or fail_count != 0) {
std.process.exit(1);
}
}
// ── Custom log function ──────────────────────────────────────────
// Unlike the standard test runner, this always prints the message,
// regardless of `testing.log_level`. Debug logs are no longer suppressed.
pub fn log(
comptime message_level: std.log.Level,
comptime scope: @EnumLiteral(),
comptime format: []const u8,
args: anytype,
) void {
@disableInstrumentation();
if (@intFromEnum(message_level) <= @intFromEnum(std.log.Level.err)) {
log_err_count +|= 1;
}
std.debug.print(
"[" ++ @tagName(scope) ++ "] (" ++ @tagName(message_level) ++ "): " ++ format ++ "\n",
args,
);
}
/// Simpler main(), exercising fewer language features, so that
/// work-in-progress backends can handle it.
pub fn mainSimple() anyerror!void {
@disableInstrumentation();
const enable_write = switch (builtin.zig_backend) {
.stage2_aarch64, .stage2_riscv64 => true,
else => false,
};
const enable_print = switch (builtin.zig_backend) {
.stage2_aarch64, .stage2_riscv64 => true,
else => false,
};
testing.io_instance = .init(testing.allocator, .{});
var passed: u64 = 0;
var skipped: u64 = 0;
var failed: u64 = 0;
const stdout = if (enable_write) Io.File.stdout() else {};
for (builtin.test_functions) |test_fn| {
if (enable_write) {
stdout.writeStreamingAll(runner_threaded_io, test_fn.name) catch {};
stdout.writeStreamingAll(runner_threaded_io, "... ") catch {};
}
if (test_fn.func()) |_| {
if (enable_write) stdout.writeStreamingAll(runner_threaded_io, "PASS\n") catch {};
} else |err| {
if (err != error.SkipZigTest) {
if (enable_write) stdout.writeStreamingAll(runner_threaded_io, "FAIL\n") catch {};
failed += 1;
if (!enable_write) return err;
continue;
}
if (enable_write) stdout.writeStreamingAll(runner_threaded_io, "SKIP\n") catch {};
skipped += 1;
continue;
}
passed += 1;
}
if (enable_print) {
var unbuffered_stdout_writer = stdout.writer(runner_threaded_io, &.{});
unbuffered_stdout_writer.interface.print(
"{} passed, {} skipped, {} failed\n",
.{ passed, skipped, failed },
) catch {};
}
if (failed != 0) std.process.exit(1);
}
var is_fuzz_test: bool = undefined;
var fuzz_runner: if (builtin.fuzz) struct {
indexes: []u32,
server: *std.zig.Server,
gpa: std.mem.Allocator,
io: Io,
input_poller: Io.Future(Io.Cancelable!void),
comptime {
assert(builtin.fuzz);
}
export fn runner_test_run(i: u32) void {
@disableInstrumentation();
fuzz_runner.server.serveU32Message(.fuzz_test_change, i) catch |e| switch (e) {
error.WriteFailed => panic("failed to write to stdout: {t}", .{stdout_writer.err.?}),
};
testing.allocator_instance = .{};
defer if (testing.allocator_instance.deinit() == .leak) std.process.exit(1);
is_fuzz_test = false;
builtin.test_functions[fuzz_runner.indexes[i]].func() catch |err| switch (err) {
error.SkipZigTest => return,
else => {
if (@errorReturnTrace()) |trace| {
std.debug.dumpErrorReturnTrace(trace);
}
std.debug.print("failed with error.{t}\n", .{err});
std.process.exit(1);
},
};
if (!is_fuzz_test) @panic("missed call to std.testing.fuzz");
if (log_err_count != 0) @panic("error logs detected");
}
export fn runner_test_name(i: u32) fuzz_abi.Slice {
@disableInstrumentation();
return .fromSlice(builtin.test_functions[fuzz_runner.indexes[i]].name);
}
export fn runner_broadcast_input(test_i: u32, bytes_slice: fuzz_abi.Slice) void {
@disableInstrumentation();
const bytes = bytes_slice.toSlice();
fuzz_runner.server.serveBroadcastFuzzInputMessage(test_i, bytes) catch |e| switch (e) {
error.WriteFailed => panic("failed to write to stdout: {t}", .{stdout_writer.err.?}),
};
}
export fn runner_start_input_poller() void {
@disableInstrumentation();
const future = fuzz_runner.io.concurrent(inputPoller, .{}) catch |e| switch (e) {
error.ConcurrencyUnavailable => @panic("failed to spawn concurrent fuzz input poller"),
};
fuzz_runner.input_poller = future;
}
export fn runner_stop_input_poller() void {
@disableInstrumentation();
assert(fuzz_runner.input_poller.cancel(fuzz_runner.io) == error.Canceled);
}
export fn runner_futex_wait(ptr: *const u32, expected: u32) bool {
@disableInstrumentation();
return fuzz_runner.io.futexWait(u32, ptr, expected) == error.Canceled;
}
export fn runner_futex_wake(ptr: *const u32, waiters: u32) void {
@disableInstrumentation();
fuzz_runner.io.futexWake(u32, ptr, waiters);
}
fn inputPoller() Io.Cancelable!void {
@disableInstrumentation();
switch (inputPollerInner()) {
error.Canceled => return error.Canceled,
error.ReadFailed => {
if (stdin_reader.err.? == error.Canceled) return error.Canceled;
panic("failed to read from stdin: {t}", .{stdin_reader.err.?});
},
error.EndOfStream => @panic("unexpected end of stdin"),
}
}
fn inputPollerInner() (Io.Cancelable || Io.Reader.Error) {
@disableInstrumentation();
const server = fuzz_runner.server;
var large_bytes_list: std.ArrayList(u8) = .empty;
defer large_bytes_list.deinit(fuzz_runner.gpa);
while (true) {
const hdr = try server.receiveMessage();
if (hdr.tag != .new_fuzz_input) {
panic("unexpected message: {x}\n", .{@intFromEnum(hdr.tag)});
}
const test_i = try server.receiveBody_u32();
const input_len = hdr.bytes_len - 4;
const bytes = if (input_len <= server.in.buffer.len)
try server.in.take(input_len)
else bytes: {
large_bytes_list.resize(fuzz_runner.gpa, @intCast(input_len)) catch @panic("OOM");
try server.in.readSliceAll(large_bytes_list.items);
break :bytes large_bytes_list.items;
};
if (fuzz_abi.fuzzer_receive_input(test_i, .fromSlice(bytes))) {
return error.Canceled;
}
}
}
} else void = undefined;
pub fn fuzz(
context: anytype,
comptime testOne: fn (context: @TypeOf(context), *std.testing.Smith) anyerror!void,
options: testing.FuzzInputOptions,
) anyerror!void {
@disableInstrumentation();
if (need_simple) return;
is_fuzz_test = true;
if (log_err_count != 0) @panic("error logs detected");
const global = struct {
var ctx: @TypeOf(context) = undefined;
fn test_one() callconv(.c) bool {
@disableInstrumentation();
testing.allocator_instance = .{};
defer if (testing.allocator_instance.deinit() == .leak) std.process.exit(1);
log_err_count = 0;
testOne(ctx, @constCast(&testing.Smith{ .in = null })) catch |err| switch (err) {
error.SkipZigTest => return true,
else => {
const stderr = std.debug.lockStderr(&.{}).terminal();
p: {
if (@errorReturnTrace()) |trace| {
std.debug.writeStackTrace(trace, stderr) catch break :p;
}
stderr.writer.print("failed with error.{t}\n", .{err}) catch break :p;
}
std.process.exit(1);
},
};
if (log_err_count != 0) {
const stderr = std.debug.lockStderr(&.{}).terminal();
stderr.writer.print("error logs detected\n", .{}) catch {};
std.process.exit(1);
}
return false;
}
};
if (builtin.fuzz) {
const prev_allocator_state = testing.allocator_instance;
testing.allocator_instance = .{};
defer testing.allocator_instance = prev_allocator_state;
global.ctx = context;
fuzz_abi.fuzzer_set_test(&global.test_one);
for (options.corpus) |elem|
fuzz_abi.fuzzer_new_input(.fromSlice(elem));
fuzz_abi.fuzzer_start_test();
return;
}
for (options.corpus) |input| {
var smith: testing.Smith = .{ .in = input };
try testOne(context, &smith);
}
var smith: testing.Smith = .{ .in = "" };
try testOne(context, &smith);
}