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