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

POC: Speed up compilation by freezing container during compilation. #11940

Merged
merged 11 commits into from
Jun 18, 2024

Conversation

jonathanmetzman
Copy link
Contributor

@jonathanmetzman jonathanmetzman commented May 10, 2024

Instead of rebuilding the entire project every time we want to compile a single fuzz target, a better workflow is to build the project once and somehow compile the target against the already compiled project code.
This POC does that by interrupting building in when it detects it is compiling the fuzz target.
On detection it does the following:

  1. Writes the command to /out/statefile TODO: write the cwd.
  2. Commits the current container as "frozen" for use later. TODO: make this changeable.
  3. Returns 1 so compilation stops. TODO: It would be better to exit the container.
    This step may be important to prevent clean up of the environment.

Then the frozen container can be used to compile fuzz targets against the project without recompiling the project in its entirety.

TODO:

  1. Support this in oss-fuzz-gen
  2. Install docker command line tool in base-builder (or use sneaky inheritance) because it must be used within the container.
  3. Automate the compilation of the new fuzz target

@jonathanmetzman
Copy link
Contributor Author

jonathanmetzman commented May 31, 2024

Command to test:

export PROJECT=libpng; python3 infra/helper.py build_image $PROJECT --no-pull && sudo rm -f build/out/$PROJECT/statefile.json  && docker run --rm --privileged --shm-size=2g --platform linux/amd64 -i -e FUZZING_ENGINE=libfuzzer -e SANITIZER=address -e ARCHITECTURE=x86_64 -e PROJECT_NAME=$PROJECT -e HELPER=True -e FUZZING_LANGUAGE=c++ -e CC="/usr/local/bin/clang-jcc" -e CXX="/usr/local/bin/clang++-jcc" -v $HOME/oss-fuzz/build/out/$PROJECT/:/out -v $HOME/oss-fuzz/build/work/$PROJECT:/work -v /var/run/docker.sock:/var/run/docker.sock -it gcr.io/oss-fuzz/$PROJECT "bash" "-c" "cp /work/clang*-jcc /usr/local/bin/ && compile"

infra/base-images/base-builder/Dockerfile Outdated Show resolved Hide resolved
infra/base-images/base-builder/Dockerfile Outdated Show resolved Hide resolved
infra/base-images/base-builder/jcc/jcc2.go Outdated Show resolved Hide resolved
infra/base-images/base-builder/jcc/jcc2.go Outdated Show resolved Hide resolved
infra/base-images/base-builder/jcc/jcc2.go Outdated Show resolved Hide resolved
infra/base-images/base-builder/jcc/jcc2.go Outdated Show resolved Hide resolved
infra/base-images/base-builder/jcc/jcc2.go Outdated Show resolved Hide resolved
infra/base-images/base-builder/jcc/jcc2.go Outdated Show resolved Hide resolved
infra/base-images/base-builder/jcc/jcc2.go Outdated Show resolved Hide resolved

func WriteTargetArgsAndCommitImage(cmdline []string) {
fmt.Println("WRITE COMMAND")
f, _ := os.OpenFile("/out/statefile.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC, os.O_APPEND caused an invalid JSON format during our hackathon, is it the same case here?

Instead of rebuilding the entire project every time we want to compile
a single fuzz target, a better workflow is to build the project once
and somehow compile the target against the already compiled project
code.
This POC does that by interrupting building in when it detects
it is compiling the fuzz target.
On detection it does the following:
1. Writes the command to /out/statefile
TODO: write the cwd.
2. Commits the current container as "frozen" for use later.
TODO: make this changeable.
3. Returns 1 so compilation stops.
TODO: It would be better to exit the container.
This step may be important to prevent clean up of the environment.

Then the frozen container can be used to compile fuzz targets
against the project without recompiling the project in its
entirety.

TODO:
1. Support this in oss-fuzz-gen
2. Install docker command line tool in base-builder (or use sneaky
inheritance) because it must be used within the container.
@jonathanmetzman jonathanmetzman merged commit deef8c5 into master Jun 18, 2024
18 of 19 checks passed
@jonathanmetzman jonathanmetzman deleted the jcc2.0 branch June 18, 2024 01:06
manunio added a commit to manunio/oss-fuzz that referenced this pull request Jun 22, 2024
manunio added a commit to manunio/oss-fuzz that referenced this pull request Jun 24, 2024
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.

2 participants