This is an archive of the discontinued LLVM Phabricator instance.

[-Wunsafe-buffer-usage] FixableGadget for handling stand alone pointers under UPC.
ClosedPublic

Authored by malavikasamak on Feb 9 2023, 1:04 PM.

Details

Summary

This patch introduces UPCStandalonePointerGadget, a FixableGadget that emits fixits to handle cases where a pointer identified as unsafe is simply referenced. An example of such a case is when the pointer is input as an argument to a method call, where we can not change the type of the argument. For cases where the strategy for the unsafe pointer is to use std::span, the idea is to extract the underlying pointer by invoking the "data()" method on the span instance.

For example, the gadget emits a fixit for S3, where S1, S2 are handled by other gadgets:

S1: int *ptr = new int[10]; 
S2: int val1 = ptr[k];  // Unsafe operation on ptr
S3: foo(ptr); // Some method that accepts raw pointer => FIXIT: foo(ptr.data());

Diff Detail

Event Timeline

malavikasamak created this revision.Feb 9 2023, 1:04 PM
Herald added a project: Restricted Project. · View Herald Transcript
malavikasamak requested review of this revision.Feb 9 2023, 1:04 PM

Updating diff to get rid of white-space changes.

ziqingluo-90 added inline comments.Feb 9 2023, 3:58 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
182

I haven't thought it through carefully but I'm afraid that this could result in one match result with both LHS and RHS being matched and bound to the same tag. Only one Fixable will be created from this match result but we actually need two. Besides, reading the tag from the match result could return either LHS or RHS non-deterministically I guess.

clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp
29

we could also test when both p and q are used somewhere unsafely.

Added a new test case.

malavikasamak marked an inline comment as done.Feb 9 2023, 4:36 PM
malavikasamak added inline comments.
clang/lib/Analysis/UnsafeBufferUsage.cpp
182

Do you mean that this can result in only one side of the equation will be fixed? Added a test case "condition_check_two_usafe_buffers" to check such cases.

NoQ added inline comments.Feb 9 2023, 6:27 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
182

This code produces two matches, one for LHS one for RHS, and never produces a match in which both are bound: https://godbolt.org/z/dadETKf8d

So there's no "either LHS or RHS non-deterministically", it's just two independent MatchResults.

So I think this looks correct, just like the callExpr() case.

ziqingluo-90 added inline comments.Feb 9 2023, 6:45 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
182

Oh yes, you are right. I really didn't think it through.

jkorous added inline comments.Feb 13 2023, 10:30 AM
clang/lib/Analysis/UnsafeBufferUsage.cpp
499

I get confused by the word Access. I think here we are actually processing cases where the memory address pointed to is not accessed.

527

We can now remove the dyn_cast since Node has type DeclRefExpr*.

1172

We should be able to remove this dyn_cast too.

malavikasamak marked 3 inline comments as done.
malavikasamak retitled this revision from [-Wunsafe-buffer-usage][WIP] Introducing a new fixable for handling pointer context DRE access. to [-Wunsafe-buffer-usage] Introducing a new fixable for handling pointer context DRE access..
malavikasamak retitled this revision from [-Wunsafe-buffer-usage] Introducing a new fixable for handling pointer context DRE access. to [-Wunsafe-buffer-usage] FixableGadget for handling pointer references under UPC..Mar 2 2023, 11:55 AM
malavikasamak edited the summary of this revision. (Show Details)
NoQ added a comment.Mar 16 2023, 3:16 PM

Other than my usual complaint about declRefExpr(to(varDecl())), LGTM!

clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
37

🚲🏠 : "Reference" kinda reads like "int *&q = p" which is not what we mean. (Yes, in DeclRefExpr "Ref" stands for "Reference" but nobody likes it.) Folks also seem to be adding UPC/ULC when appropriate. Maybe UPCStandalonePointer???

malavikasamak marked an inline comment as done.
malavikasamak edited the summary of this revision. (Show Details)
NoQ added inline comments.Mar 23 2023, 3:43 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1172

Nitpick: This is one of the few recommended use cases for auto (https://www.llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable).

1178–1182

This code probably wants to check for empty optional before dereferencing(?)

malavikasamak marked an inline comment as done.
malavikasamak added inline comments.
clang/lib/Analysis/UnsafeBufferUsage.cpp
1178–1182

The getEndCharLoc method does not return an optional at this point of time in our stack. So, I am going to change the type of endOfOperand to SourceLocation to reflect this.

ziqingluo-90 accepted this revision.Mar 29 2023, 11:20 AM

LGTM now!

This revision is now accepted and ready to land.Mar 29 2023, 11:20 AM
NoQ accepted this revision.Apr 3 2023, 12:28 PM

I think this is good to go!

malavikasamak retitled this revision from [-Wunsafe-buffer-usage] FixableGadget for handling pointer references under UPC. to [-Wunsafe-buffer-usage] FixableGadget for handling stand alone pointers under UPC..Apr 7 2023, 11:10 AM
This revision was landed with ongoing or failed builds.Apr 7 2023, 3:32 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptApr 7 2023, 3:32 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript