================
@@ -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()),
----------------
kuroyukiasuna wrote:

You're right. I dropped the Type, so the static data is just the source 
location. The runtime now prints the signed distance itself.

https://github.com/llvm/llvm-project/pull/210635
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to