This is an archive of the discontinued LLVM Phabricator instance.

[-Wunsafe-buffer-usage] Ignore the FixableGadgets that will not be fixed at an earlier stage
ClosedPublic

Authored by ziqingluo-90 on Jul 17 2023, 3:58 PM.

Details

Summary

FixableGadgets are not always associated with variables that is unsafe (warned). For example, they could be associated with variables whose unsafe operations are suppressed or that are not used in any unsafe operation. Such FixableGadgets will not be fixed. Removing these FixableGadget as early as possible could help improve the performance and stability of the analysis.

The variable grouping algorithm introduced in D145739 nearly completes this task.
For a variable that is neither warned about nor reachable from a warned variable, it does not exist in the graph computed by the algorithm.
So this patch simply removes FixableGadgets whose variables are not present in the graph computed for variable grouping.

Diff Detail

Event Timeline

ziqingluo-90 created this revision.Jul 17 2023, 3:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2023, 3:58 PM
ziqingluo-90 requested review of this revision.Jul 17 2023, 3:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2023, 3:58 PM
ziqingluo-90 edited the summary of this revision. (Show Details)Jul 17 2023, 4:15 PM
t-rasmud accepted this revision.Jul 17 2023, 4:36 PM

LGTM!

clang/lib/Analysis/UnsafeBufferUsage.cpp
2378

Nit: Maybe also mention when a variable is neither warned nor is reachable? Are there scenarios besides variables inside pragmas where this constraint is satisfied?

This revision is now accepted and ready to land.Jul 17 2023, 4:36 PM
ziqingluo-90 marked an inline comment as done.
ziqingluo-90 added inline comments.
clang/lib/Analysis/UnsafeBufferUsage.cpp
2378

good question! I add some explanation in the comment.

NoQ added inline comments.Jul 17 2023, 5:47 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
2242–2269

Maybe early-return here when there are no gadgets?

ziqingluo-90 marked an inline comment as done.
ziqingluo-90 marked an inline comment as done.
NoQ added inline comments.Jul 18 2023, 3:04 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1092–1093

Let's also skip this part when there are no warning gadgets? Your initial patch was clearing the FixableGadgets list so it was naturally skipped, but now it's active again.

ziqingluo-90 added inline comments.Jul 18 2023, 4:01 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1092–1093

Oh, I intentionally chose not to skip it:

  • 1. To keep the Tracker consistent with CB.FixableGadgets in case in the future we want to use these two objects even if there is no WarningGadget;
  • 2. The findGadgets function can stay as straightforward as its name suggests. It doesn't have to know the specific optimization for empty WarningGadgets.

But the two thoughts above probably aren't important enough. I will change it back to skipping the loop when there is no warnings.

Address comments

NoQ accepted this revision.Jul 25 2023, 1:57 PM

LGTM, thanks!!

clang/lib/Analysis/UnsafeBufferUsage.cpp
1092–1093

Hmm, I agree that this turns into a confusing contract. Maybe move this loop out of the function, to the call site, so that it was more obvious that we skip it? This would leave the tracker in a mildly inconsistent state at return, so the caller will have to make it consistent by doing the claiming, but that's arguably less confusing because we clearly communicate that this is an optional step. So the function will do exactly what it says it'll do: find gadgets and that's it.

Separately, we can probably consider not searching for fixable gadgets at all when no warning gadgets are found. This could be a performance improvement, but definitely a story for another time.

This revision was landed with ongoing or failed builds.Jul 25 2023, 4:59 PM
This revision was automatically updated to reflect the committed changes.
ziqingluo-90 marked an inline comment as done.
ziqingluo-90 added inline comments.Jul 25 2023, 4:59 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1092–1093

Thanks for the suggestion. I moved the loop to the end of the call to findGadgets and landed it.