Skip to content

Commit 109c85e

Browse files
Merge pull request #72291 from cachemeifyoucan/eng/PR-123711823
[ScanDependency] Move binary module validation into scanner
2 parents da3c12e + d4c90d6 commit 109c85e

File tree

57 files changed

+427
-245
lines changed

Some content is hidden

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

57 files changed

+427
-245
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,9 @@ REMARK(module_api_import_aliases,none,
11931193
"%select{, which reexports definition from %2|}3",
11941194
(const Decl *, ModuleDecl *, ModuleDecl *, bool))
11951195

1196+
REMARK(skip_module_invalid,none,"skip invalid swiftmodule: %0", (StringRef))
1197+
REMARK(skip_module_testable,none,"skip swiftmodule built with '-enable-testing': %0", (StringRef))
1198+
11961199
REMARK(macro_loaded,none,
11971200
"loaded macro implementation module %0 from "
11981201
"%select{shared library '%2'|executable '%2'|"

include/swift/AST/ModuleDependencies.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,12 @@ class ModuleDependenciesCache {
11151115
std::optional<const ModuleDependencyInfo *>
11161116
findDependency(StringRef moduleName) const;
11171117

1118+
/// Look for known existing dependencies.
1119+
///
1120+
/// \returns the cached result.
1121+
const ModuleDependencyInfo &
1122+
findKnownDependency(const ModuleDependencyID &moduleID) const;
1123+
11181124
/// Record dependencies for the given module.
11191125
void recordDependency(StringRef moduleName,
11201126
ModuleDependencyInfo dependencies);

include/swift/AST/SearchPathOptions.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ enum class ModuleSearchPathKind {
3838
RuntimeLibrary,
3939
};
4040

41+
/// Specifies how to load modules when both a module interface and serialized
42+
/// AST are present, or whether to disallow one format or the other altogether.
43+
enum class ModuleLoadingMode {
44+
PreferInterface,
45+
PreferSerialized,
46+
OnlyInterface,
47+
OnlySerialized
48+
};
49+
4150
/// A single module search path that can come from different sources, e.g.
4251
/// framework search paths, import search path etc.
4352
class ModuleSearchPath : public llvm::RefCountedBase<ModuleSearchPath> {
@@ -499,6 +508,12 @@ class SearchPathOptions {
499508
/// original form.
500509
PathObfuscator DeserializedPathRecoverer;
501510

511+
/// Specify the module loading behavior of the compilation.
512+
ModuleLoadingMode ModuleLoadMode = ModuleLoadingMode::PreferSerialized;
513+
514+
/// Legacy scanner search behavior.
515+
bool NoScannerModuleValidation = false;
516+
502517
/// Return all module search paths that (non-recursively) contain a file whose
503518
/// name is in \p Filenames.
504519
SmallVector<const ModuleSearchPath *, 4>
@@ -546,7 +561,9 @@ class SearchPathOptions {
546561
RuntimeResourcePath,
547562
hash_combine_range(RuntimeLibraryImportPaths.begin(),
548563
RuntimeLibraryImportPaths.end()),
549-
DisableModulesValidateSystemDependencies);
564+
DisableModulesValidateSystemDependencies,
565+
NoScannerModuleValidation,
566+
ModuleLoadMode);
550567
}
551568

552569
/// Return a hash code of any components from these options that should

include/swift/Option/FrontendOptions.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,4 +1348,10 @@ def disable_strict_concurrency_region_based_isolation : Flag<["-"],
13481348
HelpText<"Disable region based isolation when running with strict concurrency enabled. Only enabled with asserts">,
13491349
Flags<[HelpHidden]>;
13501350

1351+
def no_scanner_module_validation: Flag<["-"], "no-scanner-module-validation">,
1352+
HelpText<"Do not validate binary modules in scanner and delegate the validation to swift-frontend">;
1353+
def module_load_mode: Separate<["-"], "module-load-mode">,
1354+
MetaVarName<"only-interface|prefer-interface|prefer-serialized|only-serialized">,
1355+
HelpText<"Module loading mode">;
1356+
13511357
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Serialization/ScanningLoaders.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
3434

3535
/// Scan the given interface file to determine dependencies.
3636
llvm::ErrorOr<ModuleDependencyInfo>
37-
scanInterfaceFile(Twine moduleInterfacePath, bool isFramework);
37+
scanInterfaceFile(Twine moduleInterfacePath, bool isFramework,
38+
bool isTestableImport);
3839

3940
InterfaceSubContextDelegate &astDelegate;
4041

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,20 @@
1616
#include "swift/AST/FileUnit.h"
1717
#include "swift/AST/Module.h"
1818
#include "swift/AST/ModuleLoader.h"
19+
#include "swift/AST/SearchPathOptions.h"
1920
#include "llvm/Support/MemoryBuffer.h"
2021
#include "llvm/Support/PrefixMapper.h"
2122
#include "llvm/TargetParser/Triple.h"
2223

2324
namespace swift {
2425
class ModuleFile;
2526
class PathObfuscator;
27+
class ModuleFileSharedCore;
2628
enum class ModuleLoadingBehavior;
2729
namespace file_types {
2830
enum ID : uint8_t;
2931
}
3032

31-
/// Specifies how to load modules when both a module interface and serialized
32-
/// AST are present, or whether to disallow one format or the other altogether.
33-
enum class ModuleLoadingMode {
34-
PreferInterface,
35-
PreferSerialized,
36-
OnlyInterface,
37-
OnlySerialized
38-
};
39-
4033
/// How a dependency should be loaded.
4134
///
4235
/// \sa getTransitiveLoadingBehavior
@@ -170,22 +163,18 @@ class SerializedModuleLoaderBase : public ModuleLoader {
170163
}
171164

172165
/// Scan the given serialized module file to determine dependencies.
173-
llvm::ErrorOr<ModuleDependencyInfo> scanModuleFile(Twine modulePath, bool isFramework);
166+
llvm::ErrorOr<ModuleDependencyInfo>
167+
scanModuleFile(Twine modulePath, bool isFramework, bool isTestableImport);
174168

175169
struct BinaryModuleImports {
176170
llvm::StringSet<> moduleImports;
177171
std::string headerImport;
178172
};
179173

180-
static llvm::ErrorOr<BinaryModuleImports>
181-
getImportsOfModule(Twine modulePath,
174+
static BinaryModuleImports
175+
getImportsOfModule(const ModuleFileSharedCore &loadedModule,
182176
ModuleLoadingBehavior transitiveBehavior,
183-
bool isFramework,
184-
bool isRequiredOSSAModules,
185-
StringRef SDKName,
186-
StringRef packageName,
187-
llvm::vfs::FileSystem *fileSystem,
188-
PathObfuscator &recoverer);
177+
StringRef packageName);
189178

190179
/// Load the module file into a buffer and also collect its module name.
191180
static std::unique_ptr<llvm::MemoryBuffer>

lib/AST/ModuleDependencies.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,13 @@ ModuleDependenciesCache::findDependency(StringRef moduleName) const {
740740
return std::nullopt;
741741
}
742742

743+
const ModuleDependencyInfo &ModuleDependenciesCache::findKnownDependency(
744+
const ModuleDependencyID &moduleID) const {
745+
auto dep = findDependency(moduleID);
746+
assert(dep && "dependency unknown");
747+
return **dep;
748+
}
749+
743750
bool ModuleDependenciesCache::hasDependency(const ModuleDependencyID &moduleID) const {
744751
return hasDependency(moduleID.ModuleName, moduleID.Kind);
745752
}

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ ModuleDependencyScanningWorker::ModuleDependencyScanningWorker(
174174
ScanASTContext,
175175
*static_cast<ModuleInterfaceCheckerImpl *>(
176176
ScanASTContext.getModuleInterfaceChecker()),
177-
&DependencyTracker, ModuleLoadingMode::OnlyInterface);
177+
&DependencyTracker,
178+
ScanCompilerInvocation.getSearchPathOptions().ModuleLoadMode);
178179
}
179180

180181
ModuleDependencyVector

0 commit comments

Comments
 (0)