-
Notifications
You must be signed in to change notification settings - Fork 2.8k
chronos: Pause compile just before compiling the fuzz target so that we can reuse it later.
#11937
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
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e62bd0b
A PoC
DonggeLiu 4933e40
Some auxiliaries
DonggeLiu 198edfc
Save env var
DonggeLiu 31f727a
Cleanup script
DonggeLiu bcdf1a6
A dedicated directory
DonggeLiu 13a6702
Revert testing changes on libiec61850
DonggeLiu 0dafb68
A Guide
DonggeLiu 3dcab50
fix lint: add headers
DonggeLiu 009e152
Improve documentation
DonggeLiu 0a8f45e
Fix header
DonggeLiu c276e0c
Minor word change
DonggeLiu 432e69a
Add a file description
DonggeLiu deabbe7
Add doc about supporting alternative fuzzer and sanitizer
DonggeLiu 2821e62
sed build.sh in the container as some project's build.sh is not in
DonggeLiu 0e3755a
Remove original binaries in /out
DonggeLiu 89fc50d
Minor fix
DonggeLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Usage | ||
| Under `OSS-Fuzz` root directory: | ||
| ```bash | ||
| export PROJECT=libiec61850 | ||
| export FUZZ_TARGET=fuzz_mms_decode.c | ||
| export FUZZING_LANGUAGE=c | ||
| infra/experimental/chronos/prepare-recompile "$PROJECT" "$FUZZ_TARGET" "$FUZZING_LANGUAGE" | ||
| python infra/helper.py build_image "$PROJECT" | ||
| docker run -ti --entrypoint="/bin/sh" --name "${PROJECT}-origin" "gcr.io/oss-fuzz/${PROJECT}" -c "compile && rm -rf /out/*" | ||
| docker commit "${PROJECT}-origin" "gcr.io/oss-fuzz/${PROJECT}-ofg-cached" | ||
| docker run -ti --entrypoint="recompile" "gcr.io/oss-fuzz/${PROJECT}-ofg-cached" | ||
| ``` | ||
|
|
||
| # Assumptions | ||
| 1. Fuzzer: Chronos assumes `libFuzzer`. Other fuzzers are not well-supported, but may work by setting ENV `FUZZING_ENGINE` in project's `Dockerfile`. | ||
| 2. Sanitizer: Chronos assumes `AddressSanitizer`. Other sanitizers may work by adding setting ENV `SANITIZER` in project's `Dockerfile`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| ################################################################################ | ||
|
|
||
| # This script records the ENV and commands needed for fuzz target recompilation. | ||
| # It intercepts bash commands to save: 1) the ENV variable values before | ||
| # building the fuzz target (`recompile_env.sh`) and 2) all subsequent bash | ||
| # commands from that point (`recompile`). Combined with Docker, this setup | ||
| # allows for recompiling the fuzz target without rebuilding the entire project. | ||
| # Usage: | ||
| # 1. Set FUZZ_TARGET (e.g., in project's Dockerfile) | ||
| # 2. Source this file before compiling the fuzz target (e.g., source chronos.sh | ||
| # at the beginning of project's build.sh). | ||
|
|
||
| export START_RECORDING="false" | ||
| RECOMPILE_ENV="/usr/local/bin/recompile_env.sh" | ||
|
|
||
|
|
||
| # Initialize the recompile script. | ||
| initialize_recompile_script() { | ||
| export RECOMPILE_SCRIPT="/usr/local/bin/recompile" | ||
| echo "#!/bin/bash" > "$RECOMPILE_SCRIPT" | ||
| echo "source $RECOMPILE_ENV" >> "$RECOMPILE_SCRIPT" | ||
| chmod +x "$RECOMPILE_SCRIPT" | ||
| } | ||
|
|
||
|
|
||
| # Execute or record command for recompilation. | ||
| execute_or_record_command() { | ||
| record_command() { | ||
| echo "cd \"$(pwd)\"" >> "$RECOMPILE_SCRIPT" | ||
| echo "$@" >> "$RECOMPILE_SCRIPT" | ||
| } | ||
|
|
||
| # Check if any element in the command array contains the FUZZ_TARGET. | ||
| if [[ "$BASH_COMMAND" == *"$FUZZ_TARGET"* ]]; then | ||
| export START_RECORDING="true" | ||
| # Save all environment variables, excluding read-only ones | ||
| declare -p | grep -Ev 'declare -[^ ]*r[^ ]*' > "$RECOMPILE_ENV" | ||
| fi | ||
|
|
||
| if [[ "$START_RECORDING" == "true" ]]; then | ||
| record_command "$BASH_COMMAND" | ||
| echo "Recorded execution of: $BASH_COMMAND" | ||
| fi | ||
| } | ||
|
|
||
|
|
||
| main() { | ||
| # Initialize. | ||
| initialize_recompile_script | ||
|
|
||
| # Set up trap for DEBUG to intercept commands. | ||
| trap 'execute_or_record_command' DEBUG | ||
|
|
||
| # Enable extended debugging mode | ||
| shopt -s extdebug | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat! |
||
| # Ensure trap works in subshells and functions. | ||
| set -T | ||
| } | ||
|
|
||
| main | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #!/usr/bin/bash | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| ################################################################################ | ||
|
|
||
| # Prepare a project for fuzz target recompilation via chronos.sh | ||
| # Usage: | ||
| # (in OSS-Fuzz root dir) infra/experimental/chronos/prepare-recompile.sh <project> <fuzz-target-name> <fuzzing-language> | ||
| # E.g.: | ||
| #infra/experimental/chronos/prepare-recompile.sh libiec61850 fuzz_mms_decode.c c | ||
|
|
||
|
|
||
|
|
||
| PROJECT=$1 | ||
| FUZZ_TARGET=$2 | ||
| FUZZING_LANGUAGE=$3 | ||
|
|
||
| # Step 1: Copy chronos.sh to its project directory. | ||
| cp infra/experimental/chronos/chronos.sh "projects/$PROJECT/" | ||
|
|
||
| # Step 2: Copy chronos.sh to image and set FUZZ_TARGET and FUZZING_LANGUAGE in its Dockerfile. | ||
| { | ||
| echo "COPY chronos.sh /src"; | ||
| echo "ENV FUZZ_TARGET=\"$FUZZ_TARGET\""; | ||
| echo "ENV FUZZING_LANGUAGE=\"$FUZZING_LANGUAGE\""; | ||
| # Step 3: Source chronos.sh at the beginning of its build.sh. | ||
| echo "RUN sed -i.bak \"1s|^|source \\\"/src/chronos.sh\\\"\\n|\" \"/src/build.sh\"" | ||
| } >> "projects/$PROJECT/Dockerfile" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a general high level few sentences as a comment to describe how this script works?
e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks