Commit Graph

12166 Commits

Author SHA1 Message Date
Robert Xiao
ccd27f1c40 Don't create empty segments; this avoids angry warnings in the log 2024-06-10 08:38:03 +08:00
Robert Xiao
0d27154046 spectest bugfix: element section type param ("flags") is actually LEB128
This is tested by binary-leb128, and previously was failing to load the module
when the element section type parameter was encoded as a multi-byte LEB128.
However, since we weren't checking to see whether the modules loaded
successfully, this failed silently. The addition of WasmAnalysis forces us to
check whether the Wasm module parses correctly.
2024-06-10 08:38:03 +08:00
Robert Xiao
453fd1b751 Provide a simulateCall convenience function to set up a simulated call frame 2024-06-10 08:38:03 +08:00
Robert Xiao
e7490326d6 spectest bugfix: zero out locals when emulating
This fixes some failures from call_indirect, among others. Instead of using an
uponentry injection, this new implementation hijacks the ".locals" directive to
add a CallOther pcodeop, which is usable from both the decompiler and emulator.
2024-06-10 08:38:03 +08:00
Robert Xiao
4fa6ea3dab spectest bugfix: fix copysign to work with nans, etc.
Fixes test failures in f{32,64}_bitwise and float_misc.
2024-06-10 08:38:03 +08:00
Robert Xiao
c21512cc50 spectest bugfix: fix br_table semantics when unreachable
When unreachable, br_table may underflow the stack, which is ok. However,
br_table needs to push Unknown types back on the stack, rather than the expected
types. This subtlety was caught by the unreached-valid meet-bottom testcase.
2024-06-10 08:38:03 +08:00
Robert Xiao
86e552decd spectest bugfix: fix table.grow semantics
table.grow produces an i32 (the old table size); this was incorrectly omitted
and caused validation failures on modules containing that instruction.
2024-06-10 08:38:02 +08:00
Robert Xiao
7fb06d3be0 spectest bugfix: avoid crash on parsing select after br
After a br, the remaining code in the block is unreachable and types are no
longer tracked on the stack. Thus, multiple null types can creep in, which was
causing (among other things) crashes when analyzing a select instruction after a
br.
2024-06-10 08:38:02 +08:00
Robert Xiao
95eb40a244 spectest bugfix: initialize tables and element segments with 0x00
Tables and element segments apparently default to holding null refs, and since
we're using 0x00000000 for null (`ref.null`, `ref.is_null`), we need to ensure
that they are initialized appropriately.

In the future, we may want to revisit whether zero is the right value for null -
e.g. can zero be a valid value for some reference type?
2024-06-10 08:38:02 +08:00
Robert Xiao
97b74acde9 spectest bugfix: don't crash when jumping to no-context addresses
The VM may jump to an uninitialized address such as 0x00000000. If no context is
available at that address, this would crash the emulator before the instruction
can even be hit (e.g. for a breakpoint).
2024-06-10 08:38:02 +08:00
Robert Xiao
5b06763908 spectest bugfix: do not use float2float on constants
float2float may modify the floating point value, but f{32,64}.const instructions
are supposed to load the raw constant value. Fixes test failures on
float_literals.
2024-06-10 08:38:02 +08:00
Robert Xiao
2abda76bc0 SIMD refactoring: implement some bitwise and arith ops in P-code 2024-06-10 08:38:02 +08:00
Robert Xiao
d3ebad765a SIMD refactoring: implement shuffle in P-code 2024-06-10 08:38:02 +08:00
Robert Xiao
da7d334871 SIMD refactoring: implement extract and replace in P-code 2024-06-10 08:38:02 +08:00
Robert Xiao
784ef5f543 SIMD refactoring: implement splat in P-code 2024-06-10 08:38:02 +08:00
Robert Xiao
1700bf6adb Remove helpPath task - not needed after a7668c7f8 2024-06-10 08:38:02 +08:00
Robert Xiao
06fe349e14 WebAssembly: minor updates to sync to master 2024-06-10 08:38:02 +08:00
Robert Xiao
9684b738c7 WebAssembly: fix load32/load64, simplify some SIMD pcode
load32/load64 were incorrectly taking a laneidx, causing disassembly failures.
Also, take this opportunity to implement some of the SIMD operations using
common pcodeops. This will reduce emulation burden, as well as making loads and
stores explicit for dataflow analysis.
2024-06-10 08:38:02 +08:00
Robert Xiao
67f16d97d6 WebAssembly: Simplify laneidx vector operands.
laneidx is actually just a byte, not a LEB128. Attempting to use 16 arguments in
a pcodeop hits a hardcoded limit of 8 args in PcodeEmit, so switch i8x16.shuffle
to using 4 32-bit arguments instead. This is the same syntax as used by
wasm-objdump by default.
2024-06-10 08:38:02 +08:00
Robert Xiao
1e5572ac52 WebAssembly: add a SIMD pcodetest.
Similarly to the x86 AVX2 test, this will allow us to do (basic) testing of Wasm
SIMD opcodes via autovectorization. Only the -O3 binary is expected to contain
SIMD opcodes.
2024-06-10 08:38:02 +08:00
Robert Xiao
93bff2b669 WebAssembly: Fix pcode_conversions_Main test.
This is fixed by implementing unsigned-to-float conversions properly.
2024-06-10 08:38:02 +08:00
Robert Xiao
00722cb6ae WebAssembly: implement pcodetest tests.
We pass almost every test with -O0 and -O3, with the exception of pcode_conversions_Main.

There's a rather ugly hack needed to convert certain function pointers from
table indices (used by the actual code) to byte addresses (used by the emulator
to set PC). The way this is implemented is decidedly not ideal; a much better
solution would be to somehow hook readCodePointer in the
ProcessorEmulatorTestAdapter subclass; this would also enable the "procedure
descriptor indirection" fix to be moved into a processor-specific
implementation.

Changing the structs to protected in `ProcessorEmulatorTestAdapter` is for
convenience, so that the subclass does not need to go look those up again.

The zero-size check added to `BytesPcodeExecutorStateSpace#read` fixes a bug
which caused an exception when reading 0 bytes (`offset + size - 1` is not a
valid calculation in that case).
2024-06-10 08:38:02 +08:00
Robert Xiao
6726b40fc3 WebAssembly: add wasm build instructions to pcodetest
This successfully builds a working binary with Clang 15.

- `mem*` definitions in `misc_BODY.c` and new `main` definition in `tpp.py`
  are for C99 compatibility
- `encoding='utf8'` in build.py produces a more readable log
- `TestInfo_force` in pcode_test.c ensures that the entire `MainInfo` structure
  is included in the binary
2024-06-10 08:38:02 +08:00
Robert Xiao
907a7b52e9 WebAssembly: update ConstantExpression comment 2024-06-10 08:38:01 +08:00
Robert Xiao
681617cc84 WebAssembly: Add a stack analyzer which creates C stack variables for Wasm.
The traditional stack analyzer, StackVariableAnalyzer
(NewFunctionStackAnalysisCmd) depends on having registers that contain stack
addresses visible as assembly operands. However, due to the Wasm disassembler
design, which hides the Wasm stack registers from the disassembly, this stack
analysis cannot automatically extract stack variables.

As a fix, adapt FunctionStackAnalysisCmd to operate on the hidden Wasm stack
register operands to detect operations that interact with C stack addresses.

This helps with cross references to C stack variables, as well as allowing C
stack variables to be properly interacted with in the decompiler.
2024-06-10 08:38:01 +08:00
Robert Xiao
277c0461d8 WebAssembly: Mark __wasm prototypes as register strategies 2024-06-10 08:38:01 +08:00
Robert Xiao
b250cb9de0 WebAssembly: Add a new script to handle "flat" dyncall namespaces.
It seems like LLVM, for instance, uses table indices as function pointers, so
this script is likely to be useful for anything compiled with LLVM. As a guess,
analyze_dyncalls is probably only useful for programs compiled using the
Emscripten fastcomp backend.
2024-06-10 08:38:01 +08:00
Robert Xiao
55ad5fd854 WebAssembly: disable 2+ output vars for pos-stack as it is unsupported.
Without this, we get the error "<pentry> tags within a group must be
distinguished by size or type" when attempting to load the pos-stack compiler
spec. This will break functions that have multiple output arguments, but those
are expected to be rare (and they are not well-supported by Ghidra anyway).
2024-06-10 08:38:01 +08:00
Robert Xiao
74b5f591e7 WebAssembly: update to use standard LEB128 utilities 2024-06-10 08:38:01 +08:00
Robert Xiao
dccfe6625d WebAssembly: remove now-deleted DataTypeConflictException 2024-06-10 08:38:01 +08:00
Robert Xiao
caf8c116de WebAssembly: update help stylesheet to recommended style 2024-06-10 08:38:01 +08:00
Robert Xiao
e744db5a1d WebAssembly: create code length data to avoid undefined bytes 2024-06-10 08:38:01 +08:00
Robert Xiao
48b2a2e385 WebAssembly: SIMD support.
Disassembly and verification are implemented, but almost all of the SIMD opcodes
are just stubbed out with pcodeops for now, so semantics aren't implemented. This
is probably good enough for now.
2024-06-10 08:38:01 +08:00
Robert Xiao
54467230bd WebAssembly: correctly handle u32 opcodes.
0xFC (and 0xFD) have a u32 as the second operand, so we need to handle this
correctly.
2024-06-10 08:38:01 +08:00
Robert Xiao
97a4fb1948 WebAssembly: Emulation support
This patch implements a EmulateInstructionStateModifier for Wasm which provides
support for emulating Wasm instructions.

The memory contents must contain a full module for this to work, because
instruction semantics still depend on module details (e.g. the type of certain
operations depends on metadata like the types of imports or globals).
2024-06-10 08:38:01 +08:00
Robert Xiao
8d9cf40cac WebAssembly: extend regs to 16 bytes to prepare for vector insts
Vector instructions will require 128-bit values, so prepare for that change by
widening all registers to 128 bits.
2024-06-10 08:38:01 +08:00
Robert Xiao
593b824b13 WebAssembly: load .module directly to 0x8000000 in RAM
This allows the whole module to be loaded in memory and eliminates the previous
duplication of the .code bytes in the .module and the .function bytes in RAM.
2024-06-10 08:38:01 +08:00
Robert Xiao
e692786367 WebAssembly: reduce dependency on Program class.
Using Program throughout is rather lazy, and in many cases totally unnecessary.
Using finer-grained interfaces and classes like AddressFactory allows usage of
the code in non-Program contexts, such as emulation.
2024-06-10 08:38:01 +08:00
Robert Xiao
34f2ce8eee WebAssembly: make funcref/externref 32 bits in size.
The use of 64 bits was mostly a debugging feature to ensure we didn't accidentally
mix references with normal types. 64 bit addresses cause some problems, though,
such as 64-bit immediates (e.g. from ref.func) not always being treated as addresses
in the decompiler.
2024-06-10 08:38:00 +08:00
Robert Xiao
3c3a19fa2f WebAssembly: add a help page to explain the finer points of the extension.
I was hoping to add this to a Processors help submenu, but it doesn't exist,
so I put it in the Appendix instead for now.
2024-06-10 08:38:00 +08:00
Robert Xiao
58c48b654a WebAssembly support.
Merged from https://github.com/nneonneo/ghidra-wasm-plugin/.

Closes #2937.
2024-06-10 08:38:00 +08:00
ghidra1
dee48e96c1 GP-0 WhatsNew formatting change for improved help viewer rendering 2024-06-07 14:10:51 -04:00
ghidra1
db8da86456 GP-0 GNU Demangler process startup improvement 2024-06-07 12:30:21 -04:00
ghidra1
b54a7e85b0 GP-0 Corrected arch compiler mapping 2024-06-06 20:58:15 -04:00
d-millar
1e4882d82d GP-0: fix for opinion failures in dbgeng 2024-06-06 19:59:00 -04:00
ghidra1
b1a67cce48 GP-3878 Corrected ELF x86 32-bit PLT processing issue 2024-06-06 19:29:38 -04:00
ghidra1
ad5975fdc2 GP-0 Additional WhatsNew edits 2024-06-06 16:52:09 -04:00
ghidra1
f14b7db25a GP-0 Additional WhatsNew HTML formatting corrections 2024-06-06 16:26:04 -04:00
ghidra1
3ebc46a2ca GP-0 Improved process error handling for Decompiler and GNU Demangler.
Corrected minor GNU Demangler demangler command and script issues.
2024-06-06 15:11:14 -04:00
Ryan Kurtz
7079179b59 GP-0: Adding null log check in LibraryLookupTable.java 2024-06-06 10:02:28 -04:00