================
@@ -935,25 +935,80 @@ propagateMemProfMetadata(Function *Callee, CallBase &CB,
}
}
-/// Collect all calls that produce RetVal, following only pointer-preserving
-/// instructions (cast, phi, select).
-static void collectPointerReturningCalls(Value *RetVal,
- SmallVectorImpl<CallBase *> &Out) {
+/// Returns true for a {begin pointer, size} pair. This is the only span shape
+/// returned by malloc_span functions whose begin pointer is known from the
+/// type: a {pointer, pointer} pair may be in either order, and a span coerced
+/// by the ABI (e.g. to [2 x i64] on AArch64) has no pointer field at all, so
+/// those are only followed when returned as a whole.
+static bool isPointerSizeSpan(Type *Ty) {
+ auto *STy = dyn_cast<StructType>(Ty);
+ return STy && STy->getNumElements() == 2 &&
+ STy->getElementType(0)->isPointerTy() &&
+ STy->getElementType(1)->isIntegerTy();
+}
+
+/// Collect the calls whose result RetVal returns, either as a whole or, for a
+/// {pointer, size} span, as its begin pointer. Follows casts, phis and
+/// selects; field 0 of such a span (extractvalue, insertvalue); and a local
+/// temporary the value is stored to at offset 0 and loaded from, as emitted
+/// for returned aggregates before SROA.
+static void collectReturnedCalls(Value *RetVal,
+ SmallVectorImpl<CallBase *> &Out) {
+ auto IsField0 = [](ArrayRef<unsigned> Indices) {
+ return Indices.size() == 1 && Indices[0] == 0;
+ };
SmallVector<Value *, 8> Worklist{RetVal};
SmallPtrSet<Value *, 8> Visited;
while (!Worklist.empty()) {
Value *V = Worklist.pop_back_val();
- if (!V->getType()->isPointerTy() || !Visited.insert(V).second)
+ if (!Visited.insert(V).second)
continue;
- if (auto *CB = dyn_cast<CallBase>(V))
+ if (auto *CB = dyn_cast<CallBase>(V)) {
Out.push_back(CB);
- else if (isa<BitCastInst, AddrSpaceCastInst>(V))
+ } else if (isa<BitCastInst, AddrSpaceCastInst>(V)) {
Worklist.push_back(cast<CastInst>(V)->getOperand(0));
- else if (auto *PN = dyn_cast<PHINode>(V))
+ } else if (auto *PN = dyn_cast<PHINode>(V)) {
append_range(Worklist, PN->incoming_values());
- else if (auto *SI = dyn_cast<SelectInst>(V)) {
+ } else if (auto *SI = dyn_cast<SelectInst>(V)) {
Worklist.push_back(SI->getTrueValue());
Worklist.push_back(SI->getFalseValue());
+ } else if (auto *EVI = dyn_cast<ExtractValueInst>(V)) {
+ // The begin pointer of a span.
+ if (isPointerSizeSpan(EVI->getAggregateOperand()->getType()) &&
+ IsField0(EVI->getIndices()))
+ Worklist.push_back(EVI->getAggregateOperand());
+ } else if (auto *IVI = dyn_cast<InsertValueInst>(V)) {
+ // A span built from its begin pointer.
+ if (isPointerSizeSpan(IVI->getType()))
+ Worklist.push_back(IsField0(IVI->getIndices())
+ ? IVI->getInsertedValueOperand()
+ : IVI->getAggregateOperand());
+ } else if (auto *LI = dyn_cast<LoadInst>(V)) {
----------------
melver wrote:
This collects every store to the alloca at offset 0, whether or not it
reaches the load. It also runs for pointer-typed returns (Ty is ptr, so
`Stored->getType() == Ty` holds), which is a behaviour change for the
existing malloc/alloc_size wrappers that the title and description don't
mention. Unoptimized IR for
```
void *wrapper(size_t n) {
void *p = lookup_ctx();
release_ctx(p);
p = malloc(n);
return p;
}
```
stores both calls to %p, so both get the caller's token. Before this patch
neither did (the load stopped the walk). With
-fsanitize-alloc-token-extended, lookup_ctx() is then treated as an
allocation and rewritten to its __alloc_token_ variant.
This is the same problem as the reassignment case on #241548
(`p = malloc(8); free(p); p = malloc(8);`), one level down. There is also no
check that the alloca does not escape.
https://github.com/llvm/llvm-project/pull/244314
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits