xref: /llvm-project/compiler-rt/test/sanitizer_common/ios_commands/iossim_run.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
1#!/usr/bin/env python3
2
3import glob, os, pipes, sys, subprocess
4
5
6device_id = os.environ.get("SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER")
7iossim_run_verbose = os.environ.get("SANITIZER_IOSSIM_RUN_VERBOSE")
8wait_for_debug = os.environ.get("SANITIZER_IOSSIM_RUN_WAIT_FOR_DEBUGGER")
9
10if not device_id:
11    raise EnvironmentError(
12        "Specify SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER to select which simulator to use."
13    )
14
15for e in [
16    "ASAN_OPTIONS",
17    "TSAN_OPTIONS",
18    "UBSAN_OPTIONS",
19    "LSAN_OPTIONS",
20    "APPLE_ASAN_INIT_FOR_DLOPEN",
21    "ASAN_ACTIVATION_OPTIONS",
22    "MallocNanoZone",
23]:
24    if e in os.environ:
25        os.environ["SIMCTL_CHILD_" + e] = os.environ[e]
26
27find_atos_cmd = "xcrun -sdk iphonesimulator -f atos"
28atos_path = (
29    subprocess.run(
30        find_atos_cmd.split(),
31        stdout=subprocess.PIPE,
32        stderr=subprocess.PIPE,
33        check=True,
34    )
35    .stdout.decode()
36    .strip()
37)
38for san in ["ASAN", "TSAN", "UBSAN", "LSAN"]:
39    os.environ[f"SIMCTL_CHILD_{san}_SYMBOLIZER_PATH"] = atos_path
40
41prog = sys.argv[1]
42exit_code = None
43if prog == "rm":
44    # The simulator and host actually share the same file system so we can just
45    # execute directly on the host.
46    rm_args = []
47    for arg in sys.argv[2:]:
48        if "*" in arg or "?" in arg:
49            # Don't quote glob pattern
50            rm_args.append(arg)
51        else:
52            # FIXME(dliew): pipes.quote() is deprecated
53            rm_args.append(pipes.quote(arg))
54    rm_cmd_line = ["/bin/rm"] + rm_args
55    rm_cmd_line_str = " ".join(rm_cmd_line)
56    # We use `shell=True` so that any wildcard globs get expanded by the shell.
57
58    if iossim_run_verbose:
59        print("RUNNING: \t{}".format(rm_cmd_line_str), flush=True)
60
61    exitcode = subprocess.call(rm_cmd_line_str, shell=True)
62
63else:
64    cmd = ["xcrun", "simctl", "spawn", "--standalone"]
65
66    if wait_for_debug:
67        cmd.append("--wait-for-debugger")
68
69    cmd.append(device_id)
70    cmd += sys.argv[1:]
71
72    if iossim_run_verbose:
73        print("RUNNING: \t{}".format(" ".join(cmd)), flush=True)
74
75    exitcode = subprocess.call(cmd)
76if exitcode > 125:
77    exitcode = 126
78sys.exit(exitcode)
79