Skip to content

Commit 9d74012

Browse files
arthurscchanjonathanmetzmanDavidKorczynski
authored
Extend java coverage reports to include non-covered files (#9658)
Jacoco is used for code coverage report generation of JVM projects. It will make use of the dumped class file to generate the report. But only java classes covered by at least one fuzzer will be included in the report, other files in the project will be ignored. This PR aims to point the Jacoco class file discovery folder to the original compiled project jar file in order to force Jacoco to generate coverage report with source for all java class of the project. This came as a need from Fuzz Introspector where we noticed some discrepancies between the static analysis and the code coverage reports. Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> --------- Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> Signed-off-by: David Korczynski <david@adalogics.com> Co-authored-by: jonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com> Co-authored-by: David Korczynski <david@adalogics.com>
1 parent d9270d0 commit 9d74012

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

infra/base-images/base-runner/coverage

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,35 @@ elif [[ $FUZZING_LANGUAGE == "jvm" ]]; then
442442
cp -r $DUMPS_DIR/${fuzz_target}_classes/* $classes_dir/
443443
done
444444

445+
# Dump classes in jar file to dump directory. This action includes all
446+
# the classes of the project that are not executed by the fuzzers to
447+
# the dump directory. Duplicate classes in the dump directory will be
448+
# overwritten. We need to do this to include all possible classes in
449+
# the coverage report and avoid duplicate classes from different jar
450+
# files which jacoco fails to handle.
451+
for jar_file in $(ls $OUT/*.jar)
452+
do
453+
if [[ $jar_file != $OUT/jazzer* ]]
454+
then
455+
tempdir=$(mktemp -d)
456+
cd $tempdir && jar xvf $jar_file && cd -
457+
cp -r $tempdir/* $classes_dir/
458+
rm -r $tempdir
459+
fi
460+
done
461+
462+
# Clean up files that can create duplicate class names which breaks Jacoco.
463+
# Remove META-INF folder because some jar may store duplications of the same
464+
# class file in the META-INF for other java versions.
465+
find $classes_dir -name "META-INF" | xargs rm -rf
466+
# Remove all files that are not a java class because some jar may contain
467+
# Kotlin class which has the same name as a java class and Jacoco fail to
468+
# distinguish them.
469+
find $classes_dir -type f -not -name "*.class" | xargs rm -f
470+
# Remove all class files which have a dot in their file name which are normally
471+
# used to name a duplication of the legitimate class file.
472+
find $classes_dir -type f -name "*.*.class" | xargs rm -f
473+
445474
# Heuristically determine source directories based on Maven structure.
446475
# Always include the $SRC root as it likely contains the fuzzer sources.
447476
sourcefiles_args=(--sourcefiles $OUT/$SRC)

0 commit comments

Comments
 (0)