|
| 1 | +#!/bin/bash -eux |
| 2 | +# Copyright 2024 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +################################################################################ |
| 17 | + |
| 18 | +# Script for running a full OSS-Fuzz-gen cycle: |
| 19 | +# 1) Generate OSS-Fuzz projects from scratch with a single harness. |
| 20 | +# 2) Extract Fuzz Introspector report to provide program analysis data. |
| 21 | +# 3) Run OSS-Fuzz-gen to create a larger set of harnesses. |
| 22 | + |
| 23 | +BENCHMARK_HEURISTICS="${VARIABLE:-far-reach-low-coverage}" |
| 24 | +OSS_FUZZ_GEN_MODEL=${MODEL} |
| 25 | +TARGETS_FUZZ="${TARGETS}" |
| 26 | +VAR_HARNESSES_PER_PROJECT="${HARNESS_PER_PROJECT:-5}" |
| 27 | + |
| 28 | +# Make sure OFG does not erase any bits in the target OSS-Fuzz project |
| 29 | +export OFG_CLEAN_UP_OSS_FUZZ=0 |
| 30 | + |
| 31 | + |
| 32 | +# Define base directories. |
| 33 | +BASE_DIR=$PWD |
| 34 | +OSS_FUZZ_GEN_DIR=${BASE_DIR}/../../ |
| 35 | +WORKDIR=$PWD/workdir |
| 36 | + |
| 37 | +# Set environment if it's not already set up. |
| 38 | +if [ -d "${WORKDIR}" ] |
| 39 | +then |
| 40 | + echo "Workdir exists, reusing set up." |
| 41 | +else |
| 42 | + echo "Creating workdir." |
| 43 | + mkdir -p $WORKDIR |
| 44 | + cd $WORKDIR |
| 45 | + # set up virtualenv |
| 46 | + python3.11 -m virtualenv .venv |
| 47 | + . .venv/bin/activate |
| 48 | + |
| 49 | + # set up fuzz introspector |
| 50 | + git clone https://github.com/ossf/fuzz-introspector |
| 51 | + cd fuzz-introspector |
| 52 | + python3 -m pip install -r ./requirements.txt |
| 53 | + cd tools/web-fuzzing-introspection/ |
| 54 | + python3 -m pip install -r ./requirements.txt |
| 55 | + |
| 56 | + # set up oss-fuzz |
| 57 | + cd $WORKDIR |
| 58 | + git clone https://github.com/google/oss-fuzz |
| 59 | + |
| 60 | + cd ${OSS_FUZZ_GEN_DIR} |
| 61 | + python3 -m pip install -r ./requirements.txt |
| 62 | + |
| 63 | + # exit virtualenv |
| 64 | + deactivate |
| 65 | +fi |
| 66 | + |
| 67 | +# Define Fuzz Introspector, OSS-Fuzz and OFG directories. |
| 68 | +ROOT_FI=${WORKDIR}/fuzz-introspector |
| 69 | +OSS_FUZZ_DIR=${WORKDIR}/oss-fuzz |
| 70 | +OFG_DIR=${BASE_DIR}/../../ |
| 71 | + |
| 72 | +cd $WORKDIR |
| 73 | +. .venv/bin/activate |
| 74 | + |
| 75 | + |
| 76 | +# Generate OSS-Fuzz projects from scratch and extract top projects. |
| 77 | +cd ${BASE_DIR} |
| 78 | +python3 ./runner.py \ |
| 79 | + -o ${OSS_FUZZ_DIR} \ |
| 80 | + --max_successful=${VAR_HARNESSES_PER_PROJECT} \ |
| 81 | + -t ${VAR_HARNESSES_PER_PROJECT} \ |
| 82 | + -m ${OSS_FUZZ_GEN_MODEL} \ |
| 83 | + -i ${TARGETS_FUZZ} |
| 84 | + |
| 85 | +AUTOGEN_DIR=${WORKDIR}/auto-generated-projects |
| 86 | +python3 ./post-process.py extract-top \ |
| 87 | + --oss-fuzz-dir=${OSS_FUZZ_DIR} \ |
| 88 | + --destination=${AUTOGEN_DIR} |
| 89 | + |
| 90 | +# Identify the names of the top projects, which will be used for core |
| 91 | +# OFG. |
| 92 | +PROJECT="" |
| 93 | +for project2 in ${AUTOGEN_DIR}/* ; do |
| 94 | + echo $(basename ${project2}) |
| 95 | + PROJECT="${PROJECT} $(basename ${project2})" |
| 96 | +done |
| 97 | + |
| 98 | +echo "Autogenerated top OSS-Fuzz projects" |
| 99 | +echo $PROJECT |
| 100 | +echo "--------------------------" |
| 101 | + |
| 102 | +# Assemble project names in comma-separated list, which we need for OFG. |
| 103 | +comma_separated="" |
| 104 | +for proj in ${PROJECT}; do |
| 105 | + echo ${proj} |
| 106 | + comma_separated="${comma_separated}${proj}," |
| 107 | +done |
| 108 | +comma_separated=${comma_separated::-1} |
| 109 | + |
| 110 | +# Generate fresh introspector reports that OFG can use as seed for auto |
| 111 | +# generation. |
| 112 | +echo "Creating introspector reports" |
| 113 | +cd ${OSS_FUZZ_DIR} |
| 114 | +for project in ${PROJECT}; do |
| 115 | + cp -rf ${AUTOGEN_DIR}/${project} ${OSS_FUZZ_DIR}/projects/${project} |
| 116 | + python3 $ROOT_FI/oss_fuzz_integration/runner.py \ |
| 117 | + introspector $project 10 --disable-webserver |
| 118 | + # Reset is necessary because some project exeuction |
| 119 | + # could break the display encoding which affect |
| 120 | + # the later oss-fuzz-gen execution. |
| 121 | + reset |
| 122 | +done |
| 123 | + |
| 124 | +# Shut down the existing webapp if it's running |
| 125 | +curl --silent http://localhost:8080/api/shutdown || true |
| 126 | + |
| 127 | +# Create Fuzz Introspector's webserver DB |
| 128 | +echo "[+] Creating the webapp DB" |
| 129 | +cd $ROOT_FI/tools/web-fuzzing-introspection/app/static/assets/db/ |
| 130 | +python3 ./web_db_creator_from_summary.py \ |
| 131 | + --local-oss-fuzz ${OSS_FUZZ_DIR} |
| 132 | + |
| 133 | +# Start webserver |
| 134 | +echo "Shutting down server in case it's running" |
| 135 | +curl --silent http://localhost:8080/api/shutdown || true |
| 136 | + |
| 137 | +echo "[+] Launching FI webapp" |
| 138 | +cd $ROOT_FI/tools/web-fuzzing-introspection/app/ |
| 139 | +FUZZ_INTROSPECTOR_LOCAL_OSS_FUZZ=${OSS_FUZZ_DIR} \ |
| 140 | + python3 ./main.py >> /dev/null & |
| 141 | + |
| 142 | +SECONDS=5 |
| 143 | +while true |
| 144 | +do |
| 145 | + # Checking if exists |
| 146 | + MSG=$(curl -v --silent 127.0.0.1:8080 2>&1 | grep "Fuzzing" | wc -l) |
| 147 | + if [[ $MSG > 0 ]]; then |
| 148 | + echo "Found it" |
| 149 | + break |
| 150 | + fi |
| 151 | + echo "- Waiting for webapp to load. Sleeping ${SECONDS} seconds." |
| 152 | + sleep ${SECONDS} |
| 153 | +done |
| 154 | + |
| 155 | +# Run OSS-Fuzz-gen on the projects |
| 156 | +echo "[+] Running OSS-Fuzz-gen experiment" |
| 157 | +cd ${OSS_FUZZ_GEN_DIR} |
| 158 | +LLM_NUM_EVA=1 LLM_NUM_EXP=1 ./run_all_experiments.py \ |
| 159 | + --model=$OSS_FUZZ_GEN_MODEL \ |
| 160 | + -g ${BENCHMARK_HEURISTICS} \ |
| 161 | + -gp ${comma_separated} \ |
| 162 | + -gm ${VAR_HARNESSES_PER_PROJECT} \ |
| 163 | + -of ${OSS_FUZZ_DIR} \ |
| 164 | + -e http://127.0.0.1:8080/api |
| 165 | + |
| 166 | +echo "Shutting down started webserver" |
| 167 | +curl --silent http://localhost:8080/api/shutdown || true |
0 commit comments