summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/passes/Print.cpp7
-rw-r--r--src/wasm.h2
-rw-r--r--src/wasm/wasm-s-parser.cpp29
-rw-r--r--src/wasm/wasm-validator.cpp1
4 files changed, 23 insertions, 16 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 04b649ff6..8bb5175ab 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -718,7 +718,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
void printTableHeader(Table* curr) {
printOpening(o, "table") << ' ';
o << curr->initial;
- if (curr->max != Table::kMaxSize) o << ' ' << curr->max;
+ if (curr->hasMax()) o << ' ' << curr->max;
o << " anyfunc)";
}
void visitTable(Table *curr) {
@@ -746,9 +746,10 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
void printMemoryHeader(Memory* curr) {
printOpening(o, "memory") << ' ';
printName(curr->name) << ' ';
+ if (curr->shared) printOpening(o, "shared ");
o << curr->initial;
- if (curr->max && curr->max != Memory::kMaxSize) o << ' ' << curr->max;
- if (curr->shared) o << " shared";
+ if (curr->hasMax()) o << ' ' << curr->max;
+ if (curr->shared) o << ")";
o << ")";
}
void visitMemory(Memory* curr) {
diff --git a/src/wasm.h b/src/wasm.h
index d2bea45a1..fc167342a 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -672,6 +672,7 @@ public:
Table() : exists(false), imported(false), initial(0), max(kMaxSize) {
name = Name::fromInt(0);
}
+ bool hasMax() { return max != kMaxSize; }
};
class Memory {
@@ -705,6 +706,7 @@ public:
Memory() : initial(0), max(kMaxSize), exists(false), imported(false), shared(false) {
name = Name::fromInt(0);
}
+ bool hasMax() { return max != kMaxSize; }
};
class Global {
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp
index b8419ad54..0e329067e 100644
--- a/src/wasm/wasm-s-parser.cpp
+++ b/src/wasm/wasm-s-parser.cpp
@@ -1512,17 +1512,9 @@ void SExpressionWasmBuilder::stringToBinary(const char* input, size_t size, std:
Index SExpressionWasmBuilder::parseMemoryLimits(Element& s, Index i) {
wasm.memory.initial = getCheckedAddress(s[i++], "excessive memory init");
if (i == s.size()) return i;
- while (i < s.size() && s[i]->isStr()) {
- auto* curr = s[i]->c_str();
- i++;
- if (strstr(curr, "shared")) {
- wasm.memory.shared = strncmp(curr, "notshared", 9) != 0;
- break;
- }
- uint64_t max = atoll(curr);
- if (max > Memory::kMaxSize) throw ParseException("total memory must be <= 4GB");
- wasm.memory.max = max;
- }
+ uint64_t max = atoll(s[i++]->c_str());
+ if (max > Memory::kMaxSize) throw ParseException("total memory must be <= 4GB");
+ wasm.memory.max = max;
return i;
}
@@ -1557,6 +1549,10 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
if (wasm.getImportOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col);
wasm.addImport(im.release());
i++;
+ } else if (inner[0]->str() == "shared") {
+ wasm.memory.shared = true;
+ parseMemoryLimits(inner, 1);
+ i++;
} else {
if (!(inner.size() > 0 ? inner[0]->str() != IMPORT : true)) throw ParseException("bad import ending");
// (memory (data ..)) format
@@ -1565,7 +1561,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
return;
}
}
- i = parseMemoryLimits(s, i);
+ if (!wasm.memory.shared) i = parseMemoryLimits(s, i);
// Parse memory initializers.
while (i < s.size()) {
@@ -1765,7 +1761,14 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
}
// ends with the table element type
} else if (im->kind == ExternalKind::Memory) {
- j = parseMemoryLimits(inner, j);
+ if (inner[j]->isList()) {
+ auto& limits = *inner[j];
+ if (!(limits[0]->isStr() && limits[0]->str() == "shared")) throw ParseException("bad memory limit declaration");
+ wasm.memory.shared = true;
+ parseMemoryLimits(limits, 1);
+ } else {
+ parseMemoryLimits(inner, j);
+ }
}
if (wasm.getImportOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col);
wasm.addImport(im.release());
diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp
index 85e2193fa..cfe84ac3e 100644
--- a/src/wasm/wasm-validator.cpp
+++ b/src/wasm/wasm-validator.cpp
@@ -597,6 +597,7 @@ static bool checkOffset(Expression* curr, Address add, Address max) {
void WasmValidator::visitMemory(Memory *curr) {
shouldBeFalse(curr->initial > curr->max, "memory", "memory max >= initial");
shouldBeTrue(curr->max <= Memory::kMaxSize, "memory", "max memory must be <= 4GB");
+ shouldBeTrue(!curr->shared || curr->hasMax(), "memory", "shared memory must have max size");
Index mustBeGreaterOrEqual = 0;
for (auto& segment : curr->segments) {
if (!shouldBeEqual(segment.offset->type, i32, segment.offset, "segment offset should be i32")) continue;