Skip to content

Commit cf50dd0

Browse files
authored
Merge pull request #82552 from artemcm/ScannerDiagnoseWrontTargetCandidate
[Dependency Scanning] Discard and diagnose discovered binary modules built for an incompatible target
2 parents 8682ed3 + c8714b5 commit cf50dd0

File tree

8 files changed

+60
-22
lines changed

8 files changed

+60
-22
lines changed

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
185185
bool isFramework,
186186
bool isRequiredOSSAModules,
187187
StringRef SDKName,
188+
const llvm::Triple &target,
188189
StringRef packageName,
189190
llvm::vfs::FileSystem *fileSystem,
190191
PathObfuscator &recoverer);

include/swift/Serialization/Validation.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
#include "llvm/ADT/ArrayRef.h"
2424
#include "llvm/ADT/SmallVector.h"
2525
#include "llvm/ADT/StringRef.h"
26-
27-
namespace llvm {
28-
class Triple;
29-
}
26+
#include "llvm/TargetParser/Triple.h"
3027

3128
namespace swift {
3229

@@ -296,6 +293,8 @@ struct SearchPath {
296293
/// compiled with -enable-ossa-modules.
297294
/// \param requiredSDK If not empty, only accept modules built with
298295
/// a compatible SDK. The StringRef represents the canonical SDK name.
296+
/// \param target The target triple of the current compilation for
297+
/// validating that the module we are attempting to load is compatible.
299298
/// \param[out] extendedInfo If present, will be populated with additional
300299
/// compilation options serialized into the AST at build time that may be
301300
/// necessary to load it properly.
@@ -307,7 +306,8 @@ ValidationInfo validateSerializedAST(
307306
ExtendedValidationInfo *extendedInfo = nullptr,
308307
SmallVectorImpl<SerializationOptions::FileDependency> *dependencies =
309308
nullptr,
310-
SmallVectorImpl<SearchPath> *searchPaths = nullptr);
309+
SmallVectorImpl<SearchPath> *searchPaths = nullptr,
310+
std::optional<llvm::Triple> target = std::nullopt);
311311

312312
/// Emit diagnostics explaining a failure to load a serialized AST.
313313
///

lib/Serialization/ModuleFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,8 @@ ModuleFile::getModuleName(ASTContext &Ctx, StringRef modulePath,
427427
serialization::ValidationInfo loadInfo = ModuleFileSharedCore::load(
428428
"", "", std::move(newBuf), nullptr, nullptr,
429429
/*isFramework=*/isFramework, Ctx.SILOpts.EnableOSSAModules,
430-
Ctx.LangOpts.SDKName, Ctx.SearchPathOpts.DeserializedPathRecoverer,
431-
loadedModuleFile);
430+
Ctx.LangOpts.SDKName, Ctx.LangOpts.Target,
431+
Ctx.SearchPathOpts.DeserializedPathRecoverer, loadedModuleFile);
432432
Name = loadedModuleFile->Name.str();
433433
return std::move(moduleBuf.get());
434434
}

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Support/MemoryBuffer.h"
2727
#include "llvm/Support/OnDiskHashTable.h"
2828
#include "llvm/Support/PrettyStackTrace.h"
29+
#include "llvm/TargetParser/Triple.h"
2930

3031
using namespace swift;
3132
using namespace swift::serialization;
@@ -241,6 +242,7 @@ static ValidationInfo validateControlBlock(
241242
bool requiresOSSAModules,
242243
bool requiresRevisionMatch,
243244
StringRef requiredSDK,
245+
std::optional<llvm::Triple> target,
244246
ExtendedValidationInfo *extendedInfo,
245247
PathObfuscator &pathRecoverer) {
246248
// The control block is malformed until we've at least read a major version
@@ -376,9 +378,17 @@ static ValidationInfo validateControlBlock(
376378
case control_block::MODULE_NAME:
377379
result.name = blobData;
378380
break;
379-
case control_block::TARGET:
381+
case control_block::TARGET: {
380382
result.targetTriple = blobData;
383+
if (target &&
384+
!areCompatible(*target, llvm::Triple(llvm::Triple::normalize(
385+
result.targetTriple)))) {
386+
result.status = Status::TargetIncompatible;
387+
return result;
388+
}
389+
381390
break;
391+
}
382392
case control_block::ALLOWABLE_CLIENT_NAME:
383393
result.allowableClients.push_back(blobData);
384394
break;
@@ -606,7 +616,8 @@ ValidationInfo serialization::validateSerializedAST(
606616
StringRef requiredSDK,
607617
ExtendedValidationInfo *extendedInfo,
608618
SmallVectorImpl<SerializationOptions::FileDependency> *dependencies,
609-
SmallVectorImpl<SearchPath> *searchPaths) {
619+
SmallVectorImpl<SearchPath> *searchPaths,
620+
std::optional<llvm::Triple> target) {
610621
ValidationInfo result;
611622

612623
// Check 32-bit alignment.
@@ -649,7 +660,7 @@ ValidationInfo serialization::validateSerializedAST(
649660
{SWIFTMODULE_VERSION_MAJOR, SWIFTMODULE_VERSION_MINOR},
650661
requiresOSSAModules,
651662
/*requiresRevisionMatch=*/true,
652-
requiredSDK,
663+
requiredSDK, target,
653664
extendedInfo, localObfuscator);
654665
if (result.status != Status::Valid)
655666
return result;
@@ -1201,7 +1212,8 @@ bool ModuleFileSharedCore::readModuleDocIfPresent(PathObfuscator &pathRecoverer)
12011212
docCursor, scratch, {SWIFTDOC_VERSION_MAJOR, SWIFTDOC_VERSION_MINOR},
12021213
RequiresOSSAModules,
12031214
/*requiresRevisionMatch*/false,
1204-
/*requiredSDK*/StringRef(), /*extendedInfo*/nullptr, pathRecoverer);
1215+
/*requiredSDK*/StringRef(), /*target*/std::nullopt,
1216+
/*extendedInfo*/nullptr, pathRecoverer);
12051217
if (info.status != Status::Valid)
12061218
return false;
12071219
// Check that the swiftdoc is actually for this module.
@@ -1346,7 +1358,8 @@ bool ModuleFileSharedCore::readModuleSourceInfoIfPresent(PathObfuscator &pathRec
13461358
{SWIFTSOURCEINFO_VERSION_MAJOR, SWIFTSOURCEINFO_VERSION_MINOR},
13471359
RequiresOSSAModules,
13481360
/*requiresRevisionMatch*/false,
1349-
/*requiredSDK*/StringRef(), /*extendedInfo*/nullptr, pathRecoverer);
1361+
/*requiredSDK*/StringRef(), /*target*/std::nullopt,
1362+
/*extendedInfo*/nullptr, pathRecoverer);
13501363
if (info.status != Status::Valid)
13511364
return false;
13521365
// Check that the swiftsourceinfo is actually for this module.
@@ -1423,6 +1436,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
14231436
bool isFramework,
14241437
bool requiresOSSAModules,
14251438
StringRef requiredSDK,
1439+
std::optional<llvm::Triple> target,
14261440
serialization::ValidationInfo &info, PathObfuscator &pathRecoverer)
14271441
: ModuleInputBuffer(std::move(moduleInputBuffer)),
14281442
ModuleDocInputBuffer(std::move(moduleDocInputBuffer)),
@@ -1475,7 +1489,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
14751489
cursor, scratch,
14761490
{SWIFTMODULE_VERSION_MAJOR, SWIFTMODULE_VERSION_MINOR},
14771491
RequiresOSSAModules,
1478-
/*requiresRevisionMatch=*/true, requiredSDK,
1492+
/*requiresRevisionMatch=*/true, requiredSDK, target,
14791493
&extInfo, pathRecoverer);
14801494
if (info.status != Status::Valid) {
14811495
error(info.status);

lib/Serialization/ModuleFileSharedCore.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ class ModuleFileSharedCore {
444444
bool isFramework,
445445
bool requiresOSSAModules,
446446
StringRef requiredSDK,
447+
std::optional<llvm::Triple> target,
447448
serialization::ValidationInfo &info, PathObfuscator &pathRecoverer);
448449

449450
/// Change the status of the current module.
@@ -571,6 +572,10 @@ class ModuleFileSharedCore {
571572
/// linking purposes.
572573
/// \param requiresOSSAModules If true, this requires dependent modules to be
573574
/// compiled with -enable-ossa-modules.
575+
/// \param requiredSDK A string denoting the name of the currently-used SDK,
576+
/// to ensure that the loaded module was built with a compatible SDK.
577+
/// \param target The target triple of the current compilation for
578+
/// validating that the module we are attempting to load is compatible.
574579
/// \param[out] theModule The loaded module.
575580
/// \returns Whether the module was successfully loaded, or what went wrong
576581
/// if it was not.
@@ -580,13 +585,14 @@ class ModuleFileSharedCore {
580585
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
581586
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
582587
bool isFramework, bool requiresOSSAModules,
583-
StringRef requiredSDK, PathObfuscator &pathRecoverer,
588+
StringRef requiredSDK, std::optional<llvm::Triple> target,
589+
PathObfuscator &pathRecoverer,
584590
std::shared_ptr<const ModuleFileSharedCore> &theModule) {
585591
serialization::ValidationInfo info;
586592
auto *core = new ModuleFileSharedCore(
587593
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
588594
std::move(moduleSourceInfoInputBuffer), isFramework,
589-
requiresOSSAModules, requiredSDK, info,
595+
requiresOSSAModules, requiredSDK, target, info,
590596
pathRecoverer);
591597
if (!moduleInterfacePath.empty()) {
592598
ArrayRef<char> path;

lib/Serialization/ScanningLoaders.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ SwiftModuleScanner::scanInterfaceFile(Identifier moduleID,
238238
auto adjacentBinaryModulePackageOnlyImports =
239239
getMatchingPackageOnlyImportsOfModule(
240240
*adjacentBinaryModule, isFramework, isRequiredOSSAModules(),
241-
Ctx.LangOpts.SDKName, ScannerPackageName,
242-
Ctx.SourceMgr.getFileSystem().get(),
241+
Ctx.LangOpts.SDKName, Ctx.LangOpts.Target,
242+
ScannerPackageName, Ctx.SourceMgr.getFileSystem().get(),
243243
Ctx.SearchPathOpts.DeserializedPathRecoverer);
244244

245245
if (!adjacentBinaryModulePackageOnlyImports)
@@ -279,7 +279,7 @@ llvm::ErrorOr<ModuleDependencyInfo> SwiftModuleScanner::scanBinaryModuleFile(
279279
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFile;
280280
serialization::ValidationInfo loadInfo = ModuleFileSharedCore::load(
281281
"", "", std::move(moduleBuf.get()), nullptr, nullptr, isFramework,
282-
isRequiredOSSAModules(), Ctx.LangOpts.SDKName,
282+
isRequiredOSSAModules(), Ctx.LangOpts.SDKName, Ctx.LangOpts.Target,
283283
Ctx.SearchPathOpts.DeserializedPathRecoverer, loadedModuleFile);
284284

285285
if (Ctx.SearchPathOpts.ScannerModuleValidation) {

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ std::optional<std::string> SerializedModuleLoaderBase::invalidModuleReason(seria
344344
llvm::ErrorOr<std::vector<ScannerImportStatementInfo>>
345345
SerializedModuleLoaderBase::getMatchingPackageOnlyImportsOfModule(
346346
Twine modulePath, bool isFramework, bool isRequiredOSSAModules,
347-
StringRef SDKName, StringRef packageName, llvm::vfs::FileSystem *fileSystem,
348-
PathObfuscator &recoverer) {
347+
StringRef SDKName, const llvm::Triple &target, StringRef packageName,
348+
llvm::vfs::FileSystem *fileSystem, PathObfuscator &recoverer) {
349349
auto moduleBuf = fileSystem->getBufferForFile(modulePath);
350350
if (!moduleBuf)
351351
return moduleBuf.getError();
@@ -355,7 +355,7 @@ SerializedModuleLoaderBase::getMatchingPackageOnlyImportsOfModule(
355355
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFile;
356356
serialization::ValidationInfo loadInfo = ModuleFileSharedCore::load(
357357
"", "", std::move(moduleBuf.get()), nullptr, nullptr, isFramework,
358-
isRequiredOSSAModules, SDKName, recoverer, loadedModuleFile);
358+
isRequiredOSSAModules, SDKName, target, recoverer, loadedModuleFile);
359359

360360
if (loadedModuleFile->getModulePackageName() != packageName)
361361
return importedModuleNames;
@@ -940,7 +940,7 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
940940
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
941941
std::move(moduleSourceInfoInputBuffer), isFramework,
942942
isRequiredOSSAModules(),
943-
Ctx.LangOpts.SDKName,
943+
Ctx.LangOpts.SDKName, Ctx.LangOpts.Target,
944944
Ctx.SearchPathOpts.DeserializedPathRecoverer, loadedModuleFileCore);
945945
SerializedASTFile *fileUnit = nullptr;
946946

@@ -1482,6 +1482,7 @@ std::string swift::extractEmbeddedBridgingHeaderContent(
14821482
serialization::ValidationInfo loadInfo = ModuleFileSharedCore::load(
14831483
"", "", std::move(file), nullptr, nullptr, false,
14841484
Context.SILOpts.EnableOSSAModules, Context.LangOpts.SDKName,
1485+
Context.LangOpts.Target,
14851486
Context.SearchPathOpts.DeserializedPathRecoverer,
14861487
loadedModuleFile);
14871488

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/module-cache)
3+
// RUN: %empty-directory(%t/moduleInputs)
4+
// RUN: split-file %s %t
5+
6+
// ERROR: error: unable to resolve Swift module dependency to a compatible module: 'FooBar'
7+
// ERROR: note: found incompatible module '{{.*}}{{/|\\}}moduleInputs{{/|\\}}FooBar.swiftmodule': compiled for a different target platform
8+
9+
// RUN: %target-swift-frontend -emit-module %t/FooBar.swift -emit-module-path %t/moduleInputs/FooBar.swiftmodule -module-name FooBar -target x86_64-unknown-linux-gnu -parse-stdlib
10+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %t/main.swift -o %t/deps.json -I %t/moduleInputs -I %t/moduleInputs2 -diagnostic-style llvm -scanner-module-validation -target arm64e-apple-macosx11.0 2>&1 | %FileCheck %s -check-prefix=ERROR
11+
12+
//--- main.swift
13+
import FooBar
14+
15+
//--- FooBar.swift
16+
public func fooBar() {}

0 commit comments

Comments
 (0)