From 5d177d1328ebf0f8405994c4fb9ce7977ea40e9a Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 26 Apr 2022 15:45:47 +0200 Subject: [PATCH] runtime: be able to deal with a very small heap See the comment in the source for details. Also see the discussion in https://github.com/tinygo-org/tinygo/pull/2755, which originally triggered this bug. Somewhat surprising, this results in a slight code size decrease for ARM targets of a few bytes. --- src/runtime/gc_conservative.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/runtime/gc_conservative.go b/src/runtime/gc_conservative.go index b1ac1eb4d..e28618b3e 100644 --- a/src/runtime/gc_conservative.go +++ b/src/runtime/gc_conservative.go @@ -310,6 +310,13 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer { index = 0 // Reset numFreeBlocks as allocations cannot wrap. numFreeBlocks = 0 + // In rare cases, the initial heap might be so small that there are + // no blocks at all. In this case, it's better to jump back to the + // start of the loop and try again, until the GC realizes there is + // no memory and grows the heap. + // This can sometimes happen on WebAssembly, where the initial heap + // is created by whatever is left on the last memory page. + continue } // Is the block we're looking at free?