Skip to content

Commit 8444eff

Browse files
committed
Rust code coverage (kcov)
1 parent 4d3bee1 commit 8444eff

File tree

4 files changed

+94
-22
lines changed

4 files changed

+94
-22
lines changed

.github/workflows/build.yml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ on:
1919
# Keep this in sync with the other *.yml files
2020
env:
2121
CI_REQ_DOTNET_SDK_VER: 5.0.300
22-
COVERAGE_FILENAME: coverage.info
2322
CI_NODE_MIN_VER: 12.0.0
2423
RUSTFLAGS: -D warnings
2524
MACOSX_DEPLOYMENT_TARGET: 10.12
@@ -57,22 +56,23 @@ jobs:
5756
- uses: actions/upload-artifact@v2
5857
with:
5958
name: coverage.info
60-
path: src/csharp/Intel/Iced.UnitTests/${{env.COVERAGE_FILENAME}}
59+
path: src/csharp/Intel/Iced.UnitTests/coverage.info
6160
if-no-files-found: error
6261

6362
- name: Upload coverage report
6463
if: github.ref == 'refs/heads/master'
6564
shell: bash
6665
continue-on-error: true
6766
run: |
67+
# https://docs.codecov.com/docs/codecov-uploader
6868
curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import
6969
curl -Os https://uploader.codecov.io/latest/linux/codecov
7070
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
7171
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
7272
gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
7373
shasum -a 256 -c codecov.SHA256SUM
7474
chmod +x codecov
75-
./codecov -t ${{secrets.CODECOV_TOKEN}} -f "src/csharp/Intel/Iced.UnitTests/${{env.COVERAGE_FILENAME}}"
75+
./codecov -t "${{secrets.CODECOV_TOKEN}}" -f "src/csharp/Intel/Iced.UnitTests/coverage.info"
7676
7777
#############################################################################
7878
#############################################################################
@@ -96,9 +96,36 @@ jobs:
9696
with:
9797
dotnet-version: ${{env.CI_REQ_DOTNET_SDK_VER}}
9898

99+
- name: Install kcov
100+
shell: bash
101+
run: |
102+
sudo apt update
103+
sudo apt install kcov
104+
99105
- name: Build and test
100106
shell: bash
101-
run: ./build/build-rust --no-set-rustflags --no-tests --test-current --test-msrv --test-no_std
107+
run: ./build/build-rust --no-set-rustflags --no-tests --test-current --test-msrv --test-no_std --coverage
108+
109+
- uses: actions/upload-artifact@v2
110+
with:
111+
name: rust-coverage
112+
path: cov-out/rust/merged/
113+
if-no-files-found: error
114+
115+
- name: Upload coverage report
116+
if: github.ref == 'refs/heads/master'
117+
shell: bash
118+
continue-on-error: true
119+
run: |
120+
# https://docs.codecov.com/docs/codecov-uploader
121+
curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import
122+
curl -Os https://uploader.codecov.io/latest/linux/codecov
123+
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
124+
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
125+
gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
126+
shasum -a 256 -c codecov.SHA256SUM
127+
chmod +x codecov
128+
./codecov -t "${{secrets.CODECOV_TOKEN}}" -f "cov-out/rust/merged/kcov-merged/cobertura.xml"
102129
103130
#############################################################################
104131
#############################################################################

.github/workflows/release.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ on:
77
# Keep this in sync with the other *.yml files
88
env:
99
CI_REQ_DOTNET_SDK_VER: 5.0.300
10-
COVERAGE_FILENAME: coverage.info
1110
CI_NODE_MIN_VER: 12.0.0
1211
RUSTFLAGS: -D warnings
1312
MACOSX_DEPLOYMENT_TARGET: 10.12
@@ -48,13 +47,6 @@ jobs:
4847
path: src/csharp/Intel/Iced/bin/Release/*.*nupkg
4948
if-no-files-found: error
5049

51-
- uses: actions/upload-artifact@v2
52-
if: startsWith(matrix.os, 'ubuntu-')
53-
with:
54-
name: coverage.info
55-
path: src/csharp/Intel/Iced.UnitTests/${{env.COVERAGE_FILENAME}}
56-
if-no-files-found: error
57-
5850
#############################################################################
5951
#############################################################################
6052
#############################################################################
@@ -554,10 +546,6 @@ jobs:
554546
- name: Verify downloads
555547
shell: bash
556548
run: |
557-
if [ ! -f "/tmp/artifacts/coverage.info/$COVERAGE_FILENAME" ]; then
558-
echo "Missing coverage"
559-
exit 1
560-
fi
561549
if [ -z "$(ls -A /tmp/artifacts/nupkg/Iced.*.nupkg)" ]; then
562550
echo "Missing nupkg files"
563551
exit 1

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.vscode/
2+
coverage.info
3+
/cov-out/

build/build-rust

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ test_features=y
1717
test_current=y
1818
test_msrv=y
1919
test_code_asm=y
20+
test_cov=n
21+
22+
kcov=kcov
23+
cov_out=$root_dir/cov-out
24+
cov_out_rust=$cov_out/rust
25+
cov_out_rust_tmp=$cov_out_rust/tmp
26+
cov_out_rust_merged=$cov_out_rust/merged
2027

2128
# Minimum supported Rust version
2229
msrv="1.48.0"
@@ -44,22 +51,31 @@ test_valid_invalid_instructions() {
4451

4552
# The C# code needs a formatter so add masm feature
4653
dotnet build -c:$configuration -p:IcedFeatureFlags="DECODER ENCODER OPCODE_INFO INSTR_INFO MASM" "$root_dir/src/csharp/Intel/IcedFuzzer/IcedFuzzer/IcedFuzzer.csproj"
54+
if [ "$test_cov" = "y" ]; then
55+
release_flag=
56+
fzgt_output_dir=debug
57+
else
58+
release_flag="--release"
59+
fzgt_output_dir=release
60+
fi
61+
cargo build --color always $release_flag --manifest-path "$root_dir/src/rust/iced-x86-fzgt/Cargo.toml"
62+
fzgt_exe="$root_dir/src/rust/iced-x86-fzgt/target/$fzgt_output_dir/iced-x86-fzgt"
4763
for bitness in 16 32 64; do
4864
echo "==== ${bitness}-bit: Generating valid/invalid files ===="
4965
dotnet run -c:$configuration --no-build -p "$root_dir/src/csharp/Intel/IcedFuzzer/IcedFuzzer/IcedFuzzer.csproj" -- -$bitness -oil "$invalid_file" -ovlc "$valid_file"
5066
echo "==== ${bitness}-bit: Testing valid instructions ===="
51-
cargo run --color always --release --manifest-path "$root_dir/src/rust/iced-x86-fzgt/Cargo.toml" -- -b $bitness -f "$valid_file"
67+
cov_test "test_valid_$bitness" "$fzgt_exe" -b $bitness -f "$valid_file"
5268
echo "==== ${bitness}-bit: Testing invalid instructions ===="
53-
cargo run --color always --release --manifest-path "$root_dir/src/rust/iced-x86-fzgt/Cargo.toml" -- -b $bitness -f "$invalid_file" --invalid
69+
cov_test "test_invalid_$bitness" "$fzgt_exe" -b $bitness -f "$invalid_file" --invalid
5470
done
5571

5672
for bitness in 16 32 64; do
5773
echo "==== ${bitness}-bit (AMD): Generating valid/invalid files ===="
5874
dotnet run -c:$configuration --no-build -p "$root_dir/src/csharp/Intel/IcedFuzzer/IcedFuzzer/IcedFuzzer.csproj" -- -$bitness -oil "$invalid_file" -ovlc "$valid_file" --amd
5975
echo "==== ${bitness}-bit (AMD): Testing valid instructions ===="
60-
cargo run --color always --release --manifest-path "$root_dir/src/rust/iced-x86-fzgt/Cargo.toml" -- -b $bitness -f "$valid_file" --amd
76+
cov_test "test_amd_valid_$bitness" "$fzgt_exe" -b $bitness -f "$valid_file" --amd
6177
echo "==== ${bitness}-bit (AMD): Testing invalid instructions ===="
62-
cargo run --color always --release --manifest-path "$root_dir/src/rust/iced-x86-fzgt/Cargo.toml" -- -b $bitness -f "$invalid_file" --invalid --amd
78+
cov_test "test_amd_invalid_$bitness" "$fzgt_exe" -b $bitness -f "$invalid_file" --invalid --amd
6379
done
6480

6581
rm "$valid_file"
@@ -164,6 +180,33 @@ build_features() {
164180
cd "$curr_dir"
165181
}
166182

183+
cov_test() {
184+
cov_test_dir=$1
185+
shift
186+
if [ "$test_cov" = "y" ]; then
187+
$kcov --verify --exclude-pattern=/tests/,/test/,/test_utils/ --include-pattern=/iced-x86/ "$cov_out_rust_tmp/$cov_test_dir" "$@"
188+
else
189+
"$@"
190+
fi
191+
}
192+
193+
cargo_test_cov() {
194+
cov_test_dir=$1
195+
shift
196+
if [ "$test_cov" = "y" ]; then
197+
test_exe=$(cargo test --color always --no-run --message-format=json "$@" | grep -- '"name":"iced-x86"' | tail -1 | sed -e 's/.*"executable":"\([^"]\+\)".*/\1/')
198+
if [ ! -x "$test_exe" ]; then
199+
echo "Couldn't get the test executable name, got '$test_exe'"
200+
echo "json output:"
201+
cargo test --color always --no-run --message-format=json "$@"
202+
exit 1
203+
fi
204+
cov_test "$cov_test_dir" "$test_exe"
205+
else
206+
cargo test --color always "$@"
207+
fi
208+
}
209+
167210
build_test_current_version() {
168211
new_func "Build, test (current version)"
169212
curr_dir=$(pwd)
@@ -194,10 +237,10 @@ build_test_current_version() {
194237
# Make sure the two read-mem methods behave the same
195238
# Also test serde code. It needs encoder to also test 'db x,y,z', see serde tests
196239
echo "==== TEST DEBUG: std decoder encoder serde __internal_flip ===="
197-
cargo test --color always --tests --no-default-features --features "std decoder encoder serde __internal_flip"
240+
cargo_test_cov test_internal_flip --tests --no-default-features --features "std decoder encoder serde __internal_flip"
198241

199242
echo "==== TEST DEBUG ===="
200-
cargo test --color always --tests --features "serde $test_code_asm_feat mvex"
243+
cargo_test_cov test_debug --tests --features "serde $test_code_asm_feat mvex"
201244

202245
echo "==== BUILD RELEASE wasm32-unknown-unknown ===="
203246
cargo check --color always --target wasm32-unknown-unknown --release --features "serde code_asm mvex"
@@ -269,6 +312,7 @@ while [ "$#" -gt 0 ]; do
269312
--test-features) test_features=y ;;
270313
--test-current) test_current=y ;;
271314
--test-msrv) test_msrv=y ;;
315+
--coverage) test_cov=y ;;
272316

273317
--no-set-rustflags) set_rustflags=n ;;
274318
*) echo "Unknown arg: $1"; exit 1 ;;
@@ -289,6 +333,13 @@ fi
289333
echo "rustup show"
290334
rustup show
291335

336+
if [ "$test_cov" = "y" ]; then
337+
mkdir -p "$cov_out"
338+
rm -rf "$cov_out_rust" "$cov_out_rust_merged"
339+
mkdir -p "$cov_out_rust_tmp"
340+
kcov --version
341+
fi
342+
292343
if [ "$test_code_asm" = "y" ]; then
293344
test_code_asm_feat="code_asm"
294345
else
@@ -318,3 +369,7 @@ fi
318369
if [ "$test_msrv" = "y" ]; then
319370
build_test_msrv
320371
fi
372+
373+
if [ "$test_cov" = "y" ]; then
374+
$kcov --merge "$cov_out_rust_merged" "$cov_out_rust_tmp/"*
375+
fi

0 commit comments

Comments
 (0)