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

feat(c-api) Implement the traps argument of wasm_instance_new #1761

Merged
merged 7 commits into from
Oct 30, 2020

Conversation

Hywan
Copy link
Contributor

@Hywan Hywan commented Oct 26, 2020

Description

When running Instance::new, it can error with an
InstantiationError. There is 2 scenarii:

  1. Either it's a InstantiationError::Link. In this case, the
    wasm_instance_new function must return NULL and register the
    error in the Wasmer error registry.

  2. Either it's a InstantiationError::Start. In this case, the
    wasm_instance_new function must return NULL and the error must be
    converted into a wasm_trap_t*, which is stored in the wasm_trap_t**.

Review

  • Add a short description of the the change to the CHANGELOG.md file

Fixes #1744.

Hywan added 2 commits October 26, 2020 15:27
When running `Instance::new`, it can error with an
`InstantiationError`. There is 2 scenarii:

1. Either it's a `InstantiationError::Link`. In this case, the
   `wasm_instance_new` function must return `NULL` and register the
   error in the Wasmer error registry.

2. Either it's a `InstantiationError::Start`. In this case, the
   `wasm_instance_new` function must return `NULL` and the error must be
   converted into a `wasm_trap_t`, which is stored in the `wasm_trap_t**`
   array. This array is initialized by `wasm_instance_new` itself.
@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-c-api About wasmer-c-api 🧪 tests I love tests labels Oct 26, 2020
@Hywan Hywan requested a review from syrusakbary as a code owner October 26, 2020 14:30
@Hywan
Copy link
Contributor Author

Hywan commented Oct 26, 2020

I'm not sure why wasm_instance_new expects a wasm_trap_t**, i.e. an array of wasm_trap_t*. I don't see how can have more than one trap. Maybe @MarkMcCaskey or @syrusakbary can help me here.

Copy link
Contributor

@MarkMcCaskey MarkMcCaskey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good change overall, but I think we need to iterate a bit more on the ownership of this

@@ -17,8 +17,7 @@ pub unsafe extern "C" fn wasm_instance_new(
_store: &wasm_store_t,
module: &wasm_module_t,
imports: &wasm_extern_vec_t,
// own
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be useful to put this comment back if it's correct, otherwise it's not clear what the ownership is of *mut *mut, own means we're responsible for freeing it I believe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't own it. We can't free it. The start.c example explicitely drops the trap value:

own wasm_trap_t* trap = NULL;
own wasm_instance_t* instance =
wasm_instance_new(store, module, &imports, &trap);

and later:

I believe that the value is owned by the caller, not by the callee.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, own wasm_trap_t** is weird in the context of using arguments as outputs.

It seems like there's no need for the Vec at all then, this just seems to be a double pointer for the sake of setting a trap*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So wasm_trap_t** must not be interpreted as an array of wasm_trap_t*, hmm. Let's fix that then.

lib/c-api/src/wasm_c_api/instance.rs Outdated Show resolved Hide resolved
lib/c-api/src/wasm_c_api/instance.rs Outdated Show resolved Hide resolved
Hywan added 2 commits October 27, 2020 11:44
Ensure that the size of the vector of the instance's traps equals its
capacity by shrinking it.
Copy link
Contributor

@MarkMcCaskey MarkMcCaskey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the example in start.c, it seems that we just need one pointer here, not two. The Vec is never freed nor used here!

lib/c-api/src/wasm_c_api/instance.rs Outdated Show resolved Hide resolved
lib/c-api/src/wasm_c_api/instance.rs Outdated Show resolved Hide resolved
Hywan added 2 commits October 30, 2020 09:37
…an array.

The `wasm_trap_t**` argument of `wasm_instance_new` represents an
output pointer to a `wasm_trap_t*`, not an array of
`wasm_trap_t*`. This patch updates the code accordingly.
@Hywan
Copy link
Contributor Author

Hywan commented Oct 30, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 30, 2020

@bors bors bot merged commit 2d59b27 into wasmerio:master Oct 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! 📦 lib-c-api About wasmer-c-api 🧪 tests I love tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How do traps work in the C API?
4 participants