================
@@ -5055,6 +5056,25 @@ Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) {
divisor = CGF.CGM.getSize(elementSize);
}
+ // If the unaligned-pointer-subtraction sanitizer is on, verify at runtime
+ // that the byte distance is an exact multiple of the element size.
+ if (CGF.SanOpts.has(SanitizerKind::UnalignedPointerSubtraction)) {
+ auto checkOrdinal = SanitizerKind::SO_UnalignedPointerSubtraction;
+ auto CheckHandler = SanitizerHandler::UnalignedPointerSubtraction;
+ SanitizerDebugLocation SanScope(&CGF, {checkOrdinal}, CheckHandler);
+
+ llvm::Value *Zero = llvm::ConstantInt::get(CGF.PtrDiffTy, 0);
+ llvm::Value *Rem = Builder.CreateSRem(diffInChars, divisor, "sub.ptr.rem");
+ llvm::Value *IsExact = Builder.CreateICmpEQ(Rem, Zero, "sub.ptr.exact");
+
+ llvm::Constant *StaticArgs[] = {
+ CGF.EmitCheckSourceLocation(op.E->getExprLoc()),
+ CGF.EmitCheckTypeDescriptor(op.E->getType())};
+ llvm::Value *DynamicArgs[] = {diffInChars, divisor};
+ CGF.EmitCheck({{IsExact, checkOrdinal}}, CheckHandler, StaticArgs,
+ DynamicArgs);
+ }
+
if (CGF.getLangOpts().StablePointerSubtraction)
----------------
kuroyukiasuna wrote:
Added TRAP / ABORT / RECOVER / MIN RUN lines to the CodeGen test
https://github.com/llvm/llvm-project/pull/210635
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits