Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: Fix jvm coverage report #12694

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 14 additions & 33 deletions infra/base-images/base-runner/coverage
Original file line number Diff line number Diff line change
Expand Up @@ -434,46 +434,27 @@ elif [[ $FUZZING_LANGUAGE == "jvm" ]]; then
java -jar /opt/jacoco-cli.jar merge $DUMPS_DIR/*.exec \
--destfile $jacoco_merged_exec

# Merge .class files from the individual targets.
# Prepare classes directory for jacoco process
classes_dir=$DUMPS_DIR/classes
mkdir $classes_dir
for fuzz_target in $FUZZ_TARGETS; do
# Continue if not a fuzz target.
if [[ $FUZZING_ENGINE != "none" ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete if [[ $FUZZING_ENGINE != "none" ]]; then? We don't need this logic still?

grep "LLVMFuzzerTestOneInput" $fuzz_target > /dev/null 2>&1 || continue

# Only copy class files found in $OUT/$SRC to ensure they are
# lively compiled from the project, avoiding inclusion of
# dependency classes. This also includes the fuzzer classes.
find "$OUT/$SRC" -type f -name "*.class" | while read -r class_file; do
# Skip module-info.class
if [[ "$(basename "$class_file")" == "module-info.class" ]]; then
continue
fi
cp -r $DUMPS_DIR/${fuzz_target}_classes/* $classes_dir/
done

# Dump classes in jar file to dump directory. This action includes all
# the classes of the project that are not executed by the fuzzers to
# the dump directory. Duplicate classes in the dump directory will be
# overwritten. We need to do this to include all possible classes in
# the coverage report and avoid duplicate classes from different jar
# files which jacoco fails to handle.
for jar_file in $(ls $OUT/*.jar)
do
if [[ $jar_file != $OUT/jazzer* ]]
then
tempdir=$(mktemp -d)
cd $tempdir && jar xvf $jar_file && cd -
cp -r $tempdir/* $classes_dir/
rm -r $tempdir
# Use javap to extract the fully qualified name of the class and copy it to $classes_dir
fqn=$(javap -verbose "$class_file" 2>/dev/null | grep "this_class:" | grep -oP '(?<=// ).*')
if [ -n "$fqn" ]; then
mkdir -p $classes_dir/$(dirname $fqn)
cp $class_file $classes_dir/$fqn.class
fi
done

# Clean up files that can create duplicate class names which breaks Jacoco.
# Remove META-INF folder because some jar may store duplications of the same
# class file in the META-INF for other java versions.
find $classes_dir -name "META-INF" | xargs rm -rf
# Remove all files that are not a java class because some jar may contain
# Kotlin class which has the same name as a java class and Jacoco fail to
# distinguish them.
find $classes_dir -type f -not -name "*.class" | xargs rm -f
# Remove all class files which have a dot in their file name which are normally
# used to name a duplication of the legitimate class file.
find $classes_dir/*/ -type f -name "*.*.class" | xargs rm -f

# Heuristically determine source directories based on Maven structure.
# Always include the $SRC root as it likely contains the fuzzer sources.
sourcefiles_args=(--sourcefiles $OUT/$SRC)
Expand Down
Loading