mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 07:03:11 +00:00
WasmAllocator: fix safety panic during OOM
This commit is contained in:
parent
ff7ca4b70f
commit
a7282d0910
@ -55,7 +55,7 @@ fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*
|
|||||||
const addr = a: {
|
const addr = a: {
|
||||||
const top_free_ptr = frees[class];
|
const top_free_ptr = frees[class];
|
||||||
if (top_free_ptr != 0) {
|
if (top_free_ptr != 0) {
|
||||||
const node = @as(*usize, @ptrFromInt(top_free_ptr + (slot_size - @sizeOf(usize))));
|
const node: *usize = @ptrFromInt(top_free_ptr + (slot_size - @sizeOf(usize)));
|
||||||
frees[class] = node.*;
|
frees[class] = node.*;
|
||||||
break :a top_free_ptr;
|
break :a top_free_ptr;
|
||||||
}
|
}
|
||||||
@ -74,11 +74,10 @@ fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*
|
|||||||
break :a next_addr;
|
break :a next_addr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return @as([*]u8, @ptrFromInt(addr));
|
return @ptrFromInt(addr);
|
||||||
}
|
}
|
||||||
const bigpages_needed = bigPagesNeeded(actual_len);
|
const bigpages_needed = bigPagesNeeded(actual_len);
|
||||||
const addr = allocBigPages(bigpages_needed);
|
return @ptrFromInt(allocBigPages(bigpages_needed));
|
||||||
return @as([*]u8, @ptrFromInt(addr));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(
|
fn resize(
|
||||||
@ -123,14 +122,14 @@ fn free(
|
|||||||
const class = math.log2(slot_size) - min_class;
|
const class = math.log2(slot_size) - min_class;
|
||||||
const addr = @intFromPtr(buf.ptr);
|
const addr = @intFromPtr(buf.ptr);
|
||||||
if (class < size_class_count) {
|
if (class < size_class_count) {
|
||||||
const node = @as(*usize, @ptrFromInt(addr + (slot_size - @sizeOf(usize))));
|
const node: *usize = @ptrFromInt(addr + (slot_size - @sizeOf(usize)));
|
||||||
node.* = frees[class];
|
node.* = frees[class];
|
||||||
frees[class] = addr;
|
frees[class] = addr;
|
||||||
} else {
|
} else {
|
||||||
const bigpages_needed = bigPagesNeeded(actual_len);
|
const bigpages_needed = bigPagesNeeded(actual_len);
|
||||||
const pow2_pages = math.ceilPowerOfTwoAssert(usize, bigpages_needed);
|
const pow2_pages = math.ceilPowerOfTwoAssert(usize, bigpages_needed);
|
||||||
const big_slot_size_bytes = pow2_pages * bigpage_size;
|
const big_slot_size_bytes = pow2_pages * bigpage_size;
|
||||||
const node = @as(*usize, @ptrFromInt(addr + (big_slot_size_bytes - @sizeOf(usize))));
|
const node: *usize = @ptrFromInt(addr + (big_slot_size_bytes - @sizeOf(usize)));
|
||||||
const big_class = math.log2(pow2_pages);
|
const big_class = math.log2(pow2_pages);
|
||||||
node.* = big_frees[big_class];
|
node.* = big_frees[big_class];
|
||||||
big_frees[big_class] = addr;
|
big_frees[big_class] = addr;
|
||||||
@ -148,15 +147,14 @@ fn allocBigPages(n: usize) usize {
|
|||||||
|
|
||||||
const top_free_ptr = big_frees[class];
|
const top_free_ptr = big_frees[class];
|
||||||
if (top_free_ptr != 0) {
|
if (top_free_ptr != 0) {
|
||||||
const node = @as(*usize, @ptrFromInt(top_free_ptr + (slot_size_bytes - @sizeOf(usize))));
|
const node: *usize = @ptrFromInt(top_free_ptr + (slot_size_bytes - @sizeOf(usize)));
|
||||||
big_frees[class] = node.*;
|
big_frees[class] = node.*;
|
||||||
return top_free_ptr;
|
return top_free_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const page_index = @wasmMemoryGrow(0, pow2_pages * pages_per_bigpage);
|
const page_index = @wasmMemoryGrow(0, pow2_pages * pages_per_bigpage);
|
||||||
if (page_index <= 0) return 0;
|
if (page_index == -1) return 0;
|
||||||
const addr = @as(u32, @intCast(page_index)) * wasm.page_size;
|
return @as(usize, @intCast(page_index)) * wasm.page_size;
|
||||||
return addr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const test_ally = Allocator{
|
const test_ally = Allocator{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user