aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/testing')
-rw-r--r--lib/std/testing/failing_allocator.zig28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/std/testing/failing_allocator.zig b/lib/std/testing/failing_allocator.zig
index 677ca6f51b..03e345986b 100644
--- a/lib/std/testing/failing_allocator.zig
+++ b/lib/std/testing/failing_allocator.zig
@@ -19,6 +19,10 @@ pub const FailingAllocator = struct {
freed_bytes: usize,
allocations: usize,
deallocations: usize,
+ stack_addresses: [num_stack_frames]usize,
+ has_induced_failure: bool,
+
+ const num_stack_frames = if (std.debug.sys_can_stack_trace) 16 else 0;
/// `fail_index` is the number of successful allocations you can
/// expect from this allocator. The next allocation will fail.
@@ -37,6 +41,8 @@ pub const FailingAllocator = struct {
.freed_bytes = 0,
.allocations = 0,
.deallocations = 0,
+ .stack_addresses = undefined,
+ .has_induced_failure = false,
};
}
@@ -52,6 +58,15 @@ pub const FailingAllocator = struct {
return_address: usize,
) error{OutOfMemory}![]u8 {
if (self.index == self.fail_index) {
+ if (!self.has_induced_failure) {
+ mem.set(usize, &self.stack_addresses, 0);
+ var stack_trace = std.builtin.StackTrace{
+ .instruction_addresses = &self.stack_addresses,
+ .index = 0,
+ };
+ std.debug.captureStackTrace(return_address, &stack_trace);
+ self.has_induced_failure = true;
+ }
return error.OutOfMemory;
}
const result = try self.internal_allocator.rawAlloc(len, ptr_align, len_align, return_address);
@@ -88,4 +103,17 @@ pub const FailingAllocator = struct {
self.deallocations += 1;
self.freed_bytes += old_mem.len;
}
+
+ /// Only valid once `has_induced_failure == true`
+ pub fn getStackTrace(self: *FailingAllocator) std.builtin.StackTrace {
+ std.debug.assert(self.has_induced_failure);
+ var len: usize = 0;
+ while (len < self.stack_addresses.len and self.stack_addresses[len] != 0) {
+ len += 1;
+ }
+ return .{
+ .instruction_addresses = &self.stack_addresses,
+ .index = len,
+ };
+ }
};