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