summaryrefslogtreecommitdiff
path: root/test/sanitizer_common/android_commands
diff options
context:
space:
mode:
authorVitaly Buka <vitalybuka@google.com>2017-09-16 05:13:56 +0000
committerVitaly Buka <vitalybuka@google.com>2017-09-16 05:13:56 +0000
commit2e99c092c389eed3bdf31282f8f06f4265d8eab0 (patch)
tree21a8e3f6b296fa310d5bc9784a64ca8a87bb27a8 /test/sanitizer_common/android_commands
parent3c6b804641aa83d6a5307ca3ea6a73e75fc4ecce (diff)
[sanitizer] Move android_commoands from asan into sanitizer_common
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@313443 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/sanitizer_common/android_commands')
-rw-r--r--test/sanitizer_common/android_commands/android_common.py41
-rwxr-xr-xtest/sanitizer_common/android_commands/android_compile.py36
-rwxr-xr-xtest/sanitizer_common/android_commands/android_run.py40
3 files changed, 117 insertions, 0 deletions
diff --git a/test/sanitizer_common/android_commands/android_common.py b/test/sanitizer_common/android_commands/android_common.py
new file mode 100644
index 000000000..41994bb87
--- /dev/null
+++ b/test/sanitizer_common/android_commands/android_common.py
@@ -0,0 +1,41 @@
+import os, subprocess, tempfile
+import time
+
+ANDROID_TMPDIR = '/data/local/tmp/Output'
+ADB = os.environ.get('ADB', 'adb')
+
+verbose = False
+if os.environ.get('ANDROID_RUN_VERBOSE') == '1':
+ verbose = True
+
+def adb(args, attempts = 1):
+ if verbose:
+ print args
+ tmpname = tempfile.mktemp()
+ out = open(tmpname, 'w')
+ ret = 255
+ while attempts > 0 and ret != 0:
+ attempts -= 1
+ ret = subprocess.call([ADB] + args, stdout=out, stderr=subprocess.STDOUT)
+ if attempts != 0:
+ ret = 5
+ if ret != 0:
+ print "adb command failed", args
+ print tmpname
+ out.close()
+ out = open(tmpname, 'r')
+ print out.read()
+ out.close()
+ os.unlink(tmpname)
+ return ret
+
+def pull_from_device(path):
+ tmp = tempfile.mktemp()
+ adb(['pull', path, tmp], 5)
+ text = open(tmp, 'r').read()
+ os.unlink(tmp)
+ return text
+
+def push_to_device(path):
+ dst_path = os.path.join(ANDROID_TMPDIR, os.path.basename(path))
+ adb(['push', path, dst_path], 5)
diff --git a/test/sanitizer_common/android_commands/android_compile.py b/test/sanitizer_common/android_commands/android_compile.py
new file mode 100755
index 000000000..4b880886b
--- /dev/null
+++ b/test/sanitizer_common/android_commands/android_compile.py
@@ -0,0 +1,36 @@
+#!/usr/bin/python
+
+import os, sys, subprocess
+from android_common import *
+
+
+here = os.path.abspath(os.path.dirname(sys.argv[0]))
+android_run = os.path.join(here, 'android_run.py')
+
+output = None
+output_type = 'executable'
+
+args = sys.argv[1:]
+while args:
+ arg = args.pop(0)
+ if arg == '-shared':
+ output_type = 'shared'
+ elif arg == '-c':
+ output_type = 'object'
+ elif arg == '-o':
+ output = args.pop(0)
+
+if output == None:
+ print "No output file name!"
+ sys.exit(1)
+
+ret = subprocess.call(sys.argv[1:])
+if ret != 0:
+ sys.exit(ret)
+
+if output_type in ['executable', 'shared']:
+ push_to_device(output)
+
+if output_type == 'executable':
+ os.rename(output, output + '.real')
+ os.symlink(android_run, output)
diff --git a/test/sanitizer_common/android_commands/android_run.py b/test/sanitizer_common/android_commands/android_run.py
new file mode 100755
index 000000000..7e599453d
--- /dev/null
+++ b/test/sanitizer_common/android_commands/android_run.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python
+
+import os, signal, sys, subprocess, tempfile
+from android_common import *
+
+ANDROID_TMPDIR = '/data/local/tmp/Output'
+
+here = os.path.abspath(os.path.dirname(sys.argv[0]))
+device_binary = os.path.join(ANDROID_TMPDIR, os.path.basename(sys.argv[0]))
+
+def build_env():
+ args = []
+ # Android linker ignores RPATH. Set LD_LIBRARY_PATH to Output dir.
+ args.append('LD_LIBRARY_PATH=%s' % (ANDROID_TMPDIR,))
+ for (key, value) in os.environ.items():
+ if key in ['ASAN_OPTIONS', 'ASAN_ACTIVATION_OPTIONS']:
+ args.append('%s="%s"' % (key, value))
+ return ' '.join(args)
+
+is_64bit = (subprocess.check_output(['file', sys.argv[0] + '.real']).find('64-bit') != -1)
+
+device_env = build_env()
+device_args = ' '.join(sys.argv[1:]) # FIXME: escape?
+device_stdout = device_binary + '.stdout'
+device_stderr = device_binary + '.stderr'
+device_exitcode = device_binary + '.exitcode'
+ret = adb(['shell', 'cd %s && %s %s %s >%s 2>%s ; echo $? >%s' %
+ (ANDROID_TMPDIR, device_env, device_binary, device_args,
+ device_stdout, device_stderr, device_exitcode)])
+if ret != 0:
+ sys.exit(ret)
+
+sys.stdout.write(pull_from_device(device_stdout))
+sys.stderr.write(pull_from_device(device_stderr))
+retcode = int(pull_from_device(device_exitcode))
+# If the device process died with a signal, do abort().
+# Not exactly the same, but good enough to fool "not --crash".
+if retcode > 128:
+ os.kill(os.getpid(), signal.SIGABRT)
+sys.exit(retcode)