Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: implement Canonical ABI realloc (cabi_realloc) #2

Closed
6 tasks
ydnar opened this issue Oct 16, 2023 · 3 comments · Fixed by #3
Closed
6 tasks

proposal: implement Canonical ABI realloc (cabi_realloc) #2

ydnar opened this issue Oct 16, 2023 · 3 comments · Fixed by #3
Assignees

Comments

@ydnar
Copy link
Collaborator

ydnar commented Oct 16, 2023

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
@dgryski
Copy link
Collaborator

dgryski commented Oct 16, 2023

The current TinyGo garbage collector exports the following realloc implementation:

func realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer

@ydnar
Copy link
Collaborator Author

ydnar commented Oct 16, 2023

@dgryski is that exported in the Wasm module?

@dgryski
Copy link
Collaborator

dgryski commented Oct 16, 2023

No, it's currently only available to the runtime. "Exports" should have read "exposes to the rest of the runtime".

ydnar added a commit that referenced this issue Oct 17, 2023
ydnar added a commit that referenced this issue Oct 17, 2023
@ydnar ydnar self-assigned this Oct 18, 2023
@ydnar ydnar changed the title proposal: cabi_realloc proposal: implement Canonical ABI realloc (cabi_realloc) Oct 19, 2023
ydnar added a commit that referenced this issue Oct 19, 2023
@ydnar ydnar closed this as completed in #3 Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants