From 9b5d026744827c4e3a42788905de89bff2c4ce04 Mon Sep 17 00:00:00 2001 From: Thomas Faber Date: Sat, 21 Nov 2015 13:45:45 +0000 Subject: [PATCH] [CRT_APITEST] - Also test static constructors/destructors in a DLL. CORE-10562 svn path=/trunk/; revision=69993 --- rostests/apitests/crt/dll_startup.cpp | 68 +++++++++++++++++++ rostests/apitests/crt/dll_startup.h | 25 +++++++ rostests/apitests/crt/dll_startup.spec | 1 + .../apitests/crt/msvcrt_crt_apitest.cmake | 17 +++++ rostests/apitests/crt/static_construct.cpp | 50 +++++++++++++- rostests/apitests/crt/static_init.c | 4 +- 6 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 rostests/apitests/crt/dll_startup.cpp create mode 100644 rostests/apitests/crt/dll_startup.h create mode 100644 rostests/apitests/crt/dll_startup.spec diff --git a/rostests/apitests/crt/dll_startup.cpp b/rostests/apitests/crt/dll_startup.cpp new file mode 100644 index 00000000000..c6487fd6ac4 --- /dev/null +++ b/rostests/apitests/crt/dll_startup.cpp @@ -0,0 +1,68 @@ +/* + * PROJECT: ReactOS API tests + * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory + * PURPOSE: Test for static C++ object construction/destruction in a DLL + * PROGRAMMER: Thomas Faber + */ + +#include +#include "dll_startup.h" + +static struct counter_values counter_values = +{ + 0, 0, 0, 0, 5656, 0, 0 +}; +static struct counter_values *p_counter_values; + +static struct init_static +{ + int m_uninit; + int m_counter; + + init_static() : + m_counter(2) + { + counter_values.static_construct_counter_at_startup = counter_values.static_construct_counter; + counter_values.m_uninit_at_startup = m_uninit; + counter_values.static_construct_counter++; + m_uninit++; + } + + ~init_static() + { + p_counter_values->dtor_counter++; + } +} init_static; + +extern "C" +{ +SET_COUNTER_VALUES_POINTER SetCounterValuesPointer; +void +WINAPI +SetCounterValuesPointer( + _Out_ struct counter_values *pcv) +{ + p_counter_values = pcv; + memcpy(pcv, &counter_values, sizeof(counter_values)); +} + +BOOL +WINAPI +DllMain( + _In_ HINSTANCE hinstDLL, + _In_ DWORD fdwReason, + _In_ PVOID pvReserved) +{ + if (fdwReason == DLL_PROCESS_ATTACH) + { + counter_values.m_uninit = init_static.m_uninit; + counter_values.m_counter = init_static.m_counter; + } + else if (fdwReason == DLL_PROCESS_DETACH) + { + p_counter_values->dtor_counter_at_detach = p_counter_values->dtor_counter; + } + return TRUE; +} + +} \ No newline at end of file diff --git a/rostests/apitests/crt/dll_startup.h b/rostests/apitests/crt/dll_startup.h new file mode 100644 index 00000000000..08dfbc4a616 --- /dev/null +++ b/rostests/apitests/crt/dll_startup.h @@ -0,0 +1,25 @@ +/* + * PROJECT: ReactOS API tests + * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory + * PURPOSE: Helper declarations for DLL construction/destruction test + * PROGRAMMER: Thomas Faber + */ + +#pragma once + +struct counter_values +{ + int m_uninit_at_startup; + int m_uninit; + int m_counter; + int static_construct_counter_at_startup; + int static_construct_counter; + int dtor_counter_at_detach; + int dtor_counter; +}; + +typedef +void +WINAPI +SET_COUNTER_VALUES_POINTER( + _Out_ struct counter_values *pcv); diff --git a/rostests/apitests/crt/dll_startup.spec b/rostests/apitests/crt/dll_startup.spec new file mode 100644 index 00000000000..f418a2a8e01 --- /dev/null +++ b/rostests/apitests/crt/dll_startup.spec @@ -0,0 +1 @@ +@ stdcall SetCounterValuesPointer(ptr) diff --git a/rostests/apitests/crt/msvcrt_crt_apitest.cmake b/rostests/apitests/crt/msvcrt_crt_apitest.cmake index 6412387d649..157bd781892 100644 --- a/rostests/apitests/crt/msvcrt_crt_apitest.cmake +++ b/rostests/apitests/crt/msvcrt_crt_apitest.cmake @@ -1380,9 +1380,26 @@ endif() #add_importlibs(static_crt_apitest kernel32 ntdll) #add_cd_file(TARGET static_crt_apitest DESTINATION reactos/bin FOR all) +#spec2def(static_crt_dll_startup.dll dll_startup.spec) +#add_library(static_crt_dll_startup SHARED +# dll_startup.cpp +# ${CMAKE_CURRENT_BINARY_DIR}/static_crt_dll_startup.def) +#target_link_libraries(static_crt_dll_startup crt) +#set_module_type(static_crt_dll_startup win32dll) +#add_importlibs(static_crt_dll_startup kernel32 ntdll) +#add_cd_file(TARGET static_crt_dll_startup DESTINATION reactos/bin FOR all) + add_executable(msvcrt_crt_apitest testlist.c ${SOURCE_MSVCRT}) add_target_compile_definitions(msvcrt_crt_apitest TEST_MSVCRT) target_link_libraries(msvcrt_crt_apitest wine ${PSEH_LIB}) set_module_type(msvcrt_crt_apitest win32cui) add_importlibs(msvcrt_crt_apitest msvcrt kernel32 ntdll) add_cd_file(TARGET msvcrt_crt_apitest DESTINATION reactos/bin FOR all) + +spec2def(msvcrt_crt_dll_startup.dll dll_startup.spec) +add_library(msvcrt_crt_dll_startup SHARED + dll_startup.cpp + ${CMAKE_CURRENT_BINARY_DIR}/msvcrt_crt_dll_startup.def) +set_module_type(msvcrt_crt_dll_startup win32dll) +add_importlibs(msvcrt_crt_dll_startup msvcrt kernel32 ntdll) +add_cd_file(TARGET msvcrt_crt_dll_startup DESTINATION reactos/bin FOR all) diff --git a/rostests/apitests/crt/static_construct.cpp b/rostests/apitests/crt/static_construct.cpp index 6bf9b1d02ae..6db9c04fc5e 100644 --- a/rostests/apitests/crt/static_construct.cpp +++ b/rostests/apitests/crt/static_construct.cpp @@ -1,11 +1,12 @@ /* - * PROJECT: ReactOS api tests + * PROJECT: ReactOS API tests * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory * PURPOSE: Test for static C++ object construction * PROGRAMMER: Thomas Faber */ #include +#include "dll_startup.h" extern "C" { @@ -13,12 +14,14 @@ extern int static_init_counter; static int static_init_counter_at_startup; static int static_construct_counter_at_startup; +static int m_uninit_at_startup; int static_construct_counter = 789; } -struct init_static +static struct init_static { + int m_uninit; int m_counter; init_static() : @@ -26,7 +29,9 @@ struct init_static { static_init_counter_at_startup = static_init_counter; static_construct_counter_at_startup = static_construct_counter; + m_uninit_at_startup = m_uninit; static_construct_counter++; + m_uninit++; } } init_static; @@ -34,9 +39,50 @@ START_TEST(static_construct) { ok(static_init_counter_at_startup == 123, "static_init_counter at startup: %d\n", static_init_counter_at_startup); ok(static_construct_counter_at_startup == 789, "static_construct_counter at startup: %d\n", static_construct_counter_at_startup); + ok(m_uninit_at_startup == 0, "init_static.m_uninit at startup: %d\n", m_uninit_at_startup); ok(static_init_counter == 123, "static_init_counter: %d\n", static_init_counter); ok(static_construct_counter == 790, "static_construct_counter: %d\n", static_construct_counter); ok(init_static.m_counter == 2, "init_static.m_counter: %d\n", init_static.m_counter); + ok(init_static.m_uninit == 1, "init_static.m_uninit: %d\n", init_static.m_uninit); + + counter_values values; +#if defined(TEST_MSVCRT) + HMODULE hDll = LoadLibraryW(L"msvcrt_crt_dll_startup.dll"); +#elif defined(TEST_STATIC_CRT) + HMODULE hDll = LoadLibraryW(L"static_crt_dll_startup.dll"); +#else +#error This test only makes sense for static CRT and msvcrt.dll +#endif + if (hDll == NULL) + { + skip("Helper dll not found\n"); + return; + } + SET_COUNTER_VALUES_POINTER *pSetCounterValuesPointer = reinterpret_cast(GetProcAddress(hDll, "SetCounterValuesPointer")); + if (pSetCounterValuesPointer == NULL) + { + skip("Helper function not found\n"); + FreeLibrary(hDll); + return; + } + pSetCounterValuesPointer(&values); + ok(values.m_uninit_at_startup == 0, "m_uninit_at_startup = %d\n", values.m_uninit_at_startup); + ok(values.m_uninit == 1, "m_uninit = %d\n", values.m_uninit); + ok(values.m_counter == 2, "m_counter = %d\n", values.m_counter); + ok(values.static_construct_counter_at_startup == 5656, "static_construct_counter_at_startup = %d\n", values.static_construct_counter_at_startup); + ok(values.static_construct_counter == 5657, "static_construct_counter = %d\n", values.static_construct_counter); + ok(values.dtor_counter_at_detach == 0, "dtor_counter_at_detach = %d\n", values.dtor_counter_at_detach); + ok(values.dtor_counter == 0, "dtor_counter = %d\n", values.dtor_counter); + values.dtor_counter_at_detach = 78789; + values.dtor_counter = 7878; + FreeLibrary(hDll); + ok(values.m_uninit_at_startup == 0, "m_uninit_at_startup = %d\n", values.m_uninit_at_startup); + ok(values.m_uninit == 1, "m_uninit = %d\n", values.m_uninit); + ok(values.m_counter == 2, "m_counter = %d\n", values.m_counter); + ok(values.static_construct_counter_at_startup == 5656, "static_construct_counter_at_startup = %d\n", values.static_construct_counter_at_startup); + ok(values.static_construct_counter == 5657, "static_construct_counter = %d\n", values.static_construct_counter); + ok(values.dtor_counter_at_detach == 7878, "dtor_counter_at_detach = %d\n", values.dtor_counter_at_detach); + ok(values.dtor_counter == 7879, "dtor_counter = %d\n", values.dtor_counter); } diff --git a/rostests/apitests/crt/static_init.c b/rostests/apitests/crt/static_init.c index 7d18b5abaa7..25aac55f28a 100644 --- a/rostests/apitests/crt/static_init.c +++ b/rostests/apitests/crt/static_init.c @@ -1,5 +1,5 @@ /* - * PROJECT: ReactOS api tests + * PROJECT: ReactOS API tests * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory * PURPOSE: Test for static variable initialization * PROGRAMMER: Thomas Faber @@ -12,7 +12,7 @@ extern int static_construct_counter; int static_init_counter = 123; START_TEST(static_init) -{ +{ ok(static_init_counter == 123, "static_init_counter: %d\n", static_init_counter); ok(static_construct_counter == 790, "static_construct_counter: %d\n", static_construct_counter); }