2 #===- lib/asan/scripts/asan_device_setup.py -----------------------------------===#
4 # The LLVM Compiler Infrastructure
6 # This file is distributed under the University of Illinois Open Source
7 # License. See LICENSE.TXT for details.
9 # Prepare Android device to run ASan applications.
11 #===------------------------------------------------------------------------===#
14 HERE
="$(cd "$
(dirname "$0")" && pwd)"
22 echo "usage: $0 [--revert] [--device device-id] [--lib path] [--extra_options options]"
23 echo " --revert: Uninstall ASan from the device."
24 echo " --lib: Path to ASan runtime library."
25 echo " --extra_options: Extra ASAN_OPTIONS."
26 echo " --device: Install to the given device. Use 'adb devices' to find"
32 while [[ $# > 0 ]]; do
39 if [[ $# == 0 ]]; then
40 echo "--extra-options requires an argument."
47 if [[ $# == 0 ]]; then
48 echo "--lib requires an argument."
55 if [[ $# == 0 ]]; then
56 echo "--device requires an argument."
69 if [[ x
$device != x
]]; then
73 ASAN_RT
="libclang_rt.asan-arm-android.so"
75 if [[ x
$revert == xyes
]]; then
76 echo '>> Uninstalling ASan'
80 $ADB shell
mv /system
/bin
/app_process.real
/system
/bin
/app_process
81 $ADB shell
rm /system
/bin
/asanwrapper
82 $ADB shell
rm /system
/lib
/$ASAN_RT
84 echo '>> Restarting shell'
92 if [[ -d "$lib" ]]; then
94 elif [[ -f "$lib" && "$lib" == *"$ASAN_RT" ]]; then
95 ASAN_RT_PATH
=$
(dirname "$lib")
96 elif [[ -f "$HERE/$ASAN_RT" ]]; then
98 elif [[ $
(basename "$HERE") == "bin" ]]; then
99 # We could be in the toolchain's base directory.
100 # Consider ../lib and ../lib/clang/$VERSION/lib/linux.
101 P
=$
(ls "$HERE"/..
/lib
/"$ASAN_RT" "$HERE"/..
/lib
/clang
/*/lib
/linux
/"$ASAN_RT" 2>/dev
/null |
sort |
tail -1)
102 if [[ -n "$P" ]]; then
103 ASAN_RT_PATH
="$(dirname "$P")"
107 if [[ -z "$ASAN_RT_PATH" ||
! -f "$ASAN_RT_PATH/$ASAN_RT" ]]; then
108 echo "ASan runtime library not found"
112 TMPDIRBASE
=$
(mktemp
-d)
113 TMPDIROLD
="$TMPDIRBASE/old"
114 TMPDIR
="$TMPDIRBASE/new"
117 echo '>> Remounting /system rw'
122 echo '>> Copying files from the device'
123 $ADB pull
/system
/bin
/app_process
"$TMPDIROLD"
124 $ADB pull
/system
/bin
/app_process.real
"$TMPDIROLD" || true
125 $ADB pull
/system
/bin
/asanwrapper
"$TMPDIROLD" || true
126 $ADB pull
/system
/lib
/libclang_rt.asan-arm-android.so
"$TMPDIROLD" || true
127 cp -r "$TMPDIROLD" "$TMPDIR"
129 if ! [[ -f "$TMPDIR/app_process" ]]; then
130 echo "app_process missing???"
134 if [[ -f "$TMPDIR/app_process.real" ]]; then
135 echo "app_process.real exists, updating the wrapper"
137 echo "app_process.real missing, new installation"
138 mv "$TMPDIR/app_process" "$TMPDIR/app_process.real"
141 echo '>> Generating wrappers'
143 cp "$ASAN_RT_PATH/$ASAN_RT" "$TMPDIR/"
145 # FIXME: alloc_dealloc_mismatch=0 prevents a failure in libdvm startup,
146 # which may or may not be a real bug (probably not).
147 ASAN_OPTIONS
=start_deactivated
=1,alloc_dealloc_mismatch
=0
148 if [[ x
$extra_options != x
]] ; then
149 ASAN_OPTIONS
="$ASAN_OPTIONS,$extra_options"
153 cat <<EOF >"$TMPDIR/app_process"
155 ASAN_OPTIONS=$ASAN_OPTIONS \\
156 LD_PRELOAD=libclang_rt.asan-arm-android.so \\
157 exec /system/bin/app_process.real \$@
161 # General command-line tool wrapper (use for anything that's not started as
163 cat <<EOF >"$TMPDIR/asanwrapper"
165 LD_PRELOAD=libclang_rt.asan-arm-android.so \\
170 if ! ( cd "$TMPDIRBASE" && diff -qr old
/ new
/ ) ; then
171 echo '>> Pushing files to the device'
172 $ADB push
"$TMPDIR/$ASAN_RT" /system
/lib
/
173 $ADB push
"$TMPDIR/app_process" /system
/bin
/app_process
174 $ADB push
"$TMPDIR/app_process.real" /system
/bin
/app_process.real
175 $ADB push
"$TMPDIR/asanwrapper" /system
/bin
/asanwrapper
176 $ADB shell chown root.shell \
177 /system
/bin
/app_process \
178 /system
/bin
/app_process.real \
179 /system
/bin
/asanwrapper
180 $ADB shell
chmod 755 \
181 /system
/bin
/app_process \
182 /system
/bin
/app_process.real \
183 /system
/bin
/asanwrapper
185 echo '>> Restarting shell (asynchronous)'
189 echo '>> Please wait until the device restarts'
191 echo '>> Device is up to date'