Skip to content

Commit

Permalink
analyze: unify similar metrics scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed Apr 29, 2024
1 parent 08fb475 commit 833e5f6
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 169 deletions.
21 changes: 19 additions & 2 deletions c2rust-analyze/scripts/pointwise_try_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ set -euo pipefail
echo

f=$1
shift 1
mode=$2
shift 2
flags=( "$@" )
echo "f=$f"
echo "mode=$mode"

name=${f%%.*.rs}
name=${name##**/}
Expand All @@ -21,7 +23,22 @@ filter_errors() {
{ grep -v -e '^aborting due to ' -e '^call to unsafe function is unsafe ' || true; }
}

sed -i -e "/fn $func\\>/s/\\<unsafe //" $f
case "$mode" in
pointwise)
sed -i -e "/fn $func\\>/s/\\<unsafe //" $f
;;
unmodified)
d="$(dirname "$f")"
f="$d/${name}_safe_${func}.rs"
cp "$d/$name.rs" "$f"
sed -i -e "/fn $func\\>/s/\\<unsafe //" $f
;;
*)
echo "unsupported mode $mode" 1>&2
exit 1
;;
esac

rustc --error-format json --emit metadata --crate-name $name "$f" "${flags[@]}" 2>rustc-$func.json || true
num_lines="$(cat rustc-$func.json | filter_errors | wc -l)"
echo "got $num_lines errors for $func"
Expand Down
36 changes: 0 additions & 36 deletions c2rust-analyze/scripts/pointwise_try_build_unmodified.sh

This file was deleted.

99 changes: 99 additions & 0 deletions c2rust-analyze/scripts/run_pointwise_metrics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash
set -euo pipefail

# Run pointwise metrics on lighttpd_rust_amalgamated.

if [[ $# -ne 1 ]]; then
echo "Usage: $0 <path/to/lighttpd_rust_amalgamated/>"
exit 1
fi

SCRIPT_DIR="$(dirname "$0")"

# Get the path to lighttpd_rust_amalgamated
MODULE_DIR="$1"
shift 1

# Find the sysroot directory of rustc
SYSROOT="$(rustc --print sysroot)"

# Find the necessary rlibs
extern() {
local name=$1
local rlib=$(find "$MODULE_DIR/target/debug/deps" -name "lib${name}*.rlib" -print -quit)
echo >&2 "found rlib for $name: $rlib"
echo --extern $name=$rlib
}

now=$(date +%Y%m%d-%H%M%S)


# Set $rustc_flags and run the analysis as appropriate for the target project.
# $rustc_flags is also used below for `pointwise_try_build.sh`.
project="$(basename "$MODULE_DIR")"
case "$project" in
lighttpd_*)
rustc_flags=(
--edition 2021
--crate-type rlib
#--sysroot "$SYSROOT"
-L "dependency=$MODULE_DIR/target/debug/deps"
$(extern c2rust_bitfields)
$(extern libc)
-A warnings
)

C2RUST_ANALYZE_NO_CARGO=1 \
C2RUST_ANALYZE_REWRITE_MODE=pointwise \
C2RUST_ANALYZE_USE_MANUAL_SHIMS=1 \
cargo run --bin c2rust-analyze --release -- "$MODULE_DIR/src/main.rs" \
--crate-name "$(basename "$MODULE_DIR")" \
"${rustc_flags[@]}" \
|& tee pointwise-lighttpd-analyze-$now.log \
|| true

;;

cfs_*)
: cargo run --bin c2rust-analyze --release -- \
--rewrite-mode pointwise --use-manual-shims -- \
build --manifest-path "$MODULE_DIR/Cargo.toml" \
|& tee pointwise-cfs-analyze-$now.log \
|| true

rustc_flags=(
--edition 2021
--crate-type rlib
#--sysroot "$SYSROOT"
-L "dependency=$MODULE_DIR/target/debug/deps"
$(extern c2rust_bitfields)
$(extern f128)
$(extern libc)
$(extern memoffset)
-A warnings
)

;;

*)
echo "unsupported project $project" 1>&2
exit 1
esac


# Try to compile each function separately.

pointwise_log_file=pointwise-lighttpd-pointwise-$now.log
for f in "$MODULE_DIR"/src/main.*.rs; do
"$SCRIPT_DIR/pointwise_try_build.sh" "$f" pointwise "${rustc_flags[@]}" || true
done |& tee "$pointwise_log_file"

unmodified_log_file=pointwise-lighttpd-unmodified-$now.log
for f in "$MODULE_DIR"/src/main.*.rs; do
"$SCRIPT_DIR/pointwise_try_build.sh" "$f" unmodified "${rustc_flags[@]}" || true
done |& tee "$unmodified_log_file"

echo
echo

python3 "$SCRIPT_DIR/pointwise_metrics.py" "$pointwise_log_file" "$unmodified_log_file"
65 changes: 0 additions & 65 deletions c2rust-analyze/scripts/run_pointwise_metrics_cfs.sh

This file was deleted.

66 changes: 0 additions & 66 deletions c2rust-analyze/scripts/run_pointwise_metrics_lighttpd.sh

This file was deleted.

0 comments on commit 833e5f6

Please sign in to comment.