diff options
author | Alon Zakai <alonzakai@gmail.com> | 2018-03-13 09:29:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-13 09:29:38 -0700 |
commit | d52213c3f5e96bb3450721d96aa68d3c5e0865b6 (patch) | |
tree | c585377fb842da0c4b87657f5047272196af9e6c /src/wasm/wasm-interpreter.cpp | |
parent | 0008b795d11d65d0759c3c0a71ee74b536f1ecf8 (diff) | |
download | binaryen-d52213c3f5e96bb3450721d96aa68d3c5e0865b6.tar.gz binaryen-d52213c3f5e96bb3450721d96aa68d3c5e0865b6.tar.bz2 binaryen-d52213c3f5e96bb3450721d96aa68d3c5e0865b6.zip |
Function pointer cast emulation (#1468)
This adds a pass that implements "function pointer cast emulation" - allows indirect calls to go through even if the number of arguments or their types is incorrect. That is undefined behavior in C/C++ but in practice somehow works in native archs. It is even relied upon in e.g. Python.
Emscripten already has such emulation for asm.js, which also worked for asm2wasm. This implements something like it in binaryen which also allows the wasm backend to use it. As a result, Python should now be portable using the wasm backend.
The mechanism used for the emulation is to make all indirect calls use a fixed number of arguments, all of type i64, and a return type of also i64. Thunks are then placed in the table which translate the arguments properly for the target, basically by reinterpreting to i64 and back. As a result, receiving an i64 when an i32 is sent will have the upper bits all zero, and the reverse would truncate the upper bits, etc. (Note that this is different than emscripten's existing emulation, which converts (as signed) to a double. That makes sense for JS where double's can contain all numeric values, but in wasm we have i64s. Also, bitwise conversion may be more like what native archs do anyhow. It is enough for Python.)
Also adds validation for a function's type matching the function's actual params and result (surprised we didn't have that before, but we didn't, and there was even a place in the test suite where that was wrong).
Also simplifies the build script by moving two cpp files into the wasm/ subdir, so they can be built once and shared between the various tools.
Diffstat (limited to 'src/wasm/wasm-interpreter.cpp')
-rw-r--r-- | src/wasm/wasm-interpreter.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/wasm/wasm-interpreter.cpp b/src/wasm/wasm-interpreter.cpp new file mode 100644 index 000000000..e7df785ac --- /dev/null +++ b/src/wasm/wasm-interpreter.cpp @@ -0,0 +1,24 @@ +#include "wasm-interpreter.h" + +namespace wasm { + +#ifdef WASM_INTERPRETER_DEBUG +int Indenter::indentLevel = 0; + +Indenter::Indenter(const char* entry) : entryName(entry) { + ++indentLevel; +} +Indenter::~Indenter() { + print(); + std::cout << "exit " << entryName << '\n'; + --indentLevel; +} +void Indenter::print() { + std::cout << indentLevel << ':'; + for (int i = 0; i <= indentLevel; ++i) { + std::cout << ' '; + } +} +#endif // WASM_INTERPRETER_DEBUG + +} // namespace wasm |