Skip to content

Commit aaf24c4

Browse files
committed
Rewrite FixedBufferAllocator. Simplify code, documenting behavior better. Fix a bug where a very large allocation could overflow a usize, causing a false positive allocation. Provide limited comptime allocator support.
1 parent e0dc2e4 commit aaf24c4

File tree

9 files changed

+205
-160
lines changed

9 files changed

+205
-160
lines changed

lib/compiler/aro/aro/Driver/Filesystem.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ fn findProgramByNameFake(entries: []const Filesystem.Entry, name: []const u8, pa
2323
}
2424
const path_env = path orelse return null;
2525
var fib = std.heap.FixedBufferAllocator.init(buf);
26+
const fib_initial_state = fib.savestate();
2627

2728
var it = mem.tokenizeScalar(u8, path_env, std.fs.path.delimiter);
2829
while (it.next()) |path_dir| {
29-
defer fib.reset();
30+
defer fib.restore(fib_initial_state);
3031
const full_path = std.fs.path.join(fib.allocator(), &.{ path_dir, name }) catch continue;
3132
if (canExecuteFake(entries, full_path)) return full_path;
3233
}
@@ -84,10 +85,11 @@ fn findProgramByNamePosix(name: []const u8, path: ?[]const u8, buf: []u8) ?[]con
8485
}
8586
const path_env = path orelse return null;
8687
var fib = std.heap.FixedBufferAllocator.init(buf);
88+
const fib_initial_state = fib.savestate();
8789

8890
var it = mem.tokenizeScalar(u8, path_env, std.fs.path.delimiter);
8991
while (it.next()) |path_dir| {
90-
defer fib.reset();
92+
defer fib.restore(fib_initial_state);
9193
const full_path = std.fs.path.join(fib.allocator(), &.{ path_dir, name }) catch continue;
9294
if (canExecutePosix(full_path)) return full_path;
9395
}

lib/compiler/aro/aro/Toolchain.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,15 @@ pub fn addFilePathLibArgs(tc: *const Toolchain, argv: *std.ArrayList([]const u8)
216216
fn getProgramPath(tc: *const Toolchain, name: []const u8, buf: []u8) []const u8 {
217217
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
218218
var fib = std.heap.FixedBufferAllocator.init(&path_buf);
219+
const fib_initial_state = fib.savestate();
219220

220221
var tool_specific_buf: [64]u8 = undefined;
221222
var possible_name_buf: [2][]const u8 = undefined;
222223
const possible_names = possibleProgramNames(tc.driver.raw_target_triple, name, &tool_specific_buf, &possible_name_buf);
223224

224225
for (possible_names) |tool_name| {
225226
for (tc.program_paths.items) |program_path| {
226-
defer fib.reset();
227+
defer fib.restore(fib_initial_state);
227228

228229
const candidate = std.fs.path.join(fib.allocator(), &.{ program_path, tool_name }) catch continue;
229230

lib/compiler/test_runner.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub fn main() void {
3737
return mainSimple() catch @panic("test failure\n");
3838
}
3939

40+
const fba_initial_state = fba.savestate();
4041
const args = std.process.argsAlloc(fba.allocator()) catch
4142
@panic("unable to parse command line args");
4243

@@ -61,7 +62,7 @@ pub fn main() void {
6162
fuzz_abi.fuzzer_init(.fromSlice(cache_dir));
6263
}
6364

64-
fba.reset();
65+
fba.restore(fba_initial_state);
6566

6667
if (listen) {
6768
return mainServer() catch @panic("internal test runner failure");

lib/std/heap.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
442442
ra: usize,
443443
) bool {
444444
const self: *Self = @ptrCast(@alignCast(ctx));
445-
if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
445+
if (mem.sliceOwnsPtr(u8, &self.buffer, @ptrCast(buf.ptr))) {
446446
return FixedBufferAllocator.resize(&self.fixed_buffer_allocator, buf, alignment, new_len, ra);
447447
} else {
448448
return self.fallback_allocator.rawResize(buf, alignment, new_len, ra);
@@ -457,7 +457,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
457457
return_address: usize,
458458
) ?[*]u8 {
459459
const self: *Self = @ptrCast(@alignCast(context));
460-
if (self.fixed_buffer_allocator.ownsPtr(memory.ptr)) {
460+
if (mem.sliceOwnsPtr(u8, &self.buffer, @ptrCast(memory.ptr))) {
461461
return FixedBufferAllocator.remap(&self.fixed_buffer_allocator, memory, alignment, new_len, return_address);
462462
} else {
463463
return self.fallback_allocator.rawRemap(memory, alignment, new_len, return_address);
@@ -471,7 +471,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
471471
ra: usize,
472472
) void {
473473
const self: *Self = @ptrCast(@alignCast(ctx));
474-
if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
474+
if (mem.sliceOwnsPtr(u8, &self.buffer, @ptrCast(buf.ptr))) {
475475
return FixedBufferAllocator.free(&self.fixed_buffer_allocator, buf, alignment, ra);
476476
} else {
477477
return self.fallback_allocator.rawFree(buf, alignment, ra);

0 commit comments

Comments
 (0)