Skip to content

Commit 66e9078

Browse files
[LTO] Fix a use-after-free in legacy LTO C APIs (llvm#107896)
Fix a bug that `lto_runtime_lib_symbols_list` is returning the address of a local variable that will be freed when getting out of scope. This is a regression from llvm#98512 that rewrites the runtime libcall function lists into a SmallVector. rdar://135559037
1 parent d9a9960 commit 66e9078

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

llvm/tools/lto/lto.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "llvm-c/lto.h"
1515
#include "llvm/ADT/STLExtras.h"
16+
#include "llvm/ADT/SmallVector.h"
1617
#include "llvm/ADT/StringExtras.h"
1718
#include "llvm/Bitcode/BitcodeReader.h"
1819
#include "llvm/CodeGen/CommandFlags.h"
@@ -88,6 +89,8 @@ struct LTOToolDiagnosticHandler : public DiagnosticHandler {
8889
}
8990
};
9091

92+
static SmallVector<const char *> RuntimeLibcallSymbols;
93+
9194
// Initialize the configured targets if they have not been initialized.
9295
static void lto_initialize() {
9396
if (!initialized) {
@@ -108,6 +111,7 @@ static void lto_initialize() {
108111
LTOContext = &Context;
109112
LTOContext->setDiagnosticHandler(
110113
std::make_unique<LTOToolDiagnosticHandler>(), true);
114+
RuntimeLibcallSymbols = lto::LTO::getRuntimeLibcallSymbols(Triple());
111115
initialized = true;
112116
}
113117
}
@@ -691,7 +695,6 @@ extern const char *lto_input_get_dependent_library(lto_input_t input,
691695
}
692696

693697
extern const char *const *lto_runtime_lib_symbols_list(size_t *size) {
694-
auto symbols = lto::LTO::getRuntimeLibcallSymbols(Triple());
695-
*size = symbols.size();
696-
return symbols.data();
698+
*size = RuntimeLibcallSymbols.size();
699+
return RuntimeLibcallSymbols.data();
697700
}

0 commit comments

Comments
 (0)