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

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 25 01:17:43 PDT 2025


https://github.com/lhames created https://github.com/llvm/llvm-project/pull/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

>From 7fd991e585924773363c381c66e6a41aa70ef63c Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Fri, 25 Jul 2025 18:07:00 +1000
Subject: [PATCH] [TextAPI] Fix memory leak in SymbolSet.

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
---
 llvm/include/llvm/TextAPI/SymbolSet.h         |  1 +
 llvm/lib/TextAPI/SymbolSet.cpp                |  5 +++++
 .../tools/llvm-readtapi/many-targets.test     | 20 +++++++++++++++++++
 3 files changed, 26 insertions(+)
 create mode 100644 llvm/test/tools/llvm-readtapi/many-targets.test

diff --git a/llvm/include/llvm/TextAPI/SymbolSet.h b/llvm/include/llvm/TextAPI/SymbolSet.h
index cd3066317f3ae..a04cb350b8f58 100644
--- a/llvm/include/llvm/TextAPI/SymbolSet.h
+++ b/llvm/include/llvm/TextAPI/SymbolSet.h
@@ -92,6 +92,7 @@ class SymbolSet {
 
 public:
   SymbolSet() = default;
+  ~SymbolSet();
   LLVM_ABI Symbol *addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags,
                              const Target &Targ);
   size_t size() const { return Symbols.size(); }
diff --git a/llvm/lib/TextAPI/SymbolSet.cpp b/llvm/lib/TextAPI/SymbolSet.cpp
index 2e0b4160c9b46..f21a0618e60a9 100644
--- a/llvm/lib/TextAPI/SymbolSet.cpp
+++ b/llvm/lib/TextAPI/SymbolSet.cpp
@@ -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);
diff --git a/llvm/test/tools/llvm-readtapi/many-targets.test b/llvm/test/tools/llvm-readtapi/many-targets.test
new file mode 100644
index 0000000000000..efb44b5affcea
--- /dev/null
+++ b/llvm/test/tools/llvm-readtapi/many-targets.test
@@ -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' ]
+...



More information about the llvm-commits mailing list