Skip to content

Commit

Permalink
read shared limits
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Nov 7, 2023
1 parent c663fe9 commit af5d302
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 10 deletions.
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.0.h"

void test() {
shared0Instance instance;
shared0Instantiate(&instance, resolveTestImports);
printStart("shared.0.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.1.h"

void test() {
shared1Instance instance;
shared1Instantiate(&instance, resolveTestImports);
printStart("shared.1.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.2.h"

void test() {
shared2Instance instance;
shared2Instantiate(&instance, resolveTestImports);
printStart("shared.2.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.3.h"

void test() {
shared3Instance instance;
shared3Instantiate(&instance, resolveTestImports);
printStart("shared.3.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.4.h"

void test() {
shared4Instance instance;
shared4Instantiate(&instance, resolveTestImports);
printStart("shared.4.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.5.h"

void test() {
shared5Instance instance;
shared5Instantiate(&instance, resolveTestImports);
printStart("shared.5.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.6.h"

void test() {
shared6Instance instance;
shared6Instantiate(&instance, resolveTestImports);
printStart("shared.6.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.7.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.7.h"

void test() {
shared7Instance instance;
shared7Instantiate(&instance, resolveTestImports);
printStart("shared.7.wasm");
}
11 changes: 11 additions & 0 deletions tests/gen/assert_shared.8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <stdio.h>
#include "w2c2_base.h"
#include "test.h"
#include "test_shared.8.h"

void test() {
shared8Instance instance;
shared8Instantiate(&instance, resolveTestImports);
printStart("shared.8.wasm");
}
Binary file added tests/gen/shared.0.wasm
Binary file not shown.
Binary file added tests/gen/shared.1.wasm
Binary file not shown.
Binary file added tests/gen/shared.2.wasm
Binary file not shown.
Binary file added tests/gen/shared.3.wasm
Binary file not shown.
Binary file added tests/gen/shared.4.wasm
Binary file not shown.
Binary file added tests/gen/shared.5.wasm
Binary file not shown.
Binary file added tests/gen/shared.6.wasm
Binary file not shown.
Binary file added tests/gen/shared.7.wasm
Binary file not shown.
Binary file added tests/gen/shared.8.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/gen/shared.json
Original file line number Diff line number Diff line change
@@ -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"}]}
6 changes: 6 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/shared.wast
Original file line number Diff line number Diff line change
@@ -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)))
6 changes: 4 additions & 2 deletions w2c2/import.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion w2c2/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
29 changes: 23 additions & 6 deletions w2c2/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ wasmReadLimits(
WasmModuleReader* reader,
U32* min,
U32* max,
bool *shared,
WasmModuleReaderError** error
) {
U8 kindIndicator = 0;
Expand All @@ -715,6 +716,7 @@ wasmReadLimits(
switch (kindIndicator) {
case 0x0: {
*max = 0;
*shared = false;
break;
}
case 0x1: {
Expand All @@ -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: {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -792,6 +808,7 @@ wasmReadTableType(
WasmModuleReader* reader,
U32* min,
U32* max,
bool* shared,
WasmModuleReaderError** error
) {
U8 tableType = 0;
Expand All @@ -808,7 +825,7 @@ wasmReadTableType(
return;
}

wasmReadLimits(reader, min, max, error);
wasmReadLimits(reader, min, max, shared, error);
if (*error != NULL) {
return;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion w2c2/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit af5d302

Please sign in to comment.