Skip to content

Commit

Permalink
PPC RZIL tests (#75)
Browse files Browse the repository at this point in the history
* Remove 32bit bin; Add load/store; rename bin
* Align `.data` to 24 bytes.
* Add tests for logical ops.
* Cleanup and add some comments.
* Set cr0 flag for each isntruction.
* Align .data to 12bytes: reduces object file size.
* Implement helper to load min/max of b/h/w/d.
* Move helper functions into own files.
* Add branch isntruciton test.
* Add branch instruction tests.
* Add asm test binary.
* Add rotate rzil tests.
* Add cr0 set test for rotate and shift instructions.
* Add NEG instruction test.
* Move branch test to beginning of binary.
* Implement `mr RA, RS`.
* Add NOP tests.
* Add MTLR/MFLR.
* Add SLWI/SRWI.
* Move MTSPR,MFSPR tests to a lower binary address.
* Add tests for  `EXTSB`, `EXTSH`, `EXTSW`.
* Add return statements to insn tests. Add clear gpr fcn.
* Add qemu runable test bin
* Build with musl; Null gprs before branch
* Fix load and store tests
The immediate values don't need to be shifted to the right.
The shift happens CPU internally. No need for us to do it.
* Add \"le\" to log message to clearigy that it is a little endian binary
* Add branch mnemonic and set CR instruction tests
* Add xnop instr.
* Add commented out isel test (Musl/gnu compiler dont recognize isel.)
* Add tests for CRCLR, CRSET, CROR
* Add: CNTLZ instruction tests.
* Add mcrf tests
* Add instructions for cmpb tests.
* Add load brx instructions.
* Fix rotate tests.
* Add DCBZ test.
* Add eqv test.
* Add helper function to copy -1 to r11
* Add high word/double word mul tests.
* Add div tests.
* Add mtxer mfxer tests
* Add qemu instruction test binary with all currently testable instructions..
* Add README with infos about the binaries and the toolchains.
* Restructure test binaries to prevent segfaults.
* Add notes about writing tests.
* Update binary
* Fix typo
* Compile QEMU exclusive binary
* Dont skip r4 register for backup.
* fix typo
* Prepare rename: ppc64_fp -> ppc64_uplifted
* Use same src for qemu and rizin tests.
* Renames the src file to ppc64.S and the binary to ppc64le_uplifted to distinguish more easily between different bins
* Add copyright info
* Restructure test binaries.
* There are now 32be and 64le test binaries
* There are instructions how to compile them and what compile to use.
* Source files has been splitted into 32bit and 64bit where necesssary.
* Update binaries
* Add load instructions to run_all_tests
* Add remarks about dcbz.
* Update asm tests.
* Add binary to pseudo fuzz each implemented instruction.
* Change asm_insn to match the asm_pseudo_fuzz_tests src file (remove usage of r30/r31 regs).
* Add build command for building asm pseudo fuzz tests.
* Update binaries.
* Add run all tests script
* Run all four random data dumps over all instructions.
* remove asm_pseudo_fuzz_tests.frames
* Rename asm_pseudo_fuzz_tests -> pseudo_fuzz_tests
* Update binaries.
* Add emulate tests.
* Move src files into repos `src` dir.
* Add 32bit emulateme bins
* Add emulateme 64bit test binaries.
* Clean up log messages and script
* Update toolchain description in README and compile emulateme bins with GNU/intel.
* Update binaries.
Co-authored-by: Giovanni <561184+wargio@users.noreply.github.com>
  • Loading branch information
Rot127 authored Aug 6, 2022
1 parent 747b101 commit 7d0c31a
Show file tree
Hide file tree
Showing 27 changed files with 3,046 additions and 0 deletions.
1 change: 1 addition & 0 deletions elf/ppc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.frames
47 changes: 47 additions & 0 deletions elf/ppc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
SPDX-FileCopyrightText: 2022 Rot127 <unisono@quyllur.org>
SPDX-License-Identifier: LGPL-3.0-only
-->

### Overview

This folder contains the test binaries for the PPC RZIL uplifting.

The binaries are used for validation with [rz-tracetest](https://github.com/rizinorg/rz-tracetest) against QEMU and the RZIL related asm and analysis tests.

Run `./build_tests.sh` to build the binaries.

### Toolchains

For building we use the following toolchains:

- `ppc64le`: Intel [Intel advance toolchain](https://www.ibm.com/support/pages/advtool-cross-compilers)
- `ppc32` GNU toolchain.
- `ppc32le`, `ppc64`: MUSL toolchains

Intel and GNU toolchains can be installed via the package manager. (You can find the install instructions for the Intel toolchain at the link above).

MUSL toolchains can be downloaded [here](https://musl.cc/#binaries).

**Please note**: Exclusively using MUSL was not possible because binaries from MUSL segfault in QEMU before main is reached. If you get it to work, please open a PR.

After you've installed the toolchains simply run `./build_tests.sh`.

### Testing

- To generate the trace of the binary test files you need to build [BAPs QEMU](https://github.com/BinaryAnalysisPlatform/qemu) for `ppc64le` and `ppc`.
- Afterwards build [rz-tracetest](https://github.com/rizinorg/rz-tracetest).
- Make sure all tools are in your `PATH` and run `./run_trace_tests.sh`.

**Notes for manual testing**:

- Big endian traces need the `-b` option passed to `rz-tracetest`.
- Some instructions are broken in Capstone and cannot be emulated properly. Check Rizin's issues to find out which one and ignore them via `rz-tracetests` `-s` option.

### Adding new instructions

- The tests never use the stack to backup the LR register, stack and base pointers. The GPRs are backed up in `run_all_tests` and restored on exit. If you test instructions which manipulate the `LR` register, backup `LR` into `r30` and restored it when the test code returns.

- Due to this backup of `LR` your tests should never use the `r30` register.

- Please add all new instructions to both the 64 and 32bit src files. If it is a 64bit only instruction add it to the corresponding 32bit source file anyways (and comment it out). This way we can do a simple diff between both source files and check that no instruction has been forgotten.
Binary file added elf/ppc/asm_tests
Binary file not shown.
33 changes: 33 additions & 0 deletions elf/ppc/build_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
# SPDX-FileCopyrightText: 2022 Rot127 <unisono@quyllur.org>
# SPDX-License-Identifier: LGPL-3.0-only

CWD=$(pwd)
cd ../../src/ppc-rzil

echo "* asm_tests"
powerpc64le-linux-gnu-as -a64 -mregnames asm_tests.S -o "$CWD"/asm_tests

echo "* pseudo_fuzz_tests"
powerpc64le-linux-gnu-gcc -static -Wa,-mregnames pseudo_fuzz_main.c pseudo_fuzz_tests.S -o "$CWD"/pseudo_fuzz_tests

echo "* ppc64le_uplifted"
powerpc64le-linux-gnu-gcc -Ttext 0x100000 -static -Wa,-mregnames ppc_main.c ppc64.S -o "$CWD"/ppc64le_uplifted

echo "* ppc32be_uplifted"
powerpc-linux-gnu-gcc -Ttext 0x100000 -static -m32 -Wa,-mregnames ppc_main.c ppc32.S -o "$CWD"/ppc32be_uplifted

echo "* ppc32be_uplifted"
powerpc-linux-gnu-gcc -Ttext 0x100000 -static -m32 -Wa,-mregnames ppc_main.c ppc32.S -o "$CWD"/ppc32be_uplifted

echo "* emulateme-ppc32le"
powerpcle-linux-musl-gcc -Ttext 0x100000 -Wl,-no-pie -static ../../src/ppc-rzil/emulateme.c -o "$CWD"/emulateme-ppc32le

echo "* emulateme-ppc32be"
powerpc-linux-gnu-gcc -Ttext 0x100000 -Wl,-no-pie -static ../../src/ppc-rzil/emulateme.c -o "$CWD"/emulateme-ppc32be

echo "* emulateme-ppc64le"
powerpc64le-linux-gnu-gcc -Ttext 0x100000 -Wl,-no-pie -static ../../src/ppc-rzil/emulateme.c -o "$CWD"/emulateme-ppc64le

echo "* emulateme-ppc64be"
powerpc64-linux-musl-gcc -Ttext 0x100000 -Wl,-no-pie -static ../../src/ppc-rzil/emulateme.c -o "$CWD"/emulateme-ppc64be
Binary file added elf/ppc/emulateme-ppc32be
Binary file not shown.
Binary file added elf/ppc/emulateme-ppc32le
Binary file not shown.
Binary file added elf/ppc/emulateme-ppc64be
Binary file not shown.
Binary file added elf/ppc/emulateme-ppc64le
Binary file not shown.
Binary file added elf/ppc/ppc32be_uplifted
Binary file not shown.
Binary file added elf/ppc/ppc64le_uplifted
Binary file not shown.
Binary file removed elf/ppc/ppc_insn_tests_32
Binary file not shown.
Binary file removed elf/ppc/ppc_insn_tests_64
Binary file not shown.
Binary file added elf/ppc/pseudo_fuzz_tests
Binary file not shown.
41 changes: 41 additions & 0 deletions elf/ppc/run_trace_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2022 Rot127 <unisono@quyllur.org>
# SPDX-License-Identifier: LGPL-3.0-only

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "$0 \"<rz-tracetest exclude regex>\" (optional))"
exit
fi


echo "* Emulate pseudo_fuzz_tests"
qemu-ppc64le -tracefile pseudo_fuzz_tests.frames pseudo_fuzz_tests
echo "* Tracetest"
if [ $# -eq 1 ]; then
rz-tracetest -i -s "$1" pseudo_fuzz_tests.frames
else
rz-tracetest -i pseudo_fuzz_tests.frames
fi
echo "\n\n* DONE Test pseudo_fuzz_tests\n\n"


echo "* Emulate ppc32be"
qemu-ppc -tracefile ppc32be_uplifted.frames ppc32be_uplifted
echo "* Tracetest"
if [ $# -eq 1 ]; then
rz-tracetest -b -i -s "$1" ppc32be_uplifted.frames
else
rz-tracetest -b -i ppc32be_uplifted.frames
fi
echo "\n\n* DONE Test ppc32be\n\n"


echo "* Emulate ppc64le"
qemu-ppc64le -tracefile ppc64le_uplifted.frames ppc64le_uplifted
echo "* Tracetest"
if [ $# -eq 1 ]; then
rz-tracetest -i -s "$1" ppc64le_uplifted.frames
else
rz-tracetest -i ppc64le_uplifted.frames
fi
echo "\n\n* DONE Test ppc64le\n\n"
Loading

0 comments on commit 7d0c31a

Please sign in to comment.