[TextAPI] Fix memory leak in SymbolSet. (#150589)

The SymbolSet class bump-ptr-allocates Symbol objects, but Symbol has a
non-trivial destructor (since Symbol's Targets member is a SmallVector):
we need to explicitly destroy the Symbol objects to ensure that no
memory is leaked.

rdar://154778728
This commit is contained in:
Lang Hames 2025-07-26 09:32:10 +10:00 committed by GitHub
parent 2e3fd547de
commit 1299bba404
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 0 deletions

View File

@ -92,6 +92,7 @@ private:
public:
SymbolSet() = default;
~SymbolSet();
LLVM_ABI Symbol *addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags,
const Target &Targ);
size_t size() const { return Symbols.size(); }

View File

@ -11,6 +11,11 @@
using namespace llvm;
using namespace llvm::MachO;
SymbolSet::~SymbolSet() {
for (auto &[Key, Sym] : Symbols)
Sym->~Symbol();
}
Symbol *SymbolSet::addGlobalImpl(EncodeKind Kind, StringRef Name,
SymbolFlags Flags) {
Name = copyString(Name);

View File

@ -0,0 +1,20 @@
; RUN: rm -rf %t
; RUN: split-file %s %t
;
; RUN: llvm-readtapi %t/many-targets.tbd
;
; Check that tbds containing symbols with many targets parse correctly (and in
; particular parse without leaks).
;--- many-targets.tbd
--- !tapi-tbd
tbd-version: 4
targets: [ x86_64-macos, x86_64-maccatalyst, arm64-macos, arm64-maccatalyst,
arm64e-macos, arm64e-maccatalyst, arm64-ios, arm64e-ios ]
install-name: '/usr/lib/foo.dylib'
current-version: 1
exports:
- targets: [ x86_64-macos, x86_64-maccatalyst, arm64-macos, arm64-maccatalyst,
arm64e-macos, arm64e-maccatalyst, arm64-ios, arm64e-ios ]
symbols: [ 'foo' ]
...