Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make scripts/pdg.sh more flexible for more use cases #1113

Open
wants to merge 1 commit into
base: ahomescu/non_null
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 13 additions & 77 deletions scripts/pdg.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

set -euox pipefail

CWD="${PWD}"
SCRIPT_PATH="${0}"
SCRIPT_DIR="${CWD}/$(dirname "${SCRIPT_PATH}")"
SCRIPT_PATH="$(realpath ${0})"
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"
C2RUST_DIR="$(dirname "${SCRIPT_DIR}")"

source ${SCRIPT_DIR}/pdg_setup.sh

# Usage: `./pdg.sh <test crate dir> <test binary args...>`
#
# 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
# Environment Variables: see `scripts/pdg_functions.sh` for a full list.
#
# Instrument and run a test crate, create its PDG, and then run analysis on it.
#
Expand All @@ -27,80 +25,18 @@ SCRIPT_DIR="${CWD}/$(dirname "${SCRIPT_PATH}")"
# 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}")

local profile_dir_name="${PROFILE:-release}"

local profile_dir="target/${profile_dir_name}"
local profile="${profile_dir_name}"
if [[ "${profile}" == "debug" ]]; then
profile=dev
fi
local profile_args=(--profile "${profile}")

local metadata="${test_dir}/metadata.bc"
local pdg="${test_dir}/pdg.bc"
local event_log="${test_dir}/log.bc"
local runtime="analysis/runtime"
main() {
CARGO_INSTRUMENT_COMMAND=run

# set the environment variables for instrumentation
c2rust-set-instrument-vars "${@}"
# build and run a test with instrumentation
(
unset RUSTFLAGS # transpiled code has tons of warnings; don't allow `-D warnings`
export RUST_BACKTRACE=1
export INSTRUMENT_RUNTIME=bg
export INSTRUMENT_BACKEND=log
export INSTRUMENT_OUTPUT="${event_log}"
export INSTRUMENT_OUTPUT_APPEND=false
export METADATA_FILE="${metadata}"

cargo run \
--bin c2rust-instrument \
"${profile_args[@]}" \
-- \
--metadata "${metadata}" \
--set-runtime \
--runtime-path "${runtime}" \
-- run \
--manifest-path "${test_dir}/Cargo.toml" \
"${profile_args[@]}" \
-- "${args[@]}"
)
c2rust-instrument "${@}"
# construct pdg from log events
(
export RUST_BACKTRACE=full # print sources w/ color-eyre
export RUST_LOG=error
cargo run \
--bin c2rust-pdg \
"${profile_args[@]}" \
-- \
--event-log "${event_log}" \
--metadata "${metadata}" \
--print graphs \
--print write-permissions \
--print counts \
--output "${pdg}" \
> "${test_dir}/pdg.log"
)
c2rust-pdg "${@}"
# 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"
)
c2rust-analyze-with-pdg "${@}"
}

main "${@}"
117 changes: 117 additions & 0 deletions scripts/pdg_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env bash
#
# This script can be source'd to provide a few useful
# shell functions for analysis using the PDG.
#
# Environment Variables:
# * `RUST_PROFILE` (default `release`):
# a `cargo` profile as in `target/$RUST_PROFILE`
# * `NO_USE_PDG` (default empty):
# if non-empty, do not use the PDG as a starting point for analysis
# * `CARGO_INSTRUMENT_COMMAND` (default `build`):
# the `cargo` command to run for instrumentation, e.g., `run` or `build`
# * `INSTRUMENT_OUTPUT_APPEND` (default `false`):
# set to `true` to append instrumentation events to the log instead
# of replacing the existing log on every invocation of the instrumented
# binary

# Variables that are computed when the script is sourced
SCRIPT_PATH="$(realpath ${BASH_SOURCE[0]})"
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"

# Variables that are exported to the functions below
export C2RUST_DIR="$(dirname "${SCRIPT_DIR}")"

_c2rust_prepare_vars() {
test_dir="$(realpath ${1})"
args=("${@:2}")

profile_dir_name="${RUST_PROFILE:-release}"

profile_dir="target/${profile_dir_name}"
profile="${profile_dir_name}"
if [[ "${profile}" == "debug" ]]; then
profile=dev
fi
profile_args=(--profile "${profile}")

metadata="${test_dir}/metadata.bc"
pdg="${test_dir}/pdg.bc"
event_log="${test_dir}/log.bc"
runtime="${C2RUST_DIR}/analysis/runtime"
}

c2rust-set-instrument-vars() {
_c2rust_prepare_vars "${@}"

# We need these variables to have exactly these values
export INSTRUMENT_RUNTIME=bg
export INSTRUMENT_BACKEND=log
export INSTRUMENT_OUTPUT="${event_log}"
export METADATA_FILE="${metadata}"

# These variables can be overridden by the user
export INSTRUMENT_OUTPUT_APPEND=${INSTRUMENT_OUTPUT_APPEND:-false}
}

c2rust-instrument() (
_c2rust_prepare_vars "${@}"

unset RUSTFLAGS # transpiled code has tons of warnings; don't allow `-D warnings`
export RUST_BACKTRACE=full

cd "${C2RUST_DIR}"
cargo run \
--bin c2rust-instrument \
"${profile_args[@]}" \
-- \
--metadata "${metadata}" \
--set-runtime \
--runtime-path "${runtime}" \
-- "${CARGO_INSTRUMENT_COMMAND:-build}" \
--manifest-path "${test_dir}/Cargo.toml" \
"${profile_args[@]}" \
-- "${args[@]}"
)

c2rust-pdg() (
_c2rust_prepare_vars "${@}"

export RUST_BACKTRACE=full # print sources w/ color-eyre
export RUST_LOG=error

cd "${C2RUST_DIR}"
cargo run \
--bin c2rust-pdg \
"${profile_args[@]}" \
-- \
--event-log "${event_log}" \
--metadata "${metadata}" \
--print graphs \
--print write-permissions \
--print counts \
--output "${pdg}" \
> "${test_dir}/pdg.log"
)

c2rust-analyze-with-pdg() (
_c2rust_prepare_vars "${@}"

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="${pdg}"
fi

cd "${C2RUST_DIR}"
cargo run \
--bin c2rust-analyze \
"${profile_args[@]}" \
-- \
build \
-- \
"${profile_args[@]}" \
--features=c2rust-analysis-rt \
--manifest-path "${test_dir}/Cargo.toml"
)
Loading