Measure coverage using cargo-llvm-cov

This deletes quite a bit of ad-hoc scripting that is
replaced by llvm-cov.
This commit is contained in:
Joseph Birr-Pixton 2022-12-31 12:21:36 +00:00
parent 6da1d8d711
commit 6087246dbf
6 changed files with 29 additions and 143 deletions

View File

@ -183,31 +183,24 @@ jobs:
with:
persist-credentials: false
# Install a version of clang that (more or less) has the same coverage
# instrumentation format.
- name: Install LLVM
run: |
curl https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository -y 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main'
sudo apt-get install -y clang-15
- name: Install rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
default: true
components: llvm-tools
- name: Install cargo-llvm-cov
run: cargo install cargo-llvm-cov
- name: Install golang toolchain
uses: actions/setup-go@v2
with:
go-version: "1.17.1"
- name: Build lcov
run: admin/build-lcov
- name: Measure coverage
run: admin/coverage
run: ./admin/coverage --lcov --output-path final.info
- name: Report to codecov.io
uses: codecov/codecov-action@v1.0.10

View File

@ -1,9 +0,0 @@
#!/bin/sh
set -xe
if [ ! -e lcov/ ] ; then
git clone https://github.com/linux-test-project/lcov.git
cd lcov
git checkout v1.16
sudo make install
fi

View File

@ -1,97 +1,8 @@
#!/usr/bin/env python3
#!/bin/bash -e
import os
import glob
import subprocess
source <(cargo llvm-cov show-env --export-prefix)
cargo llvm-cov clean --workspace
COVERAGE_OPTIONS = '-Ccodegen-units=1 -Clink-dead-code -Zprofile -Cpanic=abort -Zpanic-abort-tests -Cinstrument-coverage'
LLVM_GCOV = os.path.abspath('./admin/llvm-gcov')
LCOVOPTS = ['--gcov-tool', LLVM_GCOV, '--rc' ,'lcov_excl_line=assert']
def check_call(*args, **kwargs):
subprocess.check_call(*args, **kwargs)
def lcov_exe(*args):
return ['lcov'] + LCOVOPTS + list(args)
def sh(cmd):
check_call(cmd, shell = True)
def cleanup():
sh('cargo clean')
sh("rm -rf *.gcda *.gcno")
def run(which):
exe = next(filter(lambda x: '.' not in x, glob.glob('target/debug/deps/' + which + '-*')))
sh('./' + exe)
def rustc(*args):
# `--profile` is unstable, so pass `-Zunstable-options` for now
exe = ['cargo', 'rustc', '--all-features', '-Zunstable-options'] + list(args)
env = dict(os.environ)
env.update(RUSTC_WRAPPER = './admin/coverage-rustc',
COVERAGE_OPTIONS = COVERAGE_OPTIONS,
CARGO_INCREMENTAL = '0')
check_call(exe, env = env)
def lcov(outfile):
exe = lcov_exe('--capture', '--directory', '.', '--base-directory', '.', '-o', outfile)
check_call(exe)
return outfile
def merge(outfile, infiles):
arg = []
for i in infiles:
arg.append('--add')
arg.append(i)
arg.append('-o')
arg.append(outfile)
check_call(lcov_exe(*arg))
def remove(outfile, infile, pattern):
check_call(lcov_exe('--remove', infile, os.getcwd() + pattern, '-o', outfile))
def extract(outfile, infile):
check_call(lcov_exe('--extract', infile, os.getcwd() + '/rustls/src/*', '-o', outfile))
def genhtml(outdir, infile):
check_call(['genhtml', '--branch-coverage', '--demangle-cpp', '--legend',
infile, '-o', outdir, '--ignore-errors', 'source'])
def rustc_llvm_version():
lines = subprocess.check_output(['rustc', '-vV']).decode('ascii').splitlines()
ln = [ln for ln in lines if ln.split(':', 1)[0] == 'LLVM version'][0]
return int(ln.split(':')[1].split('.')[0].strip())
LLVM_VERSION = 15
llvm_version = rustc_llvm_version()
if llvm_version != LLVM_VERSION:
print('expected rustc to use LLVM {}, found {}'.format(LLVM_VERSION, llvm_version))
raise SystemExit(-1)
all_infos = []
# unit tests
cleanup()
rustc('--package', 'rustls', '--profile', 'test', '--lib')
run('rustls')
all_infos.append(lcov('rustls.info'))
cleanup()
for example in 'bench bogo_shim trytls_shim'.split():
rustc('--package', 'rustls', '--profile', 'dev', '--example', example)
# crate-level tests
for test in 'api client_cert_verifier key_log_file_env server_cert_verifier'.split():
rustc('--package', 'rustls', '--profile', 'dev', '--test', test)
run(test)
# trytls/bogo
#sh('cd trytls && ./runme')
sh('cd bogo && env USE_EXISTING_BOGO_SHIM=1 ./runme')
all_infos.append(lcov('tests.info'))
merge('merged.info', all_infos)
remove('coverage.info', 'merged.info', '/rustls/src/msgs/macros.rs')
extract('final.info', 'coverage.info')
genhtml('target/coverage/', 'final.info')
cargo build --all-targets --all-features
cargo test --all-features
cargo llvm-cov report "$@"

View File

@ -1,25 +0,0 @@
#!/bin/bash -e
get_crate_name()
{
while [[ $# -gt 1 ]] ; do
v=$1
case $v in
--crate-name)
echo $2
return
;;
esac
shift
done
}
case $(get_crate_name "$@") in
rustls|tlsclient|tlsserver|features|client_cert_verifier|server_cert_verifier|key_log_file_env|errors|api|badssl|bugs|topsites|bogo_shim|trytls_shim|bench)
EXTRA=$COVERAGE_OPTIONS
;;
*)
;;
esac
exec "$@" $EXTRA

View File

@ -1,2 +0,0 @@
#!/bin/sh -e
llvm-cov-15 gcov $*

18
rustls/tests/bogo.rs Normal file
View File

@ -0,0 +1,18 @@
// Runs the bogo test suite, in the form of a rust test.
// Note that bogo requires a golang environment to build
// and run.
#[test]
#[cfg(all(coverage, feature = "quic", feature = "dangerous_configuration"))]
fn run_bogo_tests() {
use std::process::Command;
let rc = Command::new("./runme")
.current_dir("../bogo")
.spawn()
.expect("cannot run bogo/runme")
.wait()
.expect("cannot wait for bogo");
assert!(rc.success(), "bogo exited non-zero");
}