xref: /llvm-project/compiler-rt/test/sanitizer_common/android_commands/android_run.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
1#!/usr/bin/env python
2
3import os, signal, sys, subprocess, tempfile
4from android_common import *
5
6ANDROID_TMPDIR = "/data/local/tmp/Output"
7
8device_binary = host_to_device_path(sys.argv[0])
9
10
11def build_env():
12    args = []
13    # Android linker ignores RPATH. Set LD_LIBRARY_PATH to Output dir.
14    args.append("LD_LIBRARY_PATH=%s" % (ANDROID_TMPDIR,))
15    for (key, value) in list(os.environ.items()):
16        if key in ["ASAN_ACTIVATION_OPTIONS", "SCUDO_OPTIONS"] or key.endswith(
17            "SAN_OPTIONS"
18        ):
19            args.append('%s="%s"' % (key, value.replace('"', '\\"')))
20    return " ".join(args)
21
22
23is_64bit = (
24    str(subprocess.check_output(["file", sys.argv[0] + ".real"])).find("64-bit") != -1
25)
26
27device_env = build_env()
28device_args = " ".join(sys.argv[1:])  # FIXME: escape?
29device_stdout = device_binary + ".stdout"
30device_stderr = device_binary + ".stderr"
31device_exitcode = device_binary + ".exitcode"
32ret = adb(
33    [
34        "shell",
35        "cd %s && %s %s %s >%s 2>%s ; echo $? >%s"
36        % (
37            ANDROID_TMPDIR,
38            device_env,
39            device_binary,
40            device_args,
41            device_stdout,
42            device_stderr,
43            device_exitcode,
44        ),
45    ]
46)
47if ret != 0:
48    sys.exit(ret)
49
50sys.stdout.write(pull_from_device(device_stdout))
51sys.stderr.write(pull_from_device(device_stderr))
52retcode = int(pull_from_device(device_exitcode))
53# If the device process died with a signal, do abort().
54# Not exactly the same, but good enough to fool "not --crash".
55if retcode > 128:
56    os.kill(os.getpid(), signal.SIGABRT)
57sys.exit(retcode)
58