-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 12 commits
e62bd0b
4933e40
198edfc
31f727a
bcdf1a6
13a6702
0dafb68
3dcab50
009e152
0a8f45e
c276e0c
432e69a
deabbe7
2821e62
0e3755a
89fc50d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 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="compile" --name "${PROJECT}-origin" "gcr.io/oss-fuzz/${PROJECT}" | ||
docker commit "${PROJECT}-origin" "gcr.io/oss-fuzz/${PROJECT}-ofg-cached" | ||
docker run -ti --entrypoint="recompile" "gcr.io/oss-fuzz/${PROJECT}-ofg-cached" | ||
``` |
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: | ||
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks |
||
# 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 | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/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\""; | ||
} >> "projects/$PROJECT/Dockerfile" | ||
|
||
# Step 3: Source chronos.sh at the beginning of its build.sh. | ||
sed -i.bak "1s|^|source \"/src/chronos.sh\"\n|" "projects/$PROJECT/build.sh" | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
when
compile
is run here,SANITIZER
wont be set and consequently we won't set the any sanitizer flags in the following code (since sanitizer is''
) (oss-fuzz/infra/base-images/base-builder/compile
Lines 69 to 72 in 432e69a
oss-fuzz/infra/base-images/base-builder/Dockerfile
Lines 60 to 76 in 432e69a
For OFG we need to have two different builds:
SANITIZER == 'coverage'
SANITIZER == 'address'
but I assume in general it's intended to use this with sanitizers, which might make it nice to show how to do this in the README. I think just adding
-e SANITIZER="address"
to thedocker run
command would do the jobThere 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.
@DonggeLiu re #11937 (comment) -- I think my comment only applies to the README for now
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.
OK Done