| 1 |
#!/bin/bash -e |
| 2 |
########################################################################## |
| 3 |
# # |
| 4 |
# This file is part of Frama-C. # |
| 5 |
# # |
| 6 |
# Copyright (C) 2007-2022 # |
| 7 |
# CEA (Commissariat à l'énergie atomique et aux énergies # |
| 8 |
# alternatives) # |
| 9 |
# # |
| 10 |
# you can redistribute it and/or modify it under the terms of the GNU # |
| 11 |
# Lesser General Public License as published by the Free Software # |
| 12 |
# Foundation, version 2.1. # |
| 13 |
# # |
| 14 |
# It is distributed in the hope that it will be useful, # |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 17 |
# GNU Lesser General Public License for more details. # |
| 18 |
# # |
| 19 |
# See the GNU Lesser General Public License version 2.1 # |
| 20 |
# for more details (enclosed in the file licenses/LGPLv2.1). # |
| 21 |
# # |
| 22 |
########################################################################## |
| 23 |
|
| 24 |
usage=" |
| 25 |
Script to run C-Reduce (https://embed.cs.utah.edu/creduce) when debugging |
| 26 |
Frama-C, or when minimizing test cases resulting in a Frama-C error. |
| 27 |
|
| 28 |
NOTE: some distributions now use 'cvise' (a Python port of creduce) |
| 29 |
instead of 'creduce', but it is backwards-compatible. |
| 30 |
|
| 31 |
# Requirements |
| 32 |
|
| 33 |
- C-Reduce installed in the PATH, or its path in environment variable CREDUCE; |
| 34 |
- Frama-C installed in the PATH, or the path to its 'bin' directory in |
| 35 |
environment variable FRAMAC_BIN; |
| 36 |
- a source file + a command line which causes Frama-C to crash with |
| 37 |
a 'fatal' error (producing a stack trace); can also be used when |
| 38 |
Frama-C reports other errors, but in this case a separate script must be |
| 39 |
completed by the user. |
| 40 |
|
| 41 |
# Basic usage |
| 42 |
|
| 43 |
arguments: <script options> <file> <Frama-C command line options> |
| 44 |
|
| 45 |
This script creates a temporary directory named 'creducing'. It copies <file> |
| 46 |
inside it, then runs creduce with 'frama-c <command line options>' |
| 47 |
on the copied file, modifying it to make it smaller while still crashing. |
| 48 |
|
| 49 |
When done, you need to copy the reduced file and erase directory 'creducing'. |
| 50 |
|
| 51 |
# Script options |
| 52 |
|
| 53 |
Note: these options must be placed _before_ all other arguments. |
| 54 |
|
| 55 |
--help: show this help message. |
| 56 |
|
| 57 |
--ignore-parsability: do not check that each reduced program conforms to |
| 58 |
a gcc parsability check; helps avoid issues due to |
| 59 |
preprocessing flags. |
| 60 |
|
| 61 |
# Advanced usage |
| 62 |
|
| 63 |
- if <file> is not a preprocessed (.i) source, this script |
| 64 |
will retrieve the preprocessing arguments from Frama-C. It will try to |
| 65 |
preprocess the file in GCC to make sure it is parseable during reduction. |
| 66 |
This may cause some issues; use a preprocessed file if you can. |
| 67 |
|
| 68 |
- For 'fatal' crashes (with stack trace and 'Please report...' message), |
| 69 |
this script should work automatically. For other crashes (e.g. invalid |
| 70 |
input), this script will copy a template to the current directory and you |
| 71 |
need to fill it so that creduce will work. This usage mode requires knowing |
| 72 |
how C-Reduce works. |
| 73 |
" |
| 74 |
|
| 75 |
#### Command-line and environment validation |
| 76 |
# |
| 77 |
# |
| 78 |
|
| 79 |
ignore_parsability=0 |
| 80 |
while [ $# -ge 1 ]; do |
| 81 |
case "$1" in |
| 82 |
--help) echo "$usage"; exit;; |
| 83 |
--h) echo "$usage"; exit;; |
| 84 |
-help) echo "$usage"; exit;; |
| 85 |
-h) echo "$usage"; exit;; |
| 86 |
--ignore-parsability) |
| 87 |
ignore_parsability=1 |
| 88 |
shift |
| 89 |
;; |
| 90 |
*) break;; |
| 91 |
esac |
| 92 |
done |
| 93 |
|
| 94 |
if [ $# -lt 1 ]; then |
| 95 |
echo "$usage" |
| 96 |
exit |
| 97 |
fi |
| 98 |
|
| 99 |
file="$1" |
| 100 |
base=$(basename "$file") |
| 101 |
shift |
| 102 |
|
| 103 |
dir_for_reduction="creducing" |
| 104 |
|
| 105 |
script_for_creduce="./script_for_creduce.sh" |
| 106 |
|
| 107 |
if [ -z "$CREDUCE" ]; then |
| 108 |
# Now some distributions have 'cvise' instead of 'creduce', so try it |
| 109 |
# if found in PATH |
| 110 |
if command -v cvise 2>&1 >/dev/null; then |
| 111 |
CREDUCE="cvise" |
| 112 |
else |
| 113 |
CREDUCE="creduce" |
| 114 |
fi |
| 115 |
fi |
| 116 |
|
| 117 |
if ! command -v "$CREDUCE" 2>&1 >/dev/null; then |
| 118 |
echo "cvise/creduce not found; install it in the PATH or" |
| 119 |
echo "put it in environment variable CREDUCE." |
| 120 |
exit 1 |
| 121 |
fi |
| 122 |
|
| 123 |
if [[ ! -f "$file" ]]; then |
| 124 |
echo "Source file not found (must be first argument): $file" |
| 125 |
exit 1 |
| 126 |
fi |
| 127 |
|
| 128 |
too_many_sources="" |
| 129 |
for f in "$@"; do |
| 130 |
if [ -e "$f" ]; then |
| 131 |
too_many_sources+=" $f" |
| 132 |
fi |
| 133 |
done |
| 134 |
if [ ! -z "$too_many_sources" ]; then |
| 135 |
echo "error: too many sources; only the first argument must be a file: $file" |
| 136 |
echo " remove these from the command-line:$too_many_sources" |
| 137 |
exit 1 |
| 138 |
fi |
| 139 |
|
| 140 |
if [[ $(dirname "$file") = "$dir_for_reduction" ]]; then |
| 141 |
echo "error: cannot reduce a file inside the directory used for reduction," |
| 142 |
echo " $dir_for_reduction" |
| 143 |
exit 1 |
| 144 |
fi |
| 145 |
|
| 146 |
if [ -z "$FRAMAC_BIN" ]; then |
| 147 |
if ! command -v "frama-c" >/dev/null; then |
| 148 |
echo "error: frama-c must be in the PATH, or FRAMAC_BIN must point to" |
| 149 |
echo " the directory containing the frama-c binary" |
| 150 |
exit 1 |
| 151 |
fi |
| 152 |
FRAMAC="frama-c" |
| 153 |
else |
| 154 |
FRAMAC="$FRAMAC_BIN/frama-c" |
| 155 |
fi |
| 156 |
echo "[info] using FRAMAC: $FRAMAC" |
| 157 |
|
| 158 |
if [ -z "$FRAMAC_SHARE" ]; then |
| 159 |
FRAMAC_SHARE="$("$FRAMAC" -print-share-path)" |
| 160 |
fi |
| 161 |
|
| 162 |
if ! sed --version >/dev/null 2>&1; then |
| 163 |
echo "[info] sed is not GNU, trying gsed..." |
| 164 |
if command -v gsed >/dev/null; then |
| 165 |
echo "[info] gsed found, using it." |
| 166 |
SED="gsed" |
| 167 |
else |
| 168 |
echo "error: GNU sed is required but was not found (neither sed nor gsed)" |
| 169 |
exit 1 |
| 170 |
fi |
| 171 |
else |
| 172 |
SED="sed" |
| 173 |
fi |
| 174 |
|
| 175 |
# |
| 176 |
# |
| 177 |
#### End of command-line and environment validation |
| 178 |
|
| 179 |
if [[ ! "$options" =~ no-autoload-plugins ]]; then |
| 180 |
echo "********************************************************************" |
| 181 |
echo "Hint: consider using -no-autoload-plugins -load-module [modules]" |
| 182 |
echo " for faster reduction" |
| 183 |
echo "********************************************************************" |
| 184 |
fi |
| 185 |
|
| 186 |
if [[ "$base" =~ \.c$ ]]; then |
| 187 |
echo "********************************************************************" |
| 188 |
echo "Hint: consider passing a preprocessed (.i) file if possible," |
| 189 |
echo " otherwise #includes may prevent further reduction" |
| 190 |
echo "********************************************************************" |
| 191 |
# TODO: check for libc includes and suggest -print-libc |
| 192 |
fi |
| 193 |
|
| 194 |
# Obtain cpp flags by running Frama-C with `-kernel-msg-key pp` |
| 195 |
if [[ "$base" =~ \.i$ ]]; then |
| 196 |
# Preprocessed file: no need to get preprocessing flags |
| 197 |
# TODO: check for '-machdep' option and add/remove flags accordingly |
| 198 |
CPP="gcc -fsyntax-only" |
| 199 |
else |
| 200 |
set +e |
| 201 |
cpp_output=$("$FRAMAC" -print-cpp-commands "$@" "$file" 2>/dev/null) |
| 202 |
cpp_retcode=$? |
| 203 |
set -e |
| 204 |
if [ $cpp_retcode -ne 0 ]; then |
| 205 |
echo "error trying to get preprocessing flags (exit code: $cpp_retcode): $FRAMAC -print-cpp-commands $@ $file" |
| 206 |
exit $cpp_retcode |
| 207 |
fi |
| 208 |
CPP=$(echo "$cpp_output" | \ |
| 209 |
"$SED" -n '/Preprocessing command:/{n;p;}' | "$SED" "s|'.*' -o '.*'||") |
| 210 |
if [[ "$CPP" = "" ]]; then |
| 211 |
echo "[info] Could not get preprocessing flags when running Frama-C," |
| 212 |
echo " using default flags" |
| 213 |
# TODO: check for '-machdep' option and add/remove flags accordingly |
| 214 |
CPP="gcc -fsyntax-only -m32" |
| 215 |
else |
| 216 |
# Replace -E with -fsyntax-only to force GCC to try parsing the file; |
| 217 |
# also remove Frama-C's libc; use the standard system libraries. |
| 218 |
CPP=$(echo $CPP | \ |
| 219 |
"$SED" 's| -E | -fsyntax-only |' | \ |
| 220 |
"$SED" 's| -nostdinc | |' | \ |
| 221 |
"$SED" 's| -I[^ ]\+/libc | |' ) |
| 222 |
fi |
| 223 |
fi |
| 224 |
|
| 225 |
if [ ! -e "$script_for_creduce" ]; then |
| 226 |
echo "[info] $script_for_creduce not found." |
| 227 |
echo "Which kind of error are you reducing for?" |
| 228 |
echo "1. Fatal crash (error message with stacktrace)" |
| 229 |
echo "2. Non-fatal error" |
| 230 |
read -p "Please enter 1 or 2: " errorkind |
| 231 |
case $errorkind in |
| 232 |
1) |
| 233 |
cp "$FRAMAC_SHARE/analysis-scripts/script_for_creduce_fatal.sh" "$script_for_creduce" |
| 234 |
;; |
| 235 |
2) |
| 236 |
cp "$FRAMAC_SHARE/analysis-scripts/script_for_creduce_non_fatal.sh" "$script_for_creduce" |
| 237 |
echo "Script copied. Please edit $script_for_creduce and re-run this script." |
| 238 |
exit 0 |
| 239 |
;; |
| 240 |
*) |
| 241 |
echo "Invalid choice, aborting." |
| 242 |
exit 0 |
| 243 |
;; |
| 244 |
esac |
| 245 |
fi |
| 246 |
|
| 247 |
if [ "$ignore_parsability" -eq 0 ]; then |
| 248 |
echo "[info] the following command will be used to check for syntactic validity during reduction:" |
| 249 |
echo " $CPP" |
| 250 |
fi |
| 251 |
|
| 252 |
mkdir -p "$dir_for_reduction" |
| 253 |
cp "$file" "$dir_for_reduction" |
| 254 |
cp "$script_for_creduce" "$dir_for_reduction" |
| 255 |
cd "$dir_for_reduction" |
| 256 |
|
| 257 |
if [ "$ignore_parsability" -eq 0 ]; then |
| 258 |
"$SED" -i "s|@CPP@|$CPP|g" "$script_for_creduce" |
| 259 |
else |
| 260 |
"$SED" -i "s|^.*@CPP@.*$||g" "$script_for_creduce" |
| 261 |
fi |
| 262 |
"$SED" -i "s|@FRAMAC@|$FRAMAC|g" "$script_for_creduce" |
| 263 |
"$SED" -i "s|@BASE@|$base|g" "$script_for_creduce" |
| 264 |
"$SED" -i "s|@FCFLAGS@|$(echo $@ | tr "'" "\\'")|g" "$script_for_creduce" |
| 265 |
chmod u+x "$script_for_creduce" |
| 266 |
|
| 267 |
trap '{ echo "Creduce interrupted!"; echo ""; echo "(partially) reduced file: $dir_for_reduction/$base"; exit 0; }' SIGINT |
| 268 |
|
| 269 |
echo "" |
| 270 |
echo "File being reduced: $dir_for_reduction/$base (press Ctrl+C to stop at any time)" |
| 271 |
|
| 272 |
set +e |
| 273 |
rm -rf /tmp/script_for_creduce.out |
| 274 |
$script_for_creduce >>/tmp/script_for_creduce.out 2>&1 |
| 275 |
if [ $? -ne 0 ]; then |
| 276 |
# TODO: check if -cpp-extra-args exists and contains relative paths, |
| 277 |
# and if so, warn about them |
| 278 |
echo "#################################################" |
| 279 |
echo "ERROR: script did not produce the expected error." |
| 280 |
echo " check the options given to Frama-C." |
| 281 |
echo " If you edited '$script_for_creduce', check it as well." |
| 282 |
echo "" |
| 283 |
if [ $(wc -l /tmp/script_for_creduce.out | cut -d' ' -f1) -gt 20 ]; then |
| 284 |
echo "# Script output (first 20 lines):" |
| 285 |
head -n 20 /tmp/script_for_creduce.out |
| 286 |
echo "(...) [truncated; full output in /tmp/script_for_creduce.out]" |
| 287 |
else |
| 288 |
echo "# Script output:" |
| 289 |
cat /tmp/script_for_creduce.out |
| 290 |
fi |
| 291 |
exit 1 |
| 292 |
fi |
| 293 |
set -e |
| 294 |
|
| 295 |
"$CREDUCE" script_for_creduce.sh "$base" |
| 296 |
|
| 297 |
echo "Finished reducing file: $dir_for_reduction/$base" |
| 298 |
echo "Remember to remove 'script_for_creduce.sh' after you are done." |