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