This is an archive of the discontinued LLVM Phabricator instance.

[clang][dataflow] Only model struct fields that are used in the function being analyzed.
ClosedPublic

Authored by ymandel on Dec 27 2022, 7:40 AM.

Details

Summary

Previously, the model for structs modeled all fields in a struct when
createValue was called for that type. This patch adds a prepass on the
function under analysis to discover the fields referenced in the scope and then
limits modeling to only those fields. This reduces wasted memory usage
(modeling unused fields) which can be important for programss that use large
structs.

Note: This patch obviates the need for https://reviews.llvm.org/D123032.

Diff Detail

Event Timeline

ymandel created this revision.Dec 27 2022, 7:40 AM
Herald added a project: Restricted Project. · View Herald Transcript
ymandel requested review of this revision.Dec 27 2022, 7:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 27 2022, 7:40 AM
gribozavr2 accepted this revision.Dec 27 2022, 9:59 AM
gribozavr2 added inline comments.
clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
147–148
380
clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
455
clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp
61
clang/unittests/Analysis/FlowSensitive/TestingSupport.h
167

It is not clear what "optional" means for a boolean.

This revision is now accepted and ready to land.Dec 27 2022, 9:59 AM
ymandel updated this revision to Diff 485393.Dec 27 2022, 10:16 AM

address comments; fix lifetime bug in new code.

ymandel marked 5 inline comments as done.Dec 27 2022, 10:23 AM

Gabor -- do you want to review before I land this patch (Dmitri already accepted it)? thx

xazax.hun accepted this revision.Jan 4 2023, 7:50 AM

This is a big improvement, I skimmed through it and looks good to me. There might be some sneaky ways to change values of fields without mentioning them, e.g., casting a pointer to a struct to an array of chars. But I think reasoning about those scenarios is probably not something we want to model anytime soon.

Another pesky scenario when we are not doing context sensitive analysis:

int* pointsToField = getField(myObj);

But this can be handled correctly in a conservative way buy treating `myObj' as escaped. Overall, I dont anticipate any blockers at the moment.

I realized I did not say this explicitly in my previous comment, but feel free to commit :)

clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
263

Is there any scenario where check authors might invoke this function? If no, I wonder if it would be better to make this private and make Environment a friend of this class. Keeping the number of public methods minimal could be a great service to future check authors.

ymandel updated this revision to Diff 486657.Jan 5 2023, 1:05 PM

rebase onto HEAD

ymandel updated this revision to Diff 486658.Jan 5 2023, 1:11 PM

address comments

ymandel marked an inline comment as done.Jan 5 2023, 1:13 PM

This is a big improvement, I skimmed through it and looks good to me. There might be some sneaky ways to change values of fields without mentioning them, e.g., casting a pointer to a struct to an array of chars. But I think reasoning about those scenarios is probably not something we want to model anytime soon.

Thanks!

Another pesky scenario when we are not doing context sensitive analysis:

int* pointsToField = getField(myObj);

But this can be handled correctly in a conservative way buy treating `myObj' as escaped. Overall, I dont anticipate any blockers at the moment.

Agreed. I think any other field access either falls into 1) we don't handle it soundly anyhow or 2) a conservative analysis will rule it out.

clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
263

Good point. Done.

ymandel updated this revision to Diff 486659.Jan 5 2023, 1:14 PM
ymandel marked an inline comment as done.

fix typo

ymandel updated this revision to Diff 486666.Jan 5 2023, 1:30 PM

fix broken test (from rebase)

This revision was landed with ongoing or failed builds.Jan 5 2023, 1:47 PM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Jan 5 2023, 3:35 PM

This breaks tests on Mac: http://45.33.8.238/macm1/52112/step_7.txt

Please take a look and revert for now if it takes a while to fix.

I'll revert now and take a look in the morning...

ymandel reopened this revision.Jan 6 2023, 5:44 AM
This revision is now accepted and ready to land.Jan 6 2023, 5:44 AM
ymandel updated this revision to Diff 486926.Jan 6 2023, 10:26 AM

fix use-after-free bug in test. Possible cause of buildbot failures.

xazax.hun accepted this revision.Jan 6 2023, 2:05 PM

I think in these cases you can recommit without waiting for a new round of reviews, but I hit accept just in case :)

gribozavr2 accepted this revision.Jan 6 2023, 2:14 PM
ymandel updated this revision to Diff 487506.Jan 9 2023, 11:12 AM

fix memory error that cause buildbot failures

Fixes a self-move assign. This operation should be safe, but is known to cause
problems in some implementations of the standard library. The patch drops the
assignment, which was unnecessary since the object is modified in place.