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) Handle initialized but empty results in wasm_func_call #1783

Merged

Conversation

Hywan
Copy link
Contributor

@Hywan Hywan commented Oct 30, 2020

Description

Our implementation of wasm_func_call was correct for C code as
follows:

wasm_val_vec_t arguments = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_EMPTY_VEC;
wasm_func_call(func, &arguments, &results);

However, for a C code such as:

wasm_val_t vals[1];
wasm_val_vec_t arguments = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_ARRAY_VEC(vals);
wasm_func_call(func, &arguments, &results);

the vals array were kept empty/unchanged. Why?

Because wasm_func_call was replacing the value of results by a new
wasm_val_vec_t. It is correct when results is an empty vector, but
it is incorrect when results is initialized with empty values.

This patch tries to detect this pattern: If results.data is null,
it means the vector is empty/uninitialized, and we can set a new
wasm_val_vec_t, otherwise it means the vector is initialized with
empty values, and we need to update each item individually.

This pattern can be found in the global.c example of the Wasm C API:

#define check_call(func, type, expected) \
{ \
wasm_val_t vs[1]; \
wasm_val_vec_t args = WASM_EMPTY_VEC; \
wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
wasm_func_call(func, &args, &results); \
check(vs[0], type, expected); \
}

Without this patch, this test fails.

Review

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

Hywan added 3 commits October 30, 2020 13:29
Our implementation of `wasm_func_call` was correct for C code as
follows:

```c
wasm_val_vec_t arguments = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_EMPTY_VEC;
wasm_func_call(func, &arguments, &results);
```

However, for a C code such as:

```c
wasm_val_t vals[1];
wasm_val_vec_t arguments = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_ARRAY_VEC(vals);
wasm_func_call(func, &arguments, &results);
```

the `vals` array were kept empty/unchanged. Why?

Because `wasm_func_call` was replacing the value of `results` by a new
`wasm_val_vec_t`. It is correct when `results` is an empty vector, but
it is incorrect when `results` is initialized with empty values.

This patch tries to detect this pattern: If `results.data` is `null`,
it means the vector is empty/uninitialized, and we can set a new
`wasm_val_vec_t`, otherwise it means the vector is initialized with
empty values, and we need to update each item individually.
@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-c-api About wasmer-c-api 🧪 tests I love tests labels Oct 30, 2020
@Hywan Hywan requested a review from syrusakbary as a code owner October 30, 2020 12:43
@Hywan Hywan self-assigned this Oct 30, 2020
@Hywan
Copy link
Contributor Author

Hywan commented Oct 30, 2020

Opened question: Do we need to error when the size of results is not equal to the number of results?

@wasmerio wasmerio deleted a comment from bors bot Oct 30, 2020
@Hywan
Copy link
Contributor Author

Hywan commented Oct 30, 2020

bors try

bors bot added a commit that referenced this pull request Oct 30, 2020
@bors
Copy link
Contributor

bors bot commented Oct 30, 2020

@Hywan
Copy link
Contributor Author

Hywan commented Nov 2, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Nov 2, 2020

@bors bors bot merged commit 0ca87b8 into wasmerio:master Nov 2, 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.

2 participants