Skip to content

Commit ffef4da

Browse files
authored
Merge pull request #33172 from xymus/volatile-swiftmodules-5.3
[5.3][Serialization] Add option to load swiftmodule files as volatile and avoid mmap
2 parents 20b155a + c7053b8 commit ffef4da

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ namespace swift {
358358
/// If set to \c false, fall back to the legacy manual reference name tracking code.
359359
bool EnableRequestBasedIncrementalDependencies = true;
360360

361+
/// Load swiftmodule files in memory as volatile and avoid mmap.
362+
bool EnableVolatileModules = false;
363+
361364
/// Sets the target we are building for and updates platform conditions
362365
/// to match.
363366
///

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ def warn_long_expression_type_checking_EQ : Joined<["-"], "warn-long-expression-
455455
def Rmodule_interface_rebuild : Flag<["-"], "Rmodule-interface-rebuild">,
456456
HelpText<"Emits a remark if an imported module needs to be re-compiled from its module interface">;
457457

458+
def enable_volatile_modules : Flag<["-"], "enable-volatile-modules">,
459+
HelpText<"Load Swift modules in memory">;
460+
458461
def solver_expression_time_threshold_EQ : Joined<["-"], "solver-expression-time-threshold=">;
459462

460463
def solver_disable_shrink :

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
603603

604604
Opts.VerifyAllSubstitutionMaps |= Args.hasArg(OPT_verify_all_substitution_maps);
605605

606+
Opts.EnableVolatileModules |= Args.hasArg(OPT_enable_volatile_modules);
607+
606608
Opts.UseDarwinPreStableABIBit =
607609
(Target.isMacOSX() && Target.isMacOSXVersionLT(10, 14, 4)) ||
608610
(Target.isiOS() && Target.isOSVersionLT(12, 2)) ||

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,27 @@ std::error_code SerializedModuleLoaderBase::openModuleFile(
337337
}
338338

339339
// Actually load the file and error out if necessary.
340+
//
341+
// Use the default arguments except for IsVolatile that is set by the
342+
// frontend option -enable-volatile-modules. If set, we avoid the use of
343+
// mmap to workaround issues on NFS when the swiftmodule file loaded changes
344+
// on disk while it's in use.
345+
//
346+
// In practice, a swiftmodule file can chane when a client uses a
347+
// swiftmodule file from a framework while the framework is recompiled and
348+
// installed over existing files. Or when many processes rebuild the same
349+
// module interface.
350+
//
351+
// We have seen these scenarios leading to deserialization errors that on
352+
// the surface look like memory corruption.
353+
//
354+
// rdar://63755989
355+
bool enableVolatileModules = Ctx.LangOpts.EnableVolatileModules;
340356
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ModuleOrErr =
341-
FS.getBufferForFile(ModulePath);
357+
FS.getBufferForFile(ModulePath,
358+
/*FileSize=*/-1,
359+
/*RequiresNullTerminator=*/true,
360+
/*IsVolatile=*/enableVolatileModules);
342361
if (!ModuleOrErr)
343362
return ModuleOrErr.getError();
344363

0 commit comments

Comments
 (0)