[clang] d2db5bb - [CodeGen] Use unique_ptr for FunctionInfo to prevent memory leaks (#196603)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 8 18:18:46 PDT 2026
Author: Vitaly Buka
Date: 2026-05-09T01:18:41Z
New Revision: d2db5bba03413f6d4bcae1a4ae3d284bf2a7de8e
URL: https://github.com/llvm/llvm-project/commit/d2db5bba03413f6d4bcae1a4ae3d284bf2a7de8e
DIFF: https://github.com/llvm/llvm-project/commit/d2db5bba03413f6d4bcae1a4ae3d284bf2a7de8e.diff
LOG: [CodeGen] Use unique_ptr for FunctionInfo to prevent memory leaks (#196603)
Raw pointer return from `FunctionInfo::create` caused leaks in callers
like `computeABIInfoUsingLib`, breaking BPF tests on ASan bots.
Using `std::unique_ptr` enforces automatic cleanup.
Fixes leak from #194460.
Buildbot: https://lab.llvm.org/buildbot/#/builders/52/builds/17090
Assisted-by: Gemini
Added:
Modified:
clang/lib/CodeGen/CGCall.cpp
llvm/include/llvm/ABI/FunctionInfo.h
llvm/lib/ABI/FunctionInfo.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 1cafe364c4c42..a2b9c945788ee 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -843,7 +843,7 @@ void CodeGenModule::computeABIInfoUsingLib(CGFunctionInfo &FI) {
if (Required.allowsOptionalArgs())
NumRequired = Required.getNumRequiredArgs();
- llvm::abi::FunctionInfo *AbiFI = llvm::abi::FunctionInfo::create(
+ auto AbiFI = llvm::abi::FunctionInfo::create(
FI.getCallingConvention(), AbiMapper->convertType(FI.getReturnType()),
MappedArgTypes, NumRequired);
diff --git a/llvm/include/llvm/ABI/FunctionInfo.h b/llvm/include/llvm/ABI/FunctionInfo.h
index 7f7b6a44ba6ad..0ebd0700836e2 100644
--- a/llvm/include/llvm/ABI/FunctionInfo.h
+++ b/llvm/include/llvm/ABI/FunctionInfo.h
@@ -234,7 +234,7 @@ class FunctionInfo final : private TrailingObjects<FunctionInfo, ArgEntry> {
unsigned arg_size() const { return NumArgs; }
- static FunctionInfo *
+ static std::unique_ptr<FunctionInfo>
create(CallingConv::ID CC, const Type *ReturnType,
ArrayRef<const Type *> ArgTypes,
std::optional<unsigned> NumRequired = std::nullopt);
diff --git a/llvm/lib/ABI/FunctionInfo.cpp b/llvm/lib/ABI/FunctionInfo.cpp
index f89d90c74ea03..4e3b4c3f22aff 100644
--- a/llvm/lib/ABI/FunctionInfo.cpp
+++ b/llvm/lib/ABI/FunctionInfo.cpp
@@ -12,16 +12,19 @@
using namespace llvm;
using namespace llvm::abi;
-FunctionInfo *FunctionInfo::create(CallingConv::ID CC, const Type *ReturnType,
- ArrayRef<const Type *> ArgTypes,
- std::optional<unsigned> NumRequired) {
+std::unique_ptr<FunctionInfo>
+FunctionInfo::create(CallingConv::ID CC, const Type *ReturnType,
+ ArrayRef<const Type *> ArgTypes,
+ std::optional<unsigned> NumRequired) {
assert(!NumRequired || *NumRequired <= ArgTypes.size());
void *Buffer = operator new(totalSizeToAlloc<ArgEntry>(ArgTypes.size()));
- FunctionInfo *FI =
- new (Buffer) FunctionInfo(CC, ReturnType, ArgTypes.size(), NumRequired);
+ // FunctionInfo overloads operator delete, so we can use std::unique_ptr
+ // without worrying about sized deallocation of trailing objects.
+ std::unique_ptr<FunctionInfo> FI(
+ new (Buffer) FunctionInfo(CC, ReturnType, ArgTypes.size(), NumRequired));
ArgEntry *Args = FI->getTrailingObjects();
for (unsigned I = 0; I < ArgTypes.size(); ++I)
More information about the cfe-commits
mailing list