Skip to content

Commit 6361a8a

Browse files
committed
[CaptureTracking] Check for equality predicate for null comparisons
The logic here is not valid for non-equality comparisons. E.g. using slt will leak the sign bit, regardless of whether the pointer is dereferenceable. This fix is split out from #125880.
1 parent 54f14d9 commit 6361a8a

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

llvm/lib/Analysis/CaptureTracking.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,12 @@ UseCaptureKind llvm::DetermineUseCaptureKind(
376376
case Instruction::ICmp: {
377377
unsigned Idx = U.getOperandNo();
378378
unsigned OtherIdx = 1 - Idx;
379-
if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(OtherIdx))) {
379+
if (isa<ConstantPointerNull>(I->getOperand(OtherIdx)) &&
380+
cast<ICmpInst>(I)->isEquality()) {
380381
// Don't count comparisons of a no-alias return value against null as
381382
// captures. This allows us to ignore comparisons of malloc results
382383
// with null, for example.
383-
if (CPN->getType()->getAddressSpace() == 0)
384+
if (U->getType()->getPointerAddressSpace() == 0)
384385
if (isNoAliasCall(U.get()->stripPointerCasts()))
385386
return UseCaptureKind::NO_CAPTURE;
386387
if (!I->getFunction()->nullPointerIsDefined()) {

llvm/test/Transforms/FunctionAttrs/nocapture.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ define i1 @captureICmpWrongPred(ptr %x) {
791791
define i1 @captureICmpWrongPredDereferenceableOrNull(ptr dereferenceable_or_null(1) %x) {
792792
; FNATTRS: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
793793
; FNATTRS-LABEL: define noundef i1 @captureICmpWrongPredDereferenceableOrNull
794-
; FNATTRS-SAME: (ptr readnone captures(none) dereferenceable_or_null(1) [[X:%.*]]) #[[ATTR0]] {
794+
; FNATTRS-SAME: (ptr readnone dereferenceable_or_null(1) [[X:%.*]]) #[[ATTR0]] {
795795
; FNATTRS-NEXT: [[TMP1:%.*]] = icmp slt ptr [[X]], null
796796
; FNATTRS-NEXT: ret i1 [[TMP1]]
797797
;

0 commit comments

Comments
 (0)