Closed
Description
In order for host → guest calls to pass complex types with unknown sizes, the host must be able to allocate memory in the guest. See: https://github.com/search?q=repo%3Abytecodealliance%2Fwit-bindgen%20cabi_realloc&type=code
The realloc
function defined in the Canonical ABI has the following signature: (func (param i32 i32 i32 i32) (result i32))
Prerequisites
- Go
- cmd/compile: create GOARCH=wasm32 golang/go#63131
- proposal: cmd/compile: go:wasmexport directive golang/go#42372
- How does this interact with the Go GC?
- Who owns memory after guest → host call?
- Who owns memory after host → guest call?
- TinyGo
- How does this interact with the TinyGo GC?
TODO
- Where should this live?
import _ github.com/ydnar/wasm-tools-go/abi
? - Blank import or will this package also include lift/lower helpers?
- Long term plan to move the
abi
package into Go stdlib, or vendor it forGOOS=wasip2
support?
Design
Go
Hypothetical Go design:
// Probably should be an unsafe.Pointer
type uintptr32 = uint32
//go:wasmexport cabi_realloc
func cabi_realloc(ptr, oldsize, align, newsize uintptr32) uintptr32
Prior Art
C
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
(void) old_size;
if (new_size == 0) return (void*) align;
void *ret = realloc(ptr, new_size);
if (!ret) abort();
return ret;
}
Rust
unsafe extern "C" fn cabi_realloc(
old_ptr: *mut u8,
old_len: usize,
align: usize,
new_len: usize,
) -> *mut u8 {
// ...
}
Python
def realloc(self, original_ptr, original_size, alignment, new_size):
if original_ptr != 0 and new_size < original_size:
return align_to(original_ptr, alignment)
ret = align_to(self.last_alloc, alignment)
self.last_alloc = ret + new_size
if self.last_alloc > len(self.memory):
print('oom: have {} need {}'.format(len(self.memory), self.last_alloc))
trap()
self.memory[ret : ret + original_size] = self.memory[original_ptr : original_ptr + original_size]
return ret
Metadata
Metadata
Assignees
Labels
No labels