|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Infer script for performing |
| 4 | +# static analysis on the MariaDB codebase |
| 5 | + |
| 6 | +set -x -e |
| 7 | + |
| 8 | +if [ $# -lt 1 ]; then |
| 9 | + echo insufficient args >&2 |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +# Testing this version |
| 14 | +branch=$1 |
| 15 | + |
| 16 | +if [ -z "$branch" ]; then |
| 17 | + echo "usage $0 {branch/commit}" >&2 |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +: "${JOBS:=4}" |
| 22 | + |
| 23 | +base=$PWD |
| 24 | +result_dir=$PWD/infer_results |
| 25 | + |
| 26 | +rm -rf "${result_dir}" index.txt report.json |
| 27 | + |
| 28 | +## Fetch |
| 29 | + |
| 30 | +pushd /mnt/src |
| 31 | +git fetch origin "$branch" |
| 32 | +git checkout -f FETCH_HEAD |
| 33 | +git submodule update --init --recursive |
| 34 | +git clean -df |
| 35 | +commit=$(git rev-parse FETCH_HEAD) |
| 36 | + |
| 37 | +if [ -d "/mnt/infer/$commit" ]; then |
| 38 | + echo "Already scanned $commit" |
| 39 | + exit 0 |
| 40 | +fi |
| 41 | + |
| 42 | +# What can we use as a reference |
| 43 | + |
| 44 | +populate_differences() |
| 45 | +# input $merge_base |
| 46 | +{ |
| 47 | + # Find something closer - e.g. we've appended to a branch |
| 48 | + # we've already tested |
| 49 | + mapfile -t commits < <(git rev-list "${merge_base}..FETCH_HEAD") |
| 50 | + for common_commit in "${commits[@]}"; do |
| 51 | + if [ -d /mnt/infer/"$common_commit" ]; then |
| 52 | + break; |
| 53 | + fi |
| 54 | + done |
| 55 | + if [ ! -d "/mnt/infer/$common_commit" ]; then |
| 56 | + return 1 |
| 57 | + fi |
| 58 | + merge_base=$common_commit |
| 59 | + # The file changes we from last results |
| 60 | + git diff --name-only FETCH_HEAD.."${merge_base}" | tee "$base"/index.txt |
| 61 | + |
| 62 | + if [ ! -s "$base"/index.txt ]; then |
| 63 | + echo "Empty changes - nothing necessary" |
| 64 | + rm "$base"/index.txt |
| 65 | + exit 0 |
| 66 | + fi |
| 67 | + |
| 68 | + limit=50 |
| 69 | + if [ "$(wc -l < "${base}"/index.txt)" -gt $limit ]; then |
| 70 | + echo "More than $limit changes, just do a full generation" |
| 71 | + rm "$base/index.txt" |
| 72 | + return 1 |
| 73 | + fi |
| 74 | + |
| 75 | + # use previous results as a base |
| 76 | + cp -a "/mnt/infer/$merge_base" "$result_dir" |
| 77 | + |
| 78 | + # Using as a recently used maker |
| 79 | + # Eventually we can remove/clear based on not being looked at |
| 80 | + touch "/mnt/infer/$merge_base" |
| 81 | + return 0 |
| 82 | +} |
| 83 | + |
| 84 | +# Just assume we diverged from main at some point |
| 85 | +# Using $commit because merge-base didn't process |
| 86 | +# pull request references. |
| 87 | +merge_base=$(git merge-base "$commit" origin/main) |
| 88 | + |
| 89 | +if populate_differences; then |
| 90 | + echo "No common commit ancestor with analysis or over depth limit($limit)" >&2 |
| 91 | + |
| 92 | + echo "This is going to take a while for a full scan" |
| 93 | +fi |
| 94 | + |
| 95 | +# back from /mnt/src |
| 96 | +popd |
| 97 | + |
| 98 | +# Build |
| 99 | + |
| 100 | +build() |
| 101 | +{ |
| 102 | + cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ |
| 103 | + -DCMAKE_C_COMPILER=clang \ |
| 104 | + -DCMAKE_CXX_COMPILER=clang++ \ |
| 105 | + -S /mnt/src -B bld |
| 106 | + cmake --build bld \ |
| 107 | + --target GenError GenServerSource GenUnicodeDataSource GenFixPrivs \ |
| 108 | + --parallel "$JOBS" |
| 109 | +} |
| 110 | + |
| 111 | +if [ ! -d bld ]; then |
| 112 | + mkdir bld |
| 113 | + build |
| 114 | +fi |
| 115 | + |
| 116 | +# |
| 117 | +capture() |
| 118 | +{ |
| 119 | + infer capture --compilation-database compile_commands.json --project-root /mnt/src --results-dir "${result_dir}" "$@" |
| 120 | +} |
| 121 | + |
| 122 | +analyze() |
| 123 | +{ |
| 124 | + infer analyze --project-root /mnt/src --results-dir "${result_dir}" --max-jobs "${JOBS}" "$@" |
| 125 | +} |
| 126 | +# Capture and analyze the feature of the files changes in index |
| 127 | +# |
| 128 | +cd bld |
| 129 | + |
| 130 | +if [ ! -f ../index.txt ]; then |
| 131 | + echo "full run, this could take a while" |
| 132 | + capture |
| 133 | + analyze |
| 134 | + mv "$result_dir" /mnt/infer/"$commit" |
| 135 | + cd .. |
| 136 | + rm -rf bld |
| 137 | + exit |
| 138 | +fi |
| 139 | + |
| 140 | +# We've copied over a result dir, so we're continuing |
| 141 | +# https://fbinfer.com/docs/infer-workflow/#differential-workflow |
| 142 | +# using 'infer capture" instead infer run |
| 143 | +capture --reactive |
| 144 | + |
| 145 | +# some form of incremental |
| 146 | +analyze --changed-files-index ../index.txt |
| 147 | + |
| 148 | +# Preserve result |
| 149 | +cp "${result_dir}"/report.json ../report.json |
| 150 | + |
| 151 | +#cp -a "${result_dir}" "${result_dir}_preserved" |
| 152 | + |
| 153 | +#pushd /mnt/src |
| 154 | +#?git checkout "$merge_base" |
| 155 | +#popd |
| 156 | + |
| 157 | +# just in case these have changed, including generated files |
| 158 | +cd .. |
| 159 | +build |
| 160 | +cd bld |
| 161 | + |
| 162 | +# Can we use the previous captured /mnt/infer/$merge_base |
| 163 | +capture --merge-capture "/mnt/infer/$merge_base" --reactive --mark-unchanged-procs |
| 164 | + |
| 165 | +analyze --incremental-analysis --changed-files-index ../index.txt |
| 166 | + |
| 167 | +# It may be merged next, or a commit pushed on top of it. |
| 168 | +infer reportdiff --report-current ../report.json --report-previous "${result_dir}"/report.json --project-root /mnt/src --results-dir "${result_dir}" |
| 169 | +cd .. |
| 170 | +rm -rf bld index.txt |
| 171 | +# report.json |
| 172 | + |
| 173 | +## At this point we have infer_results/differential/{fixed,introduced}.json |
| 174 | + |
| 175 | +# Useful enough to save as /mnt/infer/ |
| 176 | +# Its unknown if this is on main branch or now, but just save. |
| 177 | +# If its merged next, then a commit exists, if a user appends |
| 178 | +# a commit, we've got a smaller delta. |
| 179 | +mv "${result_dir}" /mnt/infer/"${commit}" |
| 180 | + |
| 181 | + |
| 182 | +# Look at the changes from the main branch |
| 183 | +# |
| 184 | +# Take the main branch report.json |
| 185 | +# remove fixed, add introduced, and then walk |
| 186 | +# though other commits, if they exist, and apply the |
| 187 | +# same again up until, and including the last commit |
| 188 | +source /mnt/src/VERSION |
| 189 | +branch=${MYSQL_VERSION_MAJOR}.${MYSQL_VERSION_MINOR} |
| 190 | + |
| 191 | +pushd /mnt/src |
| 192 | +merge_base=$(git merge-base "origin/$branch" "$commit") |
| 193 | +mapfile -t commits < <(git rev-list "${merge_base}..${commit}") |
| 194 | +popd |
| 195 | + |
| 196 | +base=/mnt/infer/$merge_base |
| 197 | +for common_commit in "${commits[@]}"; do |
| 198 | + diff_dir=/mnt/infer/"$common_commit"/differential/ |
| 199 | + if [ -d "$diff_dir" ]; then |
| 200 | + # removed fixed issues and append introduced. |
| 201 | + jq --slurpfile to_remove "${diff_dir}"/fixed.json ' |
| 202 | + ($to_remove[0] | map(.hash)) as $hashes_to_remove |
| 203 | + | map(select(.hash as $h | $hashes_to_remove | index($h) | not))' \ |
| 204 | + "${base}"/report.json > filtered.json |
| 205 | + jq -s 'add | unique_by(.hash)' filtered.json "${diff_dir}"/introduced.json > report.json |
| 206 | + fi |
| 207 | +done |
| 208 | + |
| 209 | +infer reportdiff --report-current report.json --report-previous "${base}"/report.json --project-root /mnt/src --results-dir "${result_dir}_diff" |
| 210 | + |
| 211 | +result_dir=/mnt/infer/"${commit}" |
| 212 | + |
| 213 | +check() |
| 214 | +{ |
| 215 | + file=$1 |
| 216 | + msg=$2 |
| 217 | + if [ -f "${file}" ]; then |
| 218 | + filesize=$(stat -c%s "$file") |
| 219 | + # 2 is the size of an empty json array '[]' |
| 220 | + if [ "$filesize" -gt 2 ]; then |
| 221 | + echo "$msg" |
| 222 | + echo |
| 223 | + echo "Here are the changes:" |
| 224 | + jq . "${file}" |
| 225 | + return 1 |
| 226 | + fi |
| 227 | + fi |
| 228 | + return 0 |
| 229 | +} |
| 230 | + |
| 231 | +check "${result_dir}"/differential/fixed.json "Good human! Thanks for fixing the bad things in the last commit" |
| 232 | + |
| 233 | +check "${result_dir}"/differential/introduced.json "Bad human! Don't introduce bad things in the last commit" >&2 |
| 234 | + |
| 235 | +check "${result_dir}_diff"/differential/fixed.json "Good human! Thanks for fixing the bad things" |
| 236 | + |
| 237 | +if check "${result_dir}_diff"/differential/introduced.json "Bad human! Don't introduce bad things" >&2; then |
| 238 | + exit 1 |
| 239 | +fi |
0 commit comments