Stream: general

Topic: MemoryCreator C API


view this post on Zulip dsd (Jun 13 2023 at 18:30):

Hey there!

At dfinity, we are working on integrating wasmtime with our reference implementation written in Haskell. We bind to the C-API and we have created wasmtime-hs.

While it is not an urgent issue at this point, we might want to manage the memory ourselves at some point. The rust API exposes an trait MemoryCreator which allows for this. However, analogous functions seems to be missing from the C API. Are there any plans to add such functions? Are there any principle difficulties in implementing that interface for C?

Many thanks, dsd

Haskell bindings to the wasmtime WASM engine. Contribute to dfinity/wasmtime-hs development by creating an account on GitHub.

view this post on Zulip fitzgen (he/him) (Jun 13 2023 at 18:43):

In general, all functionality exposed in the Rust API should also be exposed in the C API, it is just a matter of demand and effort.

For this particular case, since C doesn't have the concept of traits and trait objects, we would need to have C-specific equivalents that are basically structs of function pointers and then add the relevant trait impls for those concrete structs of pointers. Will need a concrete struct of pointers and trait impl for that concrete type for both MemoryCreator and LinearMemory

view this post on Zulip dsd (Jun 13 2023 at 19:09):

@fitzgen (he/him) many thanks for the quick reply.

So, essentially, when configuring the wasm store or instance, you pass a C-callback that creates memory. Internally, you would create an impl of MemoryCreator that wraps the callback. Likewise, the pointer returns from the callback would need to be turned into a LinearMemory. Is my understanding correct?

view this post on Zulip Alex Crichton (Jun 13 2023 at 19:18):

Along those lines yeah I think would work. A strawman might looks something like:

typedef wasmtime_error_t *(*wasmtime_new_memory_t)(
    wasm_memorytype_t *ty,
    size_t minimum,
    size_t maximum,
    size_t reserved_size_in_bytes,
    size_t guard_size_in_bytes,
    void **ret);

typedef void *(*wasmtime_memory_get_t)(
    void *ptr,
    size_t *byte_size,
    size_t *maximum_byte_size);

typedef wasmtime_error_t *(*wasmtime_memory_grow_t)(
    void *ptr,
    size_t new_size);

typedef struct {
  wasmtime_new_memory_t new_memory;
  wasmtime_memory_get_t get_memory;
  wasmtime_memory_grow_t grow_memory;
} wasmtime_memory_creator_t;

void wasmtime_config_memory_creator_set(wasmtime_config_t *config, wasmtime_memory_creator_t *creator);

view this post on Zulip Alex Crichton (Jun 13 2023 at 19:18):

perhaps with more contextual pointers in more places, soemthing like that

view this post on Zulip Alex Crichton (Jun 13 2023 at 19:18):

and probably with more affordances for destructors as well so probably a drop_memory callback too

view this post on Zulip Alex Crichton (Jun 13 2023 at 19:19):

but basically the Rust code would take these C function pointers and turn them into a trait implementation of MemoryCreator and LinearMemory


Last updated: Dec 23 2024 at 12:05 UTC