Skip to content

Commit

Permalink
Prevent panic when min > static bound and max is less than it
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Oct 13, 2020
1 parent 9c309ea commit 4a00ee2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

## **[Unreleased]**

- [#1718](https://github.com/wasmerio/wasmer/pull/1718) Fix panic in the API in some situations when the memory's min bound was greater than the memory's max bound.

### Added

- [#1709](https://github.com/wasmerio/wasmer/pull/1709) Implement `wasm_module_name` and `wasm_module_set_name` in the Wasm(er) C API.
Expand Down
2 changes: 0 additions & 2 deletions lib/api/src/tunables.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{MemoryType, Pages, TableType};
use more_asserts::assert_ge;
use std::cmp::min;
use std::sync::Arc;
use target_lexicon::{OperatingSystem, PointerWidth};
Expand Down Expand Up @@ -67,7 +66,6 @@ impl BaseTunables for Tunables {
// If the module doesn't declare an explicit maximum treat it as 4GiB.
let maximum = memory.maximum.unwrap_or_else(Pages::max_value);
if maximum <= self.static_memory_bound {
assert_ge!(self.static_memory_bound, memory.minimum);
MemoryStyle::Static {
bound: self.static_memory_bound,
offset_guard_size: self.static_memory_offset_guard_size,
Expand Down
60 changes: 60 additions & 0 deletions lib/c-api/tests/wasm_c_api/test-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,66 @@ int main(int argc, const char *argv[]) {
wasm_memorytype_delete(memtype3);
wasm_memory_delete(memory3);

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

wasm_memorytype_delete(memtype4);

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

wasm_memorytype_delete(memtype5);

// =====================
wasm_limits_t limits6 = {
.min = 15,
.max = 10,
};
own wasm_memorytype_t* memtype6 = wasm_memorytype_new(&limits6);
own wasm_memory_t* memory6 = wasm_memory_new(store, memtype6);
assert(memory6 == NULL);
error = get_wasmer_error();
printf("Found error string: %s\n", error);
assert(0 == strcmp("The memory is invalid because the maximum (10 pages) is less than the minimum (15 pages)", error));
free(error);

wasm_memorytype_delete(memtype6);

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

wasm_memorytype_delete(memtype7);

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

0 comments on commit 4a00ee2

Please sign in to comment.