diff options
| author | Yingwei Zheng <dtcxzyw2333@gmail.com> | 2025-05-09 14:53:00 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-09 14:53:00 +0800 |
| commit | d2b012e3913a56d3cb2d9798bf77dc2599148dfb (patch) | |
| tree | 2b598be59efe4ba03536c7fbc0594d6b01822d45 /clang/lib/CodeGen/CGExprScalar.cpp | |
| parent | 03d3e6dbe9e7a0c64e6c62b583bb187723421717 (diff) | |
[Clang][CodeGen] Enable pointer overflow check for GCC workaround (#137849)
Do not suppress the pointer overflow check for the `(i8*) nullptr + N`
idiom.
Related issue: https://github.com/llvm/llvm-project/issues/137833
Diffstat (limited to 'clang/lib/CodeGen/CGExprScalar.cpp')
| -rw-r--r-- | clang/lib/CodeGen/CGExprScalar.cpp | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index f639a87e3ad0..df793a5a4635 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -4180,11 +4180,28 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF, // The index is not pointer-sized. // The pointer type is not byte-sized. // - if (BinaryOperator::isNullPointerArithmeticExtension(CGF.getContext(), - op.Opcode, - expr->getLHS(), - expr->getRHS())) - return CGF.Builder.CreateIntToPtr(index, pointer->getType()); + // Note that we do not suppress the pointer overflow check in this case. + if (BinaryOperator::isNullPointerArithmeticExtension( + CGF.getContext(), op.Opcode, expr->getLHS(), expr->getRHS())) { + Value *Ptr = CGF.Builder.CreateIntToPtr(index, pointer->getType()); + if (CGF.getLangOpts().PointerOverflowDefined || + !CGF.SanOpts.has(SanitizerKind::PointerOverflow) || + NullPointerIsDefined(CGF.Builder.GetInsertBlock()->getParent(), + PtrTy->getPointerAddressSpace())) + return Ptr; + // The inbounds GEP of null is valid iff the index is zero. + CodeGenFunction::SanitizerScope SanScope(&CGF); + Value *IsZeroIndex = CGF.Builder.CreateIsNull(index); + llvm::Constant *StaticArgs[] = { + CGF.EmitCheckSourceLocation(op.E->getExprLoc())}; + llvm::Type *IntPtrTy = DL.getIntPtrType(PtrTy); + Value *IntPtr = llvm::Constant::getNullValue(IntPtrTy); + Value *ComputedGEP = CGF.Builder.CreateZExtOrTrunc(index, IntPtrTy); + Value *DynamicArgs[] = {IntPtr, ComputedGEP}; + CGF.EmitCheck({{IsZeroIndex, SanitizerKind::SO_PointerOverflow}}, + SanitizerHandler::PointerOverflow, StaticArgs, DynamicArgs); + return Ptr; + } if (width != DL.getIndexTypeSizeInBits(PtrTy)) { // Zero-extend or sign-extend the pointer value according to |
