summaryrefslogtreecommitdiff
path: root/src/wasm/wasm-binary.cpp
Commit message (Collapse)AuthorAgeFilesLines
...
* Require unique_ptr to Module::addFunctionType() (#1672)Paweł Bylica2019-01-101-2/+2
| | | | | This fixes the memory leak in WasmBinaryBuilder::readSignatures() caused probably the exception thrown there before the FunctionType object is safe. This also makes it clear that the Module becomes the owner of the FunctionType objects.
* Massive renaming (#1855)Thomas Lively2019-01-071-4/+4
| | | | | | Automated renaming according to https://github.com/WebAssembly/spec/issues/884#issuecomment-426433329.
* Rename `idx` to `index` in SIMD code for consistency (#1836)Thomas Lively2018-12-181-17/+17
|
* Fuzzing v128 and associated bug fixes (#1827)Thomas Lively2018-12-141-0/+2
| | | | * Fuzzing v128 and associated bug fixes
* SIMD (#1820)Thomas Lively2018-12-131-4/+299
| | | | | | | | | Implement and test the following functionality for SIMD. - Parsing and printing - Assembling and disassembling - Interpretation - C API - JS API
* Don't error on too many locals - just oom (#1822)Alon Zakai2018-12-131-8/+0
| | | I think I added this error for fuzzing, but it is harmful as it prevents a module with too many locals from being loaded - if we could load it, we might be able to optimize it to have fewer locals...
* Warn if linking section is present, as we cannot handle it yet (#1798)Alon Zakai2018-12-041-0/+3
|
* Implement nontrapping float-to-int instructions (#1780)Thomas Lively2018-12-041-0/+26
|
* Add support for a mutable globals as a Feature (#1785)Sam Clegg2018-11-301-2/+1
| | | | | This picks up from #1644 and indeed borrows the test case from there.
* Remove default cases (#1757)Thomas Lively2018-11-271-1/+3
| | | | | | Where reasonable from a readability perspective, remove default cases in switches over types and instructions. This makes future feature additions easier by making the compiler complain about each location where new types and instructions are not yet handled.
* initialize binary writer debug info even without a source map, as debug info ↵Alon Zakai2018-11-261-1/+5
| | | | may exist without a source map (#1733)
* clean up unnecessary type setting in binary format loading - finalize() will ↵Alon Zakai2018-11-141-62/+61
| | | | do it properly later (#1744)
* Support 4GB Memories (#1702)Alon Zakai2018-10-151-8/+8
| | | This fixes asm2wasm parsing of the max to allow 4GB, and also changes the internal Memory::kMaxValue values to reflect that. We used to use kMaxValue to also represent "no limit", so I split that out into kUnlimitedValue.
* Unify imported and non-imported things (#1678)Alon Zakai2018-09-191-159/+113
| | | | | | | | | | | | | | Fixes #1649 This moves us to a single object for functions, which can be imported or nor, and likewise for globals (as a result, GetGlobals do not need to check if the global is imported or not, etc.). All imported things now inherit from Importable, which has the module and base of the import, and if they are set then it is an import. For convenient iteration, there are a few helpers like ModuleUtils::iterDefinedGlobals(wasm, [&](Global* global) { .. use global .. }); as often iteration only cares about imported or defined (non-imported) things.
* Add debug information locations to the function prolog/epilog (#1674)Yury Delendik2018-09-171-49/+70
| | | | | | | The current patch: * Preserves the debug locations from function prolog and epilog * Preserves the debug locations of the nested blocks
* Misc tiny fuzz fixes (#1668)Alon Zakai2018-09-121-0/+1
| | | | | | | | | | | | * show a proper error for an empty asm2wasm input * handle end of input in processExpressions in binary reading * memory segment sizes should be unsigned * validate input in wasm-ctor-eval * update tests
* Binary format local parsing fixes (#1664)Alon Zakai2018-09-111-8/+11
| | | | | | * Error if there are more locals than browsers allow (50,000). We usually just warn about stuff like this, but we do need some limit (or else we hang or OOM), and if so, why not use the agreed-upon Web limit. * Do not generate nice string names for locals in binary parsing - the name is just $var$x instead of $x, so not much benefit, and worse as our names are interned this is actually slow (which is why the fuzz testcase here hangs instead of OOMing). Testcases and bugreport in #1663.
* Fix findField (#1660)Yury Delendik2018-08-311-0/+2
|
* Fix read-write of dylink section (#1648)Alon Zakai2018-08-311-7/+26
| | | | | | The 'dylink' user section must be emitted before all other sections, per the spec (to allow simple parsing by loaders) This PR makes reading and writing of a dynamic library remain a valid dynamic library.
* Escape name section ids in binary format reading/writing to be WebAssembly ↵Yury Delendik2018-08-311-2/+68
| | | | spec compatible. (#1646)
* Stack IR (#1623)Alon Zakai2018-07-301-771/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | This adds a new IR, "Stack IR". This represents wasm at a very low level, as a simple stream of instructions, basically the same as wasm's binary format. This is unlike Binaryen IR which is structured and in a tree format. This gives some small wins on binary sizes, less than 1% in most cases, usually 0.25-0.50% or so. That's not much by itself, but looking forward this prepares us for multi-value, which we really need an IR like this to be able to optimize well. Also, it's possible there is more we can do already - currently there are just a few stack IR optimizations implemented, DCE local2stack - check if a set_local/get_local pair can be removed, which keeps the set's value on the stack, which if the stars align it can be popped instead of the get. Block removal - remove any blocks with no branches, as they are valid in wasm binary format. Implementation-wise, the IR is defined in wasm-stack.h. A new StackInst is defined, representing a single instruction. Most are simple reflections of Binaryen IR (an add, a load, etc.), and just pointers to them. Control flow constructs are expanded into multiple instructions, like a block turns into a block begin and end, and we may also emit extra unreachables to handle the fact Binaryen IR has unreachable blocks/ifs/loops but wasm does not. Overall, all the Binaryen IR differences with wasm vanish on the way to stack IR. Where this IR lives: Each Function now has a unique_ptr to stack IR, that is, a function may have stack IR alongside the main IR. If the stack IR is present, we write it out during binary writing; if not, we do the same binaryen IR => wasm binary process as before (this PR should not affect speed there). This design lets us use normal Passes on stack IR, in particular this PR defines 3 passes: Generate stack IR Optimize stack IR (might be worth splitting out into separate passes eventually) Print stack IR for debugging purposes Having these as normal passes is convenient as then they can run in parallel across functions and all the other conveniences of our current Pass system. However, a downside of keeping the second IR as an option on Functions, and using normal Passes to operate on it, means that we may get out of sync: if you generate stack IR, then modify binaryen IR, then the stack IR may no longer be valid (for example, maybe you removed locals or modified instructions in place etc.). To avoid that, Passes now define if they modify Binaryen IR or not; if they do, we throw away the stack IR. Miscellaneous notes: Just writing Stack IR, then writing to binary - no optimizations - is 20% slower than going directly to binary, which is one reason why we still support direct writing. This does lead to some "fun" C++ template code to make that convenient: there is a single StackWriter class, templated over the "mode", which is either Binaryen2Binary (direct writing), Binaryen2Stack, or Stack2Binary. This avoids a lot of boilerplate as the 3 modes share a lot of code in overlapping ways. Stack IR does not support source maps / debug info. We just don't use that IR if debug info is present. A tiny text format comment (if emitting non-minified text) indicates stack IR is present, if it is ((; has Stack IR ;)). This may help with debugging, just in case people forget. There is also a pass to print out the stack IR for debug purposes, as mentioned above. The sieve binaryen.js test was actually not validating all along - these new opts broke it in a more noticeable manner. Fixed. Added extra checks in pass-debug mode, to verify that if stack IR should have been thrown out, it was. This should help avoid any confusion with the IR being invalid. Added a comment about the possible future of stack IR as the main IR, depending on optimization results, following some discussion earlier today.
* Fix source map entries offset when LEB is compressed. (#1628)Yury Delendik2018-07-251-15/+38
|
* Refactor stack writing code into a new StackWriter class (#1620)Alon Zakai2018-07-161-168/+172
| | | | | | | This separates out the WasmBinaryWriter parts that do stack writing into a separate class, StackWriter. Previously the WasmBinaryWriter did both the general writing and the stack stuff, and the stack stuff has global state, which it manually cleaned up etc. - seems nicer to have it as a separate class, a class focused on just that one thing. Should be no functional changes in this PR. Also add a timeout to the wasm-reduce test, which happened to fail on one of the commits here. It was running slower on that commit for some reason, could have been random - I verified that general wasm writing speed is unaffected by this PR. (But I added the timeout to prevent future random timeouts.)
* Minor code cleanups (#1617)Alon Zakai2018-07-101-123/+123
| | | | | | * code cleanups in wasm-binary: remove an & param, and standardize whitespace * add some docs for how the relooper handles blocks with no outgoing branches [ci skip]
* Improve source map parsing to handle whitespace (#1598)Sam Clegg2018-06-131-14/+34
|
* Fix MSVC warnings when compiling the binaryen target (#1535)Daniel Wirtz2018-05-091-4/+4
|
* Better binary error reporting (#1505)Alon Zakai2018-04-131-66/+73
| | | | | Report the offset with the error. Also fix a compiler warning about comparing signed/unsigned types in the LEB code.
* Fix bad param/var type error handling (#1499)Alon Zakai2018-04-101-8/+21
| | | Improve error handling, validation, and assertions for having a non-concrete type in an inappropriate place. Fixes a fuzz testcase.
* Handle literally unreachable brs (#1497)Alon Zakai2018-04-071-6/+16
| | | | | The optimization in #1495 had a bug which was found by the fuzzer: our binary format parsing will not emit unreachable code (it may be stacky, so we ignore it). However, while parsing it we note breaks that are taken there, and then we removed that code, leading to a state where a break was not taken in the code, but we thought it was. This PR clarifies the difference between unreachable code in the wasm sense (anything from the start of a block til an unreachable is "reachable") and the literal sense (even that code at the start may not be literally reachable if the block is not reachable), and then we use literal unreachability to know what code will be ignored and therefore we should ignore breaks in.
* when creating blocks in binary format parsing, we know if a block has a ↵Alon Zakai2018-04-051-1/+1
| | | | break to it - use that to avoid rescanning blocks for unreachability purposes (#1495)
* validate we are in a function context when adding a label in binary parsing. ↵Alon Zakai2018-03-161-15/+16
| | | | found by valgrind (#1478)
* Function pointer cast emulation (#1468)Alon Zakai2018-03-131-5/+5
| | | | | | | | | | | 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.
* ensure unique import names for each type, by giving them a prefix, avoiding ↵Alon Zakai2018-02-221-1/+7
| | | | collisions between say a global import and a function with a name from the name section that happens to match it (#1424)
* Dedupe function names when reading a binary (#1396)Jacob Gravelle2018-02-061-8/+9
| | | | | | | | * Dedupe function names when reading a binary * More robust name deduplication, use .s instead of _s * Add name-duplicated wasm binaries
* Rename WasmType => Type (#1398)Alon Zakai2018-02-021-26/+26
| | | | * rename WasmType to Type. it's in the wasm:: namespace anyhow, and without Wasm- it fits in better alongside Index, Address, Expression, Module, etc.
* Don't write sourceMappingURL section if URL is empty. (#1390)Yury Delendik2018-01-261-1/+1
|
* Atomic wait/wake fixes (#1383)Alon Zakai2018-01-221-7/+36
| | | | | | | | * fix wait and wake binary format support, they have alignments and offsets * don't emit unreachable parts of atomic operations, for simplicity and to avoid special handling * don't emit atomic waits by default in the fuzzer, they hang in native vm support
* First pass at LLD support for Emscripten (#1346)Jacob Gravelle2018-01-221-3/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Skeleton of a beginning of o2wasm, WIP and probably not going to be used * Get building post-cherry-pick * ast->ir, remove commented out code, include a debug module print because linking * Read linking section, print emscripten metadata json * WasmBinaryWriter emits user sections on Module * Remove debugging prints, everything that isn't needed to build metadata * Rename o2wasm to lld-metadata * lld-metadata support for outputting to file * Use tables index instead of function index for initializer functions * Add lld-emscripten tool to add emscripten-runtime functions to wasm modules (built with lld) * Handle EM_ASM in lld-emscripten * Add a list of functions to forcibly export (for initializer functions) * Disable incorrect initializer function reading * Add error printing when parsing .o files in lld-metadata * Remove ';; METADATA: ' prefix from lld-metadata, output is now standalone json * Support em_asm consts that aren't at the start of a segment * Initial test framework for lld-metadata tool * Add em_asm test * Add support for WASM_INIT_FUNCS in the linking section * Remove reloc section parsing because it's unused * lld-emscripten can read and write text * Add test harness for lld-emscripten * Export all functions for now * Add missing lld test output * Add support for reading object files differently Only difference so far is in importing mutable globals being an object file representation for symbols, but invalid wasm. * Update help strings * Update linking tests for stackAlloc fix * Rename lld-emscripten,lld-metadata to wasm-emscripten-finalize,wasm-link-metadata * Add help text to header comments * auto& instead of auto & * Extract LinkType to abi/wasm-object.h * Remove special handling for wasm object file reading, allow mutable globals * Add braces around default switch case * Fix flake8 errors * Handle generating dyncall thunks for imports as well * Use explicit bool for stackPointerGlobal * Use glob patterns for lld file iteration * Use __wasm_call_ctors for all initializer functions
* Function metrics pass (#1353)Alon Zakai2018-01-121-0/+1
| | | Emits binary size and opcode counts for each function, which helps investigating what's taking up space in a wasm binary.
* Do not emit 100k data segments, browsers reject it (#1350)Alon Zakai2018-01-091-4/+78
| | | | | Instead merge constant-offset segments if we must in order to stay under the limit. If we can't - too many non-constant-offset segments - then issue a warning.
* Fix 2 binary fuzz bugs (#1323)Alon Zakai2017-12-141-1/+6
| | | | | | * Check if there is a currFunction before using it (we need it for some stacky code; a valid wasm wouldn't need a function in that location anyhow, as what can be put in a memory/table offset is very limited). * Huge alignment led us to do a power of 2 shift that is undefined behavior. Also adds a test facility to check we don't crash on testcases.
* Binary fuzz fix: disallow popping from outside a block (#1305)Alon Zakai2017-11-281-0/+6
| | | | | | * remove unneeded code to handle a br to the return from the function. Now that we use getBlockOrSingleton there, it does that for us anyhow * fix a fuzz bug of popping from outside a block
* Fix reading breaks to the function exit (#1304)Alon Zakai2017-11-211-17/+5
| | | | * remove unneeded code to handle a br to the return from the function. Now that we use getBlockOrSingleton there, it does that for us anyhow
* name function imports using name section (#1290)Alon Zakai2017-11-211-19/+31
|
* a stacky value in the middle of a block may be consumed (#1267)Alon Zakai2017-11-131-1/+20
|
* notation change: AST => IR (#1245)Alon Zakai2017-10-241-2/+2
| | | The IR is indeed a tree, but not an "abstract syntax tree" since there is no language for which it is the syntax (except in the most trivial and meaningless sense).
* Emit binary function index in comment in text format, for convenience (#1232)Alon Zakai2017-10-201-28/+4
|
* Move pointer positioning outside of vector access operator to avoid MSVC ↵Mark A. Ropper2017-10-201-2/+2
| | | | complaining about out-of-range values (#1233)
* Atomics support in interpreter + optimizer + fuzz fixes for that (#1227)Alon Zakai2017-10-201-0/+22
|
* Add Builder::makeGlobal for nicer global creation (#1221)Alon Zakai2017-10-101-6/+8
|