Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in install_bad_alloc_error_handler #83040

Closed
fabianbs96 opened this issue Feb 26, 2024 · 0 comments · Fixed by #83160
Closed

Crash in install_bad_alloc_error_handler #83040

fabianbs96 opened this issue Feb 26, 2024 · 0 comments · Fixed by #83160

Comments

@fabianbs96
Copy link
Contributor

The function install_bad_alloc_error_handler asserts that a bad_alloc error handler is not already installed.
However, it actually checks for the fatal-error handler, not for the bad_alloc error handler, causing an assertion failure if a fatal-error handler is already installed.
Correct behavior would be to only crash if a bad_alloc handler is already installed, making it independent from fatal-error handlers.

Minimal working example:

#include <llvm/Support/ErrorHandling.h>

int main() {
    llvm::install_fatal_error_handler([](void*,const char*,bool){});
    llvm::install_bad_alloc_error_handler([](void*,const char*,bool){}); // <-- Crash occurs here
}

Propsed Fix:

diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp
index b8b3b7424ac6..c268543eb1fd 100644
--- a/llvm/lib/Support/ErrorHandling.cpp
+++ b/llvm/lib/Support/ErrorHandling.cpp
@@ -130,7 +130,7 @@ void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
 #if LLVM_ENABLE_THREADS == 1
   std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
 #endif
-  assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
+  assert(!BadAllocErrorHandler && "Bad alloc error handler already registered!\n");
   BadAllocErrorHandler = handler;
   BadAllocErrorHandlerUserData = user_data;
 }
aganea pushed a commit that referenced this issue Mar 1, 2024
Previously, the function `install_bad_alloc_error_handler` was asserting that a bad_alloc error handler was not already installed. However, it was actually checking for the fatal-error handler, not for the bad_alloc error handler, causing an assertion failure if a fatal-error handler was already installed.

Fixes #83040
cuviper added a commit to cuviper/rust that referenced this issue Mar 15, 2024
The bad-alloc installer was incorrectly asserting that the other handler
isn't set yet, instead of checking its own, but we can avoid that by
changing the order we install them.

Ref: llvm/llvm-project#83040
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants