This is an archive of the discontinued LLVM Phabricator instance.

[clang][dataflow] Fix crash when having boolean-to-integral casts.
ClosedPublic

Authored by junaire on Dec 29 2022, 1:16 AM.

Details

Summary

Since now we just ignore all (implicit) integral casts, treating the
resulting value as the same as the underlying value, it could cause
inconsistency between values after Join if in some paths the type
doesn't strictly match. This could cause intermittent crashes.

std::optional<bool> o;
int x;
if (o.has_value()) {
  x = o.value();
}

Fixes: https://github.com/llvm/llvm-project/issues/59728

Signed-off-by: Jun Zhang <jun@junz.org>

Diff Detail

Event Timeline

junaire created this revision.Dec 29 2022, 1:16 AM
Herald added a project: Restricted Project. · View Herald Transcript
junaire requested review of this revision.Dec 29 2022, 1:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 29 2022, 1:16 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gribozavr2 requested changes to this revision.Dec 29 2022, 2:32 AM

Thank you for your contribution!

While adding a conditional check fixes the crash, the problem's root cause must be deeper. Mismatched types indicate that one code path in dataflow analysis computes a bool type for a storage location, while a different code path computes an integer type. That's the actual root cause. Could you try to investigate the reasons for that, and try to fix it? The dataflow analysis should be computing values of the same type no matter through which path we arrived at a program point.

This revision now requires changes to proceed.Dec 29 2022, 2:32 AM

Thank you for your contribution!

While adding a conditional check fixes the crash, the problem's root cause must be deeper. Mismatched types indicate that one code path in dataflow analysis computes a bool type for a storage location, while a different code path computes an integer type. That's the actual root cause. Could you try to investigate the reasons for that, and try to fix it? The dataflow analysis should be computing values of the same type no matter through which path we arrived at a program point.

Thanks for your thoughts about the bug. Sure I'd love to try to investigate and will let you know what I finally got :)

junaire updated this revision to Diff 485654.Dec 29 2022, 6:24 PM

Update patch according to @ymandel 's suggestion. Thanks!

junaire retitled this revision from [clang][dataflow] Check both operand's type in mergeDistinctValues to [clang][dataflow] Fix crash when having boolean-to-integral casts..Dec 29 2022, 6:25 PM
junaire edited the summary of this revision. (Show Details)
junaire updated this revision to Diff 485657.Dec 29 2022, 6:31 PM

Fix typos

gribozavr2 accepted this revision.Dec 29 2022, 7:55 PM
gribozavr2 added inline comments.
clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
97–106
This revision is now accepted and ready to land.Dec 29 2022, 7:55 PM
This revision was landed with ongoing or failed builds.Dec 29 2022, 9:16 PM
This revision was automatically updated to reflect the committed changes.