Skip to content

Commit a5ccbf3

Browse files
committed
[Serialization] Only remark if the last digit mismatches in precise tag check
Weaken the precise tag check at loading swiftmodule to accept binary modules build by a compiler with a tag where only the last digit is different. We assume that the other digit in the version should ensure compiler and stdlib compatibility. If the last digit doesn't match, still raise a remark. rdar://105158258
1 parent e079db2 commit a5ccbf3

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,13 @@ ERROR(serialization_module_too_old,Fatal,
804804
"rebuild %0 and try again: %1",
805805
(Identifier, StringRef))
806806
ERROR(serialization_module_incompatible_revision,Fatal,
807-
"compiled module was created by a different version of the compiler; "
808-
"rebuild %0 and try again: %1",
809-
(Identifier, StringRef))
807+
"compiled module was created by a different version of the compiler '%0'; "
808+
"rebuild %1 and try again: %2",
809+
(StringRef, Identifier, StringRef))
810+
REMARK(serialization_module_problematic_revision, none,
811+
"compiled module was created by a different version of the compiler '%0': "
812+
"%1",
813+
(StringRef, StringRef))
810814
ERROR(serialization_missing_single_dependency,Fatal,
811815
"missing required module '%0'", (StringRef))
812816
ERROR(serialization_missing_dependencies,Fatal,

include/swift/Serialization/Validation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct ValidationInfo {
9191
version::Version compatibilityVersion = {};
9292
llvm::VersionTuple userModuleVersion;
9393
StringRef sdkName = {};
94+
StringRef problematicRevision = {};
9495
size_t bytes = 0;
9596
Status status = Status::Malformed;
9697
std::vector<StringRef> allowableClients;

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,18 @@ static ValidationInfo validateControlBlock(
372372
StringRef compilerRevision = forcedDebugRevision ?
373373
forcedDebugRevision : version::getCurrentCompilerSerializationTag();
374374
if (moduleRevision != compilerRevision) {
375-
result.status = Status::RevisionIncompatible;
375+
// The module versions are mismatching, record it and diagnose later.
376+
result.problematicRevision = moduleRevision;
376377

377-
// We can't trust this module format at this point.
378-
return result;
378+
// Reject the module only it still mismatches without the last digit.
379+
StringRef compilerRevisionHead = compilerRevision.rsplit('.').first;
380+
StringRef moduleRevisionHead = moduleRevision.rsplit('.').first;
381+
if (moduleRevisionHead != compilerRevisionHead) {
382+
result.status = Status::RevisionIncompatible;
383+
384+
// We can't trust the module format at this point.
385+
return result;
386+
}
379387
}
380388
}
381389
break;

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,13 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
836836
if (loadedModuleFile &&
837837
loadedModuleFile->mayHaveDiagnosticsPointingAtBuffer())
838838
OrphanedModuleFiles.push_back(std::move(loadedModuleFile));
839+
} else {
840+
// Report non-fatal compiler tag mismatch.
841+
if (!loadInfo.problematicRevision.empty()) {
842+
Ctx.Diags.diagnose(*diagLoc,
843+
diag::serialization_module_problematic_revision,
844+
loadInfo.problematicRevision, moduleBufferID);
845+
}
839846
}
840847

841848
// The -experimental-hermetic-seal-at-link flag turns on dead-stripping
@@ -896,7 +903,7 @@ void swift::serialization::diagnoseSerializedASTLoadFailure(
896903
break;
897904
case serialization::Status::RevisionIncompatible:
898905
Ctx.Diags.diagnose(diagLoc, diag::serialization_module_incompatible_revision,
899-
ModuleName, moduleBufferID);
906+
loadInfo.problematicRevision, ModuleName, moduleBufferID);
900907
break;
901908
case serialization::Status::Malformed:
902909
Ctx.Diags.diagnose(diagLoc, diag::serialization_malformed_module,

test/Serialization/restrict-swiftmodule-to-revision.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public func foo() {}
99
/// Build Lib as a resilient and non-resilient swiftmodule
1010
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -swift-version 5 -o %t/build -parse-stdlib -module-cache-path %t/cache -module-name ResilientLib -enable-library-evolution
1111
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -swift-version 5 -o %t/build -parse-stdlib -module-cache-path %t/cache -module-name NonResilientLib
12-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=my-revision \
12+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
1313
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -swift-version 5 -o %t/build -parse-stdlib -module-cache-path %t/cache -module-name TaggedLib -enable-library-evolution
1414

1515

@@ -19,9 +19,9 @@ import NonResilientLib
1919
foo()
2020

2121
/// Building a NonResilientLib client should reject the import for a tagged compiler
22-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=my-revision \
22+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
2323
// RUN: not %target-swift-frontend -typecheck %t/NonResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck -check-prefix=CHECK-NON-RESILIENT %s
24-
// CHECK-NON-RESILIENT: compiled module was created by a different version of the compiler; rebuild 'NonResilientLib' and try again: {{.*}}NonResilientLib.swiftmodule
24+
// CHECK-NON-RESILIENT: compiled module was created by a different version of the compiler ''; rebuild 'NonResilientLib' and try again: {{.*}}NonResilientLib.swiftmodule
2525

2626

2727
/// 3. Test importing the resilient untagged library
@@ -33,12 +33,12 @@ foo()
3333
// RUN: %target-swift-frontend -typecheck %t/ResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
3434

3535
/// Building a ResilientLib client should reject the import for a tagged compiler
36-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=not-a-revision \
36+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
3737
// RUN: not %target-swift-frontend -typecheck %t/ResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck %s
38-
// CHECK: compiled module was created by a different version of the compiler; rebuild 'ResilientLib' and try again: {{.*}}ResilientLib.swiftmodule
38+
// CHECK: compiled module was created by a different version of the compiler ''; rebuild 'ResilientLib' and try again: {{.*}}ResilientLib.swiftmodule
3939

4040
/// Building a ResilientLib client should succeed for a tagged compiler with SWIFT_IGNORE_SWIFTMODULE_REVISION
41-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=not-a-revision SWIFT_IGNORE_SWIFTMODULE_REVISION=true \
41+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 SWIFT_IGNORE_SWIFTMODULE_REVISION=true \
4242
// RUN: %target-swift-frontend -typecheck %t/ResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
4343

4444

@@ -47,12 +47,19 @@ foo()
4747
import TaggedLib
4848
foo()
4949

50-
/// Importing TaggedLib should succeed with the same tag or a dev compiler
51-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=my-revision \
50+
/// Importing TaggedLib should succeed with the same tag.
51+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
5252
// RUN: %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
53+
54+
/// Importing TaggedLib should succeed with a dev compiler
5355
// RUN: %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
5456

57+
/// Importing TaggedLib should succeed but remark on a last digit difference.
58+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.2 \
59+
// RUN: %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck %s --check-prefix=CHECK-LAST-DIGIT
60+
// CHECK-LAST-DIGIT: remark: compiled module was created by a different version of the compiler '1.0.0.0.1': {{.*}}TaggedLib.swiftmodule
61+
5562
/// Building a TaggedLib client should reject the import for a different tagged compiler
56-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=not-a-revision \
63+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.1.1 \
5764
// RUN: not %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck %s --check-prefix=CHECK-TAGGED
58-
// CHECK-TAGGED: compiled module was created by a different version of the compiler; rebuild 'TaggedLib' and try again: {{.*}}TaggedLib.swiftmodule
65+
// CHECK-TAGGED: error: compiled module was created by a different version of the compiler '1.0.0.0.1'; rebuild 'TaggedLib' and try again: {{.*}}TaggedLib.swiftmodule

0 commit comments

Comments
 (0)