https://github.com/naveen-seth updated https://github.com/llvm/llvm-project/pull/133462
>From 957a45312767a40b513bfd4f545c23fda9c4866f Mon Sep 17 00:00:00 2001 From: naveen-seth <naveen.ha...@outlook.com> Date: Tue, 8 Apr 2025 00:56:14 +0000 Subject: [PATCH] [clang][modules] Guard against bad -fmodule-file mappings (#132059) [clang][modules] Guard against invalid -fmodule-file mappings Fixes #132059. Providing incorrect mappings via `-fmodule-file=<name>=<path/to/bmi>` can cause the compiler to crash when loading a module that imports an incorrectly mapped module. The crash occurs during AST body deserialization when the compiler attempts to resolve remappings using the `ModuleFile` from the incorrectly mapped module's BMI file. The cause is an invalid access into an incorrectly loaded `ModuleFile`. This commit fixes the issue by verifying the identity of the imported module. --- clang/lib/Serialization/ASTReader.cpp | 12 +++++ clang/test/Modules/fmodule-file-mismatch.cppm | 48 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 clang/test/Modules/fmodule-file-mismatch.cppm diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 0cd2cedb48dd9..2f1627d234a4e 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -3318,6 +3318,18 @@ ASTReader::ReadControlBlock(ModuleFile &F, Loaded, StoredSize, StoredModTime, StoredSignature, Capabilities); + // Check the AST we just read from ImportedFile contains a different + // module than we expected (ImportedName). This can occur for C++20 + // Modules when given a mismatch via -fmodule-file=<name>=<file> + if (IsImportingStdCXXModule) { + if (const auto *Imported = + getModuleManager().lookupByFileName(ImportedFile); + Imported != nullptr && Imported->ModuleName != ImportedName) { + Diag(diag::err_failed_to_find_module_file) << ImportedName; + Result = Missing; + } + } + // If we diagnosed a problem, produce a backtrace. bool recompilingFinalized = Result == OutOfDate && (Capabilities & ARR_OutOfDate) && diff --git a/clang/test/Modules/fmodule-file-mismatch.cppm b/clang/test/Modules/fmodule-file-mismatch.cppm new file mode 100644 index 0000000000000..351f3f4295bf0 --- /dev/null +++ b/clang/test/Modules/fmodule-file-mismatch.cppm @@ -0,0 +1,48 @@ +// RUN: rm -rf %t +// RUN: split-file %s %t +// RUN: cd %t + +// Related to issue #132059 + +// Precompile the module dependencies correctly +// RUN: %clang_cc1 -std=c++20 -emit-module-interface a.cppm -o a.pcm +// RUN: %clang_cc1 -std=c++20 -emit-module-interface b.cppm -o b.pcm \ +// RUN: -fmodule-file=A=a.pcm + +// Verify that providing incorrect mappings via +// `-fmodule-file=<name>=<path/to/bmi>` does not crash the compiler when loading +// a module that imports the incorrectly mapped module. +// RUN: not %clang_cc1 -std=c++20 main1.cpp -fmodule-file=A=b.pcm + +//--- a.cppm +export module A; + +export int a() { + return 41; +} + +//--- b.cppm +export module B; +import A; + +export int b() { + return a() + 1; +} + +//--- main1.cpp +import A; + +int main() { + return a(); +} + +// Test again for the case where the BMI is first loaded correctly +// RUN: not %clang_cc1 -std=c++20 main2.cpp-fmodule-file=B=b.pcm \ +// RUN: -fmodule-file=A=b.pcm + +//--- main2.cpp +import B; + +int main() { + return b(); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits