1""" This module contains functions used by the test cases to hide the 2architecture and/or the platform dependent nature of the tests. """ 3 4# System modules 5import itertools 6import re 7import subprocess 8import sys 9import os 10from distutils.version import LooseVersion 11from urllib.parse import urlparse 12 13# LLDB modules 14import lldb 15from . import configuration 16from . import lldbtest_config 17import lldbsuite.test.lldbplatform as lldbplatform 18from lldbsuite.test.builders import get_builder 19 20 21def check_first_register_readable(test_case): 22 arch = test_case.getArchitecture() 23 24 if arch in ["x86_64", "i386"]: 25 test_case.expect("register read eax", substrs=["eax = 0x"]) 26 elif arch in ["arm", "armv7", "armv7k", "armv8l", "armv7l"]: 27 test_case.expect("register read r0", substrs=["r0 = 0x"]) 28 elif arch in ["aarch64", "arm64", "arm64e", "arm64_32"]: 29 test_case.expect("register read x0", substrs=["x0 = 0x"]) 30 elif re.match("mips", arch): 31 test_case.expect("register read zero", substrs=["zero = 0x"]) 32 elif arch in ["s390x"]: 33 test_case.expect("register read r0", substrs=["r0 = 0x"]) 34 elif arch in ["powerpc64le"]: 35 test_case.expect("register read r0", substrs=["r0 = 0x"]) 36 else: 37 # TODO: Add check for other architectures 38 test_case.fail( 39 "Unsupported architecture for test case (arch: %s)" 40 % test_case.getArchitecture() 41 ) 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(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 50 stdout, stderr = p.communicate() 51 return p.returncode, stdout, stderr 52 53 54def target_is_android(): 55 return configuration.lldb_platform_name == "remote-android" 56 57 58def android_device_api(): 59 if not hasattr(android_device_api, "result"): 60 assert configuration.lldb_platform_url is not None 61 device_id = None 62 parsed_url = urlparse(configuration.lldb_platform_url) 63 host_name = parsed_url.netloc.split(":")[0] 64 if host_name != "localhost": 65 device_id = host_name 66 if device_id.startswith("[") and device_id.endswith("]"): 67 device_id = device_id[1:-1] 68 retcode, stdout, stderr = _run_adb_command( 69 ["shell", "getprop", "ro.build.version.sdk"], device_id 70 ) 71 if retcode == 0: 72 android_device_api.result = int(stdout) 73 else: 74 raise LookupError( 75 ">>> Unable to determine the API level of the Android device.\n" 76 ">>> stdout:\n%s\n" 77 ">>> stderr:\n%s\n" % (stdout, stderr) 78 ) 79 return android_device_api.result 80 81 82def match_android_device(device_arch, valid_archs=None, valid_api_levels=None): 83 if not target_is_android(): 84 return False 85 if valid_archs is not None and device_arch not in valid_archs: 86 return False 87 if valid_api_levels is not None and android_device_api() not in valid_api_levels: 88 return False 89 90 return True 91 92 93def finalize_build_dictionary(dictionary): 94 if target_is_android(): 95 if dictionary is None: 96 dictionary = {} 97 dictionary["OS"] = "Android" 98 dictionary["PIE"] = 1 99 return dictionary 100 101 102def _get_platform_os(p): 103 # Use the triple to determine the platform if set. 104 triple = p.GetTriple() 105 if triple: 106 platform = triple.split("-")[2] 107 if platform.startswith("freebsd"): 108 platform = "freebsd" 109 elif platform.startswith("netbsd"): 110 platform = "netbsd" 111 return platform 112 113 return "" 114 115 116def getHostPlatform(): 117 """Returns the host platform running the test suite.""" 118 return _get_platform_os(lldb.SBPlatform("host")) 119 120 121def getDarwinOSTriples(): 122 return lldbplatform.translate(lldbplatform.darwin_all) 123 124 125def getPlatform(): 126 """Returns the target platform which the tests are running on.""" 127 # Use the Apple SDK to determine the platform if set. 128 if configuration.apple_sdk: 129 platform = configuration.apple_sdk 130 dot = platform.find(".") 131 if dot != -1: 132 platform = platform[:dot] 133 if platform == "iphoneos": 134 platform = "ios" 135 return platform 136 137 return _get_platform_os(lldb.selected_platform) 138 139 140def platformIsDarwin(): 141 """Returns true if the OS triple for the selected platform is any valid apple OS""" 142 return getPlatform() in getDarwinOSTriples() 143 144 145def findMainThreadCheckerDylib(): 146 if not platformIsDarwin(): 147 return "" 148 149 if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded): 150 return "/Developer/usr/lib/libMainThreadChecker.dylib" 151 152 with os.popen("xcode-select -p") as output: 153 xcode_developer_path = output.read().strip() 154 mtc_dylib_path = "%s/usr/lib/libMainThreadChecker.dylib" % xcode_developer_path 155 if os.path.isfile(mtc_dylib_path): 156 return mtc_dylib_path 157 158 return "" 159 160 161class _PlatformContext(object): 162 """Value object class which contains platform-specific options.""" 163 164 def __init__( 165 self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension 166 ): 167 self.shlib_environment_var = shlib_environment_var 168 self.shlib_path_separator = shlib_path_separator 169 self.shlib_prefix = shlib_prefix 170 self.shlib_extension = shlib_extension 171 172 173def createPlatformContext(): 174 if platformIsDarwin(): 175 return _PlatformContext("DYLD_LIBRARY_PATH", ":", "lib", "dylib") 176 elif getPlatform() in ("freebsd", "linux", "netbsd"): 177 return _PlatformContext("LD_LIBRARY_PATH", ":", "lib", "so") 178 else: 179 return _PlatformContext("PATH", ";", "", "dll") 180 181 182def hasChattyStderr(test_case): 183 """Some targets produce garbage on the standard error output. This utility function 184 determines whether the tests can be strict about the expected stderr contents.""" 185 if match_android_device( 186 test_case.getArchitecture(), ["aarch64"], range(22, 25 + 1) 187 ): 188 return True # The dynamic linker on the device will complain about unknown DT entries 189 return False 190 191 192def builder_module(): 193 return get_builder(sys.platform) 194 195 196def getArchitecture(): 197 """Returns the architecture in effect the test suite is running with.""" 198 module = builder_module() 199 arch = module.getArchitecture() 200 if arch == "amd64": 201 arch = "x86_64" 202 if arch in ["armv7l", "armv8l"]: 203 arch = "arm" 204 return arch 205 206 207lldbArchitecture = None 208 209 210def getLLDBArchitecture(): 211 """Returns the architecture of the lldb binary.""" 212 global lldbArchitecture 213 if not lldbArchitecture: 214 # These two target settings prevent lldb from doing setup that does 215 # nothing but slow down the end goal of printing the architecture. 216 command = [ 217 lldbtest_config.lldbExec, 218 "-x", 219 "-b", 220 "-o", 221 "settings set target.preload-symbols false", 222 "-o", 223 "settings set target.load-script-from-symbol-file false", 224 "-o", 225 "file " + lldbtest_config.lldbExec, 226 ] 227 228 output = subprocess.check_output(command) 229 str = output.decode() 230 231 for line in str.splitlines(): 232 m = re.search(r"Current executable set to '.*' \((.*)\)\.", line) 233 if m: 234 lldbArchitecture = m.group(1) 235 break 236 237 return lldbArchitecture 238 239 240def getCompiler(): 241 """Returns the compiler in effect the test suite is running with.""" 242 module = builder_module() 243 return module.getCompiler() 244 245 246def getCompilerBinary(): 247 """Returns the compiler binary the test suite is running with.""" 248 return getCompiler().split()[0] 249 250 251def getCompilerVersion(): 252 """Returns a string that represents the compiler version. 253 Supports: llvm, clang. 254 """ 255 compiler = getCompilerBinary() 256 version_output = subprocess.check_output([compiler, "--version"], errors="replace") 257 m = re.search("version ([0-9.]+)", version_output) 258 if m: 259 return m.group(1) 260 return "unknown" 261 262 263def getDwarfVersion(): 264 """Returns the dwarf version generated by clang or '0'.""" 265 if configuration.dwarf_version: 266 return str(configuration.dwarf_version) 267 if "clang" in getCompiler(): 268 try: 269 triple = builder_module().getTriple(getArchitecture()) 270 target = ["-target", triple] if triple else [] 271 driver_output = subprocess.check_output( 272 [getCompiler()] + target + "-g -c -x c - -o - -###".split(), 273 stderr=subprocess.STDOUT, 274 ) 275 driver_output = driver_output.decode("utf-8") 276 for line in driver_output.split(os.linesep): 277 m = re.search("dwarf-version=([0-9])", line) 278 if m: 279 return m.group(1) 280 except subprocess.CalledProcessError: 281 pass 282 return "0" 283 284 285def expectedCompilerVersion(compiler_version): 286 """Returns True iff compiler_version[1] matches the current compiler version. 287 Use compiler_version[0] to specify the operator used to determine if a match has occurred. 288 Any operator other than the following defaults to an equality test: 289 '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not' 290 291 If the current compiler version cannot be determined, we assume it is close to the top 292 of trunk, so any less-than or equal-to comparisons will return False, and any 293 greater-than or not-equal-to comparisons will return True. 294 """ 295 if compiler_version is None: 296 return True 297 operator = str(compiler_version[0]) 298 version = compiler_version[1] 299 300 if version is None: 301 return True 302 303 test_compiler_version = getCompilerVersion() 304 if test_compiler_version == "unknown": 305 # Assume the compiler version is at or near the top of trunk. 306 return operator in [">", ">=", "!", "!=", "not"] 307 308 if operator == ">": 309 return LooseVersion(test_compiler_version) > LooseVersion(version) 310 if operator == ">=" or operator == "=>": 311 return LooseVersion(test_compiler_version) >= LooseVersion(version) 312 if operator == "<": 313 return LooseVersion(test_compiler_version) < LooseVersion(version) 314 if operator == "<=" or operator == "=<": 315 return LooseVersion(test_compiler_version) <= LooseVersion(version) 316 if operator == "!=" or operator == "!" or operator == "not": 317 return str(version) not in str(test_compiler_version) 318 return str(version) in str(test_compiler_version) 319 320 321def expectedCompiler(compilers): 322 """Returns True iff any element of compilers is a sub-string of the current compiler.""" 323 if compilers is None: 324 return True 325 326 for compiler in compilers: 327 if compiler in getCompiler(): 328 return True 329 330 return False 331