Skip to content
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

[NFC]Add assert to avoid possibly deref nullptr #65564

Merged
merged 1 commit into from
Sep 8, 2023

Conversation

XinWang10
Copy link
Contributor

@XinWang10 XinWang10 commented Sep 7, 2023

These 2 functions could be called by AsmPrinter::doInitialization in AsmPrinter.cpp. doInitialization init MMI in the beginningMMI = MMIWP ? &MMIWP->getMMI() : nullptr;, MMI has the possibility to be nullptr, which could make the later deref crash. I think in most time MMI could not be nullptr, but from the view of function implementation, it could be, so I'd like to add assert to it, if this could be a problem, then we could avoid crash.

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@KanRobert KanRobert left a comment

Choose a reason for hiding this comment

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

LGTM

@XinWang10 XinWang10 merged commit 99fb65f into llvm:main Sep 8, 2023
1 of 2 checks passed
@adrian-prantl
Copy link
Collaborator

@XinWang10 This patch doesn't fix the issue that you discovered. Most vendors ship LLVM with assertions disable for performance reasons. Can you fix this properly by either making the code that depends on MMI conditional or by making it impossible to have a null MMI member in AsmPrinter?

@JDevlieghere
Copy link
Member

+1 to what Adrian said. This commit doesn't improve anything: it just changes the type of crash from a segmentation fault to an abort, if you have asserts enabled. If the internal-consistency guarantee is that MMI is never null because MMIWP is never null, then you should assert that. But looking at the code (auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();) it very much looks intentional. So it looks like these asserts are enforcing something that's not guaranteed. I think this should be reverted.

@JDevlieghere JDevlieghere self-requested a review September 8, 2023 15:56
@adrian-prantl
Copy link
Collaborator

As a general rule of thumb, assert() and llvm_unreachable() should only be used on code paths that are (at the time of writing) actually impossible to reach. They are not a replacement for error handling.

@XinWang10
Copy link
Contributor Author

@JDevlieghere @adrian-prantl I know your principle is right. The reason I changed in this way is that this code has existed for several years, we haven't found any crash here so far, so I think add an assert would be a light weight fix to it. Using if-condition must be correct, but considering I have no evidence MMI would be nullptr from author's intention and if-condition could take more resource, I made this decision. I think we could let it here and if it do have risk it's not too late to change it back.
If you guys strongly object this patch, I would revert it and use if-condition instead.

@JDevlieghere
Copy link
Member

First off, I appreciate you trying to fix this issue.

I think add an assert would be a light weight fix to it.

As Adrian and I pointed out earlier, the current patch doesn't fix anything. I would go even further and say it makes things worse, because the assert states that MMI cannot be null, while there's a code patch that shows that it can be.

I have no evidence MMI would be nullptr from author's intention

You pointed out that it can be null if MMIWP is null, which is the case if the analysis pass is not found. If you look at the implementation of findAnalysisPass, it has a comment saying:

/// Find the pass that implements Analysis AID. Search immutable
/// passes and all pass managers. If desired pass is not found
/// then return NULL.

Clearly, it's possible for a pass to not exist. If someone were to build LLVM without MachineModuleInfoWrapperPass, MMIWP would be NULL and consequently MMI would be NULL.

If you guys strongly object this patch, I would revert it and use if-condition instead.

Yes, we should use an if-condition instead.

XinWang10 added a commit to XinWang10/llvm-project that referenced this pull request Sep 13, 2023
XinWang10 added a commit that referenced this pull request Sep 13, 2023
@dwblaikie dwblaikie removed the request for review from a team September 16, 2023 05:01
@dwblaikie
Copy link
Collaborator

(doesn't seem to be debug info related)

+1 to @aprantl and @JDevlieghere - if the null deref was reachable, then this needs proper error handling, the assert is insufficient/not the right tool. (& this needs a test case, if it is reachable)

ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
dwblaikie referenced this pull request Sep 21, 2023
assert is more appropriate here and fixes
`runtime error: execution reached an unreachable program point` in a
-DLLVM_USE_SANITIZER=Undefined build (-fno-sanitize-recover=all causes
llc to exit instead of crash (report_fatal_error)) when testing
MachineVerifier/test_g_assert_[sz]ext.mir.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants