Skip to content

Commit 7237bfb

Browse files
authored
Merge pull request #4072 from apple/jdevlieghere/rdar/89840215
Fix platform selection on Apple Silicon
2 parents ce03fe4 + c93f760 commit 7237bfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+376
-80
lines changed

lldb/include/lldb/Target/Platform.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ class Platform : public PluginInterface {
9595
static lldb::PlatformSP GetHostPlatform();
9696

9797
static lldb::PlatformSP
98-
GetPlatformForArchitecture(const ArchSpec &arch, ArchSpec *platform_arch_ptr);
98+
GetPlatformForArchitecture(const ArchSpec &arch,
99+
const ArchSpec &process_host_arch = {},
100+
ArchSpec *platform_arch_ptr = nullptr);
99101

100102
static const char *GetHostPlatformName();
101103

@@ -107,6 +109,7 @@ class Platform : public PluginInterface {
107109
static lldb::PlatformSP Create(ConstString name, Status &error);
108110

109111
static lldb::PlatformSP Create(const ArchSpec &arch,
112+
const ArchSpec &process_host_arch,
110113
ArchSpec *platform_arch_ptr, Status &error);
111114

112115
/// Augments the triple either with information from platform or the host
@@ -311,7 +314,8 @@ class Platform : public PluginInterface {
311314

312315
/// Get the platform's supported architectures in the order in which they
313316
/// should be searched.
314-
virtual std::vector<ArchSpec> GetSupportedArchitectures() = 0;
317+
virtual std::vector<ArchSpec>
318+
GetSupportedArchitectures(const ArchSpec &process_host_arch) = 0;
315319

316320
virtual size_t GetSoftwareBreakpointTrapOpcode(Target &target,
317321
BreakpointSite *bp_site);
@@ -333,6 +337,7 @@ class Platform : public PluginInterface {
333337
/// Lets a platform answer if it is compatible with a given architecture and
334338
/// the target triple contained within.
335339
virtual bool IsCompatibleArchitecture(const ArchSpec &arch,
340+
const ArchSpec &process_host_arch,
336341
bool exact_arch_match,
337342
ArchSpec *compatible_arch_ptr);
338343

lldb/include/lldb/Target/Process.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,9 @@ class Process : public std::enable_shared_from_this<Process>,
707707
virtual JITLoaderList &GetJITLoaders();
708708

709709
public:
710+
/// Get the system architecture for this process.
711+
virtual ArchSpec GetSystemArchitecture() { return {}; }
712+
710713
/// Get the system runtime plug-in for this process.
711714
///
712715
/// \return
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
from lldbsuite.test.gdbclientutils import *
5+
from lldbsuite.test.lldbgdbclient import GDBRemoteTestBase
6+
7+
8+
class TestPlatformMacOSX(GDBRemoteTestBase):
9+
10+
mydir = TestBase.compute_mydir(__file__)
11+
12+
class MyResponder(MockGDBServerResponder):
13+
14+
def __init__(self, host):
15+
self.host_ostype = host
16+
MockGDBServerResponder.__init__(self)
17+
18+
def respond(self, packet):
19+
if packet == "qProcessInfo":
20+
return self.qProcessInfo()
21+
return MockGDBServerResponder.respond(self, packet)
22+
23+
def qHostInfo(self):
24+
return "cputype:16777223;cpusubtype:2;ostype:%s;vendor:apple;os_version:10.15.4;maccatalyst_version:13.4;endian:little;ptrsize:8;" % self.host_ostype
25+
26+
def qProcessInfo(self):
27+
return "pid:a860;parent-pid:d2a0;real-uid:1f5;real-gid:14;effective-uid:1f5;effective-gid:14;cputype:100000c;cpusubtype:2;ptrsize:8;ostype:ios;vendor:apple;endian:little;"
28+
29+
def vCont(self):
30+
return "vCont;"
31+
32+
def platform_test(self, host, expected_triple, expected_platform):
33+
self.server.responder = self.MyResponder(host)
34+
if self.TraceOn():
35+
self.runCmd("log enable gdb-remote packets")
36+
self.addTearDownHook(
37+
lambda: self.runCmd("log disable gdb-remote packets"))
38+
39+
target = self.dbg.CreateTargetWithFileAndArch(None, None)
40+
process = self.connect(target)
41+
42+
triple = target.GetTriple()
43+
self.assertEqual(triple, expected_triple)
44+
45+
platform = target.GetPlatform()
46+
self.assertEqual(platform.GetName(), expected_platform)
47+
48+
@skipIfRemote
49+
def test_ios(self):
50+
self.platform_test(host="ios",
51+
expected_triple="arm64e-apple-ios-",
52+
expected_platform="remote-ios")
53+
54+
@skipIfRemote
55+
@skipUnlessDarwin
56+
@skipUnlessArch("arm64")
57+
def test_macos(self):
58+
self.platform_test(host="macosx",
59+
expected_triple="arm64e-apple-ios-",
60+
expected_platform="host")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===-- PlatformMacOSXTest.cpp ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "gtest/gtest.h"
10+
11+
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
12+
#include "TestingSupport/SubsystemRAII.h"
13+
#include "lldb/Host/FileSystem.h"
14+
#include "lldb/Host/HostInfo.h"
15+
#include "lldb/Target/Platform.h"
16+
17+
using namespace lldb;
18+
using namespace lldb_private;
19+
20+
class PlatformMacOSXTest : public ::testing::Test {
21+
SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX> subsystems;
22+
};
23+
24+
static bool containsArch(const std::vector<ArchSpec> &archs,
25+
const ArchSpec &arch) {
26+
return std::find_if(archs.begin(), archs.end(), [&](const ArchSpec &other) {
27+
return arch.IsExactMatch(other);
28+
}) != archs.end();
29+
}
30+
31+
TEST_F(PlatformMacOSXTest, TestGetSupportedArchitectures) {
32+
PlatformMacOSX platform;
33+
34+
const ArchSpec x86_macosx_arch("x86_64-apple-macosx");
35+
36+
EXPECT_TRUE(containsArch(platform.GetSupportedArchitectures(x86_macosx_arch),
37+
x86_macosx_arch));
38+
EXPECT_TRUE(
39+
containsArch(platform.GetSupportedArchitectures({}), x86_macosx_arch));
40+
41+
#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
42+
const ArchSpec arm64_macosx_arch("arm64-apple-macosx");
43+
const ArchSpec arm64_ios_arch("arm64-apple-ios");
44+
45+
EXPECT_TRUE(containsArch(
46+
platform.GetSupportedArchitectures(arm64_macosx_arch), arm64_ios_arch));
47+
EXPECT_TRUE(
48+
containsArch(platform.GetSupportedArchitectures({}), arm64_ios_arch));
49+
EXPECT_FALSE(containsArch(platform.GetSupportedArchitectures(arm64_ios_arch),
50+
arm64_ios_arch));
51+
#endif
52+
}

lldb/source/Interpreter/OptionGroupPlatform.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
2323
if (!m_platform_name.empty()) {
2424
platform_sp = Platform::Create(ConstString(m_platform_name.c_str()), error);
2525
if (platform_sp) {
26-
if (platform_arch.IsValid() &&
27-
!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) {
28-
error.SetErrorStringWithFormat("platform '%s' doesn't support '%s'",
29-
platform_sp->GetName().GetCString(),
30-
arch.GetTriple().getTriple().c_str());
26+
if (platform_arch.IsValid() && !platform_sp->IsCompatibleArchitecture(
27+
arch, {}, false, &platform_arch)) {
28+
error.SetErrorStringWithFormatv("platform '{0}' doesn't support '{1}'",
29+
platform_sp->GetPluginName(),
30+
arch.GetTriple().getTriple());
3131
platform_sp.reset();
3232
return platform_sp;
3333
}
3434
}
3535
} else if (arch.IsValid()) {
36-
platform_sp = Platform::Create(arch, &platform_arch, error);
36+
platform_sp = Platform::Create(arch, {}, &platform_arch, error);
3737
}
3838

3939
if (platform_sp) {

lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ PlatformFreeBSD::PlatformFreeBSD(bool is_host)
129129
}
130130
}
131131

132-
std::vector<ArchSpec> PlatformFreeBSD::GetSupportedArchitectures() {
132+
std::vector<ArchSpec>
133+
PlatformFreeBSD::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
133134
if (m_remote_platform_sp)
134-
return m_remote_platform_sp->GetSupportedArchitectures();
135+
return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
135136
return m_supported_architectures;
136137
}
137138

lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class PlatformFreeBSD : public PlatformPOSIX {
4242

4343
void GetStatus(Stream &strm) override;
4444

45-
std::vector<ArchSpec> GetSupportedArchitectures() override;
45+
std::vector<ArchSpec>
46+
GetSupportedArchitectures(const ArchSpec &process_host_arch) override;
4647

4748
bool CanDebugProcess() override;
4849

lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ PlatformLinux::PlatformLinux(bool is_host)
126126
}
127127
}
128128

129-
std::vector<ArchSpec> PlatformLinux::GetSupportedArchitectures() {
129+
std::vector<ArchSpec>
130+
PlatformLinux::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
130131
if (m_remote_platform_sp)
131-
return m_remote_platform_sp->GetSupportedArchitectures();
132+
return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
132133
return m_supported_architectures;
133134
}
134135

lldb/source/Plugins/Platform/Linux/PlatformLinux.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class PlatformLinux : public PlatformPOSIX {
4242

4343
void GetStatus(Stream &strm) override;
4444

45-
std::vector<ArchSpec> GetSupportedArchitectures() override;
45+
std::vector<ArchSpec>
46+
GetSupportedArchitectures(const ArchSpec &process_host_arch) override;
4647

4748
uint32_t GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) override;
4849

lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ CoreSimulatorSupport::Device PlatformAppleSimulator::GetSimulatorDevice() {
266266
}
267267
#endif
268268

269-
std::vector<ArchSpec> PlatformAppleSimulator::GetSupportedArchitectures() {
269+
std::vector<ArchSpec> PlatformAppleSimulator::GetSupportedArchitectures(
270+
const ArchSpec &process_host_arch) {
270271
std::vector<ArchSpec> result(m_supported_triples.size());
271272
llvm::transform(m_supported_triples, result.begin(),
272273
[](llvm::StringRef triple) { return ArchSpec(triple); });
@@ -382,7 +383,7 @@ Status PlatformAppleSimulator::ResolveExecutable(
382383
StreamString arch_names;
383384
llvm::ListSeparator LS;
384385
ArchSpec platform_arch;
385-
for (const ArchSpec &arch : GetSupportedArchitectures()) {
386+
for (const ArchSpec &arch : GetSupportedArchitectures({})) {
386387
resolved_module_spec.GetArchitecture() = arch;
387388

388389
// Only match x86 with x86 and x86_64 with x86_64...

lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class PlatformAppleSimulator : public PlatformDarwin {
6565
lldb_private::Target &target,
6666
lldb_private::Status &error) override;
6767

68-
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures() override;
68+
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures(
69+
const lldb_private::ArchSpec &process_host_arch) override;
6970

7071
lldb_private::Status ResolveExecutable(
7172
const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp,

lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,8 @@ Status PlatformDarwinKernel::ExamineKextForMatchingUUID(
909909
return {};
910910
}
911911

912-
std::vector<ArchSpec> PlatformDarwinKernel::GetSupportedArchitectures() {
912+
std::vector<ArchSpec> PlatformDarwinKernel::GetSupportedArchitectures(
913+
const ArchSpec &process_host_arch) {
913914
std::vector<ArchSpec> result;
914915
#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
915916
ARMGetSupportedArchitectures(result);

lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class PlatformDarwinKernel : public PlatformDarwin {
5656
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
5757
bool *did_create_ptr) override;
5858

59-
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures() override;
59+
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures(
60+
const lldb_private::ArchSpec &process_host_arch) override;
6061

6162
bool SupportsModules() override { return false; }
6263

lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,10 @@ ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) {
133133
return {};
134134
}
135135

136-
std::vector<ArchSpec> PlatformMacOSX::GetSupportedArchitectures() {
136+
std::vector<ArchSpec>
137+
PlatformMacOSX::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
137138
std::vector<ArchSpec> result;
138139
#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
139-
// macOS for ARM64 support both native and translated x86_64 processes
140-
141140
// When cmdline lldb is run on iOS, watchOS, etc, it is still
142141
// using "PlatformMacOSX".
143142
llvm::Triple::OSType host_os = GetHostOSType();
@@ -151,6 +150,16 @@ std::vector<ArchSpec> PlatformMacOSX::GetSupportedArchitectures() {
151150
result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
152151
result.push_back(ArchSpec("arm64-apple-ios-macabi"));
153152
result.push_back(ArchSpec("arm64e-apple-ios-macabi"));
153+
154+
// On Apple Silicon, the host platform is compatible with iOS triples to
155+
// support unmodified "iPhone and iPad Apps on Apple Silicon Macs". Because
156+
// the binaries are identical, we must rely on the host architecture to
157+
// tell them apart and mark the host platform as compatible or not.
158+
if (!process_host_arch ||
159+
process_host_arch.GetTriple().getOS() == llvm::Triple::MacOSX) {
160+
result.push_back(ArchSpec("arm64-apple-ios"));
161+
result.push_back(ArchSpec("arm64e-apple-ios"));
162+
}
154163
}
155164
#else
156165
x86GetSupportedArchitectures(result);

lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class PlatformMacOSX : public PlatformDarwin {
4747
return PlatformDarwin::GetFile(source, destination);
4848
}
4949

50-
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures() override;
50+
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures(
51+
const lldb_private::ArchSpec &process_host_arch) override;
5152

5253
lldb_private::ConstString
5354
GetSDKDirectory(lldb_private::Target &target) override;

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteAppleBridge.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ llvm::StringRef PlatformRemoteAppleBridge::GetDescriptionStatic() {
138138
return "Remote BridgeOS platform plug-in.";
139139
}
140140

141-
std::vector<ArchSpec> PlatformRemoteAppleBridge::GetSupportedArchitectures() {
141+
std::vector<ArchSpec> PlatformRemoteAppleBridge::GetSupportedArchitectures(
142+
const ArchSpec &process_host_arch) {
142143
return {ArchSpec("arm64-apple-bridgeos")};
143144
}
144145

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteAppleBridge.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class PlatformRemoteAppleBridge : public PlatformRemoteDarwinDevice {
4040

4141
llvm::StringRef GetDescription() override { return GetDescriptionStatic(); }
4242

43-
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures() override;
43+
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures(
44+
const lldb_private::ArchSpec &process_host_arch) override;
4445

4546
protected:
4647
llvm::StringRef GetDeviceSupportDirectoryName() override;

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteAppleTV.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ llvm::StringRef PlatformRemoteAppleTV::GetDescriptionStatic() {
133133
return "Remote Apple TV platform plug-in.";
134134
}
135135

136-
std::vector<ArchSpec> PlatformRemoteAppleTV::GetSupportedArchitectures() {
136+
std::vector<ArchSpec> PlatformRemoteAppleTV::GetSupportedArchitectures(
137+
const ArchSpec &process_host_arch) {
137138
ArchSpec system_arch(GetSystemArchitecture());
138139

139140
const ArchSpec::Core system_core = system_arch.GetCore();

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteAppleTV.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class PlatformRemoteAppleTV : public PlatformRemoteDarwinDevice {
4040

4141
llvm::StringRef GetDescription() override { return GetDescriptionStatic(); }
4242

43-
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures() override;
43+
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures(
44+
const lldb_private::ArchSpec &process_host_arch) override;
4445

4546
protected:
4647
llvm::StringRef GetDeviceSupportDirectoryName() override;

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteAppleWatch.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ llvm::StringRef PlatformRemoteAppleWatch::GetDescriptionStatic() {
144144
PlatformRemoteAppleWatch::PlatformRemoteAppleWatch()
145145
: PlatformRemoteDarwinDevice() {}
146146

147-
std::vector<ArchSpec> PlatformRemoteAppleWatch::GetSupportedArchitectures() {
147+
std::vector<ArchSpec>
148+
PlatformRemoteAppleWatch::GetSupportedArchitectures(const ArchSpec &host_info) {
148149
ArchSpec system_arch(GetSystemArchitecture());
149150

150151
const ArchSpec::Core system_core = system_arch.GetCore();

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteAppleWatch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class PlatformRemoteAppleWatch : public PlatformRemoteDarwinDevice {
4343

4444
// lldb_private::Platform functions
4545

46-
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures() override;
46+
std::vector<lldb_private::ArchSpec> GetSupportedArchitectures(
47+
const lldb_private::ArchSpec &process_host_arch) override;
4748

4849
protected:
4950
llvm::StringRef GetDeviceSupportDirectoryName() override;

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteDarwinDevice.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ Status PlatformRemoteDarwinDevice::ResolveExecutable(
9292
// the correct order) and see if we can find a match that way
9393
StreamString arch_names;
9494
llvm::ListSeparator LS;
95-
for (const ArchSpec &arch : GetSupportedArchitectures()) {
95+
ArchSpec process_host_arch;
96+
for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
9697
resolved_module_spec.GetArchitecture() = arch;
9798
error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
9899
nullptr, nullptr, nullptr);

0 commit comments

Comments
 (0)