Skip to content

Commit

Permalink
build: use initial-exec TLS when building seastar as shared library
Browse files Browse the repository at this point in the history
quote from
https://patchwork.ozlabs.org/project/glibc/patch/[email protected]/

> On the glibc side, we should recommend that intercepting mallocs and its
> dependencies use initial-exec TLS because that kind of TLS does not use
> malloc.  If intercepting mallocs using dynamic TLS work at all, that's
> totally by accident, and was in the past helped by glibc bug 19924.

so instead of allocating TLS variables using malloc, let's allocate them
using initial-exec TLS model. another approach is to single out the
static TLS variables in the code path of malloc/free and apply
`__attribute__ ((tls_model("initial-exec")))` to them, and optionally
only do this when we are building shared library.

but this could be overkill as

1. we build static library in the release build
2. the total size of the static TLS variables is presumably small, so
   the application linking against the seastar shared library
   should be able to afford this.

see also
https://patchwork.ozlabs.org/project/glibc/patch/[email protected]/
and
https://sourceware.org/bugzilla/show_bug.cgi?id=19924

Fixes scylladb#2247
Signed-off-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed May 20, 2024
1 parent 855fc4b commit bf8e5bd
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,16 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
-Wno-error=deprecated-declarations)
endif ()

if (NOT BUILD_SHARED_LIBS)
if (BUILD_SHARED_LIBS)
# use initial-exec TLS, as it puts the TLS variables in the static TLS space
# instead of allocates them using malloc. otherwise intercepting mallocs and
# friends could lead to recursive call of malloc functions when a dlopen'ed
# shared object references a TLS variable and it in turn uses malloc. the
# downside of this workaround is that the static TLS space is used, and it is
# a global resource.
list (APPEND Seastar_PRIVATE_CXX_FLAGS
$<$<IN_LIST:$<CONFIG>,RelWithDebInfo;Dev>:-ftls-model=initial-exec>)
else ()
list (APPEND Seastar_PRIVATE_CXX_FLAGS -fvisibility=hidden)
endif ()

Expand Down

0 comments on commit bf8e5bd

Please sign in to comment.