From ac1f21b71cce2a8f1b7eb4ee04c1e24083b41bcf Mon Sep 17 00:00:00 2001 From: max-lt Date: Wed, 26 Nov 2025 23:53:46 +0100 Subject: [PATCH] fix(ext/runtime): rollback memory count #645 When allocate, allocate_uninitialized, or reallocate exceeded the memory limit, the count was incremented but never rolled back on failure. This caused all future allocations to fail permanently, even small legitimate ones. --- ext/runtime/external_memory.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/runtime/external_memory.rs b/ext/runtime/external_memory.rs index 17bcc8d62..53fdb55b9 100644 --- a/ext/runtime/external_memory.rs +++ b/ext/runtime/external_memory.rs @@ -59,6 +59,7 @@ unsafe extern "C" fn allocate( let count_loaded = allocator.count.load(Ordering::SeqCst); if count_loaded > allocator.max { + allocator.count.fetch_sub(n, Ordering::SeqCst); return std::ptr::null::<*mut [u8]>() as *mut c_void; } @@ -78,6 +79,7 @@ unsafe extern "C" fn allocate_uninitialized( let count_loaded = allocator.count.load(Ordering::SeqCst); if count_loaded > allocator.max { + allocator.count.fetch_sub(n, Ordering::SeqCst); return std::ptr::null::<*mut [u8]>() as *mut c_void; } @@ -114,6 +116,9 @@ unsafe extern "C" fn reallocate( let count_loaded = allocator.count.load(Ordering::SeqCst); if count_loaded > allocator.max { + allocator + .count + .fetch_sub(newlen.wrapping_sub(oldlen), Ordering::SeqCst); return std::ptr::null::<*mut [u8]>() as *mut c_void; }