authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-03 13:28:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-04 17:12:45-07:00
logcac7e5afc7624ef18fe7bc8c6acd743f8b5a7eaf
treeb918d0f139f66b74d95b98ded3fdc66a4c52b661
parent790b8428a26457e7ed9ea20485b9d3085011b989

add std.debug.assertReadable

Useful when trying to figure out whether a slice is valid memory.

1 files changed, 17 insertions(+), 7 deletions(-)

lib/std/debug.zig+17-7
...@@ -398,20 +398,30 @@ pub fn dumpStackTrace(stack_trace: std.builtin.StackTrace) void {...@@ -398,20 +398,30 @@ pub fn dumpStackTrace(stack_trace: std.builtin.StackTrace) void {
398 }398 }
399}399}
400400
401/// This function invokes undefined behavior when `ok` is `false`.401/// Invokes detectable illegal behavior when `ok` is `false`.
402///
402/// In Debug and ReleaseSafe modes, calls to this function are always403/// In Debug and ReleaseSafe modes, calls to this function are always
403/// generated, and the `unreachable` statement triggers a panic.404/// generated, and the `unreachable` statement triggers a panic.
404/// In ReleaseFast and ReleaseSmall modes, calls to this function are405///
405/// optimized away, and in fact the optimizer is able to use the assertion406/// In ReleaseFast and ReleaseSmall modes, calls to this function are optimized
406/// in its heuristics.407/// away, and in fact the optimizer is able to use the assertion in its
407/// Inside a test block, it is best to use the `std.testing` module rather408/// heuristics.
408/// than this function, because this function may not detect a test failure409///
409/// in ReleaseFast and ReleaseSmall mode. Outside of a test block, this assert410/// Inside a test block, it is best to use the `std.testing` module rather than
411/// this function, because this function may not detect a test failure in
412/// ReleaseFast and ReleaseSmall mode. Outside of a test block, this assert
410/// function is the correct function to use.413/// function is the correct function to use.
411pub fn assert(ok: bool) void {414pub fn assert(ok: bool) void {
412 if (!ok) unreachable; // assertion failure415 if (!ok) unreachable; // assertion failure
413}416}
414417
418/// Invokes detectable illegal behavior when the provided slice is not mapped
419/// or lacks read permissions.
420pub fn assertReadable(slice: []const volatile u8) void {
421 if (!runtime_safety) return;
422 for (slice) |*byte| _ = byte.*;
423}
424
415pub fn panic(comptime format: []const u8, args: anytype) noreturn {425pub fn panic(comptime format: []const u8, args: anytype) noreturn {
416 @setCold(true);426 @setCold(true);
417427