Skip to content

[Serialization] Add warning when .swiftsourceinfo is malformed #37237

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 1 commit into from
May 6, 2021
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ ERROR(serialization_error_type,Fatal,
ERROR(serialization_allowing_error_type,none,
"allowing deserialization of error type '%0' in module '%1'",
(StringRef, StringRef))
WARNING(serialization_malformed_sourceinfo,none,
"unable to use malformed module source info '%0'", (StringRef))

ERROR(reserved_member_name,none,
"type member must not be named %0, since it would conflict with the"
Expand Down
7 changes: 7 additions & 0 deletions lib/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ class ModuleFile
/// \c true if this module has incremental dependency information.
bool hasIncrementalInfo() const { return Core->hasIncrementalInfo(); }

/// \c true if this module has a corresponding .swiftsourceinfo file.
bool hasSourceInfoFile() const { return Core->hasSourceInfoFile(); }

/// \c true if this module has information from a corresponding
/// .swiftsourceinfo file (ie. the file exists and has been read).
bool hasSourceInfo() const { return Core->hasSourceInfo(); }

/// Associates this module file with the AST node representing it.
///
/// Checks that the file is compatible with the AST module it's being loaded
Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/ModuleFileSharedCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,3 +1490,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
return;
}
}

bool ModuleFileSharedCore::hasSourceInfo() const {
return !!DeclUSRsTable;
}
7 changes: 7 additions & 0 deletions lib/Serialization/ModuleFileSharedCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,13 @@ class ModuleFileSharedCore {
/// Returns \c true if this module file contains a section with incremental
/// information.
bool hasIncrementalInfo() const { return HasIncrementalInfo; }

/// Returns \c true if a corresponding .swiftsourceinfo has been found.
bool hasSourceInfoFile() const { return !!ModuleSourceInfoInputBuffer; }

/// Returns \c true if a corresponding .swiftsourceinfo has been found *and
/// read*.
bool hasSourceInfo() const;
};

template <typename T, typename RawData>
Expand Down
12 changes: 12 additions & 0 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,16 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
bool isFramework) {
assert(moduleInputBuffer);

// The buffers are moved into the shared core, so grab their IDs now in case
// they're needed for diagnostics later.
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
StringRef moduleDocBufferID;
if (moduleDocInputBuffer)
moduleDocBufferID = moduleDocInputBuffer->getBufferIdentifier();
StringRef moduleSourceInfoID;
if (moduleSourceInfoInputBuffer)
moduleSourceInfoID = moduleSourceInfoInputBuffer->getBufferIdentifier();

if (moduleInputBuffer->getBufferSize() % 4 != 0) {
if (diagLoc)
Expand Down Expand Up @@ -742,6 +748,12 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
(Ctx.LangOpts.AllowModuleWithCompilerErrors &&
(loadInfo.status == serialization::Status::TargetTooNew ||
loadInfo.status == serialization::Status::TargetIncompatible))) {
if (loadedModuleFile->hasSourceInfoFile() &&
!loadedModuleFile->hasSourceInfo())
Ctx.Diags.diagnose(diagLocOrInvalid,
diag::serialization_malformed_sourceinfo,
moduleSourceInfoID);

Ctx.bumpGeneration();
LoadedModuleFiles.emplace_back(std::move(loadedModuleFile),
Ctx.getCurrentGeneration());
Expand Down
17 changes: 17 additions & 0 deletions test/Serialization/load-invalid-sourceinfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t %S/../Inputs/empty.swift
// RUN: %target-swift-frontend -typecheck -I %t %s

// RUN: touch %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify

// RUN: echo -n 'a' > %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify

// RUN: echo -n 'abcd' > %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify

// RUN: echo -n 'abcde' > %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify

import empty // expected-warning{{unable to use malformed module source info}}