Skip to content

Commit a95df6a

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

File tree

2 files changed

+130
-77
lines changed

2 files changed

+130
-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

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
# Variables that are computed when the script is sourced
19+
SCRIPT_PATH="$(realpath ${BASH_SOURCE[0]})"
20+
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"
21+
22+
# Variables that are exported to the functions below
23+
export C2RUST_DIR="$(dirname "${SCRIPT_DIR}")"
24+
25+
_c2rust_prepare_vars() {
26+
test_dir="$(realpath ${1})"
27+
args=("${@:2}")
28+
29+
profile_dir_name="${RUST_PROFILE:-release}"
30+
31+
profile_dir="target/${profile_dir_name}"
32+
profile="${profile_dir_name}"
33+
if [[ "${profile}" == "debug" ]]; then
34+
profile=dev
35+
fi
36+
profile_args=(--profile "${profile}")
37+
38+
metadata="${test_dir}/metadata.bc"
39+
pdg="${test_dir}/pdg.bc"
40+
event_log="${test_dir}/log.bc"
41+
runtime="${C2RUST_DIR}/analysis/runtime"
42+
}
43+
44+
c2rust-set-instrument-vars() {
45+
_c2rust_prepare_vars "${@}"
46+
47+
# We need these variables to have exactly these values
48+
export INSTRUMENT_RUNTIME=bg
49+
export INSTRUMENT_BACKEND=log
50+
export INSTRUMENT_OUTPUT="${event_log}"
51+
export METADATA_FILE="${metadata}"
52+
53+
# These variables can be overridden by the user
54+
export INSTRUMENT_OUTPUT_APPEND=${INSTRUMENT_OUTPUT_APPEND:-false}
55+
}
56+
57+
c2rust-instrument() (
58+
_c2rust_prepare_vars "${@}"
59+
60+
unset RUSTFLAGS # transpiled code has tons of warnings; don't allow `-D warnings`
61+
export RUST_BACKTRACE=full
62+
63+
cd "${C2RUST_DIR}"
64+
cargo run \
65+
--bin c2rust-instrument \
66+
"${profile_args[@]}" \
67+
-- \
68+
--metadata "${metadata}" \
69+
--set-runtime \
70+
--runtime-path "${runtime}" \
71+
-- "${CARGO_INSTRUMENT_COMMAND:-build}" \
72+
--manifest-path "${test_dir}/Cargo.toml" \
73+
"${profile_args[@]}" \
74+
-- "${args[@]}"
75+
)
76+
77+
c2rust-pdg() (
78+
_c2rust_prepare_vars "${@}"
79+
80+
export RUST_BACKTRACE=full # print sources w/ color-eyre
81+
export RUST_LOG=error
82+
83+
cd "${C2RUST_DIR}"
84+
cargo run \
85+
--bin c2rust-pdg \
86+
"${profile_args[@]}" \
87+
-- \
88+
--event-log "${event_log}" \
89+
--metadata "${metadata}" \
90+
--print graphs \
91+
--print write-permissions \
92+
--print counts \
93+
--output "${pdg}" \
94+
> "${test_dir}/pdg.log"
95+
)
96+
97+
c2rust-analyze-with-pdg() (
98+
_c2rust_prepare_vars "${@}"
99+
100+
export RUST_BACKTRACE=full # print sources w/ color-eyre
101+
export RUST_LOG=error
102+
if [[ "${NO_USE_PDG:-}" == "" ]]; then
103+
# cargo runs this from a different pwd, so make path absolute
104+
export PDG_FILE="${pdg}"
105+
fi
106+
107+
cd "${C2RUST_DIR}"
108+
cargo run \
109+
--bin c2rust-analyze \
110+
"${profile_args[@]}" \
111+
-- \
112+
build \
113+
-- \
114+
"${profile_args[@]}" \
115+
--features=c2rust-analysis-rt \
116+
--manifest-path "${test_dir}/Cargo.toml"
117+
)

0 commit comments

Comments
 (0)