summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt11
-rw-r--r--src/binaryen-c.cpp4
-rw-r--r--src/wasm-binary.h17
-rw-r--r--src/wasm-dis.cpp4
-rw-r--r--src/wasm-s-parser.h96
m---------test/spec0
6 files changed, 76 insertions, 56 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8b90dc0b8..f3f11d190 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -123,17 +123,6 @@ SET_PROPERTY(TARGET asm2wasm PROPERTY CXX_STANDARD 11)
SET_PROPERTY(TARGET asm2wasm PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS asm2wasm DESTINATION bin)
-SET(wasm2asm_SOURCES
- src/wasm2asm-main.cpp
- src/wasm.cpp
-)
-ADD_EXECUTABLE(wasm2asm
- ${wasm2asm_SOURCES})
-TARGET_LINK_LIBRARIES(wasm2asm asmjs emscripten-optimizer support)
-SET_PROPERTY(TARGET wasm2asm PROPERTY CXX_STANDARD 11)
-SET_PROPERTY(TARGET wasm2asm PROPERTY CXX_STANDARD_REQUIRED ON)
-INSTALL(TARGETS wasm2asm DESTINATION bin)
-
SET(s2wasm_SOURCES
src/wasm-linker.cpp
src/s2wasm-main.cpp
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 583f64094..ecb70bc1b 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -416,7 +416,9 @@ BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
std::vector<char> buffer(false);
buffer.resize(inputSize);
std::copy_n(input, inputSize, buffer.begin());
- WasmBinaryBuilder parser(*wasm, buffer, false);
+ WasmBinaryBuilder parser(*wasm, buffer, []() {
+ Fatal() << "error in parsing wasm binary";
+ }, false);
parser.read();
return wasm;
}
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index c889f1418..a5cf0fe5c 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -1164,13 +1164,14 @@ class WasmBinaryBuilder {
Module& wasm;
MixedArena& allocator;
std::vector<char>& input;
+ std::function<void ()> onError;
bool debug;
size_t pos = 0;
int32_t startIndex = -1;
public:
- WasmBinaryBuilder(Module& wasm, std::vector<char>& input, bool debug) : wasm(wasm), allocator(wasm.allocator), input(input), debug(debug) {}
+ WasmBinaryBuilder(Module& wasm, std::vector<char>& input, std::function<void ()> onError, bool debug) : wasm(wasm), allocator(wasm.allocator), input(input), onError(onError), debug(debug) {}
void read() {
@@ -1225,7 +1226,7 @@ public:
}
uint8_t getInt8() {
- assert(more());
+ if (!more()) onError();
if (debug) std::cerr << "getInt8: " << (int)(uint8_t)input[pos] << " (at " << pos << ")" << std::endl;
return input[pos++];
}
@@ -1329,27 +1330,27 @@ public:
void verifyInt8(int8_t x) {
int8_t y = getInt8();
- assert(x == y);
+ if (x != y) onError();
}
void verifyInt16(int16_t x) {
int16_t y = getInt16();
- assert(x == y);
+ if (x != y) onError();
}
void verifyInt32(int32_t x) {
int32_t y = getInt32();
- assert(x == y);
+ if (x != y) onError();
}
void verifyInt64(int64_t x) {
int64_t y = getInt64();
- assert(x == y);
+ if (x != y) onError();
}
void verifyFloat32(float x) {
float y = getFloat32();
- assert(x == y);
+ if (x != y) onError();
}
void verifyFloat64(double x) {
double y = getFloat64();
- assert(x == y);
+ if (x != y) onError();
}
void ungetInt8() {
diff --git a/src/wasm-dis.cpp b/src/wasm-dis.cpp
index e2e103b46..94965518c 100644
--- a/src/wasm-dis.cpp
+++ b/src/wasm-dis.cpp
@@ -45,7 +45,9 @@ int main(int argc, const char *argv[]) {
if (options.debug) std::cerr << "parsing binary..." << std::endl;
Module wasm;
- WasmBinaryBuilder parser(wasm, input, options.debug);
+ WasmBinaryBuilder parser(wasm, input, []() {
+ Fatal() << "error in parsing wasm binary";
+ }, options.debug);
parser.read();
if (options.debug) std::cerr << "Printing..." << std::endl;
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h
index 2c81b8a27..4b27918d3 100644
--- a/src/wasm-s-parser.h
+++ b/src/wasm-s-parser.h
@@ -27,6 +27,7 @@
#include <limits>
#include "wasm.h"
+#include "wasm-binary.h"
#include "asmjs/shared-constants.h"
#include "mixed_arena.h"
#include "parsing.h"
@@ -248,6 +249,20 @@ public:
// Assumes control of and modifies the input.
SExpressionWasmBuilder(Module& wasm, Element& module, std::function<void ()> onError) : wasm(wasm), allocator(wasm.allocator), onError(onError), importCounter(0) {
assert(module[0]->str() == MODULE);
+ if (module.size() > 1 && module[1]->isStr()) {
+ // these s-expressions contain a binary module, actually
+ std::vector<char> data;
+ size_t i = 1;
+ while (i < module.size()) {
+ auto str = module[i++]->c_str();
+ if (auto size = strlen(str)) {
+ stringToBinary(str, size, data);
+ }
+ }
+ WasmBinaryBuilder binaryBuilder(wasm, data, onError, false);
+ binaryBuilder.read();
+ return;
+ }
functionCounter = 0;
for (unsigned i = 1; i < module.size(); i++) {
preParseFunctionType(*module[i]);
@@ -1051,6 +1066,50 @@ private:
return ret;
}
+ // converts an s-expression string representing binary data into an output sequence of raw bytes
+ // this appends to data, which may already contain content.
+ void stringToBinary(const char* input, size_t size, std::vector<char>& data) {
+ auto originalSize = data.size();
+ data.resize(originalSize + size);
+ char *write = data.data() + originalSize;
+ while (1) {
+ if (input[0] == 0) break;
+ if (input[0] == '\\') {
+ if (input[1] == '"') {
+ *write++ = '"';
+ input += 2;
+ continue;
+ } else if (input[1] == '\'') {
+ *write++ = '\'';
+ input += 2;
+ continue;
+ } else if (input[1] == '\\') {
+ *write++ = '\\';
+ input += 2;
+ continue;
+ } else if (input[1] == 'n') {
+ *write++ = '\n';
+ input += 2;
+ continue;
+ } else if (input[1] == 't') {
+ *write++ = '\t';
+ input += 2;
+ continue;
+ } else {
+ *write++ = (char)(unhex(input[1])*16 + unhex(input[2]));
+ input += 3;
+ continue;
+ }
+ }
+ *write++ = input[0];
+ input++;
+ }
+ assert(write >= data.data());
+ size_t actual = write - data.data();
+ assert(actual <= data.size());
+ data.resize(actual);
+ }
+
bool hasMemory = false;
void parseMemory(Element& s) {
@@ -1069,41 +1128,8 @@ private:
const char *input = curr[2]->c_str();
if (auto size = strlen(input)) {
std::vector<char> data;
- data.resize(size);
- char *write = data.data();
- while (1) {
- if (input[0] == 0) break;
- if (input[0] == '\\') {
- if (input[1] == '"') {
- *write++ = '"';
- input += 2;
- continue;
- } else if (input[1] == '\'') {
- *write++ = '\'';
- input += 2;
- continue;
- } else if (input[1] == '\\') {
- *write++ = '\\';
- input += 2;
- continue;
- } else if (input[1] == 'n') {
- *write++ = '\n';
- input += 2;
- continue;
- } else if (input[1] == 't') {
- *write++ = '\t';
- input += 2;
- continue;
- } else {
- *write++ = (char)(unhex(input[1])*16 + unhex(input[2]));
- input += 3;
- continue;
- }
- }
- *write++ = input[0];
- input++;
- }
- wasm.memory.segments.emplace_back(atoi(curr[1]->c_str()), data.data(), write - data.data());
+ stringToBinary(input, size, data);
+ wasm.memory.segments.emplace_back(atoi(curr[1]->c_str()), data.data(), data.size());
} else {
wasm.memory.segments.emplace_back(atoi(curr[1]->c_str()), "", 0);
}
diff --git a/test/spec b/test/spec
-Subproject 625707d8bb91c6563fe4047fdbbc6f9d8bf0127
+Subproject 90475b45ddc59c46922e3ee6587933f60699e6b