Files
Vitaly Buka d2db5bba03 [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
2026-05-09 01:18:41 +00:00

35 lines
1.2 KiB
C++

//===----- FunctionInfo.cpp - ABI Function Information ----------- C++ ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ABI/FunctionInfo.h"
#include <optional>
using namespace llvm;
using namespace llvm::abi;
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 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)
new (&Args[I]) ArgEntry(ArgTypes[I]);
return FI;
}