authorgravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-12-04 22:43:02-06:00
committergravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-12-04 22:43:02-06:00
log30da6d49f435b7ef317b059113ec1fab21d72d00
tree50a3cef70f3f0b3b331b77247cf9aedd489cd48b
parent86ae75363e3fbf6b3835b87c1cfb79fe4bf97790

Fix freeing memory across bounds


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

lib/std/heap.zig+7-3
...@@ -379,8 +379,11 @@ const WasmPageAllocator = struct {...@@ -379,8 +379,11 @@ const WasmPageAllocator = struct {
379 }379 }
380380
381 if (free_start < extendedOffset()) {381 if (free_start < extendedOffset()) {
382 conventional.recycle(free_start, free_end - free_start);382 const clamped_end = std.math.min(extendedOffset(), free_end);
383 } else {383 conventional.recycle(free_start, clamped_end - free_start);
384 }
385
386 if (free_end > extendedOffset()) {
384 if (extended.totalPages() == 0) {387 if (extended.totalPages() == 0) {
385 // Steal the last page from the memory currently being recycled388 // Steal the last page from the memory currently being recycled
386 // TODO: would it be better if we use the first page instead?389 // TODO: would it be better if we use the first page instead?
...@@ -390,7 +393,8 @@ const WasmPageAllocator = struct {...@@ -390,7 +393,8 @@ const WasmPageAllocator = struct {
390 // Since this is the first page being freed and we consume it, assume *nothing* is free.393 // Since this is the first page being freed and we consume it, assume *nothing* is free.
391 std.mem.set(u8, extended.bytes, FreeBlock.used);394 std.mem.set(u8, extended.bytes, FreeBlock.used);
392 }395 }
393 extended.recycle(free_start - extendedOffset(), free_end - free_start);396 const clamped_start = std.math.max(extendedOffset(), free_start);
397 extended.recycle(clamped_start - extendedOffset(), free_end - clamped_start);
394 }398 }
395 }399 }
396400