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@skipIfDarwin # rdar://problem/64552748
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(found, 1, "wrong load command")
25
26    def check_debugserver(self, log, expected_platform, expected_version):
27        """scan the debugserver packet log"""
28        logfile = open(log, "r")
29        dylib_info = None
30        response = False
31        for line in logfile:
32            if response:
33                while line[0] != '$':
34                    line = line[1:]
35                line = line[1:]
36                # Unescape '}'.
37                dylib_info = json.loads(line.replace('}]','}')[:-4])
38                response = False
39            if 'send packet: $jGetLoadedDynamicLibrariesInfos:{' in line:
40                response = True
41
42        self.assertTrue(dylib_info)
43        aout_info = None
44        for image in dylib_info['images']:
45            if image['pathname'].endswith('a.out'):
46                aout_info = image
47        self.assertTrue(aout_info)
48        self.assertEquals(aout_info['min_version_os_name'], expected_platform)
49        if expected_version:
50            self.assertEquals(aout_info['min_version_os_sdk'], expected_version)
51
52
53    def run_with(self, arch, os, vers, env, expected_load_command):
54        self.build(dictionary={'TRIPLE': arch+'-apple-'+os+vers+'-'+env})
55        self.check_load_commands(expected_load_command)
56        log = self.getBuildArtifact('packets.log')
57        self.expect("log enable gdb-remote packets -f "+log)
58        lldbutil.run_to_source_breakpoint(self, "break here",
59                                          lldb.SBFileSpec("hello.c"))
60        self.expect('image list -b -t',
61                    patterns=['a\.out '+arch+'-apple-'+os+vers+'.*-'+env])
62        self.check_debugserver(log, os+env, vers)
63
64    @skipUnlessDarwin
65    @skipIfDarwinEmbedded
66    @apple_simulator_test('iphone')
67    def test_ios(self):
68        """Test running an iOS simulator binary"""
69        self.run_with(arch=self.getArchitecture(),
70                      os='ios', vers='', env='simulator',
71                      expected_load_command='LC_BUILD_VERSION')
72
73    @skipUnlessDarwin
74    @skipIfDarwinEmbedded
75    @apple_simulator_test('appletv')
76    def test_tvos(self):
77        """Test running an tvOS simulator binary"""
78        self.run_with(arch=self.getArchitecture(),
79                      os='tvos', vers='', env='simulator',
80                      expected_load_command='LC_BUILD_VERSION')
81
82    @skipUnlessDarwin
83    @skipIfDarwinEmbedded
84    @apple_simulator_test('watch')
85    @skipIfDarwin # rdar://problem/64552748
86    @skipIf(archs=['arm64','arm64e'])
87    def test_watchos_i386(self):
88        """Test running a 32-bit watchOS simulator binary"""
89        self.run_with(arch='i386',
90                      os='watchos', vers='', env='simulator',
91                      expected_load_command='LC_BUILD_VERSION')
92
93    @skipUnlessDarwin
94    @skipIfDarwinEmbedded
95    @apple_simulator_test('watch')
96    @skipIfDarwin # rdar://problem/64552748
97    @skipIf(archs=['i386','x86_64'])
98    def test_watchos_armv7k(self):
99        """Test running a 32-bit watchOS simulator binary"""
100        self.run_with(arch='armv7k',
101                      os='watchos', vers='', env='simulator',
102                      expected_load_command='LC_BUILD_VERSION')
103
104
105    #
106    # Back-deployment tests.
107    #
108    # Older Mach-O versions used less expressive load commands, such
109    # as LC_VERSION_MIN_IPHONEOS that wouldn't distinguish between ios
110    # and ios-simulator.  When targeting a simulator on Apple Silicon
111    # macOS, however, these legacy load commands are never generated.
112    #
113
114    @skipUnlessDarwin
115    @skipIfDarwinEmbedded
116    @apple_simulator_test('iphone')
117    @skipIf(archs=['arm64','arm64e'])
118    def test_lc_version_min_iphoneos(self):
119        """Test running a back-deploying iOS simulator binary
120           with a legacy iOS load command"""
121        self.run_with(arch=self.getArchitecture(),
122                      os='ios', vers='11.0', env='simulator',
123                      expected_load_command='LC_VERSION_MIN_IPHONEOS')
124
125    @skipUnlessDarwin
126    @skipIfDarwinEmbedded
127    @apple_simulator_test('iphone')
128    @skipIf(archs=['arm64','arm64e'])
129    def test_ios_backdeploy_x86(self):
130        """Test running a back-deploying iOS simulator binary
131           with a legacy iOS load command"""
132        self.run_with(arch=self.getArchitecture(),
133                      os='ios', vers='13.0', env='simulator',
134                      expected_load_command='LC_BUILD_VERSION')
135
136    @skipUnlessDarwin
137    @skipIfDarwinEmbedded
138    @apple_simulator_test('iphone')
139    @skipIf(archs=['i386','x86_64'])
140    def test_ios_backdeploy_apple_silicon(self):
141        """Test running a back-deploying iOS simulator binary"""
142        self.run_with(arch=self.getArchitecture(),
143                      os='ios', vers='11.0', env='simulator',
144                      expected_load_command='LC_BUILD_VERSION')
145
146    @skipUnlessDarwin
147    @skipIfDarwinEmbedded
148    @apple_simulator_test('appletv')
149    @skipIf(archs=['arm64','arm64e'])
150    def test_lc_version_min_tvos(self):
151        """Test running a back-deploying tvOS simulator binary
152           with a legacy tvOS load command"""
153        self.run_with(arch=self.getArchitecture(),
154                      os='tvos', vers='11.0', env='simulator',
155                      expected_load_command='LC_VERSION_MIN_TVOS')
156
157    @skipUnlessDarwin
158    @skipIfDarwinEmbedded
159    @apple_simulator_test('appletv')
160    @skipIf(archs=['i386','x86_64'])
161    def test_tvos_backdeploy_apple_silicon(self):
162        """Test running a back-deploying tvOS simulator binary"""
163        self.run_with(arch=self.getArchitecture(),
164                      os='tvos', vers='11.0', env='simulator',
165                      expected_load_command='LC_BUILD_VERSION')
166
167    @skipUnlessDarwin
168    @skipIfDarwinEmbedded
169    @apple_simulator_test('watch')
170    @skipIf(archs=['arm64','arm64e'])
171    @skipIfDarwin # rdar://problem/64552748
172    def test_lc_version_min_watchos(self):
173        """Test running a back-deploying watchOS simulator binary
174           with a legacy watchOS load command"""
175        self.run_with(arch='i386',
176                      os='watchos', vers='4.0', env='simulator',
177                      expected_load_command='LC_VERSION_MIN_WATCHOS')
178
179    @skipUnlessDarwin
180    @skipIfDarwinEmbedded
181    @apple_simulator_test('watch')
182    @skipIf(archs=['arm64','arm64e'])
183    @skipIfDarwin # rdar://problem/64552748
184    def test_watchos_backdeploy_apple_silicon(self):
185        """Test running a back-deploying watchOS simulator binary"""
186        self.run_with(arch='armv7k',
187                      os='watchos', vers='4.0', env='simulator',
188                      expected_load_command='LC_BUILD_VERSION')
189