Skip to content

[clang] Fix a segfault when M is a nullptr #130712

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 15, 2025

Conversation

matts1
Copy link
Contributor

@matts1 matts1 commented Mar 11, 2025

If MM->getOwningModule returns nullptr, then isVisible is called with nullptr, which then calls getImportLoc(nullptr)

if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@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 Mar 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-clang-modules

Author: Matt (matts1)

Changes

If MM->getOwningModule returns nullptr, then isVisible is called with nullptr, which then calls getImportLoc(nullptr)

if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {


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

1 Files Affected:

  • (modified) clang/include/clang/Basic/Module.h (+1-1)
diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h
index 62cc8acf9588b..3d035f0a5f787 100644
--- a/clang/include/clang/Basic/Module.h
+++ b/clang/include/clang/Basic/Module.h
@@ -888,7 +888,7 @@ class VisibleModuleSet {
 
   /// Get the location at which the import of a module was triggered.
   SourceLocation getImportLoc(const Module *M) const {
-    return M->getVisibilityID() < ImportLocs.size()
+    return M && M->getVisibilityID() < ImportLocs.size()
                ? ImportLocs[M->getVisibilityID()]
                : SourceLocation();
   }

@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-clang

Author: Matt (matts1)

Changes

If MM-&gt;getOwningModule returns nullptr, then isVisible is called with nullptr, which then calls getImportLoc(nullptr)

if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {


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

1 Files Affected:

  • (modified) clang/include/clang/Basic/Module.h (+1-1)
diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h
index 62cc8acf9588b..3d035f0a5f787 100644
--- a/clang/include/clang/Basic/Module.h
+++ b/clang/include/clang/Basic/Module.h
@@ -888,7 +888,7 @@ class VisibleModuleSet {
 
   /// Get the location at which the import of a module was triggered.
   SourceLocation getImportLoc(const Module *M) const {
-    return M->getVisibilityID() < ImportLocs.size()
+    return M && M->getVisibilityID() < ImportLocs.size()
                ? ImportLocs[M->getVisibilityID()]
                : SourceLocation();
   }

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.

The patch itself seems not bad. But we prefer to ask for a test for such changes.

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

This needs a release note and it would be helpful to know how you found this and a test w/ a minimal reproducer. Are there any bugs reports for this?

@matts1
Copy link
Contributor Author

matts1 commented Mar 12, 2025

There is a bug report on the chromium bug tracker - http://crbug.com/400353616

@matts1 matts1 force-pushed the push-qwyquzltunws branch from 4b41cf9 to cf5684b Compare March 13, 2025 06:02
@matts1
Copy link
Contributor Author

matts1 commented Mar 13, 2025

I have a repro, but it may be addressing the symptoms rather than the root cause.

AFAICT, a ModuleMacro should always have an associated module.

The root cause of the issue appears to be that in the example I provided with the test case, the ModuleMacro constructor is called with OwningModule set to null. I'm not familiar enough with the code, however, to say if that's working as intended. I'm hoping someone more knowledgable than me could answer that.

ModuleMacro(Module *OwningModule, const IdentifierInfo *II, MacroInfo *Macro,

I'm happy to submit this as is, since a header guard can't hurt, but it may just hide the problem.

// 1) There must be a configuration mismatch between a header and a file it depends on
// 2) -fmodules-local-submodule-visibility must be enabled.

// Unfortunately, libcxx is compiled with exceptions, but the sysroot it depends on is likely compiled without exceptions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why libcxx is relevant here while any code in this test doesn't include STL header?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can reproduce this with libcxx, but this is a minimal repro.

Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is not relevant to the test; can you remove it?

@matts1 matts1 force-pushed the push-qwyquzltunws branch from cf5684b to 9d29127 Compare March 13, 2025 23:08
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.rst in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

// 1) There must be a configuration mismatch between a header and a file it depends on
// 2) -fmodules-local-submodule-visibility must be enabled.

// Unfortunately, libcxx is compiled with exceptions, but the sysroot it depends on is likely compiled without exceptions.
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is not relevant to the test; can you remove it?

@matts1 matts1 force-pushed the push-qwyquzltunws branch from 76fc1c9 to 2c0dc1e Compare March 17, 2025 06:19
@matts1 matts1 force-pushed the push-qwyquzltunws branch from a249bae to 195055e Compare April 15, 2025 02:12
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

LGTM

@matts1 matts1 force-pushed the push-qwyquzltunws branch from 195055e to 88512cc Compare May 15, 2025 01:12
@matts1 matts1 requested a review from cor3ntin May 15, 2025 01:12
@matts1
Copy link
Contributor Author

matts1 commented May 15, 2025

I've rebased to fix the merge conflicts in the release notes, should be ready to merge.

@ChuanqiXu9 ChuanqiXu9 merged commit 4630d46 into llvm:main May 15, 2025
12 checks passed
Copy link

@matts1 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@matts1 matts1 deleted the push-qwyquzltunws branch May 15, 2025 02:53
TIFitis pushed a commit to TIFitis/llvm-project that referenced this pull request May 19, 2025
If `MM->getOwningModule` returns nullptr, then `isVisible` is called
with nullptr, which then calls `getImportLoc(nullptr)`


https://github.com/llvm/llvm-project/blob/077e0c134a31cc16c432ce685458b1de80bfbf84/clang/lib/Lex/PPMacroExpansion.cpp#L208
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.

6 participants