Skip to content

Commit

Permalink
Merge pull request #961 from qookei/fix-960
Browse files Browse the repository at this point in the history
options/rtdl: Fix alignment overhead computation for the TLS allocation
  • Loading branch information
qookei authored Dec 21, 2023
2 parents 5ea2031 + c643fd3 commit 6567088
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion options/rtdl/generic/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ Tcb *allocateTcb() {

// To make sure that both the TCB and TLS data are sufficiently aligned, allocate
// slightly more than necessary and adjust alignment afterwards.
size_t alignOverhead = frg::min(alignof(Tcb), tlsMaxAlignment);
size_t alignOverhead = frg::max(alignof(Tcb), tlsMaxAlignment);
size_t allocSize = tlsInitialSize + sizeof(Tcb) + alignOverhead;
auto allocation = reinterpret_cast<uintptr_t>(getAllocator().allocate(allocSize));
memset(reinterpret_cast<void *>(allocation), 0, allocSize);
Expand Down
1 change: 1 addition & 0 deletions tests/rtdl/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rtdl_test_cases = [
'scope3',
'scope4',
'scope5',
'tls_align',
]

foreach test_name : rtdl_test_cases
Expand Down
1 change: 1 addition & 0 deletions tests/rtdl/tls_align/libbar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_Thread_local __attribute__((aligned(8))) char bar_thread_local[8] = "Hello!";
1 change: 1 addition & 0 deletions tests/rtdl/tls_align/libfoo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_Thread_local __attribute__((aligned(16))) char foo_thread_local[8] = "Hello!";
7 changes: 7 additions & 0 deletions tests/rtdl/tls_align/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
libfoo = shared_library('foo', 'libfoo.c')
libbar = shared_library('bar', 'libbar.c')
test_link_with = [libfoo, libbar]

libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
libbar_native = shared_library('native-bar', 'libbar.c', native: true)
test_native_link_with = [libfoo_native, libbar_native]
10 changes: 10 additions & 0 deletions tests/rtdl/tls_align/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>
#include <stdint.h>

extern _Thread_local char foo_thread_local[];
extern _Thread_local char bar_thread_local[];

int main() {
assert(!((uintptr_t)foo_thread_local & (16 - 1)));
assert(!((uintptr_t)bar_thread_local & (8 - 1)));
}

0 comments on commit 6567088

Please sign in to comment.