Skip to content

[clang][dataflow]Use cast_or_null instead of cast to prevent crash #68510

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
Oct 21, 2023

Conversation

jcsxky
Copy link
Contributor

@jcsxky jcsxky commented Oct 8, 2023

getStorageLocation may return nullptr and this will produce crash when use cast, use dyn_cast_or_null instead. I test it locally using FTXUI and it may be the cause of issue issue, but I am not sure.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:analysis labels Oct 8, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2023

@llvm/pr-subscribers-clang-tidy
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-analysis

Changes

getStorageLocation may return nullptr and this will produce crash when use cast, use dyn_cast_or_null instead. I test it locally using FTXUI and it may be the cause of issue issue, but I am not sure.


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

1 Files Affected:

  • (modified) clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp (+2-1)
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
index f61f26ff27804ec..dda1c6cfca017d0 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
@@ -26,6 +26,7 @@
 #include "clang/Analysis/FlowSensitive/NoopLattice.h"
 #include "clang/Analysis/FlowSensitive/StorageLocation.h"
 #include "clang/Analysis/FlowSensitive/Value.h"
+#include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Casting.h"
@@ -599,7 +600,7 @@ void transferAssignment(const CXXOperatorCallExpr *E, BoolValue &HasValueVal,
                         LatticeTransferState &State) {
   assert(E->getNumArgs() > 0);
 
-  if (auto *Loc = cast<RecordStorageLocation>(
+  if (auto *Loc = dyn_cast_or_null<RecordStorageLocation>(
           State.Env.getStorageLocation(*E->getArg(0)))) {
     createOptionalValue(*Loc, HasValueVal, State.Env);
 

@jcsxky jcsxky changed the title [clang][analysis]Use dyn_cast_or_null instead cast to prevent crash [clang][dataflow][]Use dyn_cast_or_null instead cast to prevent crash Oct 8, 2023
@jcsxky jcsxky changed the title [clang][dataflow][]Use dyn_cast_or_null instead cast to prevent crash [clang][dataflow]Use dyn_cast_or_null instead cast to prevent crash Oct 8, 2023
@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch from 870856a to b0f1344 Compare October 8, 2023 09:05
Copy link
Collaborator

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

The fix looks great to me! On the other hand, we usually try to add a regression test for each of the fixes. Any chance you could get a minimal reproducer from the codebase you are looking at?

@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch from b0f1344 to 2de9679 Compare October 10, 2023 03:49
@jcsxky
Copy link
Contributor Author

jcsxky commented Oct 10, 2023

The fix looks great to me! On the other hand, we usually try to add a regression test for each of the fixes. Any chance you could get a minimal reproducer from the codebase you are looking at?

Thanks for your suggestion! Test case has been added to reproduce this issue.

@jcsxky jcsxky requested a review from Xazax-hun October 10, 2023 04:22
Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

please update release note also

@jcsxky
Copy link
Contributor Author

jcsxky commented Oct 10, 2023

please update release note also

Although bugprone-unchecked-optional-access is used to test this issue, root cause is in FlowSensitiveAnalysis. So, I am confused about which file should be updated about release not. Look forward to your suggestion! Thank you.

@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch from 2de9679 to 3978a43 Compare October 10, 2023 05:32
@jcsxky jcsxky changed the title [clang][dataflow]Use dyn_cast_or_null instead cast to prevent crash [clang][dataflow]Use cast_or_null instead cast to prevent crash Oct 10, 2023
@jcsxky jcsxky requested a review from HerrCai0907 October 10, 2023 05:33
@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch from 3978a43 to 7a8c515 Compare October 10, 2023 05:36
@PiotrZSL PiotrZSL linked an issue Oct 10, 2023 that may be closed by this pull request
Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

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

LGTM,
When release notes could be nice (clang-tools-extra/doc/ReleaseNotest.rst) with something like: Improved bugprone-unchecked-optional-acces check to not crash during handling of optional values or to not crash i certain situations it may also not be so necessary, as this is just very quick fix.

@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch 2 times, most recently from 38cf358 to 466b612 Compare October 10, 2023 05:57
@jcsxky
Copy link
Contributor Author

jcsxky commented Oct 10, 2023

LGTM, When release notes could be nice (clang-tools-extra/doc/ReleaseNotest.rst) with something like: Improved bugprone-unchecked-optional-acces check to not crash during handling of optional values or to not crash i certain situations it may also not be so necessary, as this is just very quick fix.

Release notes have been updated according to your suggestion. Thank you for detail guidance!

@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch from 466b612 to eb9f834 Compare October 10, 2023 06:05
Copy link
Collaborator

@ymand ymand left a comment

Choose a reason for hiding this comment

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

Thanks for this fix! Unfortunately, I wasn't able to repro the crash in godbolt: https://godbolt.org/z/s741z5djY. Can you double check that the check crashes on that example without your fix?

@jcsxky
Copy link
Contributor Author

jcsxky commented Oct 11, 2023

Thanks for this fix! Unfortunately, I wasn't able to repro the crash in godbolt: https://godbolt.org/z/s741z5djY. Can you double check that the check crashes on that example without your fix?

The test case is different from that in this patch. Please Use float instead of int in optional as template type. But in godbolt it doesn't crash, probably doesn't show the crash stack or not latest version? I don't know. You could test it locally after changing template argument of optional from int to float and it will crash.
The reason why int type doesn't trigger crash is that

unless(hasDeclaration(cxxMethodDecl(
          anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator()))))

in isOptionalValueOrConversionAssignment() can't match

vec[0].x = 0;

so, flow can't enter into transferValueOrConversionAssignment() and produce crash.

@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch from eb9f834 to 55ca8fa Compare October 11, 2023 02:29
@jcsxky jcsxky requested a review from ymand October 11, 2023 02:32
@jcsxky
Copy link
Contributor Author

jcsxky commented Oct 16, 2023

@ymand Could you take a look at this pr?

@jcsxky jcsxky changed the title [clang][dataflow]Use cast_or_null instead cast to prevent crash [clang][dataflow]Use cast_or_null instead of cast to prevent crash Oct 16, 2023
Copy link
Collaborator

@ymand ymand left a comment

Choose a reason for hiding this comment

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

Thanks, looks good!

You can submit as is, but if you're up for it, it would actually be better to add the new test case directly to the model's unittests. Something like this test (though just one case is enough -- please put it in a separate TEST_P):

TEST_P(UncheckedOptionalAccessTest, ValueAssignment) {

In general, the lit tests for the clang tidy check only do minimal testing to ensure the check is properly calling the model.

@jcsxky jcsxky force-pushed the use_dyn_cast_or_null_instead_cast branch from 55ca8fa to a5c5fc7 Compare October 19, 2023 02:33
@jcsxky
Copy link
Contributor Author

jcsxky commented Oct 19, 2023

Thanks, looks good!

You can submit as is, but if you're up for it, it would actually be better to add the new test case directly to the model's unittests. Something like this test (though just one case is enough -- please put it in a separate TEST_P):

TEST_P(UncheckedOptionalAccessTest, ValueAssignment) {

In general, the lit tests for the clang tidy check only do minimal testing to ensure the check is properly calling the model.

Thanks for your suggestion! New test case has been added to unittests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:analysis clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang Clang issues not falling into any other category clang-tidy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-tidy crash
6 participants