summaryrefslogtreecommitdiff
path: root/src/tools/wasm-interp.cc
Commit message (Collapse)AuthorAgeFilesLines
* Add error when parameter size does not match on wasm-interp run-export (#2250)Raphael Amorim2023-06-091-0/+7
|
* wasm-interp: Add --run-export and --argument (#2176)Raphael Amorim2023-03-191-0/+114
|
* Move headers to include/wabt/ (#1998)Alex Reinking2022-09-281-9/+9
| | | This makes things easier for users and packagers of libwabt.
* Clang-format codebase (#1684)Heejin Ahn2021-12-201-9/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This applies clang-format to the whole codebase. I noticed we have .clang-format in wabt but the codebase is not very well formatted. This kind of mass-formatting PR has fans and skeptics because it can mess with `git blame`, but we did a similar thing in Binaryen a few years ago (WebAssembly/binaryen#2048, which was merged in WebAssembly/binaryen#2059) and it was not very confusing after all. If we are ever going to format the codebase, I think it is easier to do it in a single big PR than dozens of smaller PRs. This is using the existing .clang-format file in this repo, which follows the style of Chromium. If we think this does not suit the current formatting style, we can potentially tweak .clang-format too. For example, I noticed the current codebase puts many `case` statements within a single line when they are short, but the current .clang-format does not allow that. This does not include files in src/prebuilt, because they are generated. This also manually fixes some comment lines, because mechanically applying clang-format to long inline comments can look weird. I also added a clang-format check hook in the Github CI in #1683, which I think can be less controversial, given that it only checks the diff. --- After discussions, we ended up reverting many changes, especially one-liner functions and switch-cases, which are too many to wrap in `// clang-format off` and `// clang-format on`. I also considered fixing `.clang-format` to allow those one-liners but it caused a larger churn in other parts. So currently the codebase does not conform to `.clang-format` 100%, but we decided it's fine.
* Remove redundant `BindImports` call (NFC) (#1791)Heejin Ahn2021-12-201-1/+0
|
* Add error locations to BinaryReaderInterp (#1780)Sam Clegg2021-12-091-2/+3
| | | | I think it was always intended to work this way but was left as a TODO.
* Set features of the Store used in wasm-interp (#1723)Sam Clegg2021-10-121-0/+1
| | | | | | | | | | | Without this change wasm-interp always runs with default features. These features seem to be used in just a single location: src/interp/interp.cc: int pass = store.features().bulk_memory_enabled() ? Init : Check; I noticed this when trying to enable bulk memory but run the interpreter with `--disable-bulk-memory` and it was not effecting this line of code.
* Use stderr for all logging and error output (#1579)Sam Clegg2020-12-031-1/+1
| | | | I'm not sure why we were using stdout but the convention is normally to write all logging and error message to stderr.
* wasi: Implement more of the wasi API (#1430)Sam Clegg2020-05-211-3/+28
| | | | | | | | | | | | | | | | I've been used the tests from wasi-sdk to get started: https://github.com/WebAssembly/wasi-sdk/tree/master/tests All these tests now pass. Still this isn't saying much, there are still some big missing pieces. Started using the new serdes (serialize/deserialze) API in uvwasi. I think we are almost at the point were we can look at auto-generating some of this stuff from witx to avoid having the hand code all this marshelling stuff. Add support for ArgumentCount::ZeroOrMore to OptionParser and also the ability to force the end of option processing using `--`. This allows is to pass options through the underlying wasi program when running wasm-interp.
* Implement more WASI APIs (#1423)Sam Clegg2020-05-161-3/+19
| | | | This is almost enough to pass all the tiny tests in the wasi-sdk repo.
* Use stderr for reporting errors in wasm-interp (#1422)Sam Clegg2020-05-131-5/+7
| | | | We should probably send traving to stderr too but thats a bigger change.
* Add initial MVP of WASI API support to wasm-interp (#1411)Sam Clegg2020-05-121-28/+97
| | | | | | | | | | This is proof of concept that only implements the `proc_exit` and `fd_write` APIs. Extending this to the full WASI API will to follow assuming this approach seems reasonable. Fixes #1409
* Fix typo. NFC. (#1410)Sam Clegg2020-05-111-2/+1
|
* Pass current Thread to host function callbacks (#1412)Sam Clegg2020-05-111-2/+2
| | | | | | | | | | | | | | | The for experimental WASI implementation I'm working on the WASI callbacks (implemented as HostFuncs) need to know which module a call is coming from. This is because the implicitly operate on the memory of the calling instance. This approach seems the match the current convention of passing in the Thread in `Func::DoCall`. The purpose here is to allow the HostCall callback to determine from which instance it is being called. An alternative approach that I considered was created different set of WASI HostCall object for each instance so that instance and its memory we bound to the WASI functions.
* New interpreter (#1330)Ben Smith2020-02-211-100/+83
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It's modeled closely on the wasm-c-api. All runtime objects are garbage-collected. The GC'd objects are subclasses of `Object` and all stored in the `Store`. GC roots can be created using a RefPtr<T> struct. The `Module` stores no runtime objects, only description structs (named `*Desc`, e.g. `FuncDesc`). Instantiation takes these descriptors and turns them into runtime objects (e.g. `FuncDesc` is used to create a `Func`). Only import-matching occurs during instantiation; all other validation is assumed to have occurred during Module compilation. As with the previous interpreter, the wasm instructions are not executed directly, and instead compiled to an instruction stream that is easier to interpret (e.g. all branches become gotos). It's still a stack machine, though. The previous interpreter would always compile and instantiate in one step, which means that it was always known whether an imported function is a host function or wasm function. As a result, calls between modules that stayed in wasm could be very cheap (just update PC). Now that the module is compiled before instantiation, an imported function call always has to check first, which may be a slight performance hit. One major difference to the structure of the interpreter is that floating-point values are passed directly, instead of using their equivalent bit pattern. This is more convenient in general, but requires annotating all functions with WABT_VECTORCALL so Visual Studio x86 works properly (otherwise floats are passed in the x87 FP stack). Instruction stream decoding/tracing/disassembling is now all handled in the `Istream` class. This reduces a lot of duplication between the three, which makes the logging-all-opcodes and tracing-all-opcodes less valuable, so I've removed them. Here are the changes to files: binary-reader-metadata.{h,cc} -> [removed] interp-{disassemble.trace}.cc -> istream.{h,cc} There are new helper files: interp-util.{h,cc}: Primarily print debugging functions interp-math.h: Templates used for handling wasm math
* Update spec testsuite (#1237)Sam Clegg2019-11-221-3/+3
| | | | | | | The only major change to the interpreter is to move segment initialization out `ReadBinaryInterp` (in the binary reader) and into interp.cc. This is because the test suite now expects out of bound semgments to be reported during initialization rather than reported as validation errors.
* interpreter: Allow traps to include custom error strings (#1236)Sam Clegg2019-11-191-3/+3
| | | | | | | | | | | This means we can give more precise/useful errors for runtime failures. Change interp::Result from an enum to struct so it can hold the result enum plus an optional detailed error message. Add TRAP_MSG and TRAP_IF_MSG macros that work just like TRAP and TRAP_IF but contain a format string and printf-like arguments which are formatted to produce the error message.
* wasm-interp: Correctly report failure of start function (#1230)Sam Clegg2019-11-181-0/+1
| | | We were printing the error message but returning success.
* Add `--version` to wabt tools (#1175)Amir Bawab2019-10-091-1/+0
| | | | | | | | | | | | | Closes: #1106 Ported versioning system from [Binaryen CMakeLists.txt](https://github.com/WebAssembly/binaryen/blob/dc31b460fef47dfb3415b4ae6276fff4919a03e2/CMakeLists.txt#L10-L23) ``` bin/wasm2c --version 1.0.11-44-g71f883ad ``` Applied to (all) tools in `src/tools/`.
* [interp] Add flag to provide dummy import funcs (#1101)Ben Smith2019-06-281-4/+22
| | | | You can now pass `--dummy-import-func` to `wasm-interp`, which will provide a function that logs the call and returns zero.
* Refactor interpreter a bit (#919)Ben Smith2018-10-021-2/+2
| | | | | | * Create src/interp/ directory * Move Thread::Trace into src/interp/interp-trace.cc * Move Environment::Disassemble into src/interp/interp-disassemble.cc * Move shared code into src/interp/interp-internal.h
* [wasm-interp] Fix assert with non-func exports (#912)Ben Smith2018-09-131-0/+3
| | | The `--run-all-exports` flag assumed that all exports are functions.
* Replace ErrorHandler with Errors and FormatErrors*Ben Smith2018-09-041-5/+6
| | | | | | | | | | | | | `ErrorHandler` complicated all error handling in wabt, since it was callback-based. Callbacks would be useful if we ever wanted to change behavior when an error occurred, but instead all that the handler ever did was write the errors to stdout/stderr or to a buffer. This change adds a new class `Error`, which contains an `ErrorLevel`, a `Location` and an error message. It also replaces ErrorHandler with `Errors` (a typedef for `std::vector<Error>`), and adds a couple of functions that can format a list of `Errors` for output: `FormatErrorsTo{String,File}`.
* [interp] Simplify host module imports (#889)Ben Smith2018-08-131-67/+20
| | | | | | | | | | | | | | | | | | | | Importing from host modules used to be handled by a delegate, which is a powerful enough solution to allow for all host import behaviors, but makes the common case difficult and clunky. It also assumed that every imported function/global/etc. needed to be newly created and added to the environment, which is OK (though wasteful) for functions, but is incorrect for mutable values like memories and globals. This change simplifies the common case by providing a set of functions on `HostModule` that can be used to add exports, e.g. AppendFuncExport("add", {{Type::I32, Type::I32}, {Type::I32}}, AddFunc); A generic interface for functions is provided as well, to allow for automatically generating functions as needed. This is currently used for functions like "host.print" in `wasm-interp`.
* Pass Option structs by const reference (#888)Ben Smith2018-08-071-2/+2
|
* Fix GCC 8 error due to `-Werror=class-memaccess`. (#858)Marvin Löbel2018-06-051-1/+2
| | | Closes #857
* Add flag to ignore errors in custom sections (#833)Ben Smith2018-05-071-1/+2
| | | | | | | | | | | | | If a wasm engine fails to parse a custom section, it must not be an error. In wabt we often won't want to continue if a custom section can't be parsed, but it still may be useful to be able to continue. This change adds a new flag `--ignore-custom-section-errors` to `wasm2wat` and `wasm-validate`, which will partially parse a custom section with errors. This could lead to some strange behavior (partially completed data structures), but generally should be safe to do. See issue #378 and the discussion on pull #830 for more info.
* [cleanup] Always use braces with if (#691)Ben Smith2017-12-091-2/+4
|
* Fix ubsan build and remove workaround in favor of ubsan.blacklist (#678)Sam Clegg2017-11-281-1/+1
|
* Refactor interpreter execution (#650)Ben Smith2017-10-121-11/+11
| | | | | | | | | | | | | | | | | | | * Move `Thread::Run*` functions into `Executor`. The executor abstracts over any execution, which will allow us to simulated multi-threaded execution (which would be awkward to implement with an API on the Thread object). * Add `ExecResult` type which wraps an `interp::Result` and the results from the function. There are too many things called `Result` now, so perhaps some should be renamed. Not sure what to call them though. * Expose some of the low-level functionality of `Thread` as public member functions. This means that it is possible to get `Thread` into an invalid state, but IMO that's OK since the assumption is that you know what you're doing if you use the `Thread` API. * Simplify value_stack and call_stack to use array indexes instead of pointers. * Remove the `call_stack_return_top`. This was used to handle calling back and forth between the interpreter and the host, but this is not currently done anywhere anymore. It's probably better to remove this functionality until it is used (if ever).
* Rename interpreter -> interp everywhere (#649)Ben Smith2017-10-121-31/+30
| | | We already used `interp` in a few places, may as well be consistent.
* Share code between wasm-interp and spectest-interp (#644)Ben Smith2017-10-031-129/+17
| | | | | | | Also: * Add `TypedValues` typedef (for `std::vector<TypedValue>`) * Fix bug when displaying very large numbers in the interpreter; length was capped at 100 characters
* Add spectest-interp tool; split from wasm-interp (#643)Ben Smith2017-10-031-1193/+37
| | | | | | * `wasm-interp` tool now only runs `.wasm` file, not spec tests * `wasm-interp` has a new flag `--host-print` for importing a print function named "host.print" * `spectest-interp` tool runs only `.json` files
* [wasm-objdump] Continue reading sections on error (#636)Ben Smith2017-09-261-2/+4
| | | | | | | | | | | | | | | wasm-objdump wants to always try to read as much of the binary as possible. This change adds a new BinaryReader option called stop_on_first_error which can be set to false to enable this behavior. Currently this will bail on any error in a section, but will try to read the next section. We may expand the error-handling behavior to try and recover inside of a section as well, but it's probably best to keep it simple for now. Because wasm-objdump does multiple passes over the binary, the errors would normally be displayed multiple times. We also add a OnError override which will suppress the error message for all passes other than the first.
* Only allow parsing one module with wat2wasm (#622)Ben Smith2017-09-121-1/+1
| | | | | `.wat` files can only contain one module, and no assertions. This fixes issue #621.
* Rename wast2wasm -> wat2wasm, wast2json (#617)Ben Smith2017-09-081-1/+1
| | | | * wat2wasm only parses wat files (individual modules) * wast2json parses spec test files and generates json + wasm
* Remove default cases from enum switches (#607)Sam Clegg2017-09-051-7/+4
|
* Update testsuite (#609)Ben Smith2017-09-041-1/+12
| | | | | | * Mostly just updating test expectations * Function signatures need to be checked as part of parsing (see assert_malformed test in `testsuite/func.wast`; added `ValidateFuncSignatures` for this.
* Return unique_ptr<Script> from parser (#605)Ben Smith2017-09-011-2/+2
| | | | | This removes a few more places we were using `delete`. I also checked the emscripten build and found that the previous change to `wabt::Result` broke this, so I fixed those too.
* Always include quoted headers like "src/foo.h" (#601)Ben Smith2017-08-301-11/+11
| | | This way the names won't conflict with other headers with the same name.
* Move Result to its own file (result.h) (#600)Ben Smith2017-08-291-6/+0
| | | | | | | * Unify all uses of `CHECK_RESULT` * Use `CHECK_RESULT` in a few more places * Add `operator|` and `operator|=` to `Result`, use it instead of `COMBINE_RESULT` * Change a few occurrences of `!Succeeded` to `Failed`
* Refactor interpreter imports (#592)Ben Smith2017-08-171-5/+6
| | | | | | * Create Import derived classes instead of using union * Use cast/dyn_cast for interpreter Func and Module (Defined vs. Host) * Remove OnImport callback in BinaryReaderInterpreter; do all the work in the kind-specific callbacks (e.g. OnImportFunc)
* Add saturating float truncation operators (#573)Ben Smith2017-08-161-4/+6
| | | | | | | | | | | | Add saturating truncation operators as described here: https://github.com/webassembly/nontrapping-float-to-int-conversions. This change also codifies the mechanism for enabling new WebAssembly features by passing the `Features` object. Opcode now has a `IsEnabled(const Features&)` member function to query if the opcode is enabled. This means that the `--future-exceptions` flag has been renamed to `--enable-exceptions` for consistency. Checking whether the feature is enabled always happens at input; either WastParser or BinaryReader.
* Rewrite parser as recursive descent (#591)Ben Smith2017-08-151-51/+52
| | | | | | | | | | | | | | | | | | * Remove Bison dependency * Remove pre-generated parser files * Rename build config from no-re2c-bison to no-re2c * Add a simple make_unique implementation * Move handling of module bindings into ir.cc * Simplify lexer - Remove lookahead, the parser handles this now - Unify Token/LexerToken, it only contains terminal values now - Refactor setting token type and value into one function (e.g. LITERAL, RETURN => RETURN_LITERAL) * New Parser - Uses two tokens of lookahead (use Peek(0) or Peek(1)) - Consume() consumes one token of any kind - Match(t) consumes the current token if it matches - PeekMatch(t) returns true iff the token matches, but doesn't consume - Basic error synchronization; plenty of room for improvement here
* Use C++ class in wasm-interp instead of Context (#583)Ben Smith2017-07-251-323/+357
|
* Rename snake_case to MixedCase. (#579)Ben Smith2017-07-191-315/+309
| | | | There are no functional changes.
* Rename read_file -> ReadFile; now returns vector (#572)Ben Smith2017-07-141-21/+7
| | | | | | | | | | | | | | | This adds the `DataOrNull` function, which is used to get a std::vector or std::string data(), but return `nullptr` if the container is empty. This is needed because it is undefined behavior (at least according to ubsan) if you call `data()` on an empty container. It would be nicer to have the various read_* functions not take a void*, size_t pair either, but that's work for another PR (perhaps introducing a span-like API?) I've also been considering having ReadFile return a wrapper object instead, so this could just mmap the file. Fixes #570.
* Remove StringSlice from interpreter (#569)Ben Smith2017-07-141-184/+145
| | | Use std::string or string_view instead.
* Use templates instead of macros in interpreter (#564)Ben Smith2017-07-141-8/+6
| | | | | | | | | | There's still more to be done, but this is a decent cleanup. There is now a template type called `ValueTypeRep` which is the integer representation of a given type. So for example, `ValueTypeRep<float>` => `uint32_t`. Nearly all of the unary and binary operators are defined via template functions that operate on these values.
* Remove BinaryErrorHandler, rename SourceErrorHandler (#553)Ben Smith2017-07-061-15/+13
| | | | | | | Since SourceErrorHandler can support binary locations, it doesn't make much sense to have two different error handlers. There is now only an ErrorHandler base class, and the first argument specifies whether the error handler is expecting text or binary locations.