399 lines · python
1""" This module contains functions used by the test cases to hide the2architecture and/or the platform dependent nature of the tests. """3 4# System modules5import itertools6import json7import re8import subprocess9import sys10import os11from packaging import version12from urllib.parse import urlparse13 14# LLDB modules15import lldb16from . import configuration17from . import lldbtest_config18import lldbsuite.test.lldbplatform as lldbplatform19from lldbsuite.test.builders import get_builder20from lldbsuite.test.lldbutil import is_exe21 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 elif arch in ["riscv64", "riscv32"]:39 test_case.expect("register read zero", substrs=["zero = 0x"])40 else:41 # TODO: Add check for other architectures42 test_case.fail(43 "Unsupported architecture for test case (arch: %s)"44 % test_case.getArchitecture()45 )46 47 48def _run_adb_command(cmd, device_id):49 device_id_args = []50 if device_id:51 device_id_args = ["-s", device_id]52 full_cmd = ["adb"] + device_id_args + cmd53 p = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)54 stdout, stderr = p.communicate()55 return p.returncode, stdout, stderr56 57 58def target_is_android():59 return configuration.lldb_platform_name == "remote-android"60 61 62def android_device_api():63 if not hasattr(android_device_api, "result"):64 assert configuration.lldb_platform_url is not None65 device_id = None66 parsed_url = urlparse(configuration.lldb_platform_url)67 host_name = parsed_url.netloc.split(":")[0]68 if host_name != "localhost":69 device_id = host_name70 if device_id.startswith("[") and device_id.endswith("]"):71 device_id = device_id[1:-1]72 retcode, stdout, stderr = _run_adb_command(73 ["shell", "getprop", "ro.build.version.sdk"], device_id74 )75 if retcode == 0:76 android_device_api.result = int(stdout)77 else:78 raise LookupError(79 ">>> Unable to determine the API level of the Android device.\n"80 ">>> stdout:\n%s\n"81 ">>> stderr:\n%s\n" % (stdout, stderr)82 )83 return android_device_api.result84 85 86def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):87 if not target_is_android():88 return False89 if valid_archs is not None and device_arch not in valid_archs:90 return False91 if valid_api_levels is not None and android_device_api() not in valid_api_levels:92 return False93 94 return True95 96 97def finalize_build_dictionary(dictionary):98 # Provide uname-like platform name99 platform_name_to_uname = {100 "linux": "Linux",101 "netbsd": "NetBSD",102 "freebsd": "FreeBSD",103 "windows": "Windows_NT",104 "macosx": "Darwin",105 "darwin": "Darwin",106 }107 108 if dictionary is None:109 dictionary = {}110 if target_is_android():111 dictionary["OS"] = "Android"112 dictionary["PIE"] = 1113 elif platformIsDarwin():114 dictionary["OS"] = "Darwin"115 else:116 dictionary["OS"] = platform_name_to_uname[getPlatform()]117 118 dictionary["HOST_OS"] = platform_name_to_uname[getHostPlatform()]119 120 return dictionary121 122 123def _get_platform_os(p):124 # Use the triple to determine the platform if set.125 triple = p.GetTriple()126 if triple:127 platform = triple.split("-")[2]128 if platform.startswith("freebsd"):129 platform = "freebsd"130 elif platform.startswith("netbsd"):131 platform = "netbsd"132 elif platform.startswith("openbsd"):133 platform = "openbsd"134 return platform135 136 return ""137 138 139def getHostPlatform():140 """Returns the host platform running the test suite."""141 return _get_platform_os(lldb.SBPlatform("host"))142 143 144def getDarwinOSTriples():145 return lldbplatform.translate(lldbplatform.darwin_all)146 147 148def getPlatform():149 """Returns the target platform which the tests are running on."""150 # Use the Apple SDK to determine the platform if set.151 if configuration.apple_sdk:152 platform = configuration.apple_sdk153 dot = platform.find(".")154 if dot != -1:155 platform = platform[:dot]156 if platform == "iphoneos":157 platform = "ios"158 return platform159 160 return _get_platform_os(lldb.selected_platform)161 162 163def platformIsDarwin():164 """Returns true if the OS triple for the selected platform is any valid apple OS"""165 return getPlatform() in getDarwinOSTriples()166 167 168def findMainThreadCheckerDylib():169 if not platformIsDarwin():170 return ""171 172 if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded):173 return "/Developer/usr/lib/libMainThreadChecker.dylib"174 175 with os.popen("xcode-select -p") as output:176 xcode_developer_path = output.read().strip()177 mtc_dylib_path = "%s/usr/lib/libMainThreadChecker.dylib" % xcode_developer_path178 if os.path.isfile(mtc_dylib_path):179 return mtc_dylib_path180 181 return ""182 183 184def findBacktraceRecordingDylib():185 if not platformIsDarwin():186 return ""187 188 if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded):189 return "/Developer/usr/lib/libBacktraceRecording.dylib"190 191 with os.popen("xcode-select -p") as output:192 xcode_developer_path = output.read().strip()193 mtc_dylib_path = "%s/usr/lib/libBacktraceRecording.dylib" % xcode_developer_path194 if os.path.isfile(mtc_dylib_path):195 return mtc_dylib_path196 197 return ""198 199 200class _PlatformContext(object):201 """Value object class which contains platform-specific options."""202 203 def __init__(204 self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension205 ):206 self.shlib_environment_var = shlib_environment_var207 self.shlib_path_separator = shlib_path_separator208 self.shlib_prefix = shlib_prefix209 self.shlib_extension = shlib_extension210 211 212def createPlatformContext():213 if platformIsDarwin():214 return _PlatformContext("DYLD_LIBRARY_PATH", ":", "lib", "dylib")215 elif getPlatform() in ("linux", "freebsd", "netbsd", "openbsd"):216 return _PlatformContext("LD_LIBRARY_PATH", ":", "lib", "so")217 else:218 return _PlatformContext("PATH", ";", "", "dll")219 220 221def hasChattyStderr(test_case):222 """Some targets produce garbage on the standard error output. This utility function223 determines whether the tests can be strict about the expected stderr contents."""224 if match_android_device(225 test_case.getArchitecture(), ["aarch64"], range(22, 25 + 1)226 ):227 return True # The dynamic linker on the device will complain about unknown DT entries228 return False229 230 231def builder_module():232 """Return the builder for the target platform."""233 return get_builder(getPlatform())234 235 236def getArchitecture():237 """Returns the architecture in effect the test suite is running with."""238 module = builder_module()239 arch = module.getArchitecture()240 if arch == "amd64":241 arch = "x86_64"242 if arch in ["armv7l", "armv8l"]:243 arch = "arm"244 if re.match("rv64*", arch):245 arch = "riscv64"246 if re.match("rv32*", arch):247 arch = "riscv32"248 return arch249 250 251lldbArchitecture = None252 253 254def getLLDBArchitecture():255 """Returns the architecture of the lldb binary."""256 global lldbArchitecture257 if not lldbArchitecture:258 # These two target settings prevent lldb from doing setup that does259 # nothing but slow down the end goal of printing the architecture.260 command = [261 lldbtest_config.lldbExec,262 "-x",263 "-b",264 "-o",265 "settings set target.preload-symbols false",266 "-o",267 "settings set target.load-script-from-symbol-file false",268 "-o",269 "file " + lldbtest_config.lldbExec,270 ]271 272 output = subprocess.check_output(command)273 str = output.decode()274 275 for line in str.splitlines():276 m = re.search(r"Current executable set to '.*' \((.*)\)\.", line)277 if m:278 lldbArchitecture = m.group(1)279 break280 281 return lldbArchitecture282 283 284def getCompiler():285 """Returns the compiler in effect the test suite is running with."""286 module = builder_module()287 return module.getCompiler()288 289 290def getCompilerVersion():291 """Returns a string that represents the compiler version.292 Supports: llvm, clang.293 """294 version_output = subprocess.check_output(295 [getCompiler(), "--version"], errors="replace"296 )297 m = re.search("version ([0-9.]+)", version_output)298 if m:299 return m.group(1)300 return "unknown"301 302 303def getDwarfVersion():304 """Returns the dwarf version generated by clang or '0'."""305 if configuration.dwarf_version:306 return str(configuration.dwarf_version)307 if "clang" in getCompiler():308 try:309 triple = builder_module().getTriple(getArchitecture())310 target = ["-target", triple] if triple else []311 driver_output = subprocess.check_output(312 [getCompiler()] + target + "-g -c -x c - -o - -###".split(),313 stderr=subprocess.STDOUT,314 )315 driver_output = driver_output.decode("utf-8")316 for line in driver_output.split(os.linesep):317 m = re.search("dwarf-version=([0-9])", line)318 if m:319 return m.group(1)320 except subprocess.CalledProcessError:321 pass322 return "0"323 324 325def expectedCompilerVersion(compiler_version):326 """Returns True iff compiler_version[1] matches the current compiler version.327 Use compiler_version[0] to specify the operator used to determine if a match has occurred.328 Any operator other than the following defaults to an equality test:329 '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not'330 331 If the current compiler version cannot be determined, we assume it is close to the top332 of trunk, so any less-than or equal-to comparisons will return False, and any333 greater-than or not-equal-to comparisons will return True.334 """335 if compiler_version is None:336 return True337 operator = str(compiler_version[0])338 version_str = str(compiler_version[1])339 340 if not version_str:341 return True342 343 test_compiler_version_str = getCompilerVersion()344 if test_compiler_version_str == "unknown":345 # Assume the compiler version is at or near the top of trunk.346 return operator in [">", ">=", "!", "!=", "not"]347 348 actual_version = version.parse(version_str)349 test_compiler_version = version.parse(test_compiler_version_str)350 351 if operator == ">":352 return test_compiler_version > actual_version353 if operator == ">=" or operator == "=>":354 return test_compiler_version >= actual_version355 if operator == "<":356 return test_compiler_version < actual_version357 if operator == "<=" or operator == "=<":358 return test_compiler_version <= actual_version359 if operator == "!=" or operator == "!" or operator == "not":360 return version_str not in test_compiler_version_str361 return version_str in test_compiler_version_str362 363 364def expectedCompiler(compilers):365 """Returns True iff any element of compilers is a sub-string of the current compiler."""366 if compilers is None:367 return True368 369 for compiler in compilers:370 if compiler in getCompiler():371 return True372 373 return False374 375 376# This is a helper function to determine if a specific version of Xcode's linker377# contains a TLS bug. We want to skip TLS tests if they contain this bug, but378# adding a linker/linker_version conditions to a decorator is challenging due to379# the number of ways linkers can enter the build process.380def xcode15LinkerBug():381 """Returns true iff a test is running on a darwin platform and the host linker is between versions 1000 and 1109."""382 darwin_platforms = lldbplatform.translate(lldbplatform.darwin_all)383 if getPlatform() not in darwin_platforms:384 return False385 386 try:387 raw_version_details = subprocess.check_output(388 ("xcrun", "ld", "-version_details")389 )390 version_details = json.loads(raw_version_details)391 version = version_details.get("version", "0")392 version_tuple = tuple(int(x) for x in version.split("."))393 if (1000,) <= version_tuple <= (1109,):394 return True395 except:396 pass397 398 return False399