Skip to content

Commit 3f4abd9

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.8-beta.1
2 parents 72dafa1 + a4bf903 commit 3f4abd9

File tree

37 files changed

+1280
-8
lines changed

37 files changed

+1280
-8
lines changed

clang/include/clang/Basic/Sanitizers.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ SANITIZER_GROUP("bounds", Bounds, ArrayBounds | LocalBounds)
195195
// Scudo hardened allocator
196196
SANITIZER("scudo", Scudo)
197197

198+
// AllocToken
199+
SANITIZER("alloc-token", AllocToken)
200+
198201
// Magic group, containing all sanitizers. For example, "-fno-sanitize=all"
199202
// can be used to disable all the sanitizers.
200203
SANITIZER_GROUP("all", All, ~SanitizerMask())

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
@@ -1707,11 +1707,16 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
17071707
RValue RV =
17081708
EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs);
17091709

1710-
// Set !heapallocsite metadata on the call to operator new.
1711-
if (getDebugInfo())
1712-
if (auto *newCall = dyn_cast<llvm::CallBase>(RV.getScalarVal()))
1713-
getDebugInfo()->addHeapAllocSiteMetadata(newCall, allocType,
1714-
E->getExprLoc());
1710+
if (auto *newCall = dyn_cast<llvm::CallBase>(RV.getScalarVal())) {
1711+
if (auto *CGDI = getDebugInfo()) {
1712+
// Set !heapallocsite metadata on the call to operator new.
1713+
CGDI->addHeapAllocSiteMetadata(newCall, allocType, E->getExprLoc());
1714+
}
1715+
if (SanOpts.has(SanitizerKind::AllocToken)) {
1716+
// Set !alloc_token metadata.
1717+
EmitAllocToken(newCall, allocType);
1718+
}
1719+
}
17151720

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

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
846846
Fn->addFnAttr(llvm::Attribute::SanitizeNumericalStability);
847847
if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory))
848848
Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
849+
if (SanOpts.has(SanitizerKind::AllocToken))
850+
Fn->addFnAttr(llvm::Attribute::SanitizeAllocToken);
849851
}
850852
if (SanOpts.has(SanitizerKind::SafeStack))
851853
Fn->addFnAttr(llvm::Attribute::SafeStack);

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

llvm/docs/LangRef.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,9 @@ For example:
25272527
if the attributed function is called during invocation of a function
25282528
attributed with ``sanitize_realtime``.
25292529
This attribute is incompatible with the ``sanitize_realtime`` attribute.
2530+
``sanitize_alloc_token``
2531+
This attribute indicates that implicit allocation token instrumentation
2532+
is enabled for this function.
25302533
``speculative_load_hardening``
25312534
This attribute indicates that
25322535
`Speculative Load Hardening <https://llvm.org/docs/SpeculativeLoadHardening.html>`_
@@ -8548,6 +8551,21 @@ Example:
85488551
The ``nofree`` metadata indicates the memory pointed by the pointer will not be
85498552
freed after the attached instruction.
85508553

8554+
'``alloc_token``' Metadata
8555+
^^^^^^^^^^^^^^^^^^^^^^^^^^
8556+
8557+
The ``alloc_token`` metadata may be attached to calls to memory allocation
8558+
functions, and contains richer semantic information about the type of the
8559+
allocation. This information is consumed by the ``alloc-token`` pass to
8560+
instrument such calls with allocation token IDs.
8561+
8562+
The metadata contains a string with the type of an allocation.
8563+
8564+
.. code-block:: none
8565+
8566+
call ptr @malloc(i64 64), !alloc_token !0
8567+
8568+
!0 = !{!"<type-name>"}
85518569

85528570
Module Flags Metadata
85538571
=====================

llvm/docs/ReleaseNotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ Changes to Sanitizers
177177
Other Changes
178178
-------------
179179

180+
* Introduces the `AllocToken` pass, an instrumentation pass providing tokens to
181+
memory allocators enabling various heap organization strategies, such as heap
182+
partitioning.
183+
180184
External Open Source Projects Using LLVM {{env.config.release}}
181185
===============================================================
182186

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ enum AttributeKindCodes {
800800
ATTR_KIND_SANITIZE_TYPE = 101,
801801
ATTR_KIND_CAPTURES = 102,
802802
ATTR_KIND_DEAD_ON_RETURN = 103,
803+
ATTR_KIND_SANITIZE_ALLOC_TOKEN = 104,
803804
};
804805

805806
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ def SanitizeRealtime : EnumAttr<"sanitize_realtime", IntersectPreserve, [FnAttr]
342342
/// during a real-time sanitized function (see `sanitize_realtime`).
343343
def SanitizeRealtimeBlocking : EnumAttr<"sanitize_realtime_blocking", IntersectPreserve, [FnAttr]>;
344344

345+
/// Allocation token instrumentation is on.
346+
def SanitizeAllocToken : EnumAttr<"sanitize_alloc_token", IntersectPreserve, [FnAttr]>;
347+
345348
/// Speculative Load Hardening is enabled.
346349
///
347350
/// Note that this uses the default compatibility (always compatible during

llvm/include/llvm/IR/FixedMetadataKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ LLVM_FIXED_MD_KIND(MD_mmra, "mmra", 40)
5555
LLVM_FIXED_MD_KIND(MD_noalias_addrspace, "noalias.addrspace", 41)
5656
LLVM_FIXED_MD_KIND(MD_callee_type, "callee_type", 42)
5757
LLVM_FIXED_MD_KIND(MD_nofree, "nofree", 43)
58+
LLVM_FIXED_MD_KIND(MD_alloc_token, "alloc_token", 44)

0 commit comments

Comments
 (0)