311 lines · python
1import lldb2from lldbsuite.test.lldbtest import *3from lldbsuite.test.decorators import *4import lldbsuite.test.lldbutil as lldbutil5import json6 7 8class TestSimulatorPlatformLaunching(TestBase):9 NO_DEBUG_INFO_TESTCASE = True10 11 def check_load_commands(self, expected_load_command):12 """sanity check the built binary for the expected number of load commands"""13 load_cmds = subprocess.check_output(14 ["otool", "-l", self.getBuildArtifact()]15 ).decode("utf-8")16 found = 017 for line in load_cmds.split("\n"):18 if expected_load_command in line:19 found += 120 self.assertEqual(21 found,22 1,23 "wrong number of load commands for {}".format(expected_load_command),24 )25 26 def check_debugserver(self, log, expected_platform, expected_version):27 """scan the debugserver packet log"""28 process_info = lldbutil.packetlog_get_process_info(log)29 self.assertIn("ostype", process_info)30 self.assertEqual(process_info["ostype"], expected_platform)31 dylib_info = lldbutil.packetlog_get_dylib_info(log)32 self.assertTrue(dylib_info)33 aout_info = None34 for image in dylib_info["images"]:35 if image["pathname"].endswith("a.out"):36 aout_info = image37 self.assertTrue(aout_info)38 self.assertEqual(aout_info["min_version_os_name"], expected_platform)39 if expected_version:40 self.assertEqual(aout_info["min_version_os_sdk"], expected_version)41 42 def run_with(43 self, arch, os, vers, env, expected_load_command, expected_platform=None44 ):45 env_list = [env] if env else []46 triple = "-".join([arch, "apple", os + vers] + env_list)47 sdk = lldbutil.get_xcode_sdk(os, env)48 49 if not vers:50 vers = lldbutil.get_xcode_sdk_version(sdk)51 52 version_min = ""53 if env == "simulator":54 version_min = "-m{}-simulator-version-min={}".format(os, vers)55 elif os == "macosx":56 version_min = "-m{}-version-min={}".format(os, vers)57 58 sdk_root = lldbutil.get_xcode_sdk_root(sdk)59 clang = lldbutil.get_xcode_clang(sdk)60 61 print(triple)62 63 self.build(64 dictionary={65 "ARCH": arch,66 "ARCH_CFLAGS": "-target {} {}".format(triple, version_min),67 "SDKROOT": sdk_root,68 "USE_SYSTEM_STDLIB": 1,69 },70 compiler=clang,71 )72 73 self.check_load_commands(expected_load_command)74 log = self.getBuildArtifact("packets.log")75 self.expect("log enable gdb-remote packets -f " + log)76 lldbutil.run_to_source_breakpoint(77 self, "break here", lldb.SBFileSpec("hello.cpp")78 )79 triple_re = "-".join([arch, "apple", os + vers + ".*"] + env_list)80 self.expect("image list -b -t", patterns=[r"a\.out " + triple_re])81 self.check_debugserver(log, os + env, vers)82 83 if expected_platform is not None:84 # Verify the platform name.85 self.expect(86 "platform status",87 patterns=[r"Platform: " + expected_platform + "-simulator"],88 )89 90 # Launch exe in simulator and verify that `platform process list` can find the process.91 # This separate launch is needed because the command ignores processes which are being debugged.92 device_udid = lldbutil.get_latest_apple_simulator(93 expected_platform, self.trace94 )95 _, matched_strings = lldbutil.launch_exe_in_apple_simulator(96 device_udid,97 self.getBuildArtifact("a.out"),98 exe_args=[],99 stderr_lines_to_read=1, # in hello.cpp, the pid is printed first100 stderr_patterns=[r"PID: (.*)"],101 log=self.trace,102 )103 104 # Make sure we found the PID.105 self.assertIsNotNone(matched_strings[0])106 pid = int(matched_strings[0])107 108 # Verify that processes on the platform can be listed.109 self.expect(110 "platform process list",111 patterns=[112 r"\d+ matching processes were found on \"%s-simulator\""113 % expected_platform,114 r"%d .+ a.out" % pid,115 ],116 )117 118 @skipIfAsan119 @skipUnlessDarwin120 @skipIfDarwinEmbedded121 @apple_simulator_test("iphone")122 def test_ios(self):123 """Test running an iOS simulator binary"""124 self.run_with(125 arch=self.getArchitecture(),126 os="ios",127 vers="",128 env="simulator",129 expected_load_command="LC_BUILD_VERSION",130 expected_platform="ios",131 )132 133 @skipIfAsan134 @skipUnlessDarwin135 @skipIfDarwinEmbedded136 @apple_simulator_test("appletv")137 def test_tvos(self):138 """Test running an tvOS simulator binary"""139 self.run_with(140 arch=self.getArchitecture(),141 os="tvos",142 vers="",143 env="simulator",144 expected_load_command="LC_BUILD_VERSION",145 )146 147 @skipIfAsan148 @skipUnlessDarwin149 @skipIfDarwinEmbedded150 @apple_simulator_test("watch")151 @skipIfDarwin # rdar://problem/64552748152 @skipIf(archs=["arm64", "arm64e"])153 def test_watchos_i386(self):154 """Test running a 32-bit watchOS simulator binary"""155 self.run_with(156 arch="i386",157 os="watchos",158 vers="",159 env="simulator",160 expected_load_command="LC_BUILD_VERSION",161 )162 163 @skipIfAsan164 @skipUnlessDarwin165 @skipIfDarwinEmbedded166 @apple_simulator_test("watch")167 @skipIfDarwin # rdar://problem/64552748168 @skipIf(archs=["i386", "x86_64"])169 def test_watchos_armv7k(self):170 """Test running a 32-bit watchOS simulator binary"""171 self.run_with(172 arch="armv7k",173 os="watchos",174 vers="",175 env="simulator",176 expected_load_command="LC_BUILD_VERSION",177 )178 179 #180 # Back-deployment tests.181 #182 # Older Mach-O versions used less expressive load commands, such183 # as LC_VERSION_MIN_IPHONEOS that wouldn't distinguish between ios184 # and ios-simulator. When targeting a simulator on Apple Silicon185 # macOS, however, these legacy load commands are never generated.186 #187 188 @skipUnlessDarwin189 @skipIfDarwinEmbedded190 @skipIf(archs=["arm64", "arm64e"])191 def test_lc_version_min_macosx(self):192 """Test running a back-deploying non-simulator MacOS X binary"""193 self.run_with(194 arch=self.getArchitecture(),195 os="macosx",196 vers="10.9",197 env="",198 expected_load_command="LC_VERSION_MIN_MACOSX",199 )200 201 @skipIfAsan202 @skipUnlessDarwin203 @skipIfDarwinEmbedded204 @apple_simulator_test("iphone")205 @skipIf(archs=["arm64", "arm64e"])206 def test_lc_version_min_iphoneos(self):207 """Test running a back-deploying iOS simulator binary208 with a legacy iOS load command"""209 self.run_with(210 arch=self.getArchitecture(),211 os="ios",212 vers="11.0",213 env="simulator",214 expected_load_command="LC_VERSION_MIN_IPHONEOS",215 )216 217 @skipIfAsan218 @skipUnlessDarwin219 @skipIfDarwinEmbedded220 @apple_simulator_test("iphone")221 @skipIf(archs=["arm64", "arm64e"])222 def test_ios_backdeploy_x86(self):223 """Test running a back-deploying iOS simulator binary224 with a legacy iOS load command"""225 self.run_with(226 arch=self.getArchitecture(),227 os="ios",228 vers="13.0",229 env="simulator",230 expected_load_command="LC_BUILD_VERSION",231 )232 233 @skipIfAsan234 @skipUnlessDarwin235 @skipIfDarwinEmbedded236 @apple_simulator_test("iphone")237 @skipIf(archs=["i386", "x86_64"])238 def test_ios_backdeploy_apple_silicon(self):239 """Test running a back-deploying iOS simulator binary"""240 self.run_with(241 arch=self.getArchitecture(),242 os="ios",243 vers="14.0",244 env="simulator",245 expected_load_command="LC_BUILD_VERSION",246 )247 248 @skipIfAsan249 @skipUnlessDarwin250 @skipIfDarwinEmbedded251 @apple_simulator_test("appletv")252 @skipIf(archs=["arm64", "arm64e"])253 def test_lc_version_min_tvos(self):254 """Test running a back-deploying tvOS simulator binary255 with a legacy tvOS load command"""256 self.run_with(257 arch=self.getArchitecture(),258 os="tvos",259 vers="11.0",260 env="simulator",261 expected_load_command="LC_VERSION_MIN_TVOS",262 )263 264 @skipIfAsan265 @skipUnlessDarwin266 @skipIfDarwinEmbedded267 @apple_simulator_test("appletv")268 @skipIf(archs=["i386", "x86_64"])269 def test_tvos_backdeploy_apple_silicon(self):270 """Test running a back-deploying tvOS simulator binary"""271 self.run_with(272 arch=self.getArchitecture(),273 os="tvos",274 vers="14.0",275 env="simulator",276 expected_load_command="LC_BUILD_VERSION",277 )278 279 @skipIfAsan280 @skipUnlessDarwin281 @skipIfDarwinEmbedded282 @apple_simulator_test("watch")283 @skipIf(archs=["arm64", "arm64e"])284 @skipIfDarwin # rdar://problem/64552748285 def test_lc_version_min_watchos(self):286 """Test running a back-deploying watchOS simulator binary287 with a legacy watchOS load command"""288 self.run_with(289 arch="i386",290 os="watchos",291 vers="4.0",292 env="simulator",293 expected_load_command="LC_VERSION_MIN_WATCHOS",294 )295 296 @skipIfAsan297 @skipUnlessDarwin298 @skipIfDarwinEmbedded299 @apple_simulator_test("watch")300 @skipIf(archs=["arm64", "arm64e"])301 @skipIfDarwin # rdar://problem/64552748302 def test_watchos_backdeploy_apple_silicon(self):303 """Test running a back-deploying watchOS simulator binary"""304 self.run_with(305 arch="armv7k",306 os="watchos",307 vers="4.0",308 env="simulator",309 expected_load_command="LC_BUILD_VERSION",310 )311