From 5e4e174d614a301a071081c6014e0a0cde4afd3d Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 16 Jun 2025 15:07:26 +0300 Subject: [PATCH] tests: Add tls test --- userspace/tests/CMakeLists.txt | 1 + userspace/tests/test-tls/CMakeLists.txt | 11 +++++ userspace/tests/test-tls/lib.cpp | 35 +++++++++++++++ userspace/tests/test-tls/main.cpp | 57 +++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 userspace/tests/test-tls/CMakeLists.txt create mode 100644 userspace/tests/test-tls/lib.cpp create mode 100644 userspace/tests/test-tls/main.cpp diff --git a/userspace/tests/CMakeLists.txt b/userspace/tests/CMakeLists.txt index a572bd1a..03b1b8ae 100644 --- a/userspace/tests/CMakeLists.txt +++ b/userspace/tests/CMakeLists.txt @@ -10,6 +10,7 @@ set(USERSPACE_TESTS test-shared test-sort test-tcp + test-tls test-udp test-unix-socket test-window diff --git a/userspace/tests/test-tls/CMakeLists.txt b/userspace/tests/test-tls/CMakeLists.txt new file mode 100644 index 00000000..fd08beab --- /dev/null +++ b/userspace/tests/test-tls/CMakeLists.txt @@ -0,0 +1,11 @@ +add_library(libtest-tls SHARED lib.cpp) +banan_link_library(libtest-tls libc) +target_link_options(libtest-tls PRIVATE -nolibc) +set_target_properties(libtest-tls PROPERTIES LINK_FLAGS "-Wl,-soname,libtest-tls.so") + +add_executable(test-tls main.cpp) +banan_link_library(test-tls libc) +banan_link_library(test-tls libtest-tls) + +install(TARGETS test-tls OPTIONAL) +install(TARGETS libtest-tls OPTIONAL) diff --git a/userspace/tests/test-tls/lib.cpp b/userspace/tests/test-tls/lib.cpp new file mode 100644 index 00000000..55294812 --- /dev/null +++ b/userspace/tests/test-tls/lib.cpp @@ -0,0 +1,35 @@ +#include + +extern thread_local int tls_in_executable1; +extern thread_local int tls_in_executable2; + +thread_local int tls_in_library1 = 1; +thread_local int tls_in_library2 = 1; + +void test_tls_exec_lib1() +{ + printf(" exec in lib: %d (%p)\n", tls_in_executable1, &tls_in_executable1); + tls_in_executable1 = 3; + printf(" exec in lib: %d (%p)\n", tls_in_executable1, &tls_in_executable1); +} + +void test_tls_exec_lib2() +{ + printf(" exec in lib: %d (%p)\n", tls_in_executable2, &tls_in_executable2); + tls_in_executable2 = 3; + printf(" exec in lib: %d (%p)\n", tls_in_executable2, &tls_in_executable2); +} + +void test_tls_lib_lib1() +{ + printf(" lib in lib: %d (%p)\n", tls_in_library1, &tls_in_library1); + tls_in_library1 = 3; + printf(" lib in lib: %d (%p)\n", tls_in_library1, &tls_in_library1); +} + +void test_tls_lib_lib2() +{ + printf(" lib in lib: %d (%p)\n", tls_in_library2, &tls_in_library2); + tls_in_library2 = 3; + printf(" lib in lib: %d (%p)\n", tls_in_library2, &tls_in_library2); +} diff --git a/userspace/tests/test-tls/main.cpp b/userspace/tests/test-tls/main.cpp new file mode 100644 index 00000000..d5dfbf31 --- /dev/null +++ b/userspace/tests/test-tls/main.cpp @@ -0,0 +1,57 @@ +#include +#include + +extern void test_tls_exec_lib1(); +extern void test_tls_exec_lib2(); + +extern void test_tls_lib_lib1(); +extern void test_tls_lib_lib2(); + +extern thread_local int tls_in_library1; +extern thread_local int tls_in_library2; + +thread_local int tls_in_executable1 = 1; +thread_local int tls_in_executable2 = 1; + +void run_test() +{ + printf("running on thread %d (expected 1, 2, 2, 3)\n", pthread_self()); + printf(" exec in exec: %d (%p)\n", tls_in_executable1, &tls_in_executable1); + tls_in_executable1 = 2; + printf(" exec in exec: %d (%p)\n", tls_in_executable1, &tls_in_executable1); + test_tls_exec_lib1(); + + printf("running on thread %d (expected 1, 2, 2, 3)\n", pthread_self()); + printf(" exec in exec: %d (%p)\n", tls_in_executable2, &tls_in_executable2); + tls_in_executable2 = 2; + printf(" exec in exec: %d (%p)\n", tls_in_executable2, &tls_in_executable2); + test_tls_exec_lib2(); + + printf("running on thread %d (expected 1, 2, 2, 3)\n", pthread_self()); + printf(" lib in exec: %d (%p)\n", tls_in_library1, &tls_in_library1); + tls_in_library1 = 2; + printf(" lib in exec: %d (%p)\n", tls_in_library1, &tls_in_library1); + test_tls_lib_lib1(); + + printf("running on thread %d (expected 1, 2, 2, 3)\n", pthread_self()); + printf(" lib in exec: %d (%p)\n", tls_in_library2, &tls_in_library2); + tls_in_library2 = 2; + printf(" lib in exec: %d (%p)\n", tls_in_library2, &tls_in_library2); + test_tls_lib_lib2(); +} + +int main(int argc, char** argv) +{ + run_test(); + +#if 0 + pthread_t tid; + pthread_create(&tid, nullptr, + [](void*) -> void* { + run_test(); + return nullptr; + }, nullptr + ); + pthread_join(tid, nullptr); +#endif +}