Last active
July 27, 2023 04:20
-
-
Save ccat3z/aa34e7471ce25f5171aa3c91e76bcae2 to your computer and use it in GitHub Desktop.
msan-dynamic-tls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.23) | |
project(Test) | |
set(CMAKE_VERBOSE_MAKEFILE ON) | |
set(CMAKE_C_COMPILER clang-15) | |
set(CMAKE_CXX_COMPILER clang++-15) | |
add_compile_options(-fsanitize=memory -fsanitize-memory-track-origins=2) | |
add_link_options(-fsanitize=memory) | |
# add_compile_options(-stdlib=libc++) | |
# add_link_options(-stdlib=libc++ -L/home/ccat3z/Documents/moqi/llvm/llvm-project-llvmorg-15.0.7/build/lib -Wl,-rpath,/home/ccat3z/Documents/moqi/llvm/llvm-project-llvmorg-15.0.7/build/lib) | |
add_library(one SHARED one.cpp) | |
add_executable(main main.cpp) | |
# NOTE: Dynamic tls is not work for msan: use-of-uninitialized-value | |
target_compile_options(one PRIVATE -ftls-model=local-dynamic) | |
# target_compile_options(one PRIVATE -ftls-model=initial-exec) | |
# For dynamic tls model, TLS_SIZE can be value. | |
# For static tls model. If TLS_SIZE is too large, you need to increase the amount | |
# of surplus static TLS, which requires glibc >= 2.33: | |
# export GLIBC_TUNABLES=glibc.rtld.nns=10 | |
# Otherwise, dlopen will failed with `cannot allocate memory in static TLS block` | |
# See also: | |
# * https://maskray.me/blog/2021-02-14-all-about-thread-local-storage | |
# * https://www.gnu.org/software/libc/manual/html_node/Dynamic-Linking-Tunables.html#index-glibc_002ertld_002enns | |
target_compile_definitions(one PRIVATE TLS_SIZE=1600) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <dlfcn.h> | |
int main(int argc, const char *argv[]) { | |
auto handle = dlopen("./build/libone.so", RTLD_LAZY); | |
if (!handle) { | |
std::cerr << dlerror() << std::endl; | |
return 1; | |
} | |
void (*thread_data)(); | |
thread_data = reinterpret_cast<decltype(thread_data)>(dlsym(handle, "thread_data")); | |
thread_data(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
namespace { | |
struct ThreadData { | |
ThreadData() { | |
std::cerr << "tls init in lib" << std::endl; | |
} | |
char data[TLS_SIZE] = "hello"; | |
void f() { | |
std::cerr << data << std::endl; | |
} | |
static void g() { | |
static thread_local ThreadData t; | |
t.f(); | |
} | |
}; | |
} | |
extern "C" { | |
void thread_data() { | |
ThreadData::g(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment