Skip to content

Instantly share code, notes, and snippets.

@cbeuw
Last active November 10, 2025 21:32
Show Gist options
  • Select an option

  • Save cbeuw/3490fe8c58fad3d094dea483037485c9 to your computer and use it in GitHub Desktop.

Select an option

Save cbeuw/3490fe8c58fad3d094dea483037485c9 to your computer and use it in GitHub Desktop.
measure_coverage.sh from Rustlantis artifacts https://doi.org/10.5281/zenodo.12670660
#!/bin/bash
set -euxo pipefail
HOST=$(rustc -vV | awk '/^host/ { print $2 }')
# Build a Rust compiler with self-coverage instrumentation
pushd /root/rust
if [ ! -f .patched ]; then
git apply - << EOF
diff --git a/.patched b/.patched
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/src/bootstrap/src/core/builder.rs b/src/bootstrap/src/core/builder.rs
index 7f93fdc72ef..59014bab888 100644
--- a/src/bootstrap/src/core/builder.rs
+++ b/src/bootstrap/src/core/builder.rs
@@ -1463,6 +1463,11 @@ fn cargo(
rustflags.arg(sysroot_str);
}
+ if mode != Mode::Std {
+ rustflags.arg("-Cinstrument-coverage");
+ rustflags.arg("-Zcoverage-options=branch");
+ }
+
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
Some(setting) => {
// If an explicit setting is given, use that
EOF
cat << EOF > config.toml
# Includes one of the default files in src/bootstrap/defaults
profile = "compiler"
change-id = 117813
[build]
profiler = true
[llvm]
download-ci-llvm = false
build-config = {"LLVM_BUILD_INSTRUMENTED_COVERAGE" = "On", "LLVM_ENABLE_LLD" = "On"}
link-jobs = 1
targets = "X86"
[rust]
use-lld = true
EOF
fi
./x.py build --stage=1
rustup toolchain link stage1 build/$HOST/stage1
popd
LAST=199
export COVERAGE_DIR=/root/coverage_test
# Build Rustlantis and generate 200 programs
pushd /root/rustlantis
git checkout b2eeef56de9aac62a4f1018114a2803aa635e6c5
cargo build --release
echo "Generating 200 programs with Rustlantis"
mkdir -p $COVERAGE_DIR/rustlantis/programs
seq 0 $LAST | parallel --progress "target/release/generate {} > $COVERAGE_DIR/rustlantis/programs/{}.rs"
popd
# Build RustSmith and generate 200 programs
pushd /root/rustsmith
git checkout dcf049c66f094779ab9d08e9caf6e3c61e2c6cae
./gradlew --no-daemon --include-build /root/kolor build
echo "Generating 200 programs with RustSmith"
SEEDS=$(head -n $((LAST+1)) /root/rustsmith_seeds.txt)
parallel --link --progress "run/rustsmith -n 1 --seed {2} --directory seed{1}" ::: $(seq 0 $LAST) ::: $SEEDS
mkdir -p $COVERAGE_DIR/rustsmith/programs
seq 0 $LAST | xargs -I{} mv seed{}/file0/file0.rs $COVERAGE_DIR/rustsmith/programs/{}.rs
popd
function profile {
FUZZER=$1
PROGRAM=$2
LLVM_COV=llvm-cov-18
LLVM_PROFDATA=llvm-profdata-18
export LLVM_PROFILE_FILE="$COVERAGE_DIR/$FUZZER/profraw/$PROGRAM.profraw"
# RustSmith sometimes generate programs that do not compile, ignore the error
rustc +stage1 -Zmir-opt-level=4 -Copt-level=3 $COVERAGE_DIR/$FUZZER/programs/$PROGRAM.rs -o /dev/null 2> /dev/null || true
}
export -f profile
function process {
FUZZER=$1
PROFRAW_DIR="$COVERAGE_DIR/$FUZZER/profraw"
MERGED_DIR="$COVERAGE_DIR/$FUZZER/merged"
LCOV_DIR="$COVERAGE_DIR/$FUZZER/lcov"
HTML_DIR="$COVERAGE_DIR/$FUZZER/html"
JSON_DIR="$COVERAGE_DIR/$FUZZER/json"
mkdir -p $MERGED_DIR
mkdir -p $LCOV_DIR
mkdir -p $HTML_DIR
mkdir -p $JSON_DIR
llvm-profdata-18 merge --sparse $PROFRAW_DIR/0.profraw --output $MERGED_DIR/0.profdata
llvm-cov-18 export -format=lcov -instr-profile $MERGED_DIR/0.profdata /root/rust/build/$HOST/stage1/lib/librustc_driver-fded7f3034badcd8.so > $LCOV_DIR/0.info
lcov-viewer lcov -o $HTML_DIR/0 $LCOV_DIR/0.info
cat <(echo "var window={};") $HTML_DIR/0/report-data.js /root/summarize.js | node > $JSON_DIR/0.json
for i in $(seq 5 5 $LAST); do
llvm-profdata-18 merge -sparse $MERGED_DIR/$((i-5)).profdata $PROFRAW_DIR/$((i-4)).profraw $PROFRAW_DIR/$((i-3)).profraw $PROFRAW_DIR/$((i-2)).profraw $PROFRAW_DIR/$((i-1)).profraw $PROFRAW_DIR/$i.profraw \
--output $MERGED_DIR/$i.profdata
llvm-cov-18 export -format=lcov -instr-profile $MERGED_DIR/$i.profdata /root/rust/build/$HOST/stage1/lib/librustc_driver-fded7f3034badcd8.so > $LCOV_DIR/$i.info
lcov-viewer lcov -o $HTML_DIR/$i $LCOV_DIR/$i.info
cat <(echo "var window={};") $HTML_DIR/$i/report-data.js /root/summarize.js | node > $JSON_DIR/$i.json
done
}
export SHELL=$(type -p bash)
echo "Collecting Rustlantis coverage data"
seq 0 $LAST | parallel --memfree 2G --progress profile rustlantis
process rustlantis
echo "Collecting RustSmith coverage data"
seq 0 $LAST | parallel --memfree 2G --progress profile rustsmith
process rustsmith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment