Skip to content

Commit 27dfd51

Browse files
committed
Make scripts/pdg.sh more flexible for more use cases
1 parent f898e86 commit 27dfd51

File tree

2 files changed

+129
-77
lines changed

2 files changed

+129
-77
lines changed

scripts/pdg.sh

100755100644
+13-77
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
set -euox pipefail
44

5-
CWD="${PWD}"
6-
SCRIPT_PATH="${0}"
7-
SCRIPT_DIR="${CWD}/$(dirname "${SCRIPT_PATH}")"
5+
SCRIPT_PATH="$(realpath ${0})"
6+
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"
7+
C2RUST_DIR="$(dirname "${SCRIPT_DIR}")"
8+
9+
source ${SCRIPT_DIR}/pdg_setup.sh
810

911
# Usage: `./pdg.sh <test crate dir> <test binary args...>`
1012
#
11-
# Environment Variables:
12-
# * `PROFILE` (default `release`):
13-
# a `cargo` profile as in `target/$PROFILE`
14-
# * `NO_USE_PDG` (default empty):
15-
# if non-empty, do not use the PDG as a starting point for analysis
13+
# Environment Variables: see `scripts/pdg_functions.sh` for a full list.
1614
#
1715
# Instrument and run a test crate, create its PDG, and then run analysis on it.
1816
#
@@ -27,80 +25,18 @@ SCRIPT_DIR="${CWD}/$(dirname "${SCRIPT_PATH}")"
2725
# A machine-readable PDG is saved to `pdg.bc` in the same directory.
2826
# 5. Using the `pdg.bc` file as an initial state for analysis, run static
2927
# analysis using `c2rust-analyze`.
30-
main() {
31-
local test_dir="${1}"
32-
local args=("${@:2}")
33-
34-
local profile_dir_name="${PROFILE:-release}"
3528

36-
local profile_dir="target/${profile_dir_name}"
37-
local profile="${profile_dir_name}"
38-
if [[ "${profile}" == "debug" ]]; then
39-
profile=dev
40-
fi
41-
local profile_args=(--profile "${profile}")
42-
43-
local metadata="${test_dir}/metadata.bc"
44-
local pdg="${test_dir}/pdg.bc"
45-
local event_log="${test_dir}/log.bc"
46-
local runtime="analysis/runtime"
29+
main() {
30+
CARGO_INSTRUMENT_COMMAND=run
4731

32+
# set the environment variables for instrumentation
33+
c2rust-set-instrument-vars "${@}"
4834
# build and run a test with instrumentation
49-
(
50-
unset RUSTFLAGS # transpiled code has tons of warnings; don't allow `-D warnings`
51-
export RUST_BACKTRACE=1
52-
export INSTRUMENT_RUNTIME=bg
53-
export INSTRUMENT_BACKEND=log
54-
export INSTRUMENT_OUTPUT="${event_log}"
55-
export INSTRUMENT_OUTPUT_APPEND=false
56-
export METADATA_FILE="${metadata}"
57-
58-
cargo run \
59-
--bin c2rust-instrument \
60-
"${profile_args[@]}" \
61-
-- \
62-
--metadata "${metadata}" \
63-
--set-runtime \
64-
--runtime-path "${runtime}" \
65-
-- run \
66-
--manifest-path "${test_dir}/Cargo.toml" \
67-
"${profile_args[@]}" \
68-
-- "${args[@]}"
69-
)
35+
c2rust-instrument "${@}"
7036
# construct pdg from log events
71-
(
72-
export RUST_BACKTRACE=full # print sources w/ color-eyre
73-
export RUST_LOG=error
74-
cargo run \
75-
--bin c2rust-pdg \
76-
"${profile_args[@]}" \
77-
-- \
78-
--event-log "${event_log}" \
79-
--metadata "${metadata}" \
80-
--print graphs \
81-
--print write-permissions \
82-
--print counts \
83-
--output "${pdg}" \
84-
> "${test_dir}/pdg.log"
85-
)
37+
c2rust-pdg "${@}"
8638
# use pdg in analysis
87-
(
88-
export RUST_BACKTRACE=full # print sources w/ color-eyre
89-
export RUST_LOG=error
90-
if [[ "${NO_USE_PDG:-}" == "" ]]; then
91-
# cargo runs this from a different pwd, so make path absolute
92-
export PDG_FILE="$(realpath ${pdg})"
93-
fi
94-
cargo run \
95-
--bin c2rust-analyze \
96-
"${profile_args[@]}" \
97-
-- \
98-
build \
99-
-- \
100-
"${profile_args[@]}" \
101-
--features=c2rust-analysis-rt \
102-
--manifest-path "${test_dir}/Cargo.toml"
103-
)
39+
c2rust-analyze-with-pdg "${@}"
10440
}
10541

10642
main "${@}"

scripts/pdg_setup.sh

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This script can be source'd to provide a few useful
4+
# shell functions for analysis using the PDG.
5+
#
6+
# Environment Variables:
7+
# * `RUST_PROFILE` (default `release`):
8+
# a `cargo` profile as in `target/$RUST_PROFILE`
9+
# * `NO_USE_PDG` (default empty):
10+
# if non-empty, do not use the PDG as a starting point for analysis
11+
# * `CARGO_INSTRUMENT_COMMAND` (default `build`):
12+
# the `cargo` command to run for instrumentation, e.g., `run` or `build`
13+
# * `INSTRUMENT_OUTPUT_APPEND` (default `false`):
14+
# set to `true` to append instrumentation events to the log instead
15+
# of replacing the existing log on every invocation of the instrumented
16+
# binary
17+
18+
SCRIPT_PATH="$(realpath ${BASH_SOURCE[0]})"
19+
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"
20+
export C2RUST_DIR="$(dirname "${SCRIPT_DIR}")"
21+
22+
_c2rust_prepare_vars() {
23+
test_dir="$(realpath ${1})"
24+
args=("${@:2}")
25+
26+
profile_dir_name="${RUST_PROFILE:-release}"
27+
28+
profile_dir="target/${profile_dir_name}"
29+
profile="${profile_dir_name}"
30+
if [[ "${profile}" == "debug" ]]; then
31+
profile=dev
32+
fi
33+
profile_args=(--profile "${profile}")
34+
35+
metadata="${test_dir}/metadata.bc"
36+
pdg="${test_dir}/pdg.bc"
37+
event_log="${test_dir}/log.bc"
38+
runtime="${C2RUST_DIR}/analysis/runtime"
39+
}
40+
41+
c2rust-set-instrument-vars() {
42+
_c2rust_prepare_vars "${@}"
43+
44+
# We need these variables to have exactly these values
45+
export INSTRUMENT_RUNTIME=bg
46+
export INSTRUMENT_BACKEND=log
47+
export INSTRUMENT_OUTPUT="${event_log}"
48+
export METADATA_FILE="${metadata}"
49+
50+
# These variables can be overridden by the user
51+
export INSTRUMENT_OUTPUT_APPEND=${INSTRUMENT_OUTPUT_APPEND:-false}
52+
}
53+
54+
c2rust-instrument() (
55+
_c2rust_prepare_vars "${@}"
56+
57+
unset RUSTFLAGS # transpiled code has tons of warnings; don't allow `-D warnings`
58+
export RUST_BACKTRACE=full
59+
60+
local cargo_instrument_command="${CARGO_INSTRUMENT_COMMAND:-build}"
61+
62+
cd "${C2RUST_DIR}"
63+
cargo run \
64+
--bin c2rust-instrument \
65+
"${profile_args[@]}" \
66+
-- \
67+
--metadata "${metadata}" \
68+
--set-runtime \
69+
--runtime-path "${runtime}" \
70+
-- "${cargo_instrument_command}" \
71+
--manifest-path "${test_dir}/Cargo.toml" \
72+
"${profile_args[@]}" \
73+
-- "${args[@]}"
74+
)
75+
76+
c2rust-pdg() (
77+
_c2rust_prepare_vars "${@}"
78+
79+
export RUST_BACKTRACE=full # print sources w/ color-eyre
80+
export RUST_LOG=error
81+
82+
cd "${C2RUST_DIR}"
83+
cargo run \
84+
--bin c2rust-pdg \
85+
"${profile_args[@]}" \
86+
-- \
87+
--event-log "${event_log}" \
88+
--metadata "${metadata}" \
89+
--print graphs \
90+
--print write-permissions \
91+
--print counts \
92+
--output "${pdg}" \
93+
> "${test_dir}/pdg.log"
94+
)
95+
96+
c2rust-analyze-with-pdg() (
97+
_c2rust_prepare_vars "${@}"
98+
99+
export RUST_BACKTRACE=full # print sources w/ color-eyre
100+
export RUST_LOG=error
101+
if [[ "${NO_USE_PDG:-}" == "" ]]; then
102+
# cargo runs this from a different pwd, so make path absolute
103+
export PDG_FILE="${pdg}"
104+
fi
105+
106+
cd "${C2RUST_DIR}"
107+
cargo run \
108+
--bin c2rust-analyze \
109+
"${profile_args[@]}" \
110+
-- \
111+
build \
112+
-- \
113+
"${profile_args[@]}" \
114+
--features=c2rust-analysis-rt \
115+
--manifest-path "${test_dir}/Cargo.toml"
116+
)

0 commit comments

Comments
 (0)