Skip to content

Commit ecadd90

Browse files
committed
[Clang][CodeGen] Emit !alloc_token for new expressions (#162099)
[ Reland after 7815df1 ("[Clang] Fix brittle print-header-json.c test") ] For new expressions, the allocated type is syntactically known and we can trivially emit the !alloc_token metadata. A subsequent change will wire up the AllocToken pass and introduce appropriate tests. --- This change is part of the following series: 1. llvm/llvm-project#160131 2. llvm/llvm-project#156838 3. llvm/llvm-project#162098 4. llvm/llvm-project#162099 5. llvm/llvm-project#156839 6. llvm/llvm-project#156840 7. llvm/llvm-project#156841 8. llvm/llvm-project#156842
1 parent 8ba7384 commit ecadd90

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,23 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
12721272
EmitCheck(std::make_pair(Check, CheckKind), CheckHandler, StaticData, Index);
12731273
}
12741274

1275+
void CodeGenFunction::EmitAllocToken(llvm::CallBase *CB, QualType AllocType) {
1276+
assert(SanOpts.has(SanitizerKind::AllocToken) &&
1277+
"Only needed with -fsanitize=alloc-token");
1278+
1279+
PrintingPolicy Policy(CGM.getContext().getLangOpts());
1280+
Policy.SuppressTagKeyword = true;
1281+
Policy.FullyQualifiedName = true;
1282+
SmallString<64> TypeName;
1283+
llvm::raw_svector_ostream TypeNameOS(TypeName);
1284+
AllocType.getCanonicalType().print(TypeNameOS, Policy);
1285+
auto *TypeMDS = llvm::MDString::get(CGM.getLLVMContext(), TypeNameOS.str());
1286+
1287+
// Format: !{<type-name>}
1288+
auto *MDN = llvm::MDNode::get(CGM.getLLVMContext(), {TypeMDS});
1289+
CB->setMetadata(llvm::LLVMContext::MD_alloc_token, MDN);
1290+
}
1291+
12751292
CodeGenFunction::ComplexPairTy CodeGenFunction::
12761293
EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
12771294
bool isInc, bool isPre) {

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,11 +1655,16 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
16551655
RValue RV =
16561656
EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs);
16571657

1658-
// Set !heapallocsite metadata on the call to operator new.
1659-
if (getDebugInfo())
1660-
if (auto *newCall = dyn_cast<llvm::CallBase>(RV.getScalarVal()))
1661-
getDebugInfo()->addHeapAllocSiteMetadata(newCall, allocType,
1662-
E->getExprLoc());
1658+
if (auto *newCall = dyn_cast<llvm::CallBase>(RV.getScalarVal())) {
1659+
if (auto *CGDI = getDebugInfo()) {
1660+
// Set !heapallocsite metadata on the call to operator new.
1661+
CGDI->addHeapAllocSiteMetadata(newCall, allocType, E->getExprLoc());
1662+
}
1663+
if (SanOpts.has(SanitizerKind::AllocToken)) {
1664+
// Set !alloc_token metadata.
1665+
EmitAllocToken(newCall, allocType);
1666+
}
1667+
}
16631668

16641669
// If this was a call to a global replaceable allocation function that does
16651670
// not take an alignment argument, the allocator is known to produce

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,6 +3348,9 @@ class CodeGenFunction : public CodeGenTypeCache {
33483348
SanitizerAnnotateDebugInfo(ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
33493349
SanitizerHandler Handler);
33503350

3351+
/// Emit additional metadata used by the AllocToken instrumentation.
3352+
void EmitAllocToken(llvm::CallBase *CB, QualType AllocType);
3353+
33513354
llvm::Value *GetCountedByFieldExprGEP(const Expr *Base, const FieldDecl *FD,
33523355
const FieldDecl *CountDecl);
33533356

0 commit comments

Comments
 (0)