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 11 12# Third-party modules 13import six 14from six.moves.urllib import parse as urlparse 15 16# LLDB modules 17from . import configuration 18import use_lldb_suite 19import lldb 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']: 27 test_case.expect("register read r0", substrs = ['r0 = 0x']) 28 elif arch in ['aarch64']: 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 else: 33 # TODO: Add check for other architectures 34 test_case.fail("Unsupported architecture for test case (arch: %s)" % test_case.getArchitecture()) 35 36def _run_adb_command(cmd, device_id): 37 device_id_args = [] 38 if device_id: 39 device_id_args = ["-s", device_id] 40 full_cmd = ["adb"] + device_id_args + cmd 41 p = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 42 stdout, stderr = p.communicate() 43 return p.returncode, stdout, stderr 44 45def _target_is_android(): 46 if not hasattr(_target_is_android, 'result'): 47 triple = lldb.DBG.GetSelectedPlatform().GetTriple() 48 match = re.match(".*-.*-.*-android", triple) 49 _target_is_android.result = match is not None 50 return _target_is_android.result 51 52def android_device_api(): 53 if not hasattr(android_device_api, 'result'): 54 assert configuration.lldb_platform_url is not None 55 device_id = None 56 parsed_url = urlparse.urlparse(configuration.lldb_platform_url) 57 host_name = parsed_url.netloc.split(":")[0] 58 if host_name != 'localhost': 59 device_id = host_name 60 if device_id.startswith('[') and device_id.endswith(']'): 61 device_id = device_id[1:-1] 62 retcode, stdout, stderr = _run_adb_command( 63 ["shell", "getprop", "ro.build.version.sdk"], device_id) 64 if retcode == 0: 65 android_device_api.result = int(stdout) 66 else: 67 raise LookupError( 68 ">>> Unable to determine the API level of the Android device.\n" 69 ">>> stdout:\n%s\n" 70 ">>> stderr:\n%s\n" % (stdout, stderr)) 71 return android_device_api.result 72 73def match_android_device(device_arch, valid_archs=None, valid_api_levels=None): 74 if not _target_is_android(): 75 return False 76 if valid_archs is not None and device_arch not in valid_archs: 77 return False 78 if valid_api_levels is not None and android_device_api() not in valid_api_levels: 79 return False 80 81 return True 82 83def finalize_build_dictionary(dictionary): 84 if _target_is_android(): 85 if dictionary is None: 86 dictionary = {} 87 dictionary["OS"] = "Android" 88 if android_device_api() >= 16: 89 dictionary["PIE"] = 1 90 return dictionary 91 92def getHostPlatform(): 93 """Returns the host platform running the test suite.""" 94 # Attempts to return a platform name matching a target Triple platform. 95 if sys.platform.startswith('linux'): 96 return 'linux' 97 elif sys.platform.startswith('win32'): 98 return 'windows' 99 elif sys.platform.startswith('darwin'): 100 return 'darwin' 101 elif sys.platform.startswith('freebsd'): 102 return 'freebsd' 103 elif sys.platform.startswith('netbsd'): 104 return 'netbsd' 105 else: 106 return sys.platform 107 108 109def getDarwinOSTriples(): 110 return ['darwin', 'macosx', 'ios'] 111 112def getPlatform(): 113 """Returns the target platform which the tests are running on.""" 114 platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2] 115 if platform.startswith('freebsd'): 116 platform = 'freebsd' 117 elif platform.startswith('netbsd'): 118 platform = 'netbsd' 119 return platform 120 121def platformIsDarwin(): 122 """Returns true if the OS triple for the selected platform is any valid apple OS""" 123 return getPlatform() in getDarwinOSTriples() 124 125class _PlatformContext(object): 126 """Value object class which contains platform-specific options.""" 127 128 def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension): 129 self.shlib_environment_var = shlib_environment_var 130 self.shlib_prefix = shlib_prefix 131 self.shlib_extension = shlib_extension 132 133def createPlatformContext(): 134 if platformIsDarwin(): 135 return _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib') 136 elif getPlatform() in ("freebsd", "linux", "netbsd"): 137 return _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so') 138 else: 139 return None 140