diff --git a/tests/gen/assert_shared.0.c b/tests/gen/assert_shared.0.c new file mode 100644 index 00000000..2093d063 --- /dev/null +++ b/tests/gen/assert_shared.0.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.0.h" + +void test() { + shared0Instance instance; + shared0Instantiate(&instance, resolveTestImports); + printStart("shared.0.wasm"); +} diff --git a/tests/gen/assert_shared.1.c b/tests/gen/assert_shared.1.c new file mode 100644 index 00000000..5b423540 --- /dev/null +++ b/tests/gen/assert_shared.1.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.1.h" + +void test() { + shared1Instance instance; + shared1Instantiate(&instance, resolveTestImports); + printStart("shared.1.wasm"); +} diff --git a/tests/gen/assert_shared.2.c b/tests/gen/assert_shared.2.c new file mode 100644 index 00000000..0a08a790 --- /dev/null +++ b/tests/gen/assert_shared.2.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.2.h" + +void test() { + shared2Instance instance; + shared2Instantiate(&instance, resolveTestImports); + printStart("shared.2.wasm"); +} diff --git a/tests/gen/assert_shared.3.c b/tests/gen/assert_shared.3.c new file mode 100644 index 00000000..4c164456 --- /dev/null +++ b/tests/gen/assert_shared.3.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.3.h" + +void test() { + shared3Instance instance; + shared3Instantiate(&instance, resolveTestImports); + printStart("shared.3.wasm"); +} diff --git a/tests/gen/assert_shared.4.c b/tests/gen/assert_shared.4.c new file mode 100644 index 00000000..dc4c3574 --- /dev/null +++ b/tests/gen/assert_shared.4.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.4.h" + +void test() { + shared4Instance instance; + shared4Instantiate(&instance, resolveTestImports); + printStart("shared.4.wasm"); +} diff --git a/tests/gen/assert_shared.5.c b/tests/gen/assert_shared.5.c new file mode 100644 index 00000000..68c2de0d --- /dev/null +++ b/tests/gen/assert_shared.5.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.5.h" + +void test() { + shared5Instance instance; + shared5Instantiate(&instance, resolveTestImports); + printStart("shared.5.wasm"); +} diff --git a/tests/gen/assert_shared.6.c b/tests/gen/assert_shared.6.c new file mode 100644 index 00000000..5905be40 --- /dev/null +++ b/tests/gen/assert_shared.6.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.6.h" + +void test() { + shared6Instance instance; + shared6Instantiate(&instance, resolveTestImports); + printStart("shared.6.wasm"); +} diff --git a/tests/gen/assert_shared.7.c b/tests/gen/assert_shared.7.c new file mode 100644 index 00000000..804f4742 --- /dev/null +++ b/tests/gen/assert_shared.7.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.7.h" + +void test() { + shared7Instance instance; + shared7Instantiate(&instance, resolveTestImports); + printStart("shared.7.wasm"); +} diff --git a/tests/gen/assert_shared.8.c b/tests/gen/assert_shared.8.c new file mode 100644 index 00000000..9d456491 --- /dev/null +++ b/tests/gen/assert_shared.8.c @@ -0,0 +1,11 @@ + +#include +#include "w2c2_base.h" +#include "test.h" +#include "test_shared.8.h" + +void test() { + shared8Instance instance; + shared8Instantiate(&instance, resolveTestImports); + printStart("shared.8.wasm"); +} diff --git a/tests/gen/shared.0.wasm b/tests/gen/shared.0.wasm new file mode 100644 index 00000000..7a0233b2 Binary files /dev/null and b/tests/gen/shared.0.wasm differ diff --git a/tests/gen/shared.1.wasm b/tests/gen/shared.1.wasm new file mode 100644 index 00000000..63ff5d1d Binary files /dev/null and b/tests/gen/shared.1.wasm differ diff --git a/tests/gen/shared.2.wasm b/tests/gen/shared.2.wasm new file mode 100644 index 00000000..638954c4 Binary files /dev/null and b/tests/gen/shared.2.wasm differ diff --git a/tests/gen/shared.3.wasm b/tests/gen/shared.3.wasm new file mode 100644 index 00000000..638954c4 Binary files /dev/null and b/tests/gen/shared.3.wasm differ diff --git a/tests/gen/shared.4.wasm b/tests/gen/shared.4.wasm new file mode 100644 index 00000000..638954c4 Binary files /dev/null and b/tests/gen/shared.4.wasm differ diff --git a/tests/gen/shared.5.wasm b/tests/gen/shared.5.wasm new file mode 100644 index 00000000..638954c4 Binary files /dev/null and b/tests/gen/shared.5.wasm differ diff --git a/tests/gen/shared.6.wasm b/tests/gen/shared.6.wasm new file mode 100644 index 00000000..638954c4 Binary files /dev/null and b/tests/gen/shared.6.wasm differ diff --git a/tests/gen/shared.7.wasm b/tests/gen/shared.7.wasm new file mode 100644 index 00000000..638954c4 Binary files /dev/null and b/tests/gen/shared.7.wasm differ diff --git a/tests/gen/shared.8.wasm b/tests/gen/shared.8.wasm new file mode 100644 index 00000000..3de69b00 Binary files /dev/null and b/tests/gen/shared.8.wasm differ diff --git a/tests/gen/shared.json b/tests/gen/shared.json new file mode 100644 index 00000000..bce63c31 --- /dev/null +++ b/tests/gen/shared.json @@ -0,0 +1,11 @@ +{"source_filename": "shared.wast", + "commands": [ + {"type": "module", "line": 3, "filename": "shared.0.wasm"}, + {"type": "module", "line": 4, "filename": "shared.1.wasm"}, + {"type": "module", "line": 8, "filename": "shared.2.wasm"}, + {"type": "module", "line": 9, "filename": "shared.3.wasm"}, + {"type": "module", "line": 10, "filename": "shared.4.wasm"}, + {"type": "module", "line": 11, "filename": "shared.5.wasm"}, + {"type": "module", "line": 12, "filename": "shared.6.wasm"}, + {"type": "module", "line": 13, "filename": "shared.7.wasm"}, + {"type": "module", "line": 20, "filename": "shared.8.wasm"}]} diff --git a/tests/main.c b/tests/main.c index 3e1cebdf..78bef9b5 100644 --- a/tests/main.c +++ b/tests/main.c @@ -15,6 +15,7 @@ void spectest__print_i32(U32 l0) { static wasmTable spectest_table; static wasmMemory spectest_memory; +static wasmMemory spectest_shared_memory; void* resolveTestImports( @@ -42,6 +43,10 @@ resolveTestImports( return (void*)&spectest_global_i64; } + if (strcmp(name, "shared_memory") == 0) { + return (void*)&spectest_shared_memory; + } + fprintf(stderr, "FAIL: import of unknown spectest item: %s\n", name); return NULL; @@ -113,6 +118,7 @@ void printOK(const char* description) { static void initTest() { wasmMemoryAllocate(&spectest_memory, 1, 2); + wasmMemoryAllocate(&spectest_shared_memory, 1, 2); wasmTableAllocate(&spectest_table, 10, 20); } diff --git a/tests/shared.wast b/tests/shared.wast new file mode 100644 index 00000000..046554a5 --- /dev/null +++ b/tests/shared.wast @@ -0,0 +1,20 @@ +;; proposals/threads/memory.wast + +(module (memory 0 0 shared)) +(module (memory 1 2 shared)) + +;; proposals/threads/exports.wast + +(module (memory (export "a") 0 1 shared)) +(module (memory 0 1 shared) (export "a" (memory 0))) +(module (memory $a (export "a") 0 1 shared)) +(module (memory $a 0 1 shared) (export "a" (memory $a))) +(module (export "a" (memory 0)) (memory 0 1 shared)) +(module (export "a" (memory $a)) (memory $a 0 1 shared)) + + +;; proposals/threads/imports.wast + +;; Shared Memory + +(module (import "spectest" "shared_memory" (memory 1 2 shared))) diff --git a/w2c2/import.h b/w2c2/import.h index 55a87271..9a650584 100644 --- a/w2c2/import.h +++ b/w2c2/import.h @@ -57,9 +57,10 @@ typedef struct WasmMemoryImport { char* name; U32 min; U32 max; + bool shared; } WasmMemoryImport; -static const WasmMemoryImport wasmEmptyMemoryImport = {NULL, NULL, 0, 0}; +static const WasmMemoryImport wasmEmptyMemoryImport = {NULL, NULL, 0, 0, false}; ARRAY_TYPE( WasmMemoryImports, @@ -76,9 +77,10 @@ typedef struct WasmTableImport { char* name; U32 min; U32 max; + bool shared; } WasmTableImport; -static const WasmTableImport wasmEmptyTableImport = {NULL, NULL, 0, 0}; +static const WasmTableImport wasmEmptyTableImport = {NULL, NULL, 0, 0, false}; ARRAY_TYPE( WasmTableImports, diff --git a/w2c2/memory.h b/w2c2/memory.h index ba4e9324..913a3d6d 100755 --- a/w2c2/memory.h +++ b/w2c2/memory.h @@ -8,8 +8,9 @@ typedef struct WasmMemory { U32 min; U32 max; + bool shared; } WasmMemory; -static const WasmMemory wasmEmptyMemory = {0, 0}; +static const WasmMemory wasmEmptyMemory = {0, 0, false}; #endif /* W2C2_MEMORY_H */ diff --git a/w2c2/reader.c b/w2c2/reader.c index 6a916e39..c2bec2fe 100644 --- a/w2c2/reader.c +++ b/w2c2/reader.c @@ -690,6 +690,7 @@ wasmReadLimits( WasmModuleReader* reader, U32* min, U32* max, + bool *shared, WasmModuleReaderError** error ) { U8 kindIndicator = 0; @@ -715,6 +716,7 @@ wasmReadLimits( switch (kindIndicator) { case 0x0: { *max = 0; + *shared = false; break; } case 0x1: { @@ -726,6 +728,19 @@ wasmReadLimits( *error = &wasmModuleReaderError; return; } + *shared = false; + break; + } + case 0x3: { + /* Read max */ + if (leb128ReadU32(&reader->buffer, max) == 0) { + static WasmModuleReaderError wasmModuleReaderError = { + wasmModuleReaderInvalidLimitMaximum + }; + *error = &wasmModuleReaderError; + return; + } + *shared = true; break; } default: { @@ -746,9 +761,10 @@ wasmReadMemoryType( WasmModuleReader* reader, U32* min, U32* max, + bool* shared, WasmModuleReaderError** error ) { - wasmReadLimits(reader, min, max, error); + wasmReadLimits(reader, min, max, shared, error); if (*error != NULL) { return; } @@ -770,7 +786,7 @@ wasmReadMemoryImport( import.module = module; import.name = name; - wasmReadMemoryType(reader, &import.min, &import.max, error); + wasmReadMemoryType(reader, &import.min, &import.max, &import.shared, error); if (*error != NULL) { return; } @@ -792,6 +808,7 @@ wasmReadTableType( WasmModuleReader* reader, U32* min, U32* max, + bool* shared, WasmModuleReaderError** error ) { U8 tableType = 0; @@ -808,7 +825,7 @@ wasmReadTableType( return; } - wasmReadLimits(reader, min, max, error); + wasmReadLimits(reader, min, max, shared, error); if (*error != NULL) { return; } @@ -832,7 +849,7 @@ wasmReadTableImport( import.module = module; import.name = name; - wasmReadTableType(reader, &import.min, &import.max, error); + wasmReadTableType(reader, &import.min, &import.max, &import.shared, error); if (*error != NULL) { return; } @@ -1091,7 +1108,7 @@ wasmReadMemorySection( /* Read memories */ for (; memoryIndex < memoryCount; memoryIndex++) { WasmMemory memory = wasmEmptyMemory; - wasmReadMemoryType(reader, &memory.min, &memory.max, error); + wasmReadMemoryType(reader, &memory.min, &memory.max, &memory.shared, error); if (*error != NULL) { return; } @@ -1549,7 +1566,7 @@ wasmReadTableSection( U32 tableIndex = 0; for (; tableIndex < tableCount; tableIndex++) { WasmTable table = wasmEmptyTable; - wasmReadTableType(reader, &table.min, &table.max, error); + wasmReadTableType(reader, &table.min, &table.max, &table.shared, error); if (*error != NULL) { return; } diff --git a/w2c2/table.h b/w2c2/table.h index 882a9115..3775063f 100755 --- a/w2c2/table.h +++ b/w2c2/table.h @@ -10,8 +10,9 @@ static const U8 wasmTableTypeFuncRef = 0x70; typedef struct WasmTable { U32 min; U32 max; + bool shared; } WasmTable; -static const WasmTable wasmEmptyTable = {0, 0}; +static const WasmTable wasmEmptyTable = {0, 0, false}; #endif /* W2C2_TABLE_H */