Commit Graph

709 Commits

Author SHA1 Message Date
Ivan Enderlin
a68a1e67f4 feat(c-api) Transmute boxed vecs to Vec<Option<Box<T>>> when deleting.
It's a safer way to handle partially uninitialized boxed vector, since
it protects against based deletion for every item.
2020-12-18 11:40:32 +01:00
Ivan Enderlin
51fe219efb feat(c-api) wasm_$name_vec_delete for boxed vec now takes an Option<&mut T>.
This was already the case for regular vec. This patch applies the same
pattern for boxed vec.

See deec77d2df.
2020-12-18 10:45:57 +01:00
Mark McCaskey
62663ab5e1 Get multi.c working in the Wasm C API 2020-12-17 14:35:55 -08:00
Ivan Enderlin
96169de8f0 test+doc(c-api) Improve test coverage for the macros module, and improve doc. 2020-12-17 14:57:07 +01:00
Ivan Enderlin
8aa08225cd feat(c-api) wasm_$name_vec_delete checks the vec is initialized.
In case of a boxed vector, `wasm_$name_vec_delete` now checks that the
vec is correctly initialized (by checking the first item only) because
transmuting `Vec<*mut T>` to `Vec<Box<T>>`, otherwise it will crash.
2020-12-17 14:55:29 +01:00
Ivan Enderlin
940dea72e7 feat(c-api) Simplify code by using new conversion implementations. 2020-12-17 14:17:50 +01:00
Ivan Enderlin
43026e651e feat(c-api) Implement From<&[T]> for wasm_$name_vec_t for boxed vec. 2020-12-17 14:17:17 +01:00
Ivan Enderlin
4abf6f81da fix(c-api) Fix how wasm_frame_t is implemented.
In `wasm.h`, `wasm_frame_t` is implemented with `WASM_DECLARE_TYPE`,
so with `WASM_DECLARE_VEC(frame, *)`. This `*` means the C struct for
the vector is defined:

```c
struct wasm_frame_vec_t {
    size_t size;
    wasm_frame_t** data;
}
```

The way we implement `wasm_frame_vec_t` in Rust is with the
`wasm_declare_vec!` macro. And it is wrong. We must use
`wasm_declared_boxed_vec!`.
2020-12-17 14:15:28 +01:00
Ivan Enderlin
a0c34fe850 fix(c-api) Fix how wasm_tabletype_t is implemented.
In `wasm.h`, `wasm_tabletype_t` is implemented with
`WASM_DECLARE_TYPE`, so with `WASM_DECLARE_VEC(tabletype, *)`. This
`*` means the C struct for the vector is defined:

```c
struct wasm_tabletype_vec_t {
    size_t size;
    wasm_tabletype_t** data;
}
```

The way we implement `wasm_tabletype_vec_t` in Rust is with the
`wasm_declare_vec!` macro. And it is wrong. We must use
`wasm_declared_boxed_vec!`.
2020-12-17 14:00:07 +01:00
Ivan Enderlin
a3c7a2d752 fix(c-api) Fix how wasm_memorytype_t is implemented.
In `wasm.h`, `wasm_memorytype_t` is implemented with
`WASM_DECLARE_TYPE`, so with `WASM_DECLARE_VEC(memorytype, *)`. This
`*` means the C struct for the vector is defined:

```c
struct wasm_memorytype_vec_t {
    size_t size;
    wasm_memorytype_t** data;
}
```

The way we implement `wasm_memorytype_vec_t` in Rust is with the
`wasm_declare_vec!` macro. And it is wrong. We must use
`wasm_declared_boxed_vec!`.
2020-12-17 13:58:26 +01:00
Ivan Enderlin
75ceb0d9e0 fix(c-api) Fix how wasm_globaltype_t is implemented.
In `wasm.h`, `wasm_globaltype_t` is implemented with
`WASM_DECLARE_TYPE`, so with `WASM_DECLARE_VEC(globaltype, *)`. This
`*` means the C struct for the vector is defined:

```c
struct wasm_globaltype_vec_t {
    size_t size;
    wasm_globaltype_t** data;
}
```

The way we implement `wasm_globaltype_vec_t` in Rust is with the
`wasm_declare_vec!` macro. And it is wrong. We must use
`wasm_declared_boxed_vec!`.
2020-12-17 13:56:48 +01:00
Ivan Enderlin
e10ad512bc fix(c-api) Fix how wasm_functype_t is implemented.
In `wasm.h`, `wasm_functype_t` is implemented with
`WASM_DECLARE_TYPE`, so with `WASM_DECLARE_VEC(functype, *)`. This `*`
means the C struct for the vector is defined:

```c
struct wasm_functype_vec_t {
    size_t size;
    wasm_functype_t** data;
}
```

The way we implement `wasm_functype_vec_t` in Rust is with the
`wasm_declare_vec!` macro. And it is wrong. We must use
`wasm_declared_boxed_vec!`.
2020-12-17 13:51:41 +01:00
Ivan Enderlin
a4effaaffe test+doc(c-api) Continue to improve test coverage of the C API. 2020-12-17 11:36:12 +01:00
bors[bot]
bc0ba32424 Merge #1947
1947: Include a NUL byte in the message returned by wasm_trap_message(). r=nlewycky a=nlewycky



Co-authored-by: Nick Lewycky <nick@wasmer.io>
2020-12-16 21:12:13 +00:00
Mark McCaskey
ef8bf91e6a Handle cross-compilation in c-api build.rs 2020-12-16 12:35:16 -08:00
Nick Lewycky
1e07207397 Include a NUL byte in the message returned by wasm_trap_message(). 2020-12-16 12:22:48 -08:00
Mark McCaskey
eb19ffdfbe Clarify new logic with a comment 2020-12-16 11:52:37 -08:00
Mark McCaskey
dd226b1dab Add fixes from publishing 2020-12-16 10:53:09 -08:00
Mark McCaskey
59df6917d8 Prepare for 1.0.0-beta2 release 2020-12-16 07:41:59 -08:00
bors[bot]
22c7bc8e19 Merge #1865
1865: Fix memory leak in host function envs r=MarkMcCaskey a=MarkMcCaskey

TODO: link to issue

This PR contains a number of changes:

1. Make `WasmerEnv: Clone`
2. Store a pointer to the `clone` function when creating a host function (Notably this is a feature that wouldn't work even if we _could_ use a proper trait object because you can't have a `Sized` trait object and `Clone: Sized`).
3. Store a pointer to the `drop` function when creating a host function.
4. Clone the env via pointer every time an `Instance` is made. Therefore each `Instance` gets its own, unique `Env` per host function with `Env`.
5. Add reference counting and drop logic to a sub-field of `wasmer_export::ExportFunction` which frees the original version of the `Env` (the thing that gets cloned each time an `Instance` is made) with the `drop` function pointer.
6. Change some logic in `vm::Instance` from SoA (struct of arrays) to AoS (array of structs): this uses more memory but is a bit less error prone and can be easily changed later.
7. Add logic on this new struct (`vm::ImportEnv`) that contains the function pointers for each import in `Instance` to drop (with the `drop` fn pointer) when the `vm::Instance` is being dropped. This fixes the original memory leak.
8. Add wrapper functions inside the host function creation functions which makes the layout of the user supplied env-pointer the responsibility of each function.  Thus, rather than `drop` being `Env::drop`, it's a function which frees all wrapper types, traverses indirections and frees the internal `Env` with `Env::drop`.  This simplifies code at the cost of making the `host_env` pointer (`vmctx`) not consistent in terms of what it actually points to.  This change fixes another memory leak related to the creation of host functions.

tl;dr: we're leaning into manually doing virtual method dispatch on `WasmerEnv`s and it actually works great! The biggest issue I have with the PR as-is is that the code isn't as clean/readable/robust as I'd ideally like it to be.

Edit (by @Hywan): This PR fixes #1584, #1714, #1865, #1667.

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Mark McCaskey <mark@wasmer.io>
Co-authored-by: Mark McCaskey <5770194+MarkMcCaskey@users.noreply.github.com>
2020-12-15 22:37:41 +00:00
Mark McCaskey
8081aaeee4 Add WasmerEnv: Sync 2020-12-15 13:43:37 -08:00
Mark McCaskey
6a2116917f Fix up tests, make WasmerEnv: Send 2020-12-15 13:35:19 -08:00
Mark McCaskey
62d15fae36 Merge branch 'master' into fix/host-func-env-memory-leak 2020-12-15 08:37:18 -08:00
bors[bot]
873560e203 Merge #1930
1930: test+doc(c-api) Improve test coverage and documentation of the C API r=Hywan a=Hywan


# Description

This PR improves the test coverage by adding more test cases of the C API. It also largely improve the documentation of the C API by writing more documentation, adding more cross-links, improving navigation and the user-experience.

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Ivan Enderlin <ivan@mnt.io>
2020-12-15 16:20:46 +00:00
Ivan Enderlin
b8712437a4 doc(c-api) Update paste to 1.0 and document macros.
Thanks to https://github.com/dtolnay/paste/pull/48, we can generate
documentation with various tokens. It's so cool. Thanks @dtolnay!
2020-12-15 17:02:47 +01:00
Ivan Enderlin
a4012b7ed2 test+doc(c-api) Start testing and documenting the value module. 2020-12-15 16:50:14 +01:00
Ivan Enderlin
c9a919b88a doc(c-api) Fix a link. 2020-12-15 16:32:11 +01:00
Ivan Enderlin
17a1900c8b feat(c-api) wasm_module_name sets out to {0, NULL} in case of failure. 2020-12-15 16:29:17 +01:00
Ivan Enderlin
778681e8a0 test+doc(c-api) Test and document the wasmer_module_*_name functions. 2020-12-15 16:26:29 +01:00
Ivan Enderlin
fb7ce4ad9a doc(c-api) Specify where examples are for wasm_instance_new. 2020-12-15 16:07:13 +01:00
Ivan Enderlin
7a30c31a6f test+doc(c-api) Add test for wasm_instance_exports. 2020-12-15 15:59:57 +01:00
Ivan Enderlin
e82630b9ec chore(c-api) Update lazy_static. 2020-12-14 16:31:44 +01:00
Ivan Enderlin
95eff687d7 chore(c-api) Update the header files. 2020-12-14 16:31:26 +01:00
Ivan Enderlin
43169dab4c test+doc(c-api) Add test and write documentation of the wat module. 2020-12-14 16:26:26 +01:00
Ivan Enderlin
c136f25271 doc(c-api) Add one more link. 2020-12-14 16:20:21 +01:00
bors[bot]
ae9a5f0a81 Merge #1911
1911: Add support for const function signatures r=MarkMcCaskey a=webmaster128

# Description

`FunctionType` contains variable length vectors and I'm sure there is a good reason for this design. However, it makes creating them a bit annoying since it connot be done in constants. With this PR I propose a representation that uses a tuple of arrays, which can be converted into `FunctionType` on demand. This allows developers to use constant signatures and push them to a better place in their code.

In https://github.com/CosmWasm/cosmwasm/pull/659/files you see a demonstration of how this deduplicates code and pushes the definitions out of my way.

Since the conversion `&FunctionType` to `FunctionType` is implemented here, this should no be breaking anyone's code. In cases where you can pass an owned `FunctionType` instead of `&FunctionType` you save one clone.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Simon Warta <simon@warta.it>
2020-12-14 15:17:10 +00:00
Ivan Enderlin
13d5ae5075 doc(c-api) Improve the documentation of the version module. 2020-12-14 16:17:04 +01:00
Ivan Enderlin
4eb4ae95e1 doc(c-api) Write small documentation for the macros module. 2020-12-14 16:11:33 +01:00
Ivan Enderlin
d2c50f9a81 test+doc(c-api) Add tests and write documentation of the store module. 2020-12-14 16:09:00 +01:00
Ivan Enderlin
cce13c7481 doc(c-api) Fix a typo. 2020-12-14 16:08:41 +01:00
Ivan Enderlin
b2564432d3 test+doc(c-api) Improve the documentation and test of the module module. 2020-12-14 15:58:24 +01:00
Ivan Enderlin
bb9144c20f test+doc(c-api) Add more tests and improve documentation. 2020-12-14 15:48:15 +01:00
Ivan Enderlin
3079fc8e05 doc(c-api) Improve the documentation of the wasm_c_api module. 2020-12-14 15:28:50 +01:00
Ivan Enderlin
4558b8739d doc(c-api) Complete or improve the documentation of the engine module. 2020-12-14 15:28:33 +01:00
Ivan Enderlin
c8810096f8 test+doc(c-api) Add a complete test for errors + improve the documentation. 2020-12-14 15:15:11 +01:00
Ivan Enderlin
c20261fe57 doc(c-api) Improve the crate's documentation. 2020-12-14 15:14:54 +01:00
Ivan Enderlin
b045b415f6 chore(c-api) Update the header files. 2020-12-14 13:42:29 +01:00
Ivan Enderlin
f4e3f2a627 feat(c-api) Add wasmer_version_(major,minor,patch,pre) functions. 2020-12-14 13:42:08 +01:00
Simon Warta
8a8e8dc981 Avoid unnecessary &&wasmer::FunctionType 2020-12-14 13:32:42 +01:00
Ivan Enderlin
f75eb29620 chore(c-api) Update the header files. 2020-12-14 11:17:46 +01:00