Skip to content

Commit

Permalink
[LTO] Fix a use-after-free in legacy LTO C APIs (llvm#107896)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cachemeifyoucan committed Sep 9, 2024
1 parent d9a9960 commit 66e9078
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions llvm/tools/lto/lto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "llvm-c/lto.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/CodeGen/CommandFlags.h"
Expand Down Expand Up @@ -88,6 +89,8 @@ struct LTOToolDiagnosticHandler : public DiagnosticHandler {
}
};

static SmallVector<const char *> RuntimeLibcallSymbols;

// Initialize the configured targets if they have not been initialized.
static void lto_initialize() {
if (!initialized) {
Expand All @@ -108,6 +111,7 @@ static void lto_initialize() {
LTOContext = &Context;
LTOContext->setDiagnosticHandler(
std::make_unique<LTOToolDiagnosticHandler>(), true);
RuntimeLibcallSymbols = lto::LTO::getRuntimeLibcallSymbols(Triple());
initialized = true;
}
}
Expand Down Expand Up @@ -691,7 +695,6 @@ extern const char *lto_input_get_dependent_library(lto_input_t input,
}

extern const char *const *lto_runtime_lib_symbols_list(size_t *size) {
auto symbols = lto::LTO::getRuntimeLibcallSymbols(Triple());
*size = symbols.size();
return symbols.data();
*size = RuntimeLibcallSymbols.size();
return RuntimeLibcallSymbols.data();
}

0 comments on commit 66e9078

Please sign in to comment.