Skip to content

[clang][modules] Validate input file format for GenerateModuleInterfaceAction (#132692) #137711

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
Apr 29, 2025

Conversation

naveen-seth
Copy link
Contributor

Fixes #132692.

clang -cc1 crashes when generating a module interface with emit-module-interface or emit-reduced-module-interface using an input file which is already a precompiled module (.pcm) file.
This commit adds validation for the input file format and Clang will now emit an error message instead of crashing.

…ceAction (llvm#132692)

Fixes llvm#132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will
now emit an error message instead of crashing.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules labels Apr 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang

Author: Naveen Seth Hanig (naveen-seth)

Changes

Fixes #132692.

clang -cc1 crashes when generating a module interface with emit-module-interface or emit-reduced-module-interface using an input file which is already a precompiled module (.pcm) file.
This commit adds validation for the input file format and Clang will now emit an error message instead of crashing.


Full diff: https://github.com/llvm/llvm-project/pull/137711.diff

4 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticFrontendKinds.td (+4)
  • (modified) clang/include/clang/Frontend/FrontendActions.h (+1)
  • (modified) clang/lib/Frontend/FrontendActions.cpp (+14)
  • (added) clang/test/Modules/emit-module-interface-pcm-input.cpp (+13)
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 6c72775197823..8a8db27490f06 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -260,6 +260,10 @@ def err_modules_embed_file_not_found :
   DefaultFatal;
 def err_module_header_file_not_found :
   Error<"module header file '%0' not found">, DefaultFatal;
+def err_frontend_action_unsupported_input_format
+    : Error<"%0 does not support input file format of file '%1': "
+            "'%select{Source|ModuleMap|Precompiled|Unknown}2'">,
+      DefaultFatal;
 
 def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected "
diff --git a/clang/include/clang/Frontend/FrontendActions.h b/clang/include/clang/Frontend/FrontendActions.h
index a620ddfc40447..a5dfb770c58a2 100644
--- a/clang/include/clang/Frontend/FrontendActions.h
+++ b/clang/include/clang/Frontend/FrontendActions.h
@@ -156,6 +156,7 @@ class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
 /// files) for C++20 Named Modules.
 class GenerateModuleInterfaceAction : public GenerateModuleAction {
 protected:
+  bool PrepareToExecuteAction(CompilerInstance &CI) override;
   bool BeginSourceFileAction(CompilerInstance &CI) override;
 
   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp
index e6c7b9f32c29b..51e9f2c6b39a7 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -263,6 +263,20 @@ GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
                                     /*ForceUseTemporary=*/true);
 }
 
+bool GenerateModuleInterfaceAction::PrepareToExecuteAction(
+    CompilerInstance &CI) {
+  for (const auto &FIF : CI.getFrontendOpts().Inputs) {
+    if (const auto InputFormat = FIF.getKind().getFormat();
+        InputFormat != InputKind::Format::Source) {
+      CI.getDiagnostics().Report(
+          diag::err_frontend_action_unsupported_input_format)
+          << "module interface compilation" << FIF.getFile() << InputFormat;
+      return false;
+    }
+  }
+  return GenerateModuleAction::PrepareToExecuteAction(CI);
+}
+
 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
     CompilerInstance &CI) {
   CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
diff --git a/clang/test/Modules/emit-module-interface-pcm-input.cpp b/clang/test/Modules/emit-module-interface-pcm-input.cpp
new file mode 100644
index 0000000000000..9df7222068d20
--- /dev/null
+++ b/clang/test/Modules/emit-module-interface-pcm-input.cpp
@@ -0,0 +1,13 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: cd %t
+
+// Related to issue #132692
+// Verify that clang_cc1 doesn't crash when trying to generate a module
+// interface from an alreay precompiled module (`.pcm`).
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface a.cppm -o a.pcm
+// RUN: not %clang_cc1 -std=c++20 -emit-module-interface a.pcm
+// RUN: not %clang_cc1 -std=c++20 -emit-reduced-module-interface a.pcm
+
+//--- a.cppm
+export module A;

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ChuanqiXu9 ChuanqiXu9 merged commit 7ce0f5a into llvm:main Apr 29, 2025
15 checks passed
gizmondo pushed a commit to gizmondo/llvm-project that referenced this pull request Apr 29, 2025
…ceAction (llvm#132692) (llvm#137711)

Fixes llvm#132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will now
emit an error message instead of crashing.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…ceAction (llvm#132692) (llvm#137711)

Fixes llvm#132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will now
emit an error message instead of crashing.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…ceAction (llvm#132692) (llvm#137711)

Fixes llvm#132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will now
emit an error message instead of crashing.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…ceAction (llvm#132692) (llvm#137711)

Fixes llvm#132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will now
emit an error message instead of crashing.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…ceAction (llvm#132692) (llvm#137711)

Fixes llvm#132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will now
emit an error message instead of crashing.
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
…ceAction (llvm#132692) (llvm#137711)

Fixes llvm#132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will now
emit an error message instead of crashing.
@naveen-seth naveen-seth deleted the dev/fix-emit-module-crash-132692 branch May 28, 2025 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Clang] [Modules] Crash when specifying -Xclang -emit-module-interface
3 participants