From 3b9c2e85fa5d97ba08a95c0c7cce7d041e699cde Mon Sep 17 00:00:00 2001 From: Ashley Nelson Date: Tue, 21 Jun 2022 20:57:43 -0700 Subject: First class Data Segments (#4733) * Updating wasm.h/cpp for DataSegments * Updating wasm-binary.h/cpp for DataSegments * Removed link from Memory to DataSegments and updated module-utils, Metrics and wasm-traversal * checking isPassive when copying data segments to know whether to construct the data segment with an offset or not * Removing memory member var from DataSegment class as there is only one memory rn. Updated wasm-validator.cpp * Updated wasm-interpreter * First look at updating Passes * Updated wasm-s-parser * Updated files in src/ir * Updating tools files * Last pass on src files before building * added visitDataSegment * Fixing build errors * Data segments need a name * fixing var name * ran clang-format * Ensuring a name on DataSegment * Ensuring more datasegments have names * Adding explicit name support * Fix fuzzing name * Outputting data name in wasm binary only if explicit * Checking temp dataSegments vector to validateBinary because it's the one with the segments before we processNames * Pass on when data segment names are explicitly set * Ran auto_update_tests.py and check.py, success all around * Removed an errant semi-colon and corrected a counter. Everything still passes * Linting * Fixing processing memory names after parsed from binary * Updating the test from the last fix * Correcting error comment * Impl kripken@ comments * Impl tlively@ comments * Updated tests that remove data print when == 0 * Ran clang format * Impl tlively@ comments * Ran clang-format --- src/wasm/wasm-s-parser.cpp | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'src/wasm/wasm-s-parser.cpp') diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index dd92278c6..b59319be2 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -3038,8 +3038,9 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { } else { offset->set(Literal(int32_t(0))); } - parseInnerData(inner, j, {}, offset, false); - wasm.memory.initial = wasm.memory.segments[0].data.size(); + parseInnerData( + inner, j, Name::fromInt(dataCounter++), false, offset, false); + wasm.memory.initial = wasm.dataSegments[0]->data.size(); return; } } @@ -3073,9 +3074,15 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { if (auto size = strlen(input)) { std::vector data; stringToBinary(input, size, data); - wasm.memory.segments.emplace_back(offset, data.data(), data.size()); + auto segment = Builder::makeDataSegment( + Name::fromInt(dataCounter++), false, offset, data.data(), data.size()); + segment->hasExplicitName = false; + wasm.dataSegments.push_back(std::move(segment)); } else { - wasm.memory.segments.emplace_back(offset, "", 0); + auto segment = + Builder::makeDataSegment(Name::fromInt(dataCounter++), false, offset); + segment->hasExplicitName = false; + wasm.dataSegments.push_back(std::move(segment)); } i++; } @@ -3085,13 +3092,15 @@ void SExpressionWasmBuilder::parseData(Element& s) { if (!wasm.memory.exists) { throw ParseException("data but no memory", s.line, s.col); } + Index i = 1; + Name name = Name::fromInt(dataCounter++); + bool hasExplicitName = false; bool isPassive = true; Expression* offset = nullptr; - Index i = 1; - Name name; if (s[i]->isStr() && s[i]->dollared()) { name = s[i++]->str(); + hasExplicitName = true; } if (s[i]->isList()) { @@ -3112,11 +3121,15 @@ void SExpressionWasmBuilder::parseData(Element& s) { isPassive = false; } - parseInnerData(s, i, name, offset, isPassive); + parseInnerData(s, i, name, hasExplicitName, offset, isPassive); } -void SExpressionWasmBuilder::parseInnerData( - Element& s, Index i, Name name, Expression* offset, bool isPassive) { +void SExpressionWasmBuilder::parseInnerData(Element& s, + Index i, + Name name, + bool hasExplicitName, + Expression* offset, + bool isPassive) { std::vector data; while (i < s.size()) { const char* input = s[i++]->c_str(); @@ -3124,8 +3137,10 @@ void SExpressionWasmBuilder::parseInnerData( stringToBinary(input, size, data); } } - wasm.memory.segments.emplace_back( - name, isPassive, offset, data.data(), data.size()); + auto curr = + Builder::makeDataSegment(name, isPassive, offset, data.data(), data.size()); + curr->hasExplicitName = hasExplicitName; + wasm.dataSegments.push_back(std::move(curr)); } void SExpressionWasmBuilder::parseExport(Element& s) { -- cgit v1.2.3