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

Fix sentinel value in wasm_limits_t for memory in wasm_c_api #1688

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/c-api/src/wasm_c_api/types/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct wasm_limits_t {
#[no_mangle]
pub unsafe extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm_memorytype_t> {
let min_pages = Pages(limits.min as _);
// TODO: investigate if `0` is in fact a sentinel value here
let max_pages = if limits.max == 0 {
// u32::max_value() is a sentinel value for no max specified
let max_pages = if limits.max == u32::max_value() {
None
} else {
Some(Pages(limits.max as _))
Expand Down
20 changes: 18 additions & 2 deletions lib/c-api/tests/wasm_c_api/test-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ int main(int argc, const char *argv[]) {
own wasm_engine_t* engine = wasm_engine_new();
own wasm_store_t* store = wasm_store_new(engine);

// =====================
wasm_limits_t limits1 = {
.min = 0,
.max = wasm_limits_max_default,
.max = 0x7FFFFFFF,
};
own wasm_memorytype_t* memtype1 = wasm_memorytype_new(&limits1);
own wasm_memory_t* memory1 = wasm_memory_new(store, memtype1);
assert(memory1 == NULL);
char* error = get_wasmer_error();
printf("Found error string: %s\n", error);
assert(0 == strcmp("The maximum requested memory (4294967295 pages) is greater than the maximum allowed memory (65536 pages)", error));
assert(0 == strcmp("The maximum requested memory (2147483647 pages) is greater than the maximum allowed memory (65536 pages)", error));
free(error);

wasm_memorytype_delete(memtype1);

// =====================
wasm_limits_t limits2 = {
.min = 15,
.max = 25,
Expand All @@ -46,6 +48,20 @@ int main(int argc, const char *argv[]) {
wasm_memorytype_delete(memtype2);
wasm_memory_delete(memory2);

// =====================
wasm_limits_t limits3 = {
.min = 15,
.max = wasm_limits_max_default,
};
own wasm_memorytype_t* memtype3 = wasm_memorytype_new(&limits3);
own wasm_memory_t* memory3 = wasm_memory_new(store, memtype3);
assert(memory3 != NULL);
int size = wasm_memory_size(memory3);
printf("memory size: %d\n", size);

wasm_memorytype_delete(memtype3);
wasm_memory_delete(memory3);

printf("Shutting down...\n");
wasm_store_delete(store);
wasm_engine_delete(engine);
Expand Down