Skip to content

apply patches needed for fuzz introspector integration#7122

Merged
oliverchang merged 9 commits intomasterfrom
patch_form_FI
Jan 20, 2022
Merged

apply patches needed for fuzz introspector integration#7122
oliverchang merged 9 commits intomasterfrom
patch_form_FI

Conversation

@Navidem
Copy link
Copy Markdown
Contributor

@Navidem Navidem commented Jan 11, 2022

Imitating the changes from introspector diff along with the needed modifications.

Copy link
Copy Markdown
Collaborator

@DavidKorczynski DavidKorczynski left a comment

Choose a reason for hiding this comment

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

Two quick comments

Copy link
Copy Markdown
Collaborator

@oliverchang oliverchang left a comment

Choose a reason for hiding this comment

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

nice, this is a great start!

I left some suggestions to remove duplication and make things a bit cleaner.

mv /usr/bin/ar /usr/bin/old-ar
mv /usr/bin/ranlib /usr/bin/old-ranlib

ln -s /usr/local/bin/llvm-ar /usr/bin/ar
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's be more explicit here and store the patched introspector binaries in /usr/local/bin/introspector/llvm-ar etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I misunderstood what this was doing. This is just replacing the system versions of ar/ranlib with the built llvm versions (which happens to be the introspector patched one in this case).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmmm...Also, is this script idempotent. Might be a good idea to make it so to help with debugging.

infra/helper.py Outdated
'FUZZING_ENGINE=' + engine,
'SANITIZER=' + sanitizer,
'ARCHITECTURE=' + architecture,
'GITHUB_REPO=', #TODO: fix this to be loaded through main_repo property in project.yaml
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: space after '#', and use 'TODO(Navidem)'.

i.e.

# TODO(Navidem): Fix this to be ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

echo "Using LLVM revision: $LLVM_REVISION"

### For fuzz introspector
echo "Applying introspector changes"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

To avoid lots of code duplication, we can edit this file in base-clang instead.

We can just guard this with:

if [ -n "$INTROSPECTOR_PATCHES ]; then
 ...
fi

Then we can get rid of the base-clang-introspector dir completely ,and modify base-clangs Dockerfile to do something like this using a runtime build arg.

ARG introspector
ENV INTROSPECTOR_PATCHES=$instrospector

So that we can use the same Dockerfile to build both vanilla clang and patched clang:

docker build -t gcr.io/oss-fuzz-base/base-clang infra/base-images/base-clang  # vanilla
docker build --build-arg introspector=1 -t gcr.io/oss-fuzz-base/base-clang:introspector infra/base-images/base-clang   # introspector

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

$shared_libraries $LLVM_COV_COMMON_ARGS > $FUZZER_STATS_DIR/$target.json

# For introspector
llvm-cov show -instr-profile=$profdata_file -object=$target -line-coverage-gt=0 $shared_libraries $LLVM_COV_COMMON_ARGS > ${FUZZER_STATS_DIR}/$target.covreport
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does this add a significant amount of extra processing time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not significantly in the few cases that I tried.

@DavidKorczynski
Copy link
Copy Markdown
Collaborator

DavidKorczynski commented Jan 12, 2022

Is it worth considering whether we should simply include this in base-clang rather than creating a whole new image base-clang-introspector? Point being the only patching we do of clang is insert a call to our pass in the LTO pipeline, which is a very non-intrusive patching and I would not expect breakages to happen. Notice there were no fuzz-introspector changes required going from LLVM 12 to LLVM 14 - the delay happened because I did not know about the -flegacy-pass-manager flag.

If using base-clang we can use environment variables to control whether the fuzz-introspector pass should run or not, and also which ar to use etc. I think this might be conceptually quite similar to how afl++ is used. This might be easier to maintain.

EDIT: these questions have been discussed here and the conclusion is keeping base-clang-introspector

mv /usr/bin/ar /usr/bin/old-ar
mv /usr/bin/ranlib /usr/bin/old-ranlib

ln -s /usr/local/bin/llvm-ar /usr/bin/ar
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

fi

if [ "$SANITIZER" = "introspector" ]; then
echo "We are in the introspector instrumentor"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we echo every command, so this isn't necessary to know we are using introspector.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

if [ "$SANITIZER" = "introspector" ]; then
unset CXXFLAGS
unset CFLAGS
apt-get install -y libjpeg-dev zlib1g-dev
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FYI installing packages like this can mess up compilation of some projects I think

apt-get install -y libjpeg-dev zlib1g-dev
pip3 install --upgrade setuptools
pip3 install cxxfilt pyyaml beautifulsoup4 lxml soupsieve matplotlib
mkdir $SRC/inspector-tmp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Just call it inspector, not inspector-tmp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

llvm-cov export -summary-only -instr-profile=$profdata_file -object=$target \
$shared_libraries $LLVM_COV_COMMON_ARGS > $FUZZER_STATS_DIR/$target.json

# For introspector
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: end this with a period.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.


find $SRC/ -name "*.data" -exec cp {} $SRC/inspector-tmp/ \;
find $SRC/ -name "*.data.yaml" -exec cp {} $SRC/inspector-tmp/ \;
# Move coverage report
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

end with peirod.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -0,0 +1,51 @@
# Copyright 2016 Google Inc.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

wrong year

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

got rid of the dir.

Copy link
Copy Markdown
Collaborator

@oliverchang oliverchang left a comment

Choose a reason for hiding this comment

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

nice!!!

@oliverchang
Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Collaborator

@oliverchang oliverchang left a comment

Choose a reason for hiding this comment

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

LGTM with one last question!

apt-get update && apt-get install -y $LLVM_DEP_PACKAGES --no-install-recommends

if [ -n "$INTROSPECTOR_PATCHES" ]; then
apt-get install -y texinfo bison flex
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we delete these after the install?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

will look into it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

looks removing does not break it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you set this similarly to LLVM_DEP_PACKAGES, so these are defined in one place.

i.e.

INTROSPECTOR_DEP_PACKAGES="texinfo bison flex"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

infra/helper.py Outdated
'FUZZING_ENGINE=' + engine,
'SANITIZER=' + sanitizer,
'ARCHITECTURE=' + architecture,
'GITHUB_REPO=', # TODO(navidem): fix this to be loaded through main_repo property in project.yaml.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be called "GITHUB_REPO" instead of "GIT_REPO" ?
Is it really specific to github?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed it.

mv /usr/bin/ar /usr/bin/old-ar
mv /usr/bin/ranlib /usr/bin/old-ranlib

ln -s /usr/local/bin/llvm-ar /usr/bin/ar
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmmm...Also, is this script idempotent. Might be a good idea to make it so to help with debugging.

@Navidem
Copy link
Copy Markdown
Contributor Author

Navidem commented Jan 20, 2022

@oliverchang This should be ready for merge.

@oliverchang oliverchang merged commit 9e39d35 into master Jan 20, 2022
@oliverchang oliverchang deleted the patch_form_FI branch January 20, 2022 01:22
COPY precompile_afl /usr/local/bin/
RUN precompile_afl

RUN git clone https://github.com/ossf/fuzz-introspector.git fuzz-introspector && \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need to clone this again if it's already been cloned in base-clang?
I think putting this in /src is causing breakages.
Although currently we (oss-fuzz's maintainers) use /src, we probably shouldn't.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed in #7199

export AR=llvm-ar
export RANLIB=llvm-ranlib

# Move ar and ranlib
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this comment should have a period.
More importantly, I think it isn't a useful comment and I would change. It's clear that ar and ranlib are being moved, it's not clear why, a better comment would explain this.


FROM gcr.io/oss-fuzz-base/base-image

ARG introspector
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if putting this so high up impacts caching. My first guess is that it won't but I think it's worth testing.

# Install newer cmake.
ENV CMAKE_VERSION 3.21.1
RUN apt-get update && apt-get install -y wget sudo && \
RUN apt-get update && apt-get install -y wget sudo git && \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't really love the placement of this install. This line is meant to install cmake and deletes the packages used to build it., I think instead, you should install git on line 34 and then uninstall it in the same step.
I think we should uninstall it there because it will just add bloat to the image size since in checkout_build_install_llvm.sh we install git and then uninstall it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed in #7199

echo "Using LLVM revision: $LLVM_REVISION"

if [ -n "$INTROSPECTOR_PATCHES" ]; then
# For fuzz introspector.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe this comment would be improved by explaining what this section does for introspector, since that part is hard to understand.

if [ -n "$INTROSPECTOR_PATCHES" ]; then
# For fuzz introspector.
echo "Applying introspector changes"
BBBASE=$PWD
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe call this variable OLD_WORKING_DIR instead of BBBASE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed in #7199

@oliverchang
Copy link
Copy Markdown
Collaborator

Thanks @jonathanmetzman ! @Navidem please address these comments in another PR.

MartinPetkov pushed a commit to MartinPetkov/oss-fuzz that referenced this pull request Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants