[llvm] [sancov] Add -fsanitize-coverage=trace-args, trace-ret (PR #201410)
Marco Elver via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 4 04:19:29 PDT 2026
================
@@ -1266,3 +1303,467 @@ void ModuleSanitizerCoverage::createFunctionControlFlow(Function &F) {
ConstantArray::get(ArrayType::get(PtrTy, CFs.size()), CFs));
FunctionCFsArray->setConstant(true);
}
+
+// Helper: Given a DIType, resolve typedefs/qualifiers to the underlying type.
+static DIType *stripDITypedefs(DIType *Ty) {
+ while (Ty) {
+ if (auto *Derived = dyn_cast<DIDerivedType>(Ty)) {
+ unsigned Tag = Derived->getTag();
+ if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
+ Tag == dwarf::DW_TAG_volatile_type ||
+ Tag == dwarf::DW_TAG_restrict_type) {
+ Ty = Derived->getBaseType();
+ continue;
+ }
+ // pointer type - stop
+ break;
+ }
+ break;
+ }
+ return Ty;
+}
+
+// Helper: If Ty is a pointer to a struct (DICompositeType) or a struct
+// directly, collect byte offsets of all scalar members. Returns the offsets
+// array global and num_fields.
+static std::pair<GlobalVariable *, unsigned>
+getStructFieldOffsets(DIType *Ty, Module &M, const DataLayout &DL) {
+ if (!Ty)
+ return {nullptr, 0};
+
+ Ty = stripDITypedefs(Ty);
+
+ DICompositeType *Composite = nullptr;
+
+ // Case 1: pointer to struct
+ if (auto *PtrTy = dyn_cast_or_null<DIDerivedType>(Ty)) {
+ if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) {
+ DIType *PointeeTy = stripDITypedefs(PtrTy->getBaseType());
+ Composite = dyn_cast_or_null<DICompositeType>(PointeeTy);
+ }
+ }
+ // Case 2: direct struct type (for reassembled by-value args)
+ if (!Composite)
+ Composite = dyn_cast_or_null<DICompositeType>(Ty);
+
+ if (!Composite || Composite->getTag() != dwarf::DW_TAG_structure_type)
+ return {nullptr, 0};
+
+ SmallVector<uint64_t, 16> Offsets;
+ for (auto *Element : Composite->getElements()) {
+ auto *Member = dyn_cast<DIDerivedType>(Element);
+ if (!Member || Member->getTag() != dwarf::DW_TAG_member)
+ continue;
+ uint64_t OffsetBits = Member->getOffsetInBits();
+ uint64_t SizeBits = Member->getSizeInBits();
+ if (SizeBits == 0)
+ continue;
+ // Record byte offset and size in bytes as pairs: [offset, size]
+ Offsets.push_back(OffsetBits / 8);
+ Offsets.push_back(SizeBits / 8);
+ }
+
+ if (Offsets.empty())
+ return {nullptr, 0};
+
+ // Enhance #4: Compute type name hash from struct name
+ uint64_t TypeHash = 0;
+ if (auto Name = Composite->getName(); !Name.empty()) {
+ // Simple FNV-1a hash of the struct name
+ TypeHash = 0xcbf29ce484222325ULL;
+ for (char C : Name) {
+ TypeHash ^= (uint64_t)(unsigned char)C;
+ TypeHash *= 0x100000001b3ULL;
+ }
+ }
+
+ // Layout: [type_hash, off0, sz0, off1, sz1, ...]
+ // We pass &array[1] as the offsets pointer, so kernel can read array[0] as
+ // hash
+ LLVMContext &C = M.getContext();
+ Type *I64Ty = Type::getInt64Ty(C);
+ SmallVector<Constant *, 16> OffsetConstants;
+ OffsetConstants.push_back(
+ ConstantInt::get(I64Ty, TypeHash)); // index 0 = hash
+ for (uint64_t V : Offsets)
+ OffsetConstants.push_back(ConstantInt::get(I64Ty, V));
+
+ ArrayType *ArrTy = ArrayType::get(I64Ty, OffsetConstants.size());
+ auto *GV = new GlobalVariable(M, ArrTy, true, GlobalVariable::PrivateLinkage,
+ ConstantArray::get(ArrTy, OffsetConstants),
+ "__sancov_offsets_");
+ GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
+ return {GV, (unsigned)(Offsets.size() / 2)};
----------------
melver wrote:
getStructFieldOffsets returns a GlobalVariable * and forces every caller (fallback args, main args, dead args fallback, and return tracing) to duplicate the exact same runtime GEP construction:
Value *Indices[] = {ConstantInt::get(Int64Ty, 0), ConstantInt::get(Int64Ty, 1)};
OffsetsPtr = IRB.CreateInBoundsGEP(OffsetsGV->getValueType(), OffsetsGV, Indices);
Because OffsetsGV and the indices are compile-time constants, could getStructFieldOffsets return Constant *OffsetsPtr directly (using ConstantExpr::getGetElementPtr or returning null if no offsets)?
https://github.com/llvm/llvm-project/pull/201410
More information about the llvm-commits
mailing list