crt: test: New test which checks that native TLS callback inside linked DLL is called Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-crt/testcases/Makefile.am b/mingw-w64-crt/testcases/Makefile.am index c9930ce..b8cdbae 100644 --- a/mingw-w64-crt/testcases/Makefile.am +++ b/mingw-w64-crt/testcases/Makefile.am
@@ -23,6 +23,11 @@ libprocdetach.dll: libprocdetach.o $(LINK) -shared libprocdetach.o -o libprocdetach.dll +# Used by t_tls_dll_native +libtlsnat.dll: libtlsnat.o + $(LINK) -shared -o $@ $^ +t_tls_dll_native.exe: libtlsnat.dll + # Used by t_tls_dll_native_process_detach libtlsnatprocdet.dll: libtlsnatprocdet.o $(LINK) -shared -o $@ $^ @@ -126,6 +131,7 @@ t_nontls_dll \ t_nontls_exe \ t_tls_dll \ + t_tls_dll_native \ t_tls_dll_native_process_detach \ t_tls_exe_dtor \ t_tls_exe_dyn \ @@ -243,6 +249,9 @@ # Compile t_tls_c11 in C11 mode t_tls_c11_CFLAGS = -std=c11 $(AM_CFLAGS) +# Link t_tls_dll_native against libtlsnat.dll +t_tls_dll_native_LDADD = libtlsnat.dll $(LDADD) + # Link t_tls_dll_native_process_detach against libtlsnatprocdet.dll t_tls_dll_native_process_detach_LDADD = libtlsnatprocdet.dll $(LDADD) @@ -312,4 +321,5 @@ libtls09.dll \ libtls10.dll \ libtls11.dll \ + libtlsnat.dll \ libtlsnatprocdet.dll
diff --git a/mingw-w64-crt/testcases/libtlsnat.c b/mingw-w64-crt/testcases/libtlsnat.c new file mode 100644 index 0000000..80a63b6 --- /dev/null +++ b/mingw-w64-crt/testcases/libtlsnat.c
@@ -0,0 +1,40 @@ +/* This is simple DLL library which uses PE TLS callback */ + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +__attribute__((dllexport)) int libtlsnat_process_attach = 0; +__attribute__((dllexport)) int libtlsnat_thread_attach = 0; +__attribute__((dllexport)) int libtlsnat_process_detach = 0; +__attribute__((dllexport)) int libtlsnat_thread_detach = 0; + +#if defined(__i386__) +/* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ +__attribute__((force_align_arg_pointer)) +#endif +static void WINAPI pe_tls_callback(HANDLE handle __attribute__((unused)), DWORD reason, LPVOID reserved __attribute__((unused))) +{ + if (reason == DLL_PROCESS_ATTACH) { + libtlsnat_process_attach++; + } else if (reason == DLL_THREAD_ATTACH) { + libtlsnat_thread_attach++; + } else if (reason == DLL_PROCESS_DETACH) { + /* Check that DLL_PROCESS_DETACH is not called more than once */ + if (libtlsnat_process_detach != 0) { + /* exit, _exit, or ExitProcess calls TLS callbacks, so use TerminateProcess() which is not calling them */ + TerminateProcess(GetCurrentProcess(), 1); + } + libtlsnat_process_detach++; + } else if (reason == DLL_THREAD_DETACH) { + libtlsnat_thread_detach++; + } +} + +/* Register pe_tls_callback as PE TLS callback with the highest priority (=B) */ +static __attribute__((section(".CRT$XLB"), used)) const PIMAGE_TLS_CALLBACK register_pe_tls_callback = pe_tls_callback; + +/* Force tlssup.c (_tls_used symbol for .tls linker section) to be linked */ +extern const IMAGE_TLS_DIRECTORY _tls_used; +static __attribute__((destructor)) void _include_tls_used(void) { asm volatile ("" :: "r" (&_tls_used)); }
diff --git a/mingw-w64-crt/testcases/t_tls_dll_native.c b/mingw-w64-crt/testcases/t_tls_dll_native.c new file mode 100644 index 0000000..e7eb892 --- /dev/null +++ b/mingw-w64-crt/testcases/t_tls_dll_native.c
@@ -0,0 +1,95 @@ +#include <stdio.h> +#include <stdlib.h> + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +/* mingw-w64 headers */ +#include "libtest.h" + +__attribute__((dllimport)) extern int libtlsnat_process_attach; +__attribute__((dllimport)) extern int libtlsnat_thread_attach; +__attribute__((dllimport)) extern int libtlsnat_process_detach; +__attribute__((dllimport)) extern int libtlsnat_thread_detach; + +#if defined(__i386__) +/* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ +__attribute__((force_align_arg_pointer)) +#endif +static DWORD WINAPI thread_main(LPVOID user_data __attribute__((unused))) +{ + return 0; +} + +int main(void) +{ + mingw_test_init(); + + int ret = 0; + + if (_osplatform == VER_PLATFORM_WIN32_WINDOWS) { + printf("PE TLS callbacks are not supported on Win9x\n"); + return 77; + } + + if (libtlsnat_process_attach != 1) { + printf("Error: PE TLS callback in libtlsnat.dll for DLL_PROCESS_ATTACH was not called\n"); + ret = 1; + } else { + printf("PE TLS callback in libtlsnat.dll for DLL_PROCESS_ATTACH was called\n"); + } + + if (libtlsnat_thread_attach != 0) { + printf("Error: PE TLS callback in libtlsnat.dll for DLL_THREAD_ATTACH was called without spawning thread\n"); + ret = 1; + } + + if (libtlsnat_thread_detach != 0) { + printf("Error: PE TLS callback in libtlsnat.dll for DLL_THREAD_DETACH was called without spawning thread\n"); + ret = 1; + } + + printf("Creating new thread\n"); + HANDLE thread = CreateThread(NULL, 0, thread_main, NULL, 0, &(DWORD){0} /*out: ThreadId*/); + if (!thread) { + if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) { + printf("Error: failed: %lu\n", GetLastError()); + ret = 1; + } else { + printf("Skipped: not supported\n"); + } + } else { + DWORD tret; + WaitForSingleObject(thread, INFINITE); + GetExitCodeThread(thread, &tret); + CloseHandle(thread); + if (tret) { + printf("Error: thread failed\n"); + ret = 1; + } + + if (libtlsnat_thread_attach != 1) { + printf("Error: PE TLS callback in libtlsnat.dll for DLL_THREAD_ATTACH was not called\n"); + ret = 1; + } else { + printf("PE TLS callback in libtlsnat.dll for DLL_THREAD_ATTACH was called\n"); + } + + if (libtlsnat_thread_detach != 1) { + printf("Error: PE TLS callback in libtlsnat.dll for DLL_THREAD_DETACH was not called\n"); + ret = 1; + } else { + printf("PE TLS callback in libtlsnat.dll for DLL_THREAD_DETACH was called\n"); + } + } + + if (libtlsnat_process_detach != 0) { + printf("Error: PE TLS callback in libtlsnat.dll for DLL_PROCESS_DETACH was called before exiting process\n"); + ret = 1; + } + + if (ret) printf("FAILED\n"); else printf("PASSED\n"); + return ret; +}