Skip to content

Commit

Permalink
CI: add basic FFI struct validation
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z committed Feb 6, 2024
1 parent 332543a commit 245ac4a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
29 changes: 28 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,34 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Build
run: cargo build ${{ matrix.extra_args }}
run: cargo build --all --examples ${{ matrix.extra_args }}
- name: Validate FFI structs
if: runner.os == 'Linux'
run: |
set -eu
sudo apt-get install -y gdb
executable=target/debug/examples/pattern
if [[ ! -f "${executable}" ]]; then
echo "Test executable '${executable}' not built: skipping test"
exit 0
fi
gdb -q "${executable}" -batch \
-ex 'source validate-structs.py' \
1>script-out.txt 2>gdb-stderr.txt
if [[ $(cat ./script-out.txt) != *"ALL STRUCTS OK"* ]]; then
echo >&2 "ERROR: struct validation failed!"
echo >&2 "================================"
cat >&2 ./gdb-stderr.txt
echo >&2 "================================"
cat >&2 ./script-out.txt
exit 1
fi
echo PASSED.
- name: Test
run: cargo test ${{ matrix.extra_args }}

Expand Down
29 changes: 29 additions & 0 deletions validate-structs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# NOTE: this isn't a standalone script -- meant to be run within gdb!

# gdb's rust mode doesn't parse the `<` and `>` in the types correctly
gdb.execute('set language c++')


checked_types = [
('zydis::decoder::Decoder', 'ZydisDecoder'),
('zydis::ffi::decoder::DecodedOperand', 'ZydisDecodedOperand'),
('zydis::ffi::decoder::AccessedFlags<zydis::enums::CpuFlag>', 'ZydisAccessedFlags'),
('zydis::ffi::decoder::AccessedFlags<zydis::enums::FpuFlag>', 'ZydisAccessedFlags'),
('zydis::ffi::decoder::AvxInfo', 'ZydisDecodedInstructionAvx'),
('zydis::ffi::decoder::MetaInfo', 'ZydisDecodedInstructionMeta'),
('zydis::ffi::decoder::RawInfo', 'ZydisDecodedInstructionRaw'),
]


def sizeof(ty: str) -> int:
return gdb.parse_and_eval(f'sizeof({ty})')


for bind_ty, c_ty in checked_types:
bind_ty_sz = sizeof(bind_ty)
c_ty_sz = sizeof(c_ty)

assert bind_ty_sz == c_ty_sz, \
f'binding type {bind_ty} is {bind_ty_sz} bytes, but expected {c_ty_sz}'

print("ALL STRUCTS OK")

0 comments on commit 245ac4a

Please sign in to comment.