Skip to content

Instantly share code, notes, and snippets.

@fitzgen
Created July 31, 2017 21:09
Show Gist options
  • Save fitzgen/187381e358f60efa8194d0b276b4d11a to your computer and use it in GitHub Desktop.
Save fitzgen/187381e358f60efa8194d0b276b4d11a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Usage:
#
# ./oracle.sh <bindgen> <test case>
#
# <bindgen> is the path to the bindgen executable.
#
# <test case> is the path to a C or C++ header file to test.
#
# Exits 0 if the test case triggers a bindgen bug.
#
# Exits 1 if the test case is not interesting.
#
# Assumes that rustc, clang++, and libclang >= 3.9 are available.
# Exit the script with a nonzero exit code if:
# * any individual command finishes with a nonzero exit code, or
# * we access any undefined variable.
# * a process in a pipe fails
set -eu
set -o pipefail
# Print out the stack when Rust code panics.
export RUST_BACKTRACE=1
BINDGEN=$1
TEST_CASE=$2
# Make sure that the test case is valid C++ by compiling it.
clang++ --std=c++14 -c "$HEADER" || {
echo "Not interesting: bindgen can only process valid C and C++ header"
echo "files."
exit 1
}
BINDINGS=$(mktemp -t "bindings-XXXXXX.rs")
# Use bindgen to emit Rust bindings and FFI glue to the types and functions
# defined in the test case.
"$BINDGEN" "$TEST_CASE" -o "$BINDINGS" -- -x c++ -std=c++14 || {
echo "Interesting: bindgen failed to generate bindings for the test case!"
exit 0
}
TEST_BIN=$(mktemp -t "test-bindings-XXXXXX")
# Use rustc to compile the bindings and their unit tests that assert that the
# Rust layout of a type matches what libclang told us was the C/C++ layout of
# the type.
rustc --test "$BINDINGS" -o "$TEST_BIN" || {
echo "Interesting: bindgen emitted Rust code that won't compile!"
exit 0
}
# Finally, run the layout unit tests.
"$TEST_BIN" || {
echo "Interesting: bindgen emitted type definitions that are failing their"
echo "layout unit tests!"
exit 0
}
echo "Not interesting: bindgen emitted Rust bindings, they compiled, and the"
echo "layout unit tests passed."
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment