Skip to content

Commit

Permalink
memory: support reallocing foreign (non-Seastar) memory on a reactor …
Browse files Browse the repository at this point in the history
…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 scylladb#1533
  • Loading branch information
avikivity authored and nyh committed Mar 7, 2023
1 parent 5d62cad commit 3d6e38c
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/core/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3d6e38c

Please sign in to comment.