[clang] [llvm] [CodeGen] Use unique_ptr for FunctionInfo to prevent memory leaks (PR #196601)
https://github.com/vitalybuka closed https://github.com/llvm/llvm-project/pull/196601 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [CodeGen] Use unique_ptr for FunctionInfo to prevent memory leaks (PR #196601)
https://github.com/vitalybuka created
https://github.com/llvm/llvm-project/pull/196601
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.
This fix was developed with the assistance of Gemini.
Fixes leak from 07b5dfe9473c.
Buildbot: https://lab.llvm.org/buildbot/#/builders/52/builds/17090
TAG=agy
CONV=08a959c8-59e8-47cc-a0e2-f39f7aee1c9d
>From 32e34bb600403babc52e350115c5221030219dc7 Mon Sep 17 00:00:00 2001
From: Vitaly Buka
Date: Fri, 8 May 2026 11:23:50 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
---
clang/lib/CodeGen/CGCall.cpp | 2 +-
llvm/include/llvm/ABI/FunctionInfo.h | 2 +-
llvm/lib/ABI/FunctionInfo.cpp| 11 ++-
3 files changed, 8 insertions(+), 7 deletions(-)
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 {
unsigned arg_size() const { return NumArgs; }
- static FunctionInfo *
+ static std::unique_ptr
create(CallingConv::ID CC, const Type *ReturnType,
ArrayRef ArgTypes,
std::optional NumRequired = std::nullopt);
diff --git a/llvm/lib/ABI/FunctionInfo.cpp b/llvm/lib/ABI/FunctionInfo.cpp
index f89d90c74ea03..7096392135ce8 100644
--- a/llvm/lib/ABI/FunctionInfo.cpp
+++ b/llvm/lib/ABI/FunctionInfo.cpp
@@ -12,16 +12,17 @@
using namespace llvm;
using namespace llvm::abi;
-FunctionInfo *FunctionInfo::create(CallingConv::ID CC, const Type *ReturnType,
- ArrayRef ArgTypes,
- std::optional NumRequired) {
+std::unique_ptr
+FunctionInfo::create(CallingConv::ID CC, const Type *ReturnType,
+ ArrayRef ArgTypes,
+ std::optional NumRequired) {
assert(!NumRequired || *NumRequired <= ArgTypes.size());
void *Buffer = operator new(totalSizeToAlloc(ArgTypes.size()));
- FunctionInfo *FI =
- new (Buffer) FunctionInfo(CC, ReturnType, ArgTypes.size(), NumRequired);
+ std::unique_ptr FI(
+ new (Buffer) FunctionInfo(CC, ReturnType, ArgTypes.size(), NumRequired));
ArgEntry *Args = FI->getTrailingObjects();
for (unsigned I = 0; I < ArgTypes.size(); ++I)
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [CodeGen] Use unique_ptr for FunctionInfo to prevent memory leaks (PR #196601)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang-codegen
Author: Vitaly Buka (vitalybuka)
Changes
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.
This fix was developed with the assistance of Gemini.
Fixes leak from 07b5dfe9473c.
Buildbot: https://lab.llvm.org/buildbot/#/builders/52/builds/17090
TAG=agy
CONV=08a959c8-59e8-47cc-a0e2-f39f7aee1c9d
---
Full diff: https://github.com/llvm/llvm-project/pull/196601.diff
3 Files Affected:
- (modified) clang/lib/CodeGen/CGCall.cpp (+1-1)
- (modified) llvm/include/llvm/ABI/FunctionInfo.h (+1-1)
- (modified) llvm/lib/ABI/FunctionInfo.cpp (+6-5)
``diff
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 {
unsigned arg_size() const { return NumArgs; }
- static FunctionInfo *
+ static std::unique_ptr
create(CallingConv::ID CC, const Type *ReturnType,
ArrayRef ArgTypes,
std::optional NumRequired = std::nullopt);
diff --git a/llvm/lib/ABI/FunctionInfo.cpp b/llvm/lib/ABI/FunctionInfo.cpp
index f89d90c74ea03..7096392135ce8 100644
--- a/llvm/lib/ABI/FunctionInfo.cpp
+++ b/llvm/lib/ABI/FunctionInfo.cpp
@@ -12,16 +12,17 @@
using namespace llvm;
using namespace llvm::abi;
-FunctionInfo *FunctionInfo::create(CallingConv::ID CC, const Type *ReturnType,
- ArrayRef ArgTypes,
- std::optional NumRequired) {
+std::unique_ptr
+FunctionInfo::create(CallingConv::ID CC, const Type *ReturnType,
+ ArrayRef ArgTypes,
+ std::optional NumRequired) {
assert(!NumRequired || *NumRequired <= ArgTypes.size());
void *Buffer = operator new(totalSizeToAlloc(ArgTypes.size()));
- FunctionInfo *FI =
- new (Buffer) FunctionInfo(CC, ReturnType, ArgTypes.size(), NumRequired);
+ std::unique_ptr FI(
+ new (Buffer) FunctionInfo(CC, ReturnType, ArgTypes.size(), NumRequired));
ArgEntry *Args = FI->getTrailingObjects();
for (unsigned I = 0; I < ArgTypes.size(); ++I)
``
https://github.com/llvm/llvm-project/pull/196601
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
