1""" This module contains functions used by the test cases to hide the
2architecture and/or the platform dependent nature of the tests. """
3
4from __future__ import absolute_import
5
6# System modules
7import itertools
8import re
9import subprocess
10import sys
11import os
12
13# Third-party modules
14import six
15from six.moves.urllib import parse as urlparse
16
17# LLDB modules
18from . import configuration
19import lldb
20import lldbsuite.test.lldbplatform as lldbplatform
21
22
23def check_first_register_readable(test_case):
24    arch = test_case.getArchitecture()
25
26    if arch in ['x86_64', 'i386']:
27        test_case.expect("register read eax", substrs=['eax = 0x'])
28    elif arch in ['arm', 'armv7', 'armv7k', 'armv8l', 'armv7l']:
29        test_case.expect("register read r0", substrs=['r0 = 0x'])
30    elif arch in ['aarch64', 'arm64', 'arm64e', 'arm64_32']:
31        test_case.expect("register read x0", substrs=['x0 = 0x'])
32    elif re.match("mips", arch):
33        test_case.expect("register read zero", substrs=['zero = 0x'])
34    elif arch in ['s390x']:
35        test_case.expect("register read r0", substrs=['r0 = 0x'])
36    elif arch in ['powerpc64le']:
37        test_case.expect("register read r0", substrs=['r0 = 0x'])
38    else:
39        # TODO: Add check for other architectures
40        test_case.fail(
41            "Unsupported architecture for test case (arch: %s)" %
42            test_case.getArchitecture())
43
44
45def _run_adb_command(cmd, device_id):
46    device_id_args = []
47    if device_id:
48        device_id_args = ["-s", device_id]
49    full_cmd = ["adb"] + device_id_args + cmd
50    p = subprocess.Popen(
51        full_cmd,
52        stdout=subprocess.PIPE,
53        stderr=subprocess.PIPE)
54    stdout, stderr = p.communicate()
55    return p.returncode, stdout, stderr
56
57
58def target_is_android():
59    if not hasattr(target_is_android, 'result'):
60        triple = lldb.DBG.GetSelectedPlatform().GetTriple()
61        match = re.match(".*-.*-.*-android", triple)
62        target_is_android.result = match is not None
63    return target_is_android.result
64
65
66def android_device_api():
67    if not hasattr(android_device_api, 'result'):
68        assert configuration.lldb_platform_url is not None
69        device_id = None
70        parsed_url = urlparse.urlparse(configuration.lldb_platform_url)
71        host_name = parsed_url.netloc.split(":")[0]
72        if host_name != 'localhost':
73            device_id = host_name
74            if device_id.startswith('[') and device_id.endswith(']'):
75                device_id = device_id[1:-1]
76        retcode, stdout, stderr = _run_adb_command(
77            ["shell", "getprop", "ro.build.version.sdk"], device_id)
78        if retcode == 0:
79            android_device_api.result = int(stdout)
80        else:
81            raise LookupError(
82                ">>> Unable to determine the API level of the Android device.\n"
83                ">>> stdout:\n%s\n"
84                ">>> stderr:\n%s\n" %
85                (stdout, stderr))
86    return android_device_api.result
87
88
89def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):
90    if not target_is_android():
91        return False
92    if valid_archs is not None and device_arch not in valid_archs:
93        return False
94    if valid_api_levels is not None and android_device_api() not in valid_api_levels:
95        return False
96
97    return True
98
99
100def finalize_build_dictionary(dictionary):
101    if target_is_android():
102        if dictionary is None:
103            dictionary = {}
104        dictionary["OS"] = "Android"
105        dictionary["PIE"] = 1
106    return dictionary
107
108
109def getHostPlatform():
110    """Returns the host platform running the test suite."""
111    # Attempts to return a platform name matching a target Triple platform.
112    if sys.platform.startswith('linux'):
113        return 'linux'
114    elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
115        return 'windows'
116    elif sys.platform.startswith('darwin'):
117        return 'darwin'
118    elif sys.platform.startswith('freebsd'):
119        return 'freebsd'
120    elif sys.platform.startswith('netbsd'):
121        return 'netbsd'
122    else:
123        return sys.platform
124
125
126def getDarwinOSTriples():
127    return ['darwin', 'macosx', 'ios', 'watchos', 'tvos', 'bridgeos']
128
129
130def getPlatform():
131    """Returns the target platform which the tests are running on."""
132    triple = lldb.DBG.GetSelectedPlatform().GetTriple()
133    if triple is None:
134      # It might be an unconnected remote platform.
135      return ''
136
137    platform = triple.split('-')[2]
138    if platform.startswith('freebsd'):
139        platform = 'freebsd'
140    elif platform.startswith('netbsd'):
141        platform = 'netbsd'
142    return platform
143
144
145def platformIsDarwin():
146    """Returns true if the OS triple for the selected platform is any valid apple OS"""
147    return getPlatform() in getDarwinOSTriples()
148
149
150def findMainThreadCheckerDylib():
151    if not platformIsDarwin():
152        return ""
153
154    if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded):
155        return "/Developer/usr/lib/libMainThreadChecker.dylib"
156
157    with os.popen('xcode-select -p') as output:
158        xcode_developer_path = output.read().strip()
159        mtc_dylib_path = '%s/usr/lib/libMainThreadChecker.dylib' % xcode_developer_path
160        if os.path.isfile(mtc_dylib_path):
161            return mtc_dylib_path
162
163    return ""
164
165
166class _PlatformContext(object):
167    """Value object class which contains platform-specific options."""
168
169    def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension):
170        self.shlib_environment_var = shlib_environment_var
171        self.shlib_prefix = shlib_prefix
172        self.shlib_extension = shlib_extension
173
174
175def createPlatformContext():
176    if platformIsDarwin():
177        return _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib')
178    elif getPlatform() in ("freebsd", "linux", "netbsd"):
179        return _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so')
180    else:
181        return None
182
183
184def hasChattyStderr(test_case):
185    """Some targets produce garbage on the standard error output. This utility function
186    determines whether the tests can be strict about the expected stderr contents."""
187    if match_android_device(test_case.getArchitecture(), ['aarch64'], range(22, 25+1)):
188        return True  # The dynamic linker on the device will complain about unknown DT entries
189    return False
190