1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
#!/bin/bash
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# For more fine-grained instructions, see:
# https://docs.google.com/document/d/1chTvr3fSofQNV_PDPEHRyUgcJCQBgTDOOBriW9gIm9M/edit?ts=5e9549a2#heading=h.fjdnrdg1gcty
set -u # unset variables are quit-worthy errors
# Possible flag values and their default settings.
PLATFORMS="linux"
BUILD_CLANG=true
REWRITE=true
EXTRACT_EDITS=true
KEEP_BUILD=false
INCREMENTAL_CLANG_BUILD=false
# The command line string to parse them.
LONG_FLAGS="platforms::,skip-building-clang,skip-rewrite,skip-extract-edits,\
keep-build,incremental-clang-build,help"
options=$(getopt -l "$LONG_FLAGS" -o "p::brekih" -- "$@")
# Important not to access $1 if there is no passed values (due to set -u above
# will trigger an error).
# This ensures that '--' is the last value and will break the loop below.
eval set -- "$options"
while true; do
case "$1" in
-p|--platforms)
shift
PLATFORMS="$1"
;;
-b|--skip-building-clang)
BUILD_CLANG=false
KEEP_BUILD=true
;;
-r|--skip-rewrite)
REWRITE=false
;;
-e|--skip-extract-edits)
EXTRACT_EDITS=false
;;
-k|--keep-build)
KEEP_BUILD=true
;;
-i|--incremental-clang-build)
INCREMENTAL_CLANG_BUILD=true
KEEP_BUILD=true
;;
-h|--help)
echo "Usage: rewrite-multiple-platforms.sh [OPTIONS]..."
echo "Runs the clang plugin spanify over selected chromium platforms and"\
" spanifies unsafe buffers usage."
echo ""
echo "Options:"
echo " [-p|--platforms]=<comma,separated,list,of,platforms>, defaults"\
"to 'linux'"
echo " [-b|--skip-building-clang], when set we won't attempt to build"\
"third_party's clang, requires a previous run with --keep-build,"\
"but this flag also sets --keep-build for convenience."
echo " [-r|--skip-rewrite], when set we won't create/destroy the"\
"scratch directory and won't run the clang plugin."
echo " [-e|--skip-extract-edits], when set we won't run"\
"extract_edits.py to generate the patches."
echo " [-k|--keep-build], when set we won't restore third_party/llvm."\
"this should be set when you are going to run this script again,"\
"but note you need to restore the directory to prevent slow down of"\
"your regular chromium builds."
echo " [-i|--incremental-clang-build], when set we attempt to build"\
"incrementally. This only works if you previously completed a run"\
"with --keep-build and you haven't rebased, or gclient sync'd."\
"This also sets --keep-build for convenience."
echo ""
echo "Check the README for more info."
exit 0
;;
--)
shift
break;;
esac
shift
done
# Create the scratch directory that we need and ensure it is empty.
if [ $REWRITE = true ]
then
if [ -d ~/scratch ]
then
echo "*** Clearing ~/scratch ***"
rm -r ~/scratch
fi
mkdir ~/scratch
fi
COMPILE_DIRS=.
EDIT_DIRS=.
# Build and test the rewriter.
if [ $BUILD_CLANG = true ]
then
# Save llvm-build as it is about to be overwritten (if it hasn't already been
# saved).
if [ ${INCREMENTAL_CLANG_BUILD} = false ] && \
[ ! -d third_party/llvm-build-upstream/ ]
then
echo "*** Saving current build ***"
mv third_party/llvm-build third_party/llvm-build-upstream
else
echo "*** Build is already saved ***"
fi
if [ $INCREMENTAL_CLANG_BUILD = true ]
then
echo "*** Building the rewriter incrementally ***"
time ninja -C third_party/llvm-build/Release+Asserts/
else
echo "*** Building the rewriter completely ***"
time tools/clang/scripts/build.py \
--with-android \
--without-fuchsia \
--extra-tools spanify || exit 1
fi
else
echo "*** Skipping building clang ***"
fi
echo "*** Testing the rewriter ***"
tools/clang/spanify/tests/run_all_tests.py || exit 1
args_for_platform() {
case "$1" in
android)
cat <<EOF
target_os = "android"
clang_use_chrome_plugins = false
is_chrome_branded = true
is_debug = false
dcheck_always_on = true
is_official_build = true
symbol_level = 1
use_remoteexec = false
enable_remoting = true
enable_webview_bundles = true
ffmpeg_branding = "Chrome"
proprietary_codecs = true
force_enable_raw_ptr_exclusion = true
EOF
;;
win)
cat <<EOF
target_os = "win"
clang_use_chrome_plugins = false
enable_precompiled_headers = false
is_chrome_branded = true
is_debug = false
dcheck_always_on = true
is_official_build = true
symbol_level = 1
use_remoteexec = false
chrome_pgo_phase = 0
force_enable_raw_ptr_exclusion = true
EOF
;;
linux)
cat <<EOF
target_os = "linux"
clang_use_chrome_plugins = false
dcheck_always_on = true
is_chrome_branded = true
is_debug = false
is_official_build = true
use_remoteexec = false
chrome_pgo_phase = 0
force_enable_raw_ptr_exclusion = true
EOF
;;
cros)
cat <<EOF
target_os = "chromeos"
chromeos_is_browser_only = true
dcheck_always_on = true
is_chrome_branded = true
is_debug = false
is_official_build = true
use_remoteexec = false
chrome_pgo_phase = 0
force_enable_raw_ptr_exclusion = true
EOF
;;
ash)
cat <<EOF
target_os = "chromeos"
dcheck_always_on = true
is_debug = false
is_official_build = true
use_remoteexec = false
chrome_pgo_phase = 0
force_enable_raw_ptr_exclusion = true
EOF
;;
mac)
cat <<EOF
target_os = "mac"
dcheck_always_on = true
is_chrome_branded = true
is_debug = false
is_official_build = true
use_remoteexec = false
chrome_pgo_phase = 0
symbol_level = 1
force_enable_raw_ptr_exclusion = true
EOF
;;
*)
echo "unknown platform"
exit 1
;;
esac
}
# The latest rewrite directory.
OUT_DIR=""
pre_process() {
PLATFORM="$1"
OUT_DIR="out/rewrite-$PLATFORM"
mkdir -p "$OUT_DIR"
args_for_platform "$PLATFORM" > "$OUT_DIR/args.gn"
# Build generated files that a successful compilation depends on.
echo "*** Preparing targets for $PLATFORM ***"
gn gen $OUT_DIR
time ninja -C $OUT_DIR -t targets all \
| grep '^gen/.*\(\.h\|inc\|css_tokenizer_codepoints.cc\)' \
| cut -d : -f 1 \
| xargs -s $(expr $(getconf ARG_MAX) - 256) ninja -C $OUT_DIR \
|| exit 1
TARGET_OS_OPTION=""
if [ $PLATFORM = "win" ]; then
TARGET_OS_OPTION="--target_os=win"
fi
}
main_rewrite() {
PLATFORM=$1
OUT_DIR="out/rewrite-${PLATFORM}"
TARGET_OS_OPTION=""
if [ $PLATFORM = "win" ]; then
TARGET_OS_OPTION="--target_os=win"
fi
# Main rewrite.
#
# To avoid spending too much time writing to disk, we use `awk '!x[$0]++'`
# to removes duplicate lines. This reduces the output by a 62 factor: from
# 20GB to 300MB. It is faster than `sort -u`. This consumes RAM, but
# cloudtops have plenty of it.
echo "*** Running the main rewrite phase for $PLATFORM ***"
time (
tools/clang/scripts/run_tool.py \
$TARGET_OS_OPTION \
--tool spanify \
--generate-compdb \
-p $OUT_DIR \
$COMPILE_DIRS 2>~/scratch/rewriter-${PLATFORM}.main.err \
| awk '!x[$0]++' 1>~/scratch/rewriter-${PLATFORM}.main.out
)
}
if [ $REWRITE = true ]
then
for PLATFORM in ${PLATFORMS//,/ }
do
pre_process "$PLATFORM"
done
for PLATFORM in ${PLATFORMS//,/ }
do
main_rewrite "$PLATFORM"
done
else
echo "*** Skipping rewrite ***"
fi
# Apply edits generated by the main rewrite.
if [ $EXTRACT_EDITS = true ]
then
echo "*** Clearing test patches ***"
rm ~/scratch/patch*
echo "*** Applying edits ***"
cat ~/scratch/rewriter-*.main.out \
| tools/clang/spanify/extract_edits.py \
| tools/clang/scripts/apply_edits.py -p $OUT_DIR $EDIT_DIRS
else
echo "*** Skipping edits ***"
fi
# Format sources, as many lines are likely over 80 chars now.
echo "*** Formatting ***"
time git cl format
# Restore llvm-build. Without this, your future builds will be painfully slow.
if [ $KEEP_BUILD = false ]
then
echo "*** Restoring llvm-build you will need to build the plugin again ***"
rm -r -f third_party/llvm-build
mv third_party/llvm-build-upstream third_party/llvm-build
else
echo "*** Not restoring llvm-build, chromium wide builds will be slower ***"
fi
|