Skip to content

Commit

Permalink
Report OOM from allocate_buffer
Browse files Browse the repository at this point in the history
Previously, it called `::operator new` which may throw `std::bad_alloc`,
regardless of whether LLVM itself was built with exception handling, and
this can cause safety issues if outside code has destructors that will
call back into LLVM. Now we use `::operator new(..., nothrow)` and call
`llvm::report_bad_alloc_error` when allocation fails, which will abort
when LLVM is built without exceptions.

Ref: llvm#85281
  • Loading branch information
cuviper authored and nikic committed Jan 25, 2025
1 parent 3057d0f commit 8fec865
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions llvm/lib/Support/MemAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void *
llvm::allocate_buffer(size_t Size, size_t Alignment) {
return ::operator new(Size
void *Result = ::operator new(Size,
#ifdef __cpp_aligned_new
,
std::align_val_t(Alignment)
std::align_val_t(Alignment),
#endif
);
std::nothrow);
if (Result == nullptr) {
report_bad_alloc_error("Buffer allocation failed");
}
return Result;
}

void llvm::deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {
Expand Down

0 comments on commit 8fec865

Please sign in to comment.