Commit Graph

37 Commits

Author SHA1 Message Date
Amanieu d'Antras
724c59c751 More bug fixes 2021-11-17 00:03:28 +00:00
Amanieu d'Antras
75cb5ab788 Refactor the C API to eliminate memory leaks 2021-11-15 17:01:28 +00:00
Syrus Akbary
b520a5f58a Renamed wasmer_wasm.h to wasmer.h 2021-06-01 21:47:37 -07:00
Ivan Enderlin
fd6689c580 chore(c-api) Formalize API prefixes.
The new rule is the following:

* `wasm_` for the standard C API,
* `wasmer_` or `wasi_` for the Wasmer non-standard C API.

For all symbols inside the `unstable` module, the renaming `wasm_` to
`wasmer_` is done without deprecations. It was clear that those API
were unstable.

For all the other symbols, symbols have been renamed to `wasmer_` but
the old symbols have been kept with deprecation warnings.

Special note: The `wasm_named_extern_t` type (and associated
functions) was in `wasi` by mistake. Its place was in the `unstable`
module. This patch also fixes that.

The `wasm_declare_vec_*` macros have been updated to support a default
prefix, or a user-defined prefix. It's now possible to write
`wasm_declare_boxed_vec!(foo);` to get all the API prefixed by `wasm_`
(as previously), or `wasm_declare_boxed_vec!(foo, wasmer);` to prefix
with `wasmer_`.

A user not using symbols from the `unstable` module will continue to
get working code, modulo some deprecations, after this patch.
2021-02-11 23:56:36 +01:00
Ivan Enderlin
e5d5303b50 Merge branch 'master' into feat-c-api-wasi-unordered-imports 2021-02-02 12:17:01 +01:00
Ivan Enderlin
995a2d4779 feat(c-api) No longer expand with cbindgen: drop dependency to Rust nightly.
So. Let's explain a dirty hack. `cbindgen` reads the code and collects
symbols. What symbols do we need? None of the one declared in
`wasm.h`, but for non-standard API, we need to collect all of
them. The problem is that `wasm_named_extern_t` is the only
non-standard type where extra symbols are generated by a macro
(`wasm_declare_boxed_vec!`). If we want those macro-generated symbols
to be collected by `cbindgen`, we need to _expand_ the crate
(i.e. running something like `rustc -- -Zunstable-options
--pretty=expanded`). Expanding code is unstable and available only on
nightly compiler. We _don't want_ to use a nightly compiler only for
that. So how can we help `cbindgen` to _see_ those symbols?

First solution: We write the C code directly in a file, which is then
included in the generated header file with the `cbindgen`
API. Problem, it's super easy to get it outdated, and it makes the
build process more complex.

Second solution: We write those symbols in a custom module, that is
just here for `cbindgen`, never used by our Rust code (otherwise it's
duplicated code), with no particular implementation.

And that's why we have the following `cbindgen_hack` module.

But this module must not be compiled by `rustc`. How to force `rustc`
to ignore a module? With conditional compilation. Because `cbindgen`
does not support conditional compilation, it will always _ignore_ the
`#[cfg]` attribute, and will always read the content of the module.

Sorry.
2021-02-02 12:05:47 +01:00
Ivan Enderlin
b201f12494 feat(c-api) Start implementing wasm_target_t, wasm_triple_t and wasm_cpu_features_t. 2021-01-28 17:26:15 +01:00
Mark McCaskey
8724ed963d Merge branch 'master' into feature/multi-example-wasm-c-api 2020-12-21 07:51:28 -08:00
Mark McCaskey
89132dde61 Add _ back to unused variable 2020-12-18 14:52:52 -08:00
Mark McCaskey
800c6685e6 Ensure we print what the offending files are 2020-12-18 14:36:21 -08:00
Mark McCaskey
cdf9825d62 Merge branch 'master' into feature/multi-example-wasm-c-api 2020-12-18 12:42:45 -08:00
Ivan Enderlin
2ca30fe634 fix(c-api) Ensure that uninitialized boxed vec are zeroed.
This patch updates how `wasm_$name_vec_new_uninitialized` creates the
vector. The vector is now fully allocated with `null` pointer for each
item, instead of having an empty vector with the initial capacity set
to `length`. That way, we are sure the vector is zeroed correctly.
2020-12-18 11:47:04 +01:00
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
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
a4effaaffe test+doc(c-api) Continue to improve test coverage of the C API. 2020-12-17 11:36:12 +01: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
f8935b3561 Merge branch 'master' into test-c-api 2020-12-10 16:01:14 +01:00
Mark McCaskey
deec77d2df Fix memory leak in wat2wasm function 2020-12-01 11:41:28 -08:00
Ivan Enderlin
25bcc9b7fc fixup 2020-12-01 15:29:41 +01:00
Ivan Enderlin
527b7061cc fix(c-api) Fix memory leak in wasm_$name_vec_delete.
This patch does several things:

1. It applies our Rust patterns for C API by replacing the raw pointer
   by `Option<Box<T>>`,
2. It allows `wasm_$name_vec_delete` to handle null pointer,
3. Because it takes ownership of the `wasm_$name_vec_t`, the pointer
   is correctly dropped (which fix the memory leak).
2020-12-01 14:25:59 +01:00
Ivan Enderlin
c88886f09c Merge branch 'master' into test-c-api-inline-c-rs 2020-11-09 17:19:14 +01:00
Ivan Enderlin
adb84e2bf6 feat(c-api) Allow extern types to own data.
We have known memory leaks with extern types. The idea is to change
the code so that extern types can hold/own data. This patch does that.

A `wasm_externtype_t` holds a `WasmExternType` enum. This enum owns
sibling types such as `WasmFunctionType`, `WasmGlobalType`,
`WasmTableType` and `WasmMemoryType`. It is those structures that ows
the extern types data, like `params` and `results` as
`wasm_valtype_vec_t` for `WasmFunctionType`. That way, for example,
`wasm_functype_t` can return a pointer to these vec which it owns.

A `wasm_externtype_t` continues to be transmuted to `wasm_functype_t`
etc. Nothing changes on that side.
2020-11-09 15:04:22 +01:00
Ivan Enderlin
ede0420920 test(c-api) Free some wasm_valtype_vec_t. 2020-11-05 16:30:39 +01:00
Ivan Enderlin
d2a5b8e101 chore(c-api) Use wasm_$name_vec_t::is_uninitialized(). 2020-10-30 14:16:00 +01:00
Ivan Enderlin
77be1040f6 feat(c-api) Implement wasm_$name_vec_t::is_uninitialized(). 2020-10-30 14:11:09 +01:00
Ivan Enderlin
92e4cf64c1 feat(c-api) Implement wasm_$name_vec_t::into_slice_mut. 2020-10-30 13:29:28 +01:00
Mark McCaskey
e6c61c74ff Update boxed_vec to deal with boxed values 2020-10-02 16:33:03 -07:00
Mark McCaskey
34e6139570 Add conversion logic for boxed_vec, simplify vec creation code 2020-10-02 16:24:46 -07:00
Ivan Enderlin
ae1a50b5d7 chore(c-api) Move cbindgen:ignore from functions/types to modules.
This patch removes the amount of `cbindgen:ignore` instructions by
moving this instruction onto the parent module.
2020-10-02 09:51:48 +02:00
Ivan Enderlin
ecb79e2af0 fix(c-api) Cherry-pick manually from https://github.com/wasmerio/wasmer/pull/1657. 2020-10-01 21:16:29 +02:00
Ivan Enderlin
8f627d9834 feat(c-api) Instruct cbindgen to ignore all functions and types defined in wasm.h. 2020-09-28 14:49:44 +02:00
Ivan Enderlin
1a0527af78 feat(c-api) Move all types into their own modules. 2020-09-24 11:41:04 +02:00
Ivan Enderlin
ebcd59de2b feat(c-api) Extract all macros into their own module. 2020-09-24 09:48:39 +02:00