Skip to content

Commit

Permalink
scripts/pdg.sh: also run analysis
Browse files Browse the repository at this point in the history
this was way too painful to plumb and debug for what it ended up being :(
  • Loading branch information
fw-immunant committed Apr 2, 2024
1 parent f3b5623 commit fb37a81
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions scripts/pdg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ SCRIPT_DIR="${CWD}/$(dirname "${SCRIPT_PATH}")"
# Environment Variables:
# * `PROFILE` (default `release`):
# a `cargo` profile as in `target/$PROFILE`
# * `NO_USE_PDG` (default empty):
# if non-empty, do not use the PDG as a starting point for analysis
#
# Instrument and run a test crate and create its PDG.
# Instrument and run a test crate, create its PDG, and then run analysis on it.
#
# 1. Compile `c2rust-dynamic-instrumentation`.
# 3. Redirect `c2rust-dynamic-instrumentation` stdout to `instrument.out.log` in the test crate directory,
# 2. Redirect `c2rust-dynamic-instrumentation` stdout to `instrument.out.log` in the test crate directory,
# as it prints a lot of debugging info currently.
# 4. Run the instrumented binary directly.
# 3. Run the instrumented binary directly.
# The `bincode`-encoded event log is written to `log.bc`.
# 5. Using the `metadata.bc` metadata and the `log.bc` event log,
# 4. Using the `metadata.bc` metadata and the `log.bc` event log,
# run `c2rust-pdg` to generate the pdg.
# The output is saved to `pdg.log` (relative to the test crate directory).
# A machine-readable PDG is saved to `pdg.bc` in the same directory.
# 5. Using the `pdg.bc` file as an initial state for analysis, run static
# analysis using `c2rust-analyze`.
main() {
local test_dir="${1}"
local args=("${@:2}")
Expand All @@ -41,6 +45,7 @@ main() {
local event_log="${test_dir}/log.bc"
local runtime="analysis/runtime"

# build and run a test with instrumentation
(
unset RUSTFLAGS # transpiled code has tons of warnings; don't allow `-D warnings`
export RUST_BACKTRACE=1
Expand All @@ -62,6 +67,7 @@ main() {
"${profile_args[@]}" \
-- "${args[@]}"
)
# construct pdg from log events
(
export RUST_BACKTRACE=full # print sources w/ color-eyre
export RUST_LOG=error
Expand All @@ -77,6 +83,24 @@ main() {
--output "${pdg}" \
> "${test_dir}/pdg.log"
)
# use pdg in analysis
(
export RUST_BACKTRACE=full # print sources w/ color-eyre
export RUST_LOG=error
if [[ "${NO_USE_PDG:-}" == "" ]]; then
# cargo runs this from a different pwd, so make path absolute
export PDG_FILE="$(realpath ${pdg})"
fi
cargo run \
--bin c2rust-analyze \
"${profile_args[@]}" \
-- \
build \
-- \
"${profile_args[@]}" \
--features=c2rust-analysis-rt \
--manifest-path "${test_dir}/Cargo.toml"
)
}

main "${@}"

0 comments on commit fb37a81

Please sign in to comment.