From 4a532aa58cf1d76e79b694d11fe0ce69143bf428 Mon Sep 17 00:00:00 2001 From: zach Date: Fri, 27 Jun 2025 13:26:16 -0700 Subject: [PATCH] cleanup(kernel): undo some kernel changes --- kernel/src/lib.rs | 30 +++++++++++++++--------------- runtime/src/extism-runtime.wasm | Bin 3711 -> 3369 bytes runtime/src/tests/kernel.rs | 11 ++++++----- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 9b6a663..96542f7 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -263,24 +263,24 @@ impl MemoryRoot { let mem_left = self_length - self_position - core::mem::size_of::() as u64; let length_with_block = length + core::mem::size_of::() as u64; - // If the current position is large enough to hold the length of the block being - // allocated then check for existing free blocks that can be re-used before - // growing memory - if length_with_block <= self_position { - let b = self.find_free_block(length, self_position); - - // If there's a free block then re-use it - if let Some(b) = b { - b.used = length as usize; - b.status - .store(MemoryStatus::Active as u8, Ordering::Release); - return Some(b); - } - } - // When the allocation is larger than the number of bytes available // we will need to try to grow the memory if length_with_block >= mem_left { + // If the current position is large enough to hold the length of the block being + // allocated then check for existing free blocks that can be re-used before + // growing memory + if length_with_block <= self_position { + let b = self.find_free_block(length, self_position); + + // If there's a free block then re-use it + if let Some(b) = b { + b.used = length as usize; + b.status + .store(MemoryStatus::Active as u8, Ordering::Release); + return Some(b); + } + } + // Calculate the number of pages needed to cover the remaining bytes let npages = num_pages(length_with_block - mem_left); let x = core::arch::wasm32::memory_grow(0, npages); diff --git a/runtime/src/extism-runtime.wasm b/runtime/src/extism-runtime.wasm index e62df686fa2b25450cccd99d7ee5132c5d282a4b..fe7f2d096638165d55b21e8f1ab510ab47f341ba 100644 GIT binary patch literal 3369 zcmdT`!Hye65UuJS+hdPs*9#=D%B5!xQ7o~CC}GX!63oO+1Vs!*N$wj zM37@gLPA`S5C<-Zvrj&7IX><`Aa;%hX-djJPJ0hWkCM~guyd4>E@ypmD#!iK zVRHIDmE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XD7h4fYZrsV*kqQuHN7 z-}&%PGR459XejzkDKGbDsLT-!Vr8hK+i?vTIc-i8)S;upB>V#CL!%Cp`ZQ^n`ZeL< z02NV^j_PWBUA?_Eq9yGUQ)ImJ%8Px}-Stpy(5E8PFrM)TwmC?=!)qgz+F4-J3SXzz@WQoL$cbK-npKj=OITM-yqtJv0QbZ3)Cw_^oKv6`G zeq?4S-q2?#Y6^!IZr6wtwru#^C1OVp3!KY@m@L6R1RoaW8#*qUPSIQ_nu|rVistB8 zZ|Id`T(?i)+&(ZI zAPkjTK)OKK^XSsg&;>d~%#758oVQu!62yWM5kpDJVyFY~fs4wh=p6+G&%_*L7Usb; z(d8ao!~#(60XpdDa?c}11b9b;LSxFZQI*`)3zxg(bg{P$Ho~|fbbKon(r}%gF1^@zTTg7XQ8oBi^#nu$}n}F^_LdO3HH*R9VebM2bponu} z;odS-2^}~3i@w)I=0euX4jP|E(2J)L^nwI^%rCX5=T$w%k&q|9c*gO1)7fG|-g(TI xuYUO-p!`SRs`B$9)|0;3s<+TR8`tE~Ef2LEddAx)=X&*24#w(f$Yn)>{tZGgM7;n2 diff --git a/runtime/src/tests/kernel.rs b/runtime/src/tests/kernel.rs index 32dbee1..d104ca6 100644 --- a/runtime/src/tests/kernel.rs +++ b/runtime/src/tests/kernel.rs @@ -238,7 +238,7 @@ fn test_kernel_page_allocations() { let c = extism_alloc(&mut store, instance, 65500 * 2); let c_size = extism_length(&mut store, instance, c); - let d = extism_alloc(&mut store, instance, 65500); + let d = extism_alloc(&mut store, instance, 65536); assert_eq!(a + (a_size - c_size), c); assert!(c < b); @@ -461,19 +461,20 @@ quickcheck! { fn check_block_reuse(allocs: Vec) -> bool { let (mut store, mut instance) = init_kernel_test(); let instance = &mut instance; - let init = extism_alloc(&mut store, instance, allocs.iter().map(|x| *x as u64).sum::() + allocs.len() as u64 * 64); + let init = extism_alloc(&mut store, instance, allocs.iter().map(|x| *x as u64).sum::() + (allocs.len() as u64 * (65535 + 128))); let bounds = init + extism_length(&mut store, instance, init); extism_free(&mut store, instance, init); for a in allocs { - let ptr = extism_alloc(&mut store, instance, a as u64); + let ptr = extism_alloc(&mut store, instance, a as u64 + 65535); if ptr == 0 { continue } - if extism_length(&mut store, instance, ptr) != a as u64 { + if extism_length(&mut store, instance, ptr) != a as u64 + 65535 { + println!("FAILED ALLOC"); return false } - extism_free(&mut store, instance , ptr); + extism_free(&mut store, instance, ptr); if ptr > bounds { println!("ptr={ptr}, bounds={bounds}");