ZezhengLi created this revision.
ZezhengLi added reviewers: ChuanqiXu, rsmith, urnathan.
ZezhengLi added a project: clang.
ZezhengLi requested review of this revision.
Herald added a subscriber: cfe-commits.

An impilt used of module without prebuild path may cause crash.

For example:

  // ./dir1/C.cppm
  export module C;
  // ./dir2/B.cppm
  export module B;
  import C;
  // ./A.cpp
  import B;
  import C;

When we compile A.cpp without the prebuild path of C.pcm, the compiler will 
crash.

  clang++ -std=c++20 --precompile -c ./dir1/C.cppm -o dir1/C.pcm
  clang++ -std=c++20 --precompile -fprebuilt-module-path=./dir2  -c B.cppm -o 
B.pcm
  clang++ -std=c++20 -fprebuilt-module-path=./dir2 A.cpp

The prebuilt path of module C is cached when import module B, and in the 
function HeaderSearch::getCachedModuleFileName, the compiler try to get the 
filename by modulemap without check if modulemap exists,and there is no 
modulemap in C++ module.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119426

Files:
  clang/lib/Lex/HeaderSearch.cpp
  clang/test/Modules/implicit-module-with-missing-path.cpp


Index: clang/test/Modules/implicit-module-with-missing-path.cpp
===================================================================
--- /dev/null
+++ clang/test/Modules/implicit-module-with-missing-path.cpp
@@ -0,0 +1,12 @@
+// This tests that the compiler wouldn't crash if the module path misses
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t/subdir
+// RUN: echo "export module C;" >> %t/subdir/C.cppm
+// RUN: echo -e "export module B;\nimport C;" >> %t/B.cppm
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/subdir/C.cppm -o 
%t/subdir/C.pcm
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface 
-fprebuilt-module-path=%t/subdir %t/B.cppm -o %t/B.pcm
+// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %s -fsyntax-only 
-verify
+
+import B;
+import C; // expected-error {{module 'C' is needed but has not been provided, 
and implicit use of module files is disabled}}
\ No newline at end of file
Index: clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- clang/lib/Lex/HeaderSearch.cpp
+++ clang/lib/Lex/HeaderSearch.cpp
@@ -172,6 +172,10 @@
 std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
   const FileEntry *ModuleMap =
       getModuleMap().getModuleMapFileForUniquing(Module);
+  // The ModuleMap maybe a nullptr, when we load a cached C++ module without
+  // *.modulemap file. In this case, just return an empty string.
+  if (ModuleMap == nullptr)
+    return {};
   return getCachedModuleFileName(Module->Name, ModuleMap->getName());
 }
 


Index: clang/test/Modules/implicit-module-with-missing-path.cpp
===================================================================
--- /dev/null
+++ clang/test/Modules/implicit-module-with-missing-path.cpp
@@ -0,0 +1,12 @@
+// This tests that the compiler wouldn't crash if the module path misses
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t/subdir
+// RUN: echo "export module C;" >> %t/subdir/C.cppm
+// RUN: echo -e "export module B;\nimport C;" >> %t/B.cppm
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/subdir/C.cppm -o %t/subdir/C.pcm
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface -fprebuilt-module-path=%t/subdir %t/B.cppm -o %t/B.pcm
+// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %s -fsyntax-only -verify
+
+import B;
+import C; // expected-error {{module 'C' is needed but has not been provided, and implicit use of module files is disabled}}
\ No newline at end of file
Index: clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- clang/lib/Lex/HeaderSearch.cpp
+++ clang/lib/Lex/HeaderSearch.cpp
@@ -172,6 +172,10 @@
 std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
   const FileEntry *ModuleMap =
       getModuleMap().getModuleMapFileForUniquing(Module);
+  // The ModuleMap maybe a nullptr, when we load a cached C++ module without
+  // *.modulemap file. In this case, just return an empty string.
+  if (ModuleMap == nullptr)
+    return {};
   return getCachedModuleFileName(Module->Name, ModuleMap->getName());
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to