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 ctypes 8import itertools 9import os 10import re 11import subprocess 12import sys 13 14# Third-party modules 15import six 16from six.moves.urllib import parse as urlparse 17 18# LLDB modules 19from . import configuration 20import lldb 21import lldbsuite.test.lldbplatform as lldbplatform 22 23 24def check_first_register_readable(test_case): 25 arch = test_case.getArchitecture() 26 27 if arch in ['x86_64', 'i386']: 28 test_case.expect("register read eax", substrs=['eax = 0x']) 29 elif arch in ['arm', 'armv7', 'armv7k', 'armv8l', 'armv7l']: 30 test_case.expect("register read r0", substrs=['r0 = 0x']) 31 elif arch in ['aarch64', 'arm64', 'arm64e', 'arm64_32']: 32 test_case.expect("register read x0", substrs=['x0 = 0x']) 33 elif re.match("mips", arch): 34 test_case.expect("register read zero", substrs=['zero = 0x']) 35 elif arch in ['s390x']: 36 test_case.expect("register read r0", substrs=['r0 = 0x']) 37 elif arch in ['powerpc64le']: 38 test_case.expect("register read r0", substrs=['r0 = 0x']) 39 else: 40 # TODO: Add check for other architectures 41 test_case.fail( 42 "Unsupported architecture for test case (arch: %s)" % 43 test_case.getArchitecture()) 44 45 46def _run_adb_command(cmd, device_id): 47 device_id_args = [] 48 if device_id: 49 device_id_args = ["-s", device_id] 50 full_cmd = ["adb"] + device_id_args + cmd 51 p = subprocess.Popen( 52 full_cmd, 53 stdout=subprocess.PIPE, 54 stderr=subprocess.PIPE) 55 stdout, stderr = p.communicate() 56 return p.returncode, stdout, stderr 57 58 59def target_is_android(): 60 return configuration.lldb_platform_name == "remote-android" 61 62def android_device_api(): 63 if not hasattr(android_device_api, 'result'): 64 assert configuration.lldb_platform_url is not None 65 device_id = None 66 parsed_url = urlparse.urlparse(configuration.lldb_platform_url) 67 host_name = parsed_url.netloc.split(":")[0] 68 if host_name != 'localhost': 69 device_id = host_name 70 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_id) 74 if retcode == 0: 75 android_device_api.result = int(stdout) 76 else: 77 raise LookupError( 78 ">>> Unable to determine the API level of the Android device.\n" 79 ">>> stdout:\n%s\n" 80 ">>> stderr:\n%s\n" % 81 (stdout, stderr)) 82 return android_device_api.result 83 84 85def match_android_device(device_arch, valid_archs=None, valid_api_levels=None): 86 if not target_is_android(): 87 return False 88 if valid_archs is not None and device_arch not in valid_archs: 89 return False 90 if valid_api_levels is not None and android_device_api() not in valid_api_levels: 91 return False 92 93 return True 94 95 96def finalize_build_dictionary(dictionary): 97 if target_is_android(): 98 if dictionary is None: 99 dictionary = {} 100 dictionary["OS"] = "Android" 101 dictionary["PIE"] = 1 102 return dictionary 103 104 105def getHostPlatform(): 106 """Returns the host platform running the test suite.""" 107 # Attempts to return a platform name matching a target Triple platform. 108 if sys.platform.startswith('linux'): 109 return 'linux' 110 elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'): 111 return 'windows' 112 elif sys.platform.startswith('darwin'): 113 return 'macosx' 114 elif sys.platform.startswith('freebsd'): 115 return 'freebsd' 116 elif sys.platform.startswith('netbsd'): 117 return 'netbsd' 118 else: 119 return sys.platform 120 121 122def getDarwinOSTriples(): 123 return lldbplatform.translate(lldbplatform.darwin_all) 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 platform = configuration.lldb_platform_name 138 if platform is None: 139 platform = "host" 140 if platform == "qemu-user": 141 platform = "host" 142 if platform == "host": 143 return getHostPlatform() 144 if platform.startswith("remote-"): 145 return platform[7:] 146 return platform 147 148 149def platformIsDarwin(): 150 """Returns true if the OS triple for the selected platform is any valid apple OS""" 151 return getPlatform() in getDarwinOSTriples() 152 153 154def findMainThreadCheckerDylib(): 155 if not platformIsDarwin(): 156 return "" 157 158 if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded): 159 return "/Developer/usr/lib/libMainThreadChecker.dylib" 160 161 with os.popen('xcode-select -p') as output: 162 xcode_developer_path = output.read().strip() 163 mtc_dylib_path = '%s/usr/lib/libMainThreadChecker.dylib' % xcode_developer_path 164 if os.path.isfile(mtc_dylib_path): 165 return mtc_dylib_path 166 167 return "" 168 169 170class _PlatformContext(object): 171 """Value object class which contains platform-specific options.""" 172 173 def __init__(self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension): 174 self.shlib_environment_var = shlib_environment_var 175 self.shlib_path_separator = shlib_path_separator 176 self.shlib_prefix = shlib_prefix 177 self.shlib_extension = shlib_extension 178 179 180def createPlatformContext(): 181 if platformIsDarwin(): 182 return _PlatformContext('DYLD_LIBRARY_PATH', ':', 'lib', 'dylib') 183 elif getPlatform() in ("freebsd", "linux", "netbsd"): 184 return _PlatformContext('LD_LIBRARY_PATH', ':', 'lib', 'so') 185 else: 186 return _PlatformContext('PATH', ';', '', 'dll') 187 188 189def hasChattyStderr(test_case): 190 """Some targets produce garbage on the standard error output. This utility function 191 determines whether the tests can be strict about the expected stderr contents.""" 192 if match_android_device(test_case.getArchitecture(), ['aarch64'], range(22, 25+1)): 193 return True # The dynamic linker on the device will complain about unknown DT entries 194 return False 195 196if getHostPlatform() == "linux": 197 def enable_attach(): 198 """Enable attaching to _this_ process, if host requires such an action. 199 Suitable for use as a preexec_fn in subprocess.Popen and similar.""" 200 c = ctypes.CDLL(None) 201 PR_SET_PTRACER = ctypes.c_int(0x59616d61) 202 PR_SET_PTRACER_ANY = ctypes.c_ulong(-1) 203 c.prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) 204else: 205 enable_attach = None 206