This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] LValueToRValueBitCasts should evaluate to an r-value
ClosedPublic

Authored by steakhal on Jun 28 2021, 5:48 AM.

Details

Summary

Previously LValueToRValueBitCasts were modeled in the same way how
a regular BitCast was. However, this should not produce an l-value.
Modeling bitcasts accurately is tricky, so it's probably better to
model this expression by binding a fresh conjured value.

The following code should not result in a diagnostic:

  __attribute__((always_inline))
  static inline constexpr unsigned int_castf32_u32(float __A) {
	return __builtin_bit_cast(unsigned int, __A); // no-warning
  }

Previously, it reported
Address of stack memory associated with local variable '__A' returned to caller [core.StackAddressEscape].

Diff Detail

Event Timeline

steakhal created this revision.Jun 28 2021, 5:48 AM
steakhal requested review of this revision.Jun 28 2021, 5:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2021, 5:48 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
NoQ added a comment.Jun 28 2021, 10:49 AM

Thanks, nice! More stuff to support.

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
307

How difficult would it be to handle the cast properly by adding it to this branch, except eventually passing the right type to getSVal(R, ...) inside evalLoad()?

543

You didn't start it but that's, ugh, not a great name for the function to handle the new cast.

I wouldn't mind eliminating the function entirely and inlining the implementation as it actually makes the code easier to read, for once.

steakhal updated this revision to Diff 355245.Jun 29 2021, 7:58 AM
steakhal marked an inline comment as done.

Evaluate LValueToRValueBitCasts the same way as we evaluate LValueToRValue casts.
Extended the tests accordingly.

Thanks for the hint @NoQ!
It looks much better now.

steakhal added inline comments.Jun 29 2021, 7:59 AM
clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
543

I'm going to do that in an NFC follow-up patch.

steakhal marked an inline comment as done.Jun 29 2021, 8:13 AM
steakhal added inline comments.
clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
543
NoQ accepted this revision.Jun 29 2021, 2:18 PM

So it worked out of the box? Great!

This revision is now accepted and ready to land.Jun 29 2021, 2:18 PM

So it worked out of the box? Great!

Yes, it did.

vsavchenko accepted this revision.Jun 30 2021, 1:37 AM

Oh, wow, that's awesome! Thanks!