[clang] [compiler-rt] [llvm] [compiler-rt][CSan] Add a minimal value-check configuration (PR #244727)

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 12:00:18 PST 2026


================
@@ -534,6 +581,113 @@ bool ConcurrencySanitizer::instrumentMemIntrinsic(IntrinsicInst *II,
   return true;
 }
 
+void ConcurrencySanitizer::insertInlineDelay(Instruction *InsertBefore,
+                                             bool IsWrite) {
+  InstrumentationIRBuilder IRB(InsertBefore);
+  IRB.CreateCall(CsanDelay,
+                 {getFlags(IsWrite ? AccessFlags::Write : AccessFlags::None)});
+}
+
+// A memcpy has no value type. Read at most 8 bytes, or one byte when the
+// length is not a constant.
+static Type *rangeSampleTy(LLVMContext &Ctx, Value *ByteSize) {
+  uint64_t Bytes = 1;
+  if (auto *C = dyn_cast<ConstantInt>(ByteSize)) {
+    Bytes = C->getZExtValue();
+    if (Bytes > 8)
+      Bytes = 8;
+  }
+  return Type::getIntNTy(Ctx, Bytes * 8);
+}
+
+bool ConcurrencySanitizer::insertTypedValueCheck(Instruction *I, Value *Addr,
+                                                 Type *AccessTy,
+                                                 const DataLayout &DL,
+                                                 bool IsWrite) {
+  if (Addr->isSwiftError() || !AccessTy->isSized())
+    return false;
+  TypeSize StoreSize = DL.getTypeStoreSize(AccessTy);
+  if (StoreSize.isZero())
+    return false;
+
+  // icmp has no aggregate form. Load the stored bytes as an integer.
+  Type *LoadTy = AccessTy;
+  if (!AccessTy->isIntOrIntVectorTy() && !AccessTy->isPtrOrPtrVectorTy() &&
+      !AccessTy->isFPOrFPVectorTy()) {
+    TypeSize Bits = DL.getTypeStoreSizeInBits(AccessTy);
+    if (!Bits.isFixed())
+      return false;
+    LoadTy = Type::getIntNTy(AccessTy->getContext(), Bits.getFixedValue());
+  }
+
+  InstrumentationIRBuilder IRB(I);
+  Value *ByteSize = IRB.CreateTypeSize(IntptrTy, StoreSize);
+  return emitValueCheck(I, Addr, LoadTy, ByteSize, getLoadStoreAlignment(I),
+                        IsWrite);
+}
+
+bool ConcurrencySanitizer::insertRangeValueCheck(Instruction *I, Value *Addr,
+                                                 Value *ByteSize,
+                                                 bool IsWrite) {
+  if (auto *C = dyn_cast<ConstantInt>(ByteSize)) {
+    if (C->isZero())
+      return false;
+  } else {
+    // A dynamic length may be zero.
+    InstrumentationIRBuilder IRB(I);
+    I = SplitBlockAndInsertIfThen(IRB.CreateIsNotNull(ByteSize), I,
+                                  /*Unreachable=*/false);
+  }
+  return emitValueCheck(I, Addr, rangeSampleTy(I->getContext(), ByteSize),
+                        ByteSize, Align(1), IsWrite);
+}
+
+bool ConcurrencySanitizer::emitValueCheck(Instruction *I, Value *Addr,
+                                          Type *LoadTy, Value *ByteSize,
+                                          Align Alignment, bool IsWrite) {
+  const DataLayout &DL = I->getModule()->getDataLayout();
+  MDNode *NoSanitize = MDNode::get(I->getContext(), {});
+  InstrumentationIRBuilder IRB(I);
+  LoadInst *Old = IRB.CreateAlignedLoad(LoadTy, Addr, Alignment,
+                                        /*isVolatile=*/true, "csan.old");
+  Old->setMetadata(LLVMContext::MD_nosanitize, NoSanitize);
+
+  insertInlineDelay(I, IsWrite);
+
+  InstrumentationIRBuilder After(I);
+  LoadInst *New = After.CreateAlignedLoad(LoadTy, Addr, Alignment,
+                                          /*isVolatile=*/true, "csan.new");
+  New->setMetadata(LLVMContext::MD_nosanitize, NoSanitize);
+
+  // Compare the raw bits. A scalable vector has no fixed integer width.
+  TypeSize Bits = DL.getTypeSizeInBits(LoadTy);
+  Type *BitTy;
+  if (Bits.isScalable())
+    BitTy = LoadTy->isPtrOrPtrVectorTy()
+                ? DL.getIntPtrType(LoadTy)
+                : VectorType::getInteger(cast<VectorType>(LoadTy));
+  else
+    BitTy = After.getIntNTy(Bits.getFixedValue());
----------------
yxsamliu wrote:

Could we compare fixed vectors lane by lane? A 2 MiB vector copy hits LLVM’s integer-width limit when the check converts the whole vector to one integer.

https://github.com/llvm/llvm-project/pull/244727


More information about the cfe-commits mailing list