Skip to content

Commit

Permalink
Merge #1718
Browse files Browse the repository at this point in the history
1718: Prevent panic when min > static bound and max is less than it r=MarkMcCaskey a=MarkMcCaskey

This change should be safe because we check that min <= max when actually making the memory, so this assert can never fire and produce a `MemoryStyle` that would let us create a memory.

This PR also tests a lot more edge cases of memory creation in the C API.

Resolves the issue brought up here #1631 (comment)

# Review

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


Co-authored-by: Mark McCaskey <[email protected]>
Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
3 people authored Oct 15, 2020
2 parents 193d7c8 + f3bcc36 commit e65be6e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- [#1709](https://github.com/wasmerio/wasmer/pull/1709) Implement `wasm_module_name` and `wasm_module_set_name` in the Wasm(er) C API.
- [#1700](https://github.com/wasmerio/wasmer/pull/1700) Implement `wasm_externtype_copy` in the Wasm C API.

### Fixed

- [#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.

## 1.0.0-alpha4 - 2020-10-08

### Added
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 e65be6e

Please sign in to comment.