1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test.decorators import *
4import lldbsuite.test.lldbutil as lldbutil
5import json
6import unittest2
7
8
9@skipIfReproducer
10class TestSimulatorPlatformLaunching(TestBase):
11
12    mydir = TestBase.compute_mydir(__file__)
13    NO_DEBUG_INFO_TESTCASE = True
14
15    def check_load_commands(self, expected_load_command):
16        """sanity check the built binary for the expected number of load commands"""
17        load_cmds = subprocess.check_output(
18            ['otool', '-l', self.getBuildArtifact()]
19        ).decode("utf-8")
20        found = 0
21        for line in load_cmds.split('\n'):
22            if expected_load_command in line:
23                found += 1
24        self.assertEquals(
25            found, 1, "wrong number of load commands for {}".format(
26                expected_load_command))
27
28
29    def check_debugserver(self, log, expected_platform, expected_version):
30        """scan the debugserver packet log"""
31        process_info = lldbutil.packetlog_get_process_info(log)
32        self.assertTrue('ostype' in process_info)
33        self.assertEquals(process_info['ostype'], expected_platform)
34        dylib_info = lldbutil.packetlog_get_dylib_info(log)
35        self.assertTrue(dylib_info)
36        aout_info = None
37        for image in dylib_info['images']:
38            if image['pathname'].endswith('a.out'):
39                aout_info = image
40        self.assertTrue(aout_info)
41        self.assertEquals(aout_info['min_version_os_name'], expected_platform)
42        if expected_version:
43            self.assertEquals(aout_info['min_version_os_sdk'], expected_version)
44
45
46    def run_with(self, arch, os, vers, env, expected_load_command):
47        env_list = [env] if env else []
48        triple = '-'.join([arch, 'apple', os + vers] + env_list)
49        sdk = lldbutil.get_xcode_sdk(os, env)
50
51        version_min = ''
52        if not vers:
53            vers = lldbutil.get_xcode_sdk_version(sdk)
54        if env == 'simulator':
55            version_min = '-m{}-simulator-version-min={}'.format(os, vers)
56        elif os == 'macosx':
57            version_min = '-m{}-version-min={}'.format(os, vers)
58
59        sdk_root = lldbutil.get_xcode_sdk_root(sdk)
60
61        self.build(
62            dictionary={
63                'ARCH': arch,
64                'ARCH_CFLAGS': '-target {} {}'.format(triple, version_min),
65                'SDKROOT': sdk_root
66            })
67
68        self.check_load_commands(expected_load_command)
69        log = self.getBuildArtifact('packets.log')
70        self.expect("log enable gdb-remote packets -f "+log)
71        lldbutil.run_to_source_breakpoint(self, "break here",
72                                          lldb.SBFileSpec("hello.c"))
73        triple_re = '-'.join([arch, 'apple', os + vers+'.*'] + env_list)
74        self.expect('image list -b -t', patterns=['a\.out '+triple_re])
75        self.check_debugserver(log, os+env, vers)
76
77    @skipIfAsan
78    @skipUnlessDarwin
79    @skipIfDarwinEmbedded
80    @apple_simulator_test('iphone')
81    @skipIfOutOfTreeDebugserver
82    def test_ios(self):
83        """Test running an iOS simulator binary"""
84        self.run_with(arch=self.getArchitecture(),
85                      os='ios', vers='', env='simulator',
86                      expected_load_command='LC_BUILD_VERSION')
87
88    @skipIfAsan
89    @skipUnlessDarwin
90    @skipIfDarwinEmbedded
91    @apple_simulator_test('appletv')
92    @skipIfOutOfTreeDebugserver
93    def test_tvos(self):
94        """Test running an tvOS simulator binary"""
95        self.run_with(arch=self.getArchitecture(),
96                      os='tvos', vers='', env='simulator',
97                      expected_load_command='LC_BUILD_VERSION')
98
99    @skipIfAsan
100    @skipUnlessDarwin
101    @skipIfDarwinEmbedded
102    @apple_simulator_test('watch')
103    @skipIfDarwin # rdar://problem/64552748
104    @skipIf(archs=['arm64','arm64e'])
105    @skipIfOutOfTreeDebugserver
106    def test_watchos_i386(self):
107        """Test running a 32-bit watchOS simulator binary"""
108        self.run_with(arch='i386',
109                      os='watchos', vers='', env='simulator',
110                      expected_load_command='LC_BUILD_VERSION')
111
112    @skipIfAsan
113    @skipUnlessDarwin
114    @skipIfDarwinEmbedded
115    @apple_simulator_test('watch')
116    @skipIfDarwin # rdar://problem/64552748
117    @skipIf(archs=['i386','x86_64'])
118    @skipIfOutOfTreeDebugserver
119    def test_watchos_armv7k(self):
120        """Test running a 32-bit watchOS simulator binary"""
121        self.run_with(arch='armv7k',
122                      os='watchos', vers='', env='simulator',
123                      expected_load_command='LC_BUILD_VERSION')
124
125
126    #
127    # Back-deployment tests.
128    #
129    # Older Mach-O versions used less expressive load commands, such
130    # as LC_VERSION_MIN_IPHONEOS that wouldn't distinguish between ios
131    # and ios-simulator.  When targeting a simulator on Apple Silicon
132    # macOS, however, these legacy load commands are never generated.
133    #
134
135    @skipUnlessDarwin
136    @skipIfDarwinEmbedded
137    @skipIfOutOfTreeDebugserver
138    def test_lc_version_min_macosx(self):
139        """Test running a back-deploying non-simulator MacOS X binary"""
140        self.run_with(arch=self.getArchitecture(),
141                      os='macosx', vers='10.9', env='',
142                      expected_load_command='LC_VERSION_MIN_MACOSX')
143    @skipIfAsan
144    @skipUnlessDarwin
145    @skipIfDarwinEmbedded
146    @apple_simulator_test('iphone')
147    @skipIf(archs=['arm64','arm64e'])
148    @skipIfOutOfTreeDebugserver
149    def test_lc_version_min_iphoneos(self):
150        """Test running a back-deploying iOS simulator binary
151           with a legacy iOS load command"""
152        self.run_with(arch=self.getArchitecture(),
153                      os='ios', vers='11.0', env='simulator',
154                      expected_load_command='LC_VERSION_MIN_IPHONEOS')
155
156    @skipIfAsan
157    @skipUnlessDarwin
158    @skipIfDarwinEmbedded
159    @apple_simulator_test('iphone')
160    @skipIf(archs=['arm64','arm64e'])
161    @skipIfOutOfTreeDebugserver
162    def test_ios_backdeploy_x86(self):
163        """Test running a back-deploying iOS simulator binary
164           with a legacy iOS load command"""
165        self.run_with(arch=self.getArchitecture(),
166                      os='ios', vers='13.0', env='simulator',
167                      expected_load_command='LC_BUILD_VERSION')
168
169    @skipIfAsan
170    @skipUnlessDarwin
171    @skipIfDarwinEmbedded
172    @apple_simulator_test('iphone')
173    @skipIf(archs=['i386','x86_64'])
174    @skipIfOutOfTreeDebugserver
175    def test_ios_backdeploy_apple_silicon(self):
176        """Test running a back-deploying iOS simulator binary"""
177        self.run_with(arch=self.getArchitecture(),
178                      os='ios', vers='11.0', env='simulator',
179                      expected_load_command='LC_BUILD_VERSION')
180
181    @skipIfAsan
182    @skipUnlessDarwin
183    @skipIfDarwinEmbedded
184    @apple_simulator_test('appletv')
185    @skipIf(archs=['arm64','arm64e'])
186    @skipIfOutOfTreeDebugserver
187    def test_lc_version_min_tvos(self):
188        """Test running a back-deploying tvOS simulator binary
189           with a legacy tvOS load command"""
190        self.run_with(arch=self.getArchitecture(),
191                      os='tvos', vers='11.0', env='simulator',
192                      expected_load_command='LC_VERSION_MIN_TVOS')
193
194    @skipIfAsan
195    @skipUnlessDarwin
196    @skipIfDarwinEmbedded
197    @apple_simulator_test('appletv')
198    @skipIf(archs=['i386','x86_64'])
199    @skipIfOutOfTreeDebugserver
200    def test_tvos_backdeploy_apple_silicon(self):
201        """Test running a back-deploying tvOS simulator binary"""
202        self.run_with(arch=self.getArchitecture(),
203                      os='tvos', vers='11.0', env='simulator',
204                      expected_load_command='LC_BUILD_VERSION')
205
206    @skipIfAsan
207    @skipUnlessDarwin
208    @skipIfDarwinEmbedded
209    @apple_simulator_test('watch')
210    @skipIf(archs=['arm64','arm64e'])
211    @skipIfDarwin # rdar://problem/64552748
212    @skipIfOutOfTreeDebugserver
213    def test_lc_version_min_watchos(self):
214        """Test running a back-deploying watchOS simulator binary
215           with a legacy watchOS load command"""
216        self.run_with(arch='i386',
217                      os='watchos', vers='4.0', env='simulator',
218                      expected_load_command='LC_VERSION_MIN_WATCHOS')
219
220    @skipIfAsan
221    @skipUnlessDarwin
222    @skipIfDarwinEmbedded
223    @apple_simulator_test('watch')
224    @skipIf(archs=['arm64','arm64e'])
225    @skipIfDarwin # rdar://problem/64552748
226    @skipIfOutOfTreeDebugserver
227    def test_watchos_backdeploy_apple_silicon(self):
228        """Test running a back-deploying watchOS simulator binary"""
229        self.run_with(arch='armv7k',
230                      os='watchos', vers='4.0', env='simulator',
231                      expected_load_command='LC_BUILD_VERSION')
232