This is an archive of the discontinued LLVM Phabricator instance.

[Analyzer] Add checkRegionChanges for SmartPtrModeling
ClosedPublic

Authored by vrnithinkumar on Jul 14 2020, 5:58 PM.

Details

Summary

Implementing checkRegionChanges for SmartPtrModeling.
To improve the accuracy, when a smart pointer is passed by a non-const reference into a function, removed the tracked region data. Since it is not sure what happens to the smart pointer inside the function.

Diff Detail

Event Timeline

vrnithinkumar created this revision.Jul 14 2020, 5:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2020, 5:58 PM
vrnithinkumar retitled this revision from Implementing checkRegionChanges to [Analyzer] Implementing checkRegionChanges for SmartPtrModeling.Jul 14 2020, 6:06 PM
vrnithinkumar edited the summary of this revision. (Show Details)
vrnithinkumar marked 3 inline comments as done.Jul 14 2020, 6:16 PM
vrnithinkumar added inline comments.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
182

I am using the same code from the MoveChecker since the same logic applies here too.
But I am not completely sure whether this is the clean approach.

clang/test/Analysis/Inputs/system-header-simulator-cxx.h
962

added this to support use case like Q = std::move(P)

clang/test/Analysis/smart-ptr.cpp
145

@xazax.hun , Is this the type of test you were suggesting?

NoQ added inline comments.Jul 14 2020, 8:21 PM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
191–192

The whole reason why we are making this patch first is because we *don't* fully handle methods :)

Also, unlike the move checker, once we handle them, they'll probably stop changing regions, so no exceptional code path would be necessary in most cases.

So i'd rather start with fully wiping all the data on any sneeze and later see if we can relax it.

xazax.hun added inline comments.Jul 15 2020, 8:27 AM
clang/test/Analysis/Inputs/system-header-simulator-cxx.h
962

This operation should be noexcept: https://en.cppreference.com/w/cpp/memory/unique_ptr/operator%3D

While it makes little difference for the analyzer at this point we should try to be as close to the standard as possible. If you have some time feel free to add noexcept to other methods that miss it :)

clang/test/Analysis/smart-ptr.cpp
145

Yes, looks good to me, thanks! :)

vrnithinkumar edited the summary of this revision. (Show Details)

Untrack all changing regions in checkRegionChanges

vrnithinkumar marked 2 inline comments as done.Jul 15 2020, 8:48 AM
vrnithinkumar added inline comments.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
191–192

I completely missed that point and jumped ahead.
Made changes to untrack all changing regions in checkRegionChanges

vsavchenko added inline comments.Jul 15 2020, 9:44 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
97

nit: *need to be removed

98

Maybe Subregions would fit better in this name then?

188

It is not critical, but potentially we can allocate/deallocate a whole bunch of states here. We can do the same sort of operation with the map itself (State->get<TrackedRegionMap>()), which still have similar problem but to a lesser degree. Additionally, this get<MAP_TYPE> method is not compile-time, it searches for the corresponding map in runtime (also in a map), so you repeat that as many times as you have Regions.

And super-duper over-optimization note on my part: making the loop over Regions to be the inner-most is more cache-friendly. It is not really critical here, but it is overall good to have an eye for things like that.

clang/test/Analysis/smart-ptr.cpp
190

Usually we simply add the test with expectations matching current state of things, but add a TODO over those particular lines. This way when you fix the issue the test will start failing and you won't forget to uncomment it.

Additionally, I would prefer commit message to be imperative. It is sorta like a rule of a good commit message.

vrnithinkumar retitled this revision from [Analyzer] Implementing checkRegionChanges for SmartPtrModeling to [Analyzer] Add checkRegionChanges for SmartPtrModeling.Jul 16 2020, 3:31 PM
vrnithinkumar marked an inline comment as done.
  • Addressing review comments
  • Enabling commented out tests
vrnithinkumar marked 8 inline comments as done.
  • Adding a missed todo
vrnithinkumar marked 2 inline comments as done.Jul 16 2020, 4:34 PM
vrnithinkumar added inline comments.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
97

fixed

98

Changed to Subregions

188

Updated removeTrackedSubregions for passing TrackedRegionMap

clang/test/Analysis/Inputs/system-header-simulator-cxx.h
962

Added noexcept for all applicable methods

clang/test/Analysis/smart-ptr.cpp
190

Enabled the test and added the todo.

NoQ accepted this revision.Jul 17 2020, 2:08 PM

I think this looks great!

P.S. I unforgot why we won't be able to remove the option after all. That's because when we partially evalCall and partially inline, inlining breaks. Like, once we evalCalled a single method call, we can no longer trust RegionStore to have the right data inside of it on the current path, so further inlining cannot work correctly. We could forbid inlining and resort to conservative evaluation but that'd still be a regression compared to inlining. So i'd rather keep the option for as long as possible :)

This revision is now accepted and ready to land.Jul 17 2020, 2:08 PM
NoQ added a comment.Aug 2 2020, 11:43 AM

These patches look like they're done, maybe let's land them?

vrnithinkumar marked an inline comment as done.Aug 2 2020, 1:35 PM
In D83836#2189636, @NoQ wrote:

These patches look like they're done, maybe let's land them?

This one also committed. (https://github.com/llvm/llvm-project/commit/a5609102117d2384fb73a14f37d24a0c844e3864)

vrnithinkumar closed this revision.Aug 3 2020, 5:06 PM