authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-12-14 16:03:44+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-09 17:18:20-08:00
log828d23956d44b71f8b2394c6d7ab08c23d22fcc3
tree53c358e0ac6d2f81c4191527bd8fb2e4bcabf178
parent6a32d58876995f18b35ffd89b8875a99417c29cf

std.heap: add runtime safety for calling `stackFallback(N).get` multiple times

Closes #16344

2 files changed, 38 insertions(+), 13 deletions(-)

deps/aro/aro/Compilation.zig+10-6
......@@ -1350,9 +1350,10 @@ pub fn hasInclude(
13501350 }
13511351
13521352 var stack_fallback = std.heap.stackFallback(path_buf_stack_limit, comp.gpa);
1353 const sf_allocator = stack_fallback.get();
13531354
1354 while (try it.nextWithFile(filename, stack_fallback.get())) |found| {
1355 defer stack_fallback.get().free(found.path);
1355 while (try it.nextWithFile(filename, sf_allocator)) |found| {
1356 defer sf_allocator.free(found.path);
13561357 if (!std.meta.isError(cwd.access(found.path, .{}))) return true;
13571358 }
13581359 return false;
......@@ -1411,9 +1412,10 @@ pub fn findEmbed(
14111412 };
14121413 var it = IncludeDirIterator{ .comp = comp, .cwd_source_id = cwd_source_id };
14131414 var stack_fallback = std.heap.stackFallback(path_buf_stack_limit, comp.gpa);
1415 const sf_allocator = stack_fallback.get();
14141416
1415 while (try it.nextWithFile(filename, stack_fallback.get())) |found| {
1416 defer stack_fallback.get().free(found.path);
1417 while (try it.nextWithFile(filename, sf_allocator)) |found| {
1418 defer sf_allocator.free(found.path);
14171419 if (comp.getFileContents(found.path, limit)) |some|
14181420 return some
14191421 else |err| switch (err) {
......@@ -1457,8 +1459,10 @@ pub fn findInclude(
14571459 }
14581460
14591461 var stack_fallback = std.heap.stackFallback(path_buf_stack_limit, comp.gpa);
1460 while (try it.nextWithFile(filename, stack_fallback.get())) |found| {
1461 defer stack_fallback.get().free(found.path);
1462 const sf_allocator = stack_fallback.get();
1463
1464 while (try it.nextWithFile(filename, sf_allocator)) |found| {
1465 defer sf_allocator.free(found.path);
14621466 if (comp.addSourceFromPathExtra(found.path, found.kind)) |some| {
14631467 if (it.tried_ms_cwd) {
14641468 try comp.addDiagnostic(.{
lib/std/heap.zig+28-7
......@@ -521,10 +521,16 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
521521 buffer: [size]u8,
522522 fallback_allocator: Allocator,
523523 fixed_buffer_allocator: FixedBufferAllocator,
524 get_called: if (std.debug.runtime_safety) bool else void =
525 if (std.debug.runtime_safety) false else {},
524526
525527 /// This function both fetches a `Allocator` interface to this
526528 /// allocator *and* resets the internal buffer allocator.
527529 pub fn get(self: *Self) Allocator {
530 if (std.debug.runtime_safety) {
531 assert(!self.get_called); // `get` called multiple times; instead use `const allocator = stackFallback(N).get();`
532 self.get_called = true;
533 }
528534 self.fixed_buffer_allocator = FixedBufferAllocator.init(self.buffer[0..]);
529535 return .{
530536 .ptr = self,
......@@ -536,6 +542,12 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
536542 };
537543 }
538544
545 /// Unlike most std allocators `StackFallbackAllocator` modifies
546 /// its internal state before returning an implementation of
547 /// the`Allocator` interface and therefore also doesn't use
548 /// the usual `.allocator()` method.
549 pub const allocator = @compileError("use 'const allocator = stackFallback(N).get();' instead");
550
539551 fn alloc(
540552 ctx: *anyopaque,
541553 len: usize,
......@@ -675,13 +687,22 @@ test "FixedBufferAllocator.reset" {
675687}
676688
677689test "StackFallbackAllocator" {
678 const fallback_allocator = page_allocator;
679 var stack_allocator = stackFallback(4096, fallback_allocator);
680
681 try testAllocator(stack_allocator.get());
682 try testAllocatorAligned(stack_allocator.get());
683 try testAllocatorLargeAlignment(stack_allocator.get());
684 try testAllocatorAlignedShrink(stack_allocator.get());
690 {
691 var stack_allocator = stackFallback(4096, std.testing.allocator);
692 try testAllocator(stack_allocator.get());
693 }
694 {
695 var stack_allocator = stackFallback(4096, std.testing.allocator);
696 try testAllocatorAligned(stack_allocator.get());
697 }
698 {
699 var stack_allocator = stackFallback(4096, std.testing.allocator);
700 try testAllocatorLargeAlignment(stack_allocator.get());
701 }
702 {
703 var stack_allocator = stackFallback(4096, std.testing.allocator);
704 try testAllocatorAlignedShrink(stack_allocator.get());
705 }
685706}
686707
687708test "FixedBufferAllocator Reuse memory on realloc" {