Skip to content

proposal: implement Canonical ABI realloc (cabi_realloc) #2

Closed
@ydnar

Description

@ydnar

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

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 for GOOS=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
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions