Skip to content

Commit 346fe5a

Browse files
committed
[dsymutil] Also detect external downloadable toolchains (llvm#93872)
and reject them when copying Swift interface files, since they can live outside of DEVELOPER_DIR. (cherry picked from commit 22ada55)
1 parent 4264197 commit 346fe5a

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

llvm/include/llvm/DWARFLinker/Utils.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
3939
/// Make a best effort to guess the
4040
/// Xcode.app/Contents/Developer path from an SDK path.
4141
inline StringRef guessDeveloperDir(StringRef SysRoot) {
42-
SmallString<128> Result;
4342
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
4443
auto it = sys::path::rbegin(SysRoot);
4544
auto end = sys::path::rend(SysRoot);
@@ -74,6 +73,28 @@ inline StringRef guessDeveloperDir(StringRef SysRoot) {
7473
return {};
7574
}
7675

76+
/// Make a best effort to determine whether Path is inside a toolchain.
77+
inline bool isInToolchainDir(StringRef Path) {
78+
// Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-05-15-a.xctoolchain/usr/lib/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.private.swiftinterface
79+
for (auto it = sys::path::rbegin(Path), end = sys::path::rend(Path);
80+
it != end; ++it) {
81+
if (it->ends_with(".xctoolchain")) {
82+
++it;
83+
if (it == end)
84+
return false;
85+
if (*it != "Toolchains")
86+
return false;
87+
++it;
88+
if (it == end)
89+
return false;
90+
if (*it != "Developer")
91+
return false;
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
7798
inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
7899
// Debug info can contain paths from any OS, not necessarily
79100
// an OS we're currently running on. Moreover different compilation units can

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ static void analyzeImportedModule(
203203
StringRef DeveloperDir = guessDeveloperDir(SysRoot);
204204
if (!DeveloperDir.empty() && Path.starts_with(DeveloperDir))
205205
return;
206+
if (isInToolchainDir(Path))
207+
return;
206208
std::optional<const char *> Name =
207209
dwarf::toString(DIE.find(dwarf::DW_AT_name));
208210
if (!Name)

llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ void CompileUnit::analyzeImportedModule(const DWARFDebugInfoEntry *DieEntry) {
273273
StringRef DeveloperDir = guessDeveloperDir(SysRoot);
274274
if (!DeveloperDir.empty() && Path.starts_with(DeveloperDir))
275275
return;
276+
if (isInToolchainDir(Path))
277+
return;
276278
if (std::optional<DWARFFormValue> Val = find(DieEntry, dwarf::DW_AT_name)) {
277279
Expected<const char *> Name = Val->getAsCString();
278280
if (!Name) {

llvm/unittests/DWARFLinkerParallel/DWARFLinkerTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ TEST(DWARFLinker, PathTest) {
2424
"/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk"),
2525
DEVELOPER_DIR);
2626
EXPECT_EQ(guessDeveloperDir(DEVELOPER_DIR "/SDKs/MacOSX.sdk"), DEVELOPER_DIR);
27+
EXPECT_TRUE(
28+
isInToolchainDir("/Library/Developer/Toolchains/"
29+
"swift-DEVELOPMENT-SNAPSHOT-2024-05-15-a.xctoolchain/"
30+
"usr/lib/swift/macosx/_StringProcessing.swiftmodule/"
31+
"arm64-apple-macos.private.swiftinterface"));
32+
EXPECT_FALSE(isInToolchainDir("/Foo/not-an.xctoolchain/Bar/Baz"));
2733
}
2834

2935
} // anonymous namespace

0 commit comments

Comments
 (0)