From 3d6e38c96773f6c48ba7720773bd1c783d0a8091 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Mon, 6 Mar 2023 17:11:00 +0200 Subject: [PATCH] memory: support reallocing foreign (non-Seastar) memory on a reactor thread In general, memory allocated on a reactor thread should come from the Seastar memory allocator. But in certain scenarios, it doesn't. One such example is - seastar built as a shared library - another shared library (openssl) is loaded ahead of seastar and allocates some global storage in its module initializer - seastar loaded, initializes the shard memory allocator - another call into openssl, ends up reallocating the previously allocated memory This crashes in realloc(). Fortunately, swapping the two if statements in that branch is enough to fix the problem, so this patch does just that. Since the second if () is too loose (we can't realloc on a non-reactor thread if the original realloc function is unavailable), it is made unconditional. Closes #1533 --- src/core/memory.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/core/memory.cc b/src/core/memory.cc index e426edf5b50..ec1d0285159 100644 --- a/src/core/memory.cc +++ b/src/core/memory.cc @@ -1934,14 +1934,12 @@ void* realloc(void* ptr, size_t size) { // If ptr is a null pointer, the behavior is the same as calling std::malloc(new_size). return malloc(size); } else if (!is_seastar_memory(ptr)) { - // we can't realloc foreign memory on a shard - if (is_reactor_thread) { - abort(); - } // original_realloc_func might be null when previous ctor allocates if (original_realloc_func) { return original_realloc_func(ptr, size); } + // we can't realloc foreign memory without the original libc function + abort(); } // if we're here, it's a non-null seastar memory ptr // or original functions aren't available.