This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Add modeling for unque_ptr::get()
ClosedPublic

Authored by vrnithinkumar on Aug 15 2020, 4:38 PM.

Details

Summary

Modeling to return tracked inner pointer for get() method

Diff Detail

Event Timeline

vrnithinkumar created this revision.Aug 15 2020, 4:38 PM
vrnithinkumar requested review of this revision.Aug 15 2020, 4:38 PM
vrnithinkumar edited the summary of this revision. (Show Details)Aug 15 2020, 4:40 PM
NoQ accepted this revision.Aug 15 2020, 6:33 PM

Excellent, thanks!

clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
362–363

You'll have to actively handle this case, sooner or later. Consider the following test cases that won't work until you do:

void foo(std::unique_ptr<A> p) {
  A *x = p.get();
  A *y = p.get();
  clang_analyzer_eval(x == y); // expected-warning{{TRUE}}
  if (!x) {
    y->foo(); // expected-warning{{Called C++ object pointer is null}}
  }
}
This revision is now accepted and ready to land.Aug 15 2020, 6:33 PM
xazax.hun accepted this revision.Aug 17 2020, 3:55 AM
xazax.hun added inline comments.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
362–363

You mean the case where we do not have an inner pointer registered in the state yet, right?

I believe we might also have to handle similar cases for operator bool() as well.

vrnithinkumar edited the summary of this revision. (Show Details)
  • Using conjureSymbolVal in case of missing inner pointer value
vrnithinkumar marked 2 inline comments as done.Aug 19 2020, 3:36 PM
vrnithinkumar added inline comments.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
362–363

Added the above test case.
Using conjureSymbolVal in case of missing inner pointer value

362–363
void foo(std::unique_ptr<A> P) {
  A *X = P.get();
  if (!X) {
    P->foo(); // expected-warning {{Dereference of null smart pointer 'Pl' [alpha.cplusplus.SmartPtr]}}
  }
}

I was trying to add the above use case. Since we are using conjureSymbolVal in case of missing inner pointer value.

But still the inner pointer value is constrained to [0, 0] in false branch, InnerPointVal->isZeroConstant() returning false.
Also I tried State->isNull(*InnerPointVal).isConstrainedTrue(); This is also not working.
How should we check whether the conjureSymbolVal for inner pointer value is constrained to [0, 0]?

NoQ accepted this revision.Aug 22 2020, 12:06 AM

This patch looks correct to me at a glance. I think we should land it as is and debug/improve later.

clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
362–363

How should we check whether the conjureSymbolVal for inner pointer value is constrained to [0, 0]?

View exploded graphs. That's literally the only reasonable answer to every such question. In particular, it shows you constraints for all symbols at every moment of time, and given that you implemented printState() it also shows you inner pointer values that you keep track of at every moment of time.

Check if it's still the same symbol. Check that the symbol lives long enough - or does it get forgotten about in the middle? - if so you might need to get your checkLiveSymbols callback right.

Please let us know if you still can't seem to debug it on your own.

This revision was automatically updated to reflect the committed changes.