Skip to content

[5.3][Serialization] Add option to load swiftmodule files as volatile and avoid mmap #33172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ namespace swift {
/// If set to \c false, fall back to the legacy manual reference name tracking code.
bool EnableRequestBasedIncrementalDependencies = true;

/// Load swiftmodule files in memory as volatile and avoid mmap.
bool EnableVolatileModules = false;

/// Sets the target we are building for and updates platform conditions
/// to match.
///
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ def warn_long_expression_type_checking_EQ : Joined<["-"], "warn-long-expression-
def Rmodule_interface_rebuild : Flag<["-"], "Rmodule-interface-rebuild">,
HelpText<"Emits a remark if an imported module needs to be re-compiled from its module interface">;

def enable_volatile_modules : Flag<["-"], "enable-volatile-modules">,
HelpText<"Load Swift modules in memory">;

def solver_expression_time_threshold_EQ : Joined<["-"], "solver-expression-time-threshold=">;

def solver_disable_shrink :
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,

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

Opts.EnableVolatileModules |= Args.hasArg(OPT_enable_volatile_modules);

Opts.UseDarwinPreStableABIBit =
(Target.isMacOSX() && Target.isMacOSXVersionLT(10, 14, 4)) ||
(Target.isiOS() && Target.isOSVersionLT(12, 2)) ||
Expand Down
21 changes: 20 additions & 1 deletion lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,27 @@ std::error_code SerializedModuleLoaderBase::openModuleFile(
}

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

Expand Down