diff options
-rw-r--r-- | src/wasm-validator.h | 2 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 23 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.txt | 11 |
3 files changed, 35 insertions, 1 deletions
diff --git a/src/wasm-validator.h b/src/wasm-validator.h index ac8cac410..b9ad30c73 100644 --- a/src/wasm-validator.h +++ b/src/wasm-validator.h @@ -126,8 +126,10 @@ public: } } void visitSetLocal(SetLocal *curr) { + shouldBeTrue(curr->index < getFunction()->getNumLocals(), curr, "set_local index must be small enough"); if (curr->value->type != unreachable) { shouldBeEqualOrFirstIsUnreachable(curr->value->type, curr->type, curr, "set_local type must be correct"); + shouldBeEqual(getFunction()->getLocalType(curr->index), curr->value->type, curr, "set_local type must match function"); } } void visitLoad(Load *curr) { diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 444ed9384..ace95177a 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -1,11 +1,15 @@ +// We always need asserts here +#ifdef NDEBUG +#undef NDEBUG +#endif + #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <binaryen-c.h> - // kitchen sink, tests the full API @@ -467,9 +471,26 @@ void test_interpret() { BinaryenModuleDispose(module); } +void test_nonvalid() { + // create a module that fails to validate + BinaryenModuleRef module = BinaryenModuleCreate(); + + BinaryenFunctionTypeRef v = BinaryenAddFunctionType(module, "v", BinaryenNone(), NULL, 0); + BinaryenType localTypes[] = { BinaryenInt32() }; + BinaryenFunctionRef func = BinaryenAddFunction(module, "func", v, localTypes, 1, + BinaryenSetLocal(module, 0, makeInt64(module, 1234)) // wrong type! + ); + + BinaryenModulePrint(module); + printf("validation: %d\n", BinaryenModuleValidate(module)); + + BinaryenModuleDispose(module); +} + int main() { test_core(); test_relooper(); test_binaries(); test_interpret(); + test_nonvalid(); } diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index 42610c2cc..d4eb5e337 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -713,3 +713,14 @@ module loaded from binary form: ) ) (i32.const 1234) +(module + (memory 0) + (type $v (func)) + (func $func (type $v) + (local $0 i32) + (set_local $0 + (i64.const 1234) + ) + ) +) +validation: 0 |