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

Provide wasmtime_config_with_host_memory in the C API #6636

Closed
basvandijk opened this issue Jun 23, 2023 · 4 comments · Fixed by #7115
Closed

Provide wasmtime_config_with_host_memory in the C API #6636

basvandijk opened this issue Jun 23, 2023 · 4 comments · Fixed by #7115
Labels
wasmtime:c-api Issues pertaining to the C API. wasmtime:config Issues related to the configuration of Wasmtime

Comments

@basvandijk
Copy link

Feature

We're working on a Haskell binding to wasmtime called wasmtime-hs.

In our project we would like to have access to the Config::with_host_memory(...) functionality.
Could this function be provided in the C API?

Benefit

At the moment this functionality is only available in the Rust library. It would be nice if other languages have access to the same functionality.

Implementation

Do you have an implementation plan, and/or ideas for data structures or
algorithms to use?

Not yet, it probably has to look something like this but I haven't thought about it too much yet:

#[no_mangle]
pub unsafe extern "C" fn wasmtime_config_with_host_memory(
    c: &mut wasm_config_t,
    mem_creator_callback: wasmtime_new_memory_callback_t,
) {
    c.config.with_host_memory(...);
}

pub type wasmtime_new_memory_callback_t = extern "C" fn(
    ty: &wasm_memorytype_t,
    minimum: u64,
    maximum_specified: bool,
    maximum: u64,
    reserved_size_in_bytes_specified: bool,
    reserved_size_in_bytes: u64,
    guard_size_in_bytes: bool,
) -> Box<dyn LinearMemory>
@jameysharp jameysharp added wasmtime:c-api Issues pertaining to the C API. wasmtime:config Issues related to the configuration of Wasmtime labels Jun 23, 2023
@github-actions

This comment was marked as off-topic.

@jameysharp
Copy link
Contributor

This sounds reasonable to me but I'm not sure what design principles we're following for Wasmtime's C API, so I'd like to get opinions from @alexcrichton or @fitzgen on what this should look like.

Huh, maybe I shouldn't have used the wasmtime:config label. I wasn't expecting that particular auto-reply.

@alexcrichton
Copy link
Member

This was discussed a bit on Zulip as well and my strawman there was:

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);

Personally I don't think the details matter too too much so long as it all works for your use case, we can always iterate over time as necessary.

Note though that in the original suggestion an extern "C" fn can't return Box<dyn LinearMemory> because that's a Rust-defined structure which C can't mirror, so you'll need to package up those callbacks in addition to the wasmtime_new_memory_callback_t type

@basvandijk
Copy link
Author

@alexcrichton thanks, I wasn't aware of that chat by my colleague :). But your strawman looks like a great start!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:c-api Issues pertaining to the C API. wasmtime:config Issues related to the configuration of Wasmtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants