diff --git a/provider/src/alloc.rs b/provider/src/alloc.rs index e4c9bc9..ba18e39 100644 --- a/provider/src/alloc.rs +++ b/provider/src/alloc.rs @@ -1,34 +1,14 @@ -use std::alloc::{alloc, dealloc, Layout}; -use std::ptr::copy_nonoverlapping; +use std::alloc::{alloc, Layout}; const ZERO_SIZE_ALLOCATION_PTR: *mut u8 = 1 as _; // Allocation functions -#[export_name = "shopify_function_realloc"] -pub unsafe extern "C" fn shopify_function_realloc( - original_ptr: *mut u8, - original_size: usize, - alignment: usize, - new_size: usize, -) -> *mut std::ffi::c_void { - assert!(new_size >= original_size); - - let new_mem = match new_size { +#[export_name = "shopify_function_alloc"] +pub unsafe extern "C" fn shopify_function_alloc(size: usize) -> *mut std::ffi::c_void { + let new_mem = match size { 0 => ZERO_SIZE_ALLOCATION_PTR, - // this call to `alloc` is safe since `new_size` must be > 0 - _ => alloc(Layout::from_size_align(new_size, alignment).unwrap()), + // this call to `alloc` is safe since `size` must be > 0 + _ => alloc(Layout::from_size_align(size, 1).unwrap()), }; - - if !original_ptr.is_null() && original_size != 0 { - copy_nonoverlapping(original_ptr, new_mem, original_size); - shopify_function_free(original_ptr, original_size, alignment); - } new_mem as _ } - -#[export_name = "shopify_function_free"] -pub unsafe extern "C" fn shopify_function_free(ptr: *mut u8, size: usize, alignment: usize) { - if size > 0 { - dealloc(ptr, Layout::from_size_align(size, alignment).unwrap()) - }; -} diff --git a/trampoline/src/lib.rs b/trampoline/src/lib.rs index 627e493..96a956d 100644 --- a/trampoline/src/lib.rs +++ b/trampoline/src/lib.rs @@ -102,7 +102,7 @@ pub struct TrampolineCodegen { provider_memory_id: OnceCell, memcpy_to_guest: OnceCell, memcpy_to_provider: OnceCell, - imported_shopify_function_realloc: OnceCell, + imported_shopify_function_alloc: OnceCell, alloc: OnceCell, } @@ -116,7 +116,7 @@ impl TrampolineCodegen { provider_memory_id: OnceCell::new(), memcpy_to_guest: OnceCell::new(), memcpy_to_provider: OnceCell::new(), - imported_shopify_function_realloc: OnceCell::new(), + imported_shopify_function_alloc: OnceCell::new(), alloc: OnceCell::new(), }) } @@ -207,25 +207,23 @@ impl TrampolineCodegen { }) } - fn emit_shopify_function_realloc_import(&mut self) -> FunctionId { - *self.imported_shopify_function_realloc.get_or_init(|| { - let shopify_function_realloc_type = self.module.types.add( - &[ValType::I32, ValType::I32, ValType::I32, ValType::I32], - &[ValType::I32], - ); + fn emit_shopify_function_alloc_import(&mut self) -> FunctionId { + *self.imported_shopify_function_alloc.get_or_init(|| { + let shopify_function_alloc_type = + self.module.types.add(&[ValType::I32], &[ValType::I32]); - let (imported_shopify_function_realloc, _) = self.module.add_import_func( + let (imported_shopify_function_alloc, _) = self.module.add_import_func( PROVIDER_MODULE_NAME, - "shopify_function_realloc", - shopify_function_realloc_type, + "shopify_function_alloc", + shopify_function_alloc_type, ); - imported_shopify_function_realloc + imported_shopify_function_alloc }) } fn emit_alloc(&mut self) -> FunctionId { - let imported_shopify_function_realloc = self.emit_shopify_function_realloc_import(); + let imported_shopify_function_alloc = self.emit_shopify_function_alloc_import(); *self.alloc.get_or_init(|| { let mut alloc = @@ -235,11 +233,8 @@ impl TrampolineCodegen { alloc .func_body() - .i32_const(0) - .i32_const(0) - .i32_const(1) .local_get(size) - .call(imported_shopify_function_realloc); + .call(imported_shopify_function_alloc); alloc.finish(vec![size], &mut self.module.funcs) }) diff --git a/trampoline/src/snapshots/shopify_function_trampoline__test__disassemble_trampoline@consumer.wat.snap b/trampoline/src/snapshots/shopify_function_trampoline__test__disassemble_trampoline@consumer.wat.snap index ba1cfc0..ad3c150 100644 --- a/trampoline/src/snapshots/shopify_function_trampoline__test__disassemble_trampoline@consumer.wat.snap +++ b/trampoline/src/snapshots/shopify_function_trampoline__test__disassemble_trampoline@consumer.wat.snap @@ -12,21 +12,20 @@ input_file: trampoline/src/test_data/consumer.wat (type (;5;) (func (param i32 i32 i32))) (type (;6;) (func (param i32 i32 i32) (result i32))) (type (;7;) (func (param i32 i32 i32 i32))) - (type (;8;) (func (param i32 i32 i32 i32) (result i32))) - (type (;9;) (func (param i32 i64) (result i32))) - (type (;10;) (func (param i32 i64 i32) (result i64))) - (type (;11;) (func (param i32 i64 i32 i32) (result i64))) - (type (;12;) (func (param i32 f64) (result i32))) + (type (;8;) (func (param i32 i64) (result i32))) + (type (;9;) (func (param i32 i64 i32) (result i64))) + (type (;10;) (func (param i32 i64 i32 i32) (result i64))) + (type (;11;) (func (param i32 f64) (result i32))) (import "shopify_function_v1" "_shopify_function_context_new" (func (;0;) (type 0))) (import "shopify_function_v1" "_shopify_function_input_get" (func (;1;) (type 2))) - (import "shopify_function_v1" "_shopify_function_input_get_interned_obj_prop" (func (;2;) (type 10))) - (import "shopify_function_v1" "_shopify_function_input_get_at_index" (func (;3;) (type 10))) - (import "shopify_function_v1" "_shopify_function_input_get_obj_key_at_index" (func (;4;) (type 10))) - (import "shopify_function_v1" "_shopify_function_input_get_val_len" (func (;5;) (type 9))) + (import "shopify_function_v1" "_shopify_function_input_get_interned_obj_prop" (func (;2;) (type 9))) + (import "shopify_function_v1" "_shopify_function_input_get_at_index" (func (;3;) (type 9))) + (import "shopify_function_v1" "_shopify_function_input_get_obj_key_at_index" (func (;4;) (type 9))) + (import "shopify_function_v1" "_shopify_function_input_get_val_len" (func (;5;) (type 8))) (import "shopify_function_v1" "_shopify_function_output_new_bool" (func (;6;) (type 3))) (import "shopify_function_v1" "_shopify_function_output_new_null" (func (;7;) (type 1))) (import "shopify_function_v1" "_shopify_function_output_new_i32" (func (;8;) (type 1))) - (import "shopify_function_v1" "_shopify_function_output_new_f64" (func (;9;) (type 12))) + (import "shopify_function_v1" "_shopify_function_output_new_f64" (func (;9;) (type 11))) (import "shopify_function_v1" "_shopify_function_output_new_object" (func (;10;) (type 3))) (import "shopify_function_v1" "_shopify_function_output_finish_object" (func (;11;) (type 1))) (import "shopify_function_v1" "_shopify_function_output_new_array" (func (;12;) (type 3))) @@ -35,8 +34,8 @@ input_file: trampoline/src/test_data/consumer.wat (import "shopify_function_v1" "_shopify_function_output_finalize" (func (;15;) (type 1))) (import "shopify_function_v1" "_shopify_function_input_get_utf8_str_addr" (func (;16;) (type 3))) (import "shopify_function_v1" "memory" (memory (;0;) 1)) - (import "shopify_function_v1" "_shopify_function_input_get_obj_prop" (func (;17;) (type 11))) - (import "shopify_function_v1" "shopify_function_realloc" (func (;18;) (type 8))) + (import "shopify_function_v1" "_shopify_function_input_get_obj_prop" (func (;17;) (type 10))) + (import "shopify_function_v1" "shopify_function_alloc" (func (;18;) (type 1))) (import "shopify_function_v1" "_shopify_function_output_new_utf8_str" (func (;19;) (type 4))) (import "shopify_function_v1" "_shopify_function_intern_utf8_str" (func (;20;) (type 4))) (memory (;1;) 1) @@ -54,7 +53,7 @@ input_file: trampoline/src/test_data/consumer.wat i32.wrap_i64 local.get 1 local.get 2 - call 27 + call 26 ) (func (;22;) (type 6) (param i32 i32 i32) (result i32) (local i64) @@ -69,16 +68,16 @@ input_file: trampoline/src/test_data/consumer.wat i32.wrap_i64 local.get 1 local.get 2 - call 27 + call 26 ) - (func (;23;) (type 11) (param i32 i64 i32 i32) (result i64) + (func (;23;) (type 10) (param i32 i64 i32 i32) (result i64) (local i32) local.get 3 - call 25 + call 27 local.tee 4 local.get 2 local.get 3 - call 27 + call 26 local.get 0 local.get 1 local.get 4 @@ -91,27 +90,24 @@ input_file: trampoline/src/test_data/consumer.wat local.get 1 call 16 local.get 3 - call 26 - ) - (func (;25;) (type 1) (param i32) (result i32) - i32.const 0 - i32.const 0 - i32.const 1 - local.get 0 - call 18 + call 25 ) - (func (;26;) (type 5) (param i32 i32 i32) + (func (;25;) (type 5) (param i32 i32 i32) local.get 0 local.get 1 local.get 2 memory.copy 1 0 ) - (func (;27;) (type 5) (param i32 i32 i32) + (func (;26;) (type 5) (param i32 i32 i32) local.get 0 local.get 1 local.get 2 memory.copy 0 1 ) + (func (;27;) (type 1) (param i32) (result i32) + local.get 0 + call 18 + ) (@producers (processed-by "walrus" "0.23.3") )