ReactOS  0.4.14-dev-98-gb0d4763
load_notifications.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS API Tests
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Test for exception behavior in dll notifications
5  * COPYRIGHT: Copyright 2018 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
11 
14 
15 LONG g_TlsExcept = 0xffffff;
16 LONG g_DllMainExcept = 0xffffff;
17 
19 
21 
23 {
25  ULONG Count = 0;
26 
27  exc = NtCurrentTeb()->NtTib.ExceptionList;
28 
29  while (exc && exc != (EXCEPTION_REGISTRATION_RECORD*)~0)
30  {
31  Count++;
32  exc = exc->Next;
33  }
34 
35  return Count;
36 }
37 
38 int g_TLS_ATTACH = 4;
39 int g_TLS_DETACH = 3;
40 
42 {
44 
47  {
49  }
50  else
51  {
53  }
54 
56  {
58  }
59 }
60 
61 int g_DLL_ATTACH = 3;
62 int g_DLL_DETACH = 2;
63 
65 {
67 
70  {
72  }
73  else
74  {
75  ok_int(handlers, g_DLL_DETACH); // For failures, see https://jira.reactos.org/browse/CORE-14857
76  }
77 
79  {
81  }
82  return TRUE;
83 }
84 
85 
86 static void execute_test(void)
87 {
88  HMODULE mod;
89  DWORD dwErr;
90  _SEH2_TRY
91  {
92  g_TlsExcept = 0xffffff;
93  g_DllMainExcept = 0xffffff;
94  g_DllMainCalled = 0;
95  g_TlsCalled = 0;
98  dwErr = GetLastError();
99  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
102  ok_hex(g_TlsCalled, 1);
103  if (g_TlsCalled == 0)
104  trace("Tls not active\n");
106  FreeLibrary(mod);
107  dwErr = GetLastError();
108  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
111  ok_hex(g_TlsCalled, 2);
112  }
114  {
115  ok(0, "Unable to load it normally\n");
116  }
117  _SEH2_END;
118 
119 
120  _SEH2_TRY
121  {
122  g_TlsExcept = 0xffffff;
124  g_DllMainCalled = 0;
125  g_TlsCalled = 0;
128  dwErr = GetLastError();
129  ok(GetModuleHandleW(dllpath) == NULL, "Module loaded (0x%lx)\n", dwErr);
132  else
136  ok_hex(g_TlsCalled, 1);
137  if (mod)
138  {
139  FreeLibrary(mod);
140  dwErr = GetLastError();
141  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
144  ok_hex(g_TlsCalled, 1);
145  }
146  }
148  {
149  ok(0, "Unable to execute test\n");
150  }
151  _SEH2_END;
152 
153  _SEH2_TRY
154  {
155  g_TlsExcept = 0xffffff;
157  g_DllMainCalled = 0;
158  g_TlsCalled = 0;
161  dwErr = GetLastError();
162  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
165  ok_hex(g_TlsCalled, 1);
167  FreeLibrary(mod);
168  dwErr = GetLastError();
169  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
172  ok_hex(g_TlsCalled, 2);
173  }
175  {
176  ok(0, "Unable to execute test\n");
177  }
178  _SEH2_END;
179 
180  _SEH2_TRY
181  {
183  g_DllMainExcept = 0xffffff;
184  g_DllMainCalled = 0;
185  g_TlsCalled = 0;
188  dwErr = GetLastError();
189  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
192  ok_hex(g_TlsCalled, 1);
194  FreeLibrary(mod);
195  dwErr = GetLastError();
196  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
199  ok_hex(g_TlsCalled, 2);
200  }
202  {
203  ok(0, "Unable to execute test\n");
204  }
205  _SEH2_END;
206 
207  _SEH2_TRY
208  {
210  g_DllMainExcept = 0xffffff;
211  g_DllMainCalled = 0;
212  g_TlsCalled = 0;
215  dwErr = GetLastError();
216  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
219  ok_hex(g_TlsCalled, 1);
221  FreeLibrary(mod);
222  dwErr = GetLastError();
223  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
226  ok_hex(g_TlsCalled, 2);
227  }
229  {
230  ok(0, "Unable to execute test\n");
231  }
232  _SEH2_END;
233 
234 }
235 
236 
238 {
239  BOOL Success;
240  DWORD dwWritten, Size;
241  HGLOBAL hGlobal;
242  LPVOID pData;
243  HANDLE Handle;
244  HRSRC hRsrc = FindResourceW(GetModuleHandleW(NULL), ResourceName, (LPCWSTR)10);
245  ok(!!hRsrc, "Unable to find %s\n", wine_dbgstr_w(ResourceName));
246  if (!hRsrc)
247  return FALSE;
248 
249  hGlobal = LoadResource(GetModuleHandleW(NULL), hRsrc);
251  pData = LockResource(hGlobal);
252 
253  ok(Size && !!pData, "Unable to load %s\n", wine_dbgstr_w(ResourceName));
254  if (!Size || !pData)
255  return FALSE;
256 
258 
260  {
261  skip("Failed to create temp file %ls, error %lu\n", Filename, GetLastError());
262  return FALSE;
263  }
264  Success = WriteFile(Handle, pData, Size, &dwWritten, NULL);
265  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
266  ok(dwWritten == Size, "WriteFile wrote %lu bytes instead of %lu\n", dwWritten, Size);
268  Success = Success && (dwWritten == Size);
269 
271  return Success;
272 }
273 
274 
275 START_TEST(load_notifications)
276 {
278  BOOL ret;
279  UINT Length;
280  PPEB Peb = NtCurrentPeb();
281 
283  trace("Winver: 0x%lx\n", g_dwWinVer);
284 
286  {
287  g_DLL_ATTACH = 4;
288  g_DLL_DETACH = 1;
289  }
290  else if (g_dwWinVer <= _WIN32_WINNT_WS08)
291  {
292  g_TLS_ATTACH = 5;
293  g_DLL_ATTACH = 4;
294  }
295  else if (g_dwWinVer <= _WIN32_WINNT_WIN7)
296  {
297  g_TLS_ATTACH = 3;
298  g_DLL_ATTACH = 2;
299  }
300  else if (g_dwWinVer <= _WIN32_WINNT_WINBLUE)
301  {
302  g_TLS_DETACH = 5;
303  g_DLL_DETACH = 4;
304  }
305 
307  ok(ret, "GetTempPathW error: %lu\n", GetLastError());
308 
309  Length = GetTempFileNameW(workdir, L"ntdll", 0, dllpath);
310  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
311 
312  if (extract_resource(dllpath, (LPCWSTR)101))
313  {
314  _SEH2_TRY
315  {
316  execute_test();
317  }
319  {
320  ok(0, "Ldr didnt handle exception\n");
321  }
322  _SEH2_END;
323  }
324  else
325  {
326  ok(0, "Failed to extract resource\n");
327  }
328 
330 }
#define _WIN32_WINNT_WIN7
Definition: sdkddkver.h:28
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
struct _EXCEPTION_REGISTRATION_RECORD * Next
Definition: compat.h:386
#define CloseHandle
Definition: compat.h:398
PPEB Peb
Definition: dllmain.c:27
#define _WIN32_WINNT_WS03
Definition: sdkddkver.h:23
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
static xmlCharEncodingHandlerPtr * handlers
Definition: encoding.c:1297
char * wine_dbgstr_w(const wchar_t *wstr)
Definition: atltest.h:87
#define _countof(array)
Definition: fontsub.cpp:30
DWORD g_dwWinVer
#define EXCEPTION_NONCONTINUABLE
Definition: rtltypes.h:150
#define InterlockedCompareExchange
Definition: interlocked.h:104
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
int g_DLL_DETACH
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
#define _WIN32_WINNT_WINBLUE
Definition: sdkddkver.h:30
int g_TLS_DETACH
LONG g_DllMainExcept
VOID WINAPI notify_TlsCallback(IN HINSTANCE hDllHandle, IN DWORD dwReason, IN LPVOID lpvReserved)
#define DWORD
Definition: nt_native.h:44
#define ok_hex(expression, result)
Definition: atltest.h:94
HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
Definition: res.c:176
static IN DWORD IN LPVOID lpvReserved
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:925
#define ERROR_DLL_INIT_FAILED
Definition: winerror.h:650
_SEH2_TRY
Definition: create.c:4250
#define UnlockResource(h)
Definition: winbase.h:3196
START_TEST(load_notifications)
#define ERROR_NOACCESS
Definition: winerror.h:578
ULONG CountHandlers(VOID)
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2079
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
#define GENERIC_WRITE
Definition: nt_native.h:90
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
#define LoadLibraryW(x)
Definition: compat.h:404
smooth NULL
Definition: ftsmooth.c:416
#define ok_int(expression, result)
Definition: atltest.h:134
LONG g_TlsCalled
static void execute_test(void)
#define DLL_PROCESS_DETACH
Definition: compat.h:119
ULONG g_BaseHandlers
DWORD dwErr
Definition: service.c:36
_In_ HANDLE Handle
Definition: extypes.h:390
LONG g_DllMainCalled
#define FreeLibrary(x)
Definition: compat.h:405
#define trace
Definition: atltest.h:70
WCHAR dllpath[MAX_PATH]
__wchar_t WCHAR
Definition: xmlstorage.h:180
static char workdir[MAX_PATH]
Definition: batch.c:26
#define MAX_PATH
Definition: compat.h:26
#define WINAPI
Definition: msvc.h:8
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
int g_TLS_ATTACH
unsigned long DWORD
Definition: ntddk_ex.h:95
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
static IN DWORD dwReason
#define EXCEPTION_DATATYPE_MISALIGNMENT
Definition: winbase.h:309
int ret
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1250
int g_DLL_ATTACH
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
#define CREATE_ALWAYS
Definition: disk.h:72
ULONG OSMinorVersion
Definition: ntddk_ex.h:301
_SEH2_END
Definition: create.c:4424
#define InterlockedIncrement
Definition: armddk.h:53
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
VOID WINAPI RaiseException(IN DWORD dwExceptionCode, IN DWORD dwExceptionFlags, IN DWORD nNumberOfArguments, IN CONST ULONG_PTR *lpArguments OPTIONAL)
Definition: except.c:693
#define NtCurrentPeb()
Definition: FLS.c:20
#define ok(value,...)
Definition: atltest.h:57
#define _WIN32_WINNT_WS08
Definition: sdkddkver.h:26
unsigned int UINT
Definition: ndis.h:50
DWORD WINAPI SizeofResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:568
#define CreateFileW
Definition: compat.h:400
#define skip(...)
Definition: atltest.h:64
unsigned int ULONG
Definition: retypes.h:1
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:847
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
ULONG OSMajorVersion
Definition: ntddk_ex.h:300
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
BOOL WINAPI notify_DllMain(IN HINSTANCE hDllHandle, IN DWORD dwReason, IN LPVOID lpvReserved)
LONG g_TlsExcept
BOOL extract_resource(const WCHAR *Filename, LPCWSTR ResourceName)
static int mod
Definition: i386-dis.c:1273