summaryrefslogtreecommitdiff
path: root/src/binary-reader-ir.cc
Commit message (Collapse)AuthorAgeFilesLines
* Add initial support for code metadata (#1840)Yuri Iozzelli2022-02-251-0/+94
| | | | | | | | | | | | | | | | | | | | | See https://github.com/WebAssembly/tool-conventions/blob/main/CodeMetadata.md for the specification. In particular this pr implements the following: - Parsing code metadata sections in BinaryReader, providing appropriate callbacks that a BinaryReaderDelegate can implement: - BinaryReaderObjdump: show the sections in a human-readable form - BinaryReaderIr: add code metadata in the IR as expressions - Parsing code metadata annotations in text format, adding them in the IR like the BinaryReaderIR does - Writing the code metadata present in the IR in the proper sections when converting IR to binary - Support in wasm-decompiler for showing code metadata as comments in the pseudo-code All the features have corresponding tests. Support for code metadata is gated through the --enable-code-metadata feature. For reading/writing in the text format, --enable-annotations is also required. Missing features: Support for function-level code metadata (offset 0) Extensive validation in validator.cc (like making sure that all metadata instances are at the same code offset of an instruction)
* Use C++17 string_view (#1826)Sam Clegg2022-02-111-67/+67
| | | | | Now that we have C++17 we don't need our own string_view class anymore. Depends on #1825
* Update testsuite (#1795)Sam Clegg2022-01-101-4/+8
| | | | Remove test/binary/bad-function-missing-end.txt which is now covered upstream: https://github.com/WebAssembly/spec/pull/1405
* Clang-format codebase (#1684)Heejin Ahn2021-12-201-29/+50
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Share validation code between constant expressions and function bodies. NFC ↵Sam Clegg2021-12-101-18/+13
| | | | | | | | | | | | (#1783) Previously we has special cases for initializer expressions (constant expressions). This change paves the way for adding support for extended constant expressions that support a wider range of instructions. This change removes twice as many lines as it adds which shows that this simplification is probably worthwhile even without the pending extensions.
* Show tag names in objdump disassembly (#1774)Sam Clegg2021-12-071-0/+20
| | | | | | | | | Tag names are not officially part of the extended-name-section proposal (because it only deals with naming things that are in the spec already). However, I think its reasonable (and useful) to include these names under a speculative subsection ID, on the basis that tags can only exist when exceptions are enabled and that engines should ignore unknown name types.
* Remove unused checks from #1770. NFC (#1772)Sam Clegg2021-12-041-13/+0
| | | | In #1770 I introduced these (duplicate) checks but it turns out neither were necessary in the final version of the patch.
* Perform init expression validation outside of the binary reader. NFC (#1770)Sam Clegg2021-12-021-73/+31
| | | | | | | | | | | | | | | | Rather than spocial casing them in the reader we now use the same instruction callbacks for instruction that appear in init expressions as instructions that appear in normal functions. The result of this change is the validation of init expressions is pushed further up the stack. For example, objdump will now quite happily dump modules that use arbitrary instructions in thier init expressions even though they are not valid. To me, this makes sense since objdump does not do instruction validation elsewhere. The change is pre-cursor to allowing a wider variety of instruction to be present in init expressions. See https://github.com/WebAssembly/extended-const
* OnRefFuncExpr takes a func index. NFC (#1768)Sam Clegg2021-12-021-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | The name of this argument was recently changed from func_index to type_index, but I think that might have been incorrect. The immediate that read in the binary reader is (IIRC) a function index: ``` case Opcode::RefFunc: { Index func; CHECK_RESULT(ReadIndex(&func, "func index")); CALLBACK(OnRefFuncExpr, func); CALLBACK(OnOpcodeUint32, func); break; } ``` and not a type index. Indeed the interpreter seems to treat it as a function index too: ``` Result BinaryReaderInterp::OnRefFuncExpr(Index func_index) { CHECK_RESULT(validator_.OnRefFunc(loc, Var(func_index))); istream_.Emit(Opcode::RefFunc, func_index); return Result::Ok; } ```
* Add multi-memory feature support (#1751)Yuhan Deng2021-11-301-17/+24
|
* Support function references in parameters and results of functions and ↵Dmitry Bezhetskov2021-11-161-3/+3
| | | | blocks. (#1695)
* Remove separate OnEndFunc vs OnEndExpr. (#1756)Sam Clegg2021-11-051-24/+25
| | | | We already have EndFunctionBody, and this extra distinction doesn't seem like it is needed.
* Fix crash when function counts mismatch (#1739)Ng Zhi An2021-10-151-1/+7
| | | | | | | | This can happen if we don't stop on first error, and we get a malformed module where the func counts don't match. It's hard to write a test for this, since the kStopOnFirstError is fixed (not set by command line), but this case is quite easy for fuzzers to catch.
* Delay validation of elem init expressions until validation time (#1730)Sam Clegg2021-10-141-2/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Doing validation at parse time means we cannot run tests which included invalid instruction in the elem init expressions. For example. the updated test suite repo contains tests such as this: ``` (assert_invalid (module (table 1 funcref) (elem (i32.const 0) funcref (item (i32.add (i32.const 0) (i32.const 1)))) ) "constant expression required" ) ``` There we have an illegal instruction sequence in the init expresssion. However, in order to run this test we need to be able to process it with wast2json first which means it at least has to parse correctly. This change removes the `ElemExpr` and `ElemExprKind` types from the IR and instead just stores elem init expressions as `ExprList` like we do for global init expressions. This expression list can then be validated but crucially can also be invalid. This technique matches the existing `OnDataSegmentInitExpr_Other` and `OnGlobalInitExpr_Other` and indeed it seem that it was indented to work this way since the `OnElemSegmentElemExpr_Other` already existed (unused) in the codebase.
* [simd] Correctly shift alignment_log2 (#1699)Ng Zhi An2021-08-061-2/+2
| | | | | | | | | The alignment value in binary format is log2, so we need to shift it in binary-reader-ir before it is validated (since validation requires that it is shifted to be the number of bytes). We correctly did that for some Simd instructions (like load splat) but did not do it for load/store lane. Fixes #1674.
* Begin support for typed function references proposal: added the flag and ↵Dmitry Bezhetskov2021-07-251-0/+5
| | | | supported call_ref (#1691)
* Allow plain `try` with no `catch` or `delegate` (#1676)Asumu Takikawa2021-06-291-2/+2
| | | | | | Matches recent changes in the exception handling spec that allowed this case to reduce special cases in the syntax: https://github.com/WebAssembly/exception-handling/pull/157
* [EH] Remove `unwind` (#1682)Heejin Ahn2021-06-291-26/+2
| | | `unwind` was removed. See WebAssembly/exception-handling#156.
* [EH] Replace event with tag (#1678)Heejin Ahn2021-06-221-34/+38
| | | | | | | | | | | We recently decided to change 'event' to 'tag', and 'event section' to 'tag section', out of the rationale that the section contains a generalized tag that references a type, which may be used for something other than exceptions, and the name 'event' can be confusing in the web context. See - https://github.com/WebAssembly/exception-handling/issues/159#issuecomment-857910130 - https://github.com/WebAssembly/exception-handling/pull/161
* Add more support to extended names section. (#1659)Sam Clegg2021-04-071-27/+130
| | | I noticed we lacked support here while debugging #1651.
* [simd] Implement store lane (#1647)Ng Zhi An2021-03-221-0/+12
|
* [simd] Implement load lane (#1646)Ng Zhi An2021-03-221-0/+12
| | | | | | | | | This is a new kind of ir/ast node/instruction. It has 3 immediates: memarg align, memarg offset, and lane index. This required new visitor functions in all the places. Drive-by cleanup to share the simd lane parsing logic between shuffle, lane op and this new load lane instructions. This requires rebasing some tests because the error messages are slightly different now.
* [simd] Implement v128.load{32,64}_zero (#1644)Ng Zhi An2021-03-171-0/+10
| | | | This requires a new ir type, and the relevant implementation of virtual mthods in the various visitors.
* Update rethrow depth handling and catch_all opcode (#1608)Asumu Takikawa2021-02-181-2/+5
| | | | | | | | | | | | | Give `catch_all` its own opcode: Previously `catch_all` shared an opcode with `else`, but the spec now allocates it the 0x19 opcode. Adjust rethrow depth semantics: Previously this had interpreted the rethrow depth argument as counting only catch blocks, but the spec has clarified that it should count all blocks (in a similar fashion as `br` and related instructions).
* Update exception handling support to current proposal (#1596)Asumu Takikawa2021-02-101-19/+79
| | | | | | | | | | This PR updates the support of exception handling to the latest proposal (that is compatible with future 2-phase exception handling) described in https://github.com/WebAssembly/exception-handling/pull/137 and https://github.com/WebAssembly/exception-handling/pull/143. * Adds back tagged `catch $e`, `catch_all`, and `rethrow N` from a previous version of wabt, but with updates to match the current spec (e.g., `catch_all` shares an opcode with `else`, `rethrow`'s depth indexes only catch blocks, etc). * Adds `unwind` and `delegate` instructions. * Removes `exnref` and `br_on_exn`. * Updates relevant tests. There are some details that could still change (e.g., maybe how `delegate`'s depth is validated), but I'd be happy to submit further PRs if the spec details change.
* Select instr. with multiple results is invalid (#1582)Ben Smith2020-12-031-3/+5
| | | | | | The reference-types proposal adds a select instruction with a type vector, but any number greater than 1 is invalid. Fixes #1577.
* Add --relocatable support for tables (#1549) (#1549)Andy Wingo2020-10-011-0/+19
| | | | | | | | | | | | | We add relocations for table numbers on each place where we reify a table number (call_indirect, table.get, table.set...), but only if reference types are enabled. Also, fix symbol table generation with unnamed definitions, to allow for relocating references to anonymous functions or tables. As tests, add variants of the relocations and symbol-tables dump tests, with and without all features enabled. Enabling reference types causes relocs to be emitted. We also add --details to the relocations dump tests, so that we can see the target symbols for the relocations.
* Added initial "memory64" proposal support (#1500)Wouter van Oortmerssen2020-08-071-18/+18
|
* Fix linking section symbol name bugs (#1508)Ben Smith2020-07-311-0/+16
| | | Found by oss-fuzz.
* Remove ref.is_null type parameter (#1474)Ben Smith2020-07-151-3/+3
| | | | | | | | | See https://github.com/WebAssembly/reference-types/issues/99. This change also updates the testsuite, so the spec tests pass too. In addition, the behavior of `br_table` is no longer different from MVP, and has a text to confirm this. That is now fixed in `type-checker.cc` too.
* [wasm2wat] Fix a few more roundtripping issues (#1450)Ben Smith2020-05-291-0/+1
| | | | | | | * The `declare` keyword should be printed when using element expressions or element indexes (i.e. flags == 3 or flags == 7). * An imported table was not properly setting the element type in the IR Fixes #1448 and #1449.
* [wasm2wat] Write select type immediate (#1451)Ben Smith2020-05-291-1/+1
| | | | | | | | | | | | | | | The main fix is in `wat-writer.cc`, where the type immediate was never being printed. But I've also included a change to how `select` type immediates are represented in wabt. Previously, a bare `select` instruction would be stored with the type `Type::Any`. This is not a real wasm type, and is primarily used for type validation. The spec instead considers this form of `select` to have an empty type immediate, which is closer to the `Type::Void` type. This commit now uses `Type::Void` (or an empty `TypeVector`) to represent the bare `select` instruction. Fixes #1444.
* Reference types changes to remove subtyping (#1407)Ben Smith2020-05-281-12/+13
| | | | | | | | Main changes: * Rename `anyref` -> `externref` * Remove `nullref` * Rename `hostref` -> `externref` * `ref.null` and `ref.is_null` now have "ref kind" parameter * Add ref kind keywords: `func`, `extern`, `exn`
* Remove range assertions in BinaryReader (#1416)Ben Smith2020-05-111-2/+0
| | | | | | | These assertions check to see whether the `sig_index` (i.e. the function type) are in bounds. But this is the responsibility of the validator, not the binary reader. Fixes issues #1413 and #1414.
* Add support for atomic.fence from the threads proposal (#1231)Andy Wingo2020-04-201-12/+17
| | | | | See https://github.com/WebAssembly/threads/pull/141 for the binary encoding. This patch does add a field to AtomicFenceExpr for the consistency model, though without a type for the time being.
* Parse ArrayTypes (#1364)Ben Smith2020-03-231-0/+11
| | | | | | | | | | The following formats are supported: * (type (array i32)) * (type (array (field i32))) * (type (array (field (mut i32)))) This PR adds support for reading/writing binary and text, but no interpreter support yet.
* Parse struct fields (#1355)Ben Smith2020-03-161-2/+9
| | | | | | | | | | | This allows the following field formats: * `(struct (field $name i32))` * `(struct (field $name (mut i32)))` * `(struct (field i32))` * `(struct (field (mut i32)))` * `(struct (mut i32))` * `(struct i32)`
* Remove validation from the BinaryReader (#1354)Ben Smith2020-03-111-41/+16
| | | | | | | Validation should only happen in ValidateModule, BinaryReader should only check whether the binary is malformed. This change also fixes a few places in BinaryReaderIR where an index is assumed to be valid.
* Initial pass parsing/reading struct (#1352)Ben Smith2020-03-091-13/+23
| | | | | | | | | | | | | | | | | | This parses just the format `(struct)` as a new type. I added a test for this using `wat2wasm`, but that requires a rudimentary binary format. The test runner automatically attempts to rountrip all wat2wasm tests, so this required implementing the wat writing and binary reading too. Here's a summary of the changes: * binary-reader:h: Rename `BinaryReader::OnType` callbacks to `OnFuncType` * binary-reader.h: Add `BinaryReader::OnStructType` * binary-reader.cc: Use a switch after reading the type form to determine whether we're reading a function or struct. * tokens.def: Add new `TokenType::Struct` * lexer-keywords.txt: Add new `struct` keyword * type.h: Add `Type::Struct` type form * wast-parser.cc: Parse `(struct)` in text format * wat-writer.cc: Write Func or Struct type forms
* Convert Type from an enum into a class (#1350)Ben Smith2020-02-281-3/+3
| | | | | | | | | | | | | | | | | This is similar to the way Opcode is structured, which allows us to hang member functions off of the enumeration. The primary motivator for this change is the GC proposal (and the function-references proposal) where a Type can be parameterized: (type $T (struct ...)) (func (local (ref $T) ... ) In this case the type is ref, with a parameter of the type index. Making Type a class will make it easier to store this additional information.
* Update testsuite (w/ reference-types changes) (#1351)Ben Smith2020-02-281-5/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The new table-sub test, checks whether the subtyping is handled properly w/ table.init and table.copy instructions. The BeginElemSegment callback can't pass the element type anymore, since it's not known yet. The callback also can't be deferred, since the BeginElemSegmentInitExpr callback has to happen after the BeginElemSegment callback, but the reference type is not always known until after the initializer expression is read. To work around this, I added a new OnElemSegmentElemType callback. Other element segment changes: * The element type must be tracked in the SharedValidator * A subtle fix: when writing out the segment flags, we need to take into account whether the element type of the segment is not funcref, even if there are no element expressions. In that case, we have to use flag bit 0x4 (SegUseElemExprs). In addition, the TableCopy and TableInit instructions weren't handling table indexes fully. * TableCopy variables are read in the parser (both optional) * TableCopy names are now resolved + applied * TableCopy indexes are now validated * TableInit table variables are read in the parser; this is subtle, since the text format has order $table $segment, but the $table is optional.
* Add TypeEntry, base class for type section entries (#1349)Ben Smith2020-02-281-12/+12
| | | | | | | | | | | The type section currently only has one form: functions. With the GC proposal, at least two more forms are added: struct and array. To facilitate this: * Rename FuncTypeModuleField -> TypeModuleField * Rename Module::func_types -> Module::types * Rename func_type_bindings -> type_bindings * TypeEntry is added as a base class of FuncType * TypeModuleField stores a unique_ptr to a TypeEntry
* wasm-decompile: Fix access of undefined symbols. (#1319)Wouter van Oortmerssen2020-01-271-0/+4
|
* wasm-decompile: use symbols from linking section for names. (#1318)Wouter van Oortmerssen2020-01-271-0/+78
| | | | | | | | | | | | This allows wasm .o files to have more readable names, or even final linked modules if the linking information is preserved (with e.g. --emit-relocs in LLD). This is implemented as part of the WABT IR representation, so benefits wasm2wat as well. Named obtained this way are only set for functions if the function doesn't also have a name in the name section, but is preferred over the export name if there is one.
* Segment flags should be MVP-compat when possible (#1286)Ben Smith2020-01-081-2/+12
| | | | This should fix #1280, #1268, #1269.
* Several fixes for reference types (#1278)Heejin Ahn2020-01-081-0/+8
| | | | | | | - Allow `ref.func` for global initialization expressions - Allow `nullref` as a full-fledged type, after WebAssembly/reference-types#66 - Enable reference types when exnref is used (The reference types proposal is a prerequisite of the EH proposal)
* reference-types: add support for typed select (#1253)Sam Clegg2019-11-261-3/+3
|
* reference-types: add table.fill instruction (#1252)Sam Clegg2019-11-251-0/+5
|
* Switch to treating segment flags as a bitfield. NFC (#1232)Sam Clegg2019-11-181-6/+8
| | | | | | | | | | | This is in preparation for updating to latest version reference-types proposal where there is an additional flag and they can be combined. See: https://github.com/WebAssembly/bulk-memory-operations/issues/98 Also, add ERROR_IF to binary-reader.cc as logical corollary to the existing ERROR_UNLESS.
* reference-types: Add multi-table support to the interpreter (#1218)Sam Clegg2019-11-141-6/+6
| | | | | | | | | | This involved plumbing the table argument for `table.init` and `table.copy` all the way through rather than assuming they were always zero. Update the rather complex logic for assigning names to elem segments to handle active elem segments which both name themselves and the table they apply too.