aboutsummaryrefslogtreecommitdiff
path: root/contrib/compiler-rt/lib/asan/scripts/asan_device_setup
blob: 104e07b722caa61c0192f4f0140327c5fb75ed61 (plain) (blame)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
#===- lib/asan/scripts/asan_device_setup -----------------------------------===#
#
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
# Prepare Android device to run ASan applications.
#
#===------------------------------------------------------------------------===#

set -e

HERE="$(cd "$(dirname "$0")" && pwd)"

revert=no
extra_options=
device=
lib=
use_su=0

function usage {
    echo "usage: $0 [--revert] [--device device-id] [--lib path] [--extra-options options]"
    echo "  --revert: Uninstall ASan from the device."
    echo "  --lib: Path to ASan runtime library."
    echo "  --extra-options: Extra ASAN_OPTIONS."
    echo "  --device: Install to the given device. Use 'adb devices' to find"
    echo "            device-id."
    echo "  --use-su: Use 'su -c' prefix for every adb command instead of using"
    echo "            'adb root' once."
    echo
    exit 1
}

function adb_push {
  if [ $use_su -eq 0 ]; then
    $ADB push "$1" "$2"
  else
    local FILENAME=$(basename $1)
    $ADB push "$1" "/data/local/tmp/$FILENAME"
    $ADB shell su -c "rm \\\"$2/$FILENAME\\\"" >&/dev/null
    $ADB shell su -c "cat \\\"/data/local/tmp/$FILENAME\\\" > \\\"$2/$FILENAME\\\""
    $ADB shell su -c "rm \\\"/data/local/tmp/$FILENAME\\\""
  fi
}

function adb_remount {
  if [ $use_su -eq 0 ]; then
    $ADB remount
  else
    local STORAGE=`$ADB shell mount | grep /system | cut -d ' ' -f1`
    if [ "$STORAGE" != "" ]; then
      echo Remounting $STORAGE at /system
      $ADB shell su -c "mount -o remount,rw $STORAGE /system"
    else
      echo Failed to get storage device name for "/system" mount point
    fi
  fi
}

function adb_shell {
  if [ $use_su -eq 0 ]; then
    $ADB shell $@
  else
    $ADB shell su -c "$*"
  fi
}

function adb_root {
  if [ $use_su -eq 0 ]; then
    $ADB root
  fi
}

function adb_wait_for_device {
  $ADB wait-for-device
}

function adb_pull {
  if [ $use_su -eq 0 ]; then
    $ADB pull "$1" "$2"
  else
    local FILENAME=$(basename $1)
    $ADB shell rm "/data/local/tmp/$FILENAME" >&/dev/null
    $ADB shell su -c "[ -f \\\"$1\\\" ] && cat \\\"$1\\\" > \\\"/data/local/tmp/$FILENAME\\\" && chown root.shell \\\"/data/local/tmp/$FILENAME\\\" && chmod 755 \\\"/data/local/tmp/$FILENAME\\\"" &&
    $ADB pull "/data/local/tmp/$FILENAME" "$2" >&/dev/null && $ADB shell "rm \"/data/local/tmp/$FILENAME\""
  fi
}

function get_device_arch { # OUTVAR
    local _outvar=$1
    local _ABI=$(adb_shell getprop ro.product.cpu.abi)
    local _ARCH=
    if [[ $_ABI == x86* ]]; then
        _ARCH=i686
    elif [[ $_ABI == armeabi* ]]; then
        _ARCH=arm
    else
        echo "Unrecognized device ABI: $_ABI"
        exit 1
    fi
    eval $_outvar=\$_ARCH
}

while [[ $# > 0 ]]; do
  case $1 in
    --revert)
      revert=yes
      ;;
    --extra-options)
      shift
      if [[ $# == 0 ]]; then
        echo "--extra-options requires an argument."
        exit 1
      fi
      extra_options="$1"
      ;;
    --lib)
      shift
      if [[ $# == 0 ]]; then
        echo "--lib requires an argument."
        exit 1
      fi
      lib="$1"
      ;;
    --device)
      shift
      if [[ $# == 0 ]]; then
        echo "--device requires an argument."
        exit 1
      fi
      device="$1"
      ;;
    --use-su)
      use_su=1
      ;;
    *)
      usage
      ;;
  esac
  shift
done

ADB=${ADB:-adb}
if [[ x$device != x ]]; then
    ADB="$ADB -s $device"
fi

if [ $use_su -eq 1 ]; then
  # Test if 'su' is present on the device
  SU_TEST_OUT=`$ADB shell su -c "echo foo" 2>&1 | sed 's/\r$//'`
  if [ $? != 0 -o "$SU_TEST_OUT" != "foo" ]; then
    echo "ERROR: Cannot use 'su -c':"
    echo "$ adb shell su -c \"echo foo\""
    echo $SU_TEST_OUT
    echo "Check that 'su' binary is correctly installed on the device or omit"
    echo "            --use-su flag"
    exit 1
  fi
fi

echo '>> Remounting /system rw'
adb_wait_for_device
adb_root
adb_wait_for_device
adb_remount
adb_wait_for_device

get_device_arch ARCH
echo "Target architecture: $ARCH"
ASAN_RT="libclang_rt.asan-$ARCH-android.so"

if [[ x$revert == xyes ]]; then
    echo '>> Uninstalling ASan'

    if ! adb_shell ls -l /system/bin/app_process | grep -o '\->.*app_process' >&/dev/null; then
        echo '>> Pre-L device detected.'
        adb_shell mv /system/bin/app_process.real /system/bin/app_process
        adb_shell rm /system/bin/asanwrapper
    else
        adb_shell rm /system/bin/app_process.wrap
        adb_shell rm /system/bin/asanwrapper
        adb_shell rm /system/bin/app_process
        adb_shell ln -s /system/bin/app_process32 /system/bin/app_process
    fi

    echo '>> Restarting shell'
    adb_shell stop
    adb_shell start

    # Remove the library on the last step to give a chance to the 'su' binary to
    # be executed without problem.
    adb_shell rm /system/lib/$ASAN_RT

    echo '>> Done'
    exit 0
fi

if [[ -d "$lib" ]]; then
    ASAN_RT_PATH="$lib"
elif [[ -f "$lib" && "$lib" == *"$ASAN_RT" ]]; then
    ASAN_RT_PATH=$(dirname "$lib")
elif [[ -f "$HERE/$ASAN_RT" ]]; then
    ASAN_RT_PATH="$HERE"
elif [[ $(basename "$HERE") == "bin" ]]; then
    # We could be in the toolchain's base directory.
    # Consider ../lib, ../lib/asan, ../lib/linux and ../lib/clang/$VERSION/lib/linux.
    P=$(ls "$HERE"/../lib/"$ASAN_RT" "$HERE"/../lib/asan/"$ASAN_RT" "$HERE"/../lib/linux/"$ASAN_RT" "$HERE"/../lib/clang/*/lib/linux/"$ASAN_RT" 2>/dev/null | sort | tail -1)
    if [[ -n "$P" ]]; then
        ASAN_RT_PATH="$(dirname "$P")"
    fi
fi

if [[ -z "$ASAN_RT_PATH" || ! -f "$ASAN_RT_PATH/$ASAN_RT" ]]; then
    echo ">> ASan runtime library not found"
    exit 1
fi

TMPDIRBASE=$(mktemp -d)
TMPDIROLD="$TMPDIRBASE/old"
TMPDIR="$TMPDIRBASE/new"
mkdir "$TMPDIROLD"

RELEASE=$(adb_shell getprop ro.build.version.release)
PRE_L=0
if echo "$RELEASE" | grep '^4\.' >&/dev/null; then
    PRE_L=1
fi

if ! adb_shell ls -l /system/bin/app_process | grep -o '\->.*app_process' >&/dev/null; then

    if adb_pull /system/bin/app_process.real /dev/null >&/dev/null; then
        echo '>> Old-style ASan installation detected. Reverting.'
        adb_shell mv /system/bin/app_process.real /system/bin/app_process
    fi

    echo '>> Pre-L device detected. Setting up app_process symlink.'
    adb_shell mv /system/bin/app_process /system/bin/app_process32
    adb_shell ln -s /system/bin/app_process32 /system/bin/app_process
fi

echo '>> Copying files from the device'
adb_pull /system/bin/app_process.wrap "$TMPDIROLD" || true
adb_pull /system/bin/asanwrapper "$TMPDIROLD" || true
adb_pull /system/lib/"$ASAN_RT" "$TMPDIROLD" || true
cp -r "$TMPDIROLD" "$TMPDIR"

if [[ -f "$TMPDIR/app_process.wrap" ]]; then
    echo ">> Previous installation detected"
else
    echo ">> New installation"
fi

echo '>> Generating wrappers'

cp "$ASAN_RT_PATH/$ASAN_RT" "$TMPDIR/"

# FIXME: alloc_dealloc_mismatch=0 prevents a failure in libdvm startup,
# which may or may not be a real bug (probably not).
ASAN_OPTIONS=start_deactivated=1,alloc_dealloc_mismatch=0

# On Android-L not allowing user segv handler breaks some applications.
if [[ PRE_L -eq 0 ]]; then
    ASAN_OPTIONS="$ASAN_OPTIONS,allow_user_segv_handler=1"
fi

if [[ x$extra_options != x ]] ; then
    ASAN_OPTIONS="$ASAN_OPTIONS,$extra_options"
fi

# Zygote wrapper.
cat <<EOF >"$TMPDIR/app_process.wrap"
#!/system/bin/sh-from-zygote
ASAN_OPTIONS=$ASAN_OPTIONS \\
LD_PRELOAD=\$LD_PRELOAD:$ASAN_RT \\
exec /system/bin/app_process32 \$@

EOF

# General command-line tool wrapper (use for anything that's not started as
# zygote).
cat <<EOF >"$TMPDIR/asanwrapper"
#!/system/bin/sh
LD_PRELOAD=$ASAN_RT \\
exec \$@

EOF

if ! ( cd "$TMPDIRBASE" && diff -qr old/ new/ ) ; then
    echo '>> Pushing files to the device'
    adb_push "$TMPDIR/$ASAN_RT" /system/lib/
    adb_push "$TMPDIR/app_process.wrap" /system/bin
    adb_push "$TMPDIR/asanwrapper" /system/bin

    adb_shell rm /system/bin/app_process
    adb_shell ln -s /system/bin/app_process.wrap /system/bin/app_process

    adb_shell chown root.shell \
        /system/lib/"$ASAN_RT" \
        /system/bin/app_process.wrap \
        /system/bin/asanwrapper
    adb_shell chmod 644 \
        /system/lib/"$ASAN_RT"
    adb_shell chmod 755 \
        /system/bin/app_process.wrap \
        /system/bin/asanwrapper

    # Make SELinux happy by keeping app_process wrapper and the shell
    # it runs on in zygote domain.
    ENFORCING=0
    if adb_shell getenforce | grep Enforcing >/dev/null; then
        # Sometimes shell is not allowed to change file contexts.
        # Temporarily switch to permissive.
        ENFORCING=1
        adb_shell setenforce 0
    fi

    adb_shell cp /system/bin/sh /system/bin/sh-from-zygote

    if [[ PRE_L -eq 1 ]]; then
        CTX=u:object_r:system_file:s0
    else
        CTX=u:object_r:zygote_exec:s0
    fi
    adb_shell chcon $CTX \
        /system/bin/sh-from-zygote \
        /system/bin/app_process.wrap \
        /system/bin/app_process32

    if [ $ENFORCING == 1 ]; then
        adb_shell setenforce 1
    fi

    echo '>> Restarting shell (asynchronous)'
    adb_shell stop
    adb_shell start

    echo '>> Please wait until the device restarts'
else
    echo '>> Device is up to date'
fi

rm -r "$TMPDIRBASE"