[compiler-rt] [asan] Throw std::bad_alloc from operator new on allocation failure (PR #196388)

David Justo via llvm-commits llvm-commits at lists.llvm.org
Sun May 24 16:26:10 PDT 2026


================
@@ -83,8 +83,17 @@ COMMON_FLAG(
     "detect_leaks=false, or if __lsan_do_leak_check() is called before the "
     "handler has a chance to run.")
 COMMON_FLAG(bool, allocator_may_return_null, false,
-            "If false, the allocator will crash instead of returning 0 on "
-            "out-of-memory.")
+            "Controls allocator behavior on out-of-memory. C-allocator entry "
+            "points (malloc / calloc / realloc / aligned_alloc / "
+            "posix_memalign): default=false reports and aborts, true returns "
+            "nullptr. AddressSanitizer's operator new always runs the "
+            "std::get_new_handler() chain first per [new.delete.single]/3+/4 "
+            "regardless of this flag; on chain exhaustion default=false "
+            "reports and aborts for both forms, true throws std::bad_alloc "
+            "(throwing form) or returns nullptr (nothrow form). On Windows "
+            "the asan runtime is built without exceptions, so the throwing "
+            "form aborts even with true; the nothrow form still returns "
+            "nullptr.")
----------------
davidmrdavid wrote:

If I'm reading this correctly, this user-facing flag description seems to be leaking implementation details, like the fact that internally there's a `may_return_null` parameter on various functions (`asan_malloc` and its variant).

I think you could say:
"""
Controls allocator behavior on out-of-memory. If 'true', allocators may return 'null' as is standard in C++. If 'false', out of memory exceptions force a crash".
"""

Relatedly - the windows behavior seems worth documenting, but I'm not immediately convinced that it is part of the 'allocator_may_return_null' description.

Happy to hear pushback to this, let me know your thoughts. Thanks!

https://github.com/llvm/llvm-project/pull/196388


More information about the llvm-commits mailing list