ReactOS  0.4.15-dev-2721-g5912c11
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 
10 #include <pseh/pseh2.h>
11 
13 
16 
17 LONG g_TlsExcept = 0xffffff;
18 LONG g_DllMainExcept = 0xffffff;
19 
21 
23 
25 {
27  ULONG Count = 0;
28 
29  exc = NtCurrentTeb()->NtTib.ExceptionList;
30 
31  while (exc && exc != (EXCEPTION_REGISTRATION_RECORD*)~0)
32  {
33  Count++;
34  exc = exc->Next;
35  }
36 
37  return Count;
38 }
39 
40 int g_TLS_ATTACH = 4;
41 int g_TLS_DETACH = 3;
42 
44 {
46 
49  {
51  }
52  else
53  {
55  }
56 
58  {
60  }
61 }
62 
63 int g_DLL_ATTACH = 3;
64 int g_DLL_DETACH = 2;
65 
67 {
69 
72  {
74  }
75  else
76  {
77  ok_int(handlers, g_DLL_DETACH); // For failures, see https://jira.reactos.org/browse/CORE-14857
78  }
79 
81  {
83  }
84  return TRUE;
85 }
86 
87 
88 static void execute_test(void)
89 {
90  HMODULE mod;
91  DWORD dwErr;
92  _SEH2_TRY
93  {
94  g_TlsExcept = 0xffffff;
95  g_DllMainExcept = 0xffffff;
96  g_DllMainCalled = 0;
97  g_TlsCalled = 0;
100  dwErr = GetLastError();
101  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
104  ok_hex(g_TlsCalled, 1);
105  if (g_TlsCalled == 0)
106  trace("Tls not active\n");
108  FreeLibrary(mod);
109  dwErr = GetLastError();
110  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
113  ok_hex(g_TlsCalled, 2);
114  }
116  {
117  ok(0, "Unable to load it normally\n");
118  }
119  _SEH2_END;
120 
121 
122  _SEH2_TRY
123  {
124  g_TlsExcept = 0xffffff;
126  g_DllMainCalled = 0;
127  g_TlsCalled = 0;
130  dwErr = GetLastError();
131  ok(GetModuleHandleW(dllpath) == NULL, "Module loaded (0x%lx)\n", dwErr);
134  else
138  ok_hex(g_TlsCalled, 1);
139  if (mod)
140  {
141  FreeLibrary(mod);
142  dwErr = GetLastError();
143  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
146  ok_hex(g_TlsCalled, 1);
147  }
148  }
150  {
151  ok(0, "Unable to execute test\n");
152  }
153  _SEH2_END;
154 
155  _SEH2_TRY
156  {
157  g_TlsExcept = 0xffffff;
159  g_DllMainCalled = 0;
160  g_TlsCalled = 0;
163  dwErr = GetLastError();
164  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
167  ok_hex(g_TlsCalled, 1);
169  FreeLibrary(mod);
170  dwErr = GetLastError();
171  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
174  ok_hex(g_TlsCalled, 2);
175  }
177  {
178  ok(0, "Unable to execute test\n");
179  }
180  _SEH2_END;
181 
182  _SEH2_TRY
183  {
185  g_DllMainExcept = 0xffffff;
186  g_DllMainCalled = 0;
187  g_TlsCalled = 0;
190  dwErr = GetLastError();
191  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
194  ok_hex(g_TlsCalled, 1);
196  FreeLibrary(mod);
197  dwErr = GetLastError();
198  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
201  ok_hex(g_TlsCalled, 2);
202  }
204  {
205  ok(0, "Unable to execute test\n");
206  }
207  _SEH2_END;
208 
209  _SEH2_TRY
210  {
212  g_DllMainExcept = 0xffffff;
213  g_DllMainCalled = 0;
214  g_TlsCalled = 0;
217  dwErr = GetLastError();
218  ok(GetModuleHandleW(dllpath) != NULL, "Unable to load module (0x%lx)\n", dwErr);
221  ok_hex(g_TlsCalled, 1);
223  FreeLibrary(mod);
224  dwErr = GetLastError();
225  ok(GetModuleHandleW(dllpath) == NULL, "Unable to unload module (0x%lx)\n", dwErr);
228  ok_hex(g_TlsCalled, 2);
229  }
231  {
232  ok(0, "Unable to execute test\n");
233  }
234  _SEH2_END;
235 
236 }
237 
238 
240 {
241  BOOL Success;
242  DWORD dwWritten, Size;
243  HGLOBAL hGlobal;
244  LPVOID pData;
245  HANDLE Handle;
246  HRSRC hRsrc = FindResourceW(GetModuleHandleW(NULL), ResourceName, (LPCWSTR)10);
247  ok(!!hRsrc, "Unable to find %s\n", wine_dbgstr_w(ResourceName));
248  if (!hRsrc)
249  return FALSE;
250 
251  hGlobal = LoadResource(GetModuleHandleW(NULL), hRsrc);
253  pData = LockResource(hGlobal);
254 
255  ok(Size && !!pData, "Unable to load %s\n", wine_dbgstr_w(ResourceName));
256  if (!Size || !pData)
257  return FALSE;
258 
260 
262  {
263  skip("Failed to create temp file %ls, error %lu\n", Filename, GetLastError());
264  return FALSE;
265  }
266  Success = WriteFile(Handle, pData, Size, &dwWritten, NULL);
267  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
268  ok(dwWritten == Size, "WriteFile wrote %lu bytes instead of %lu\n", dwWritten, Size);
270  Success = Success && (dwWritten == Size);
271 
273  return Success;
274 }
275 
276 
277 START_TEST(load_notifications)
278 {
280  BOOL ret;
281  UINT Length;
282  PPEB Peb = NtCurrentPeb();
283 
285  trace("Winver: 0x%lx\n", g_dwWinVer);
286 
288  {
289  g_DLL_ATTACH = 4;
290  g_DLL_DETACH = 1;
291  }
292  else if (g_dwWinVer <= _WIN32_WINNT_WS08)
293  {
294  g_TLS_ATTACH = 5;
295  g_DLL_ATTACH = 4;
296  }
297  else if (g_dwWinVer <= _WIN32_WINNT_WIN7)
298  {
299  g_TLS_ATTACH = 3;
300  g_DLL_ATTACH = 2;
301  }
302  else if (g_dwWinVer <= _WIN32_WINNT_WINBLUE)
303  {
304  g_TLS_DETACH = 5;
305  g_DLL_DETACH = 4;
306  }
307 
309  ok(ret, "GetTempPathW error: %lu\n", GetLastError());
310 
311  Length = GetTempFileNameW(workdir, L"ntdll", 0, dllpath);
312  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
313 
314  if (extract_resource(dllpath, (LPCWSTR)101))
315  {
316  _SEH2_TRY
317  {
318  execute_test();
319  }
321  {
322  ok(0, "Ldr didnt handle exception\n");
323  }
324  _SEH2_END;
325  }
326  else
327  {
328  ok(0, "Failed to extract resource\n");
329  }
330 
332 }
#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:39
struct _EXCEPTION_REGISTRATION_RECORD * Next
Definition: compat.h:585
#define CloseHandle
Definition: compat.h:598
PPEB Peb
Definition: dllmain.c:27
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define _WIN32_WINNT_WS03
Definition: sdkddkver.h:23
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define TRUE
Definition: types.h:120
static xmlCharEncodingHandlerPtr * handlers
Definition: encoding.c:1297
char * wine_dbgstr_w(const wchar_t *wstr)
Definition: atltest.h:87
DWORD g_dwWinVer
#define EXCEPTION_NONCONTINUABLE
Definition: rtltypes.h:154
#define InterlockedCompareExchange
Definition: interlocked.h:104
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
int g_DLL_DETACH
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
#define UnlockResource(handle)
Definition: winbase.h:3228
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#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:934
#define ERROR_DLL_INIT_FAILED
Definition: winerror.h:650
_SEH2_TRY
Definition: create.c:4226
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
#define FALSE
Definition: types.h:117
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
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
#define LoadLibraryW(x)
Definition: compat.h:606
#define ok_int(expression, result)
Definition: atltest.h:134
LONG g_TlsCalled
static void execute_test(void)
#define DLL_PROCESS_DETACH
Definition: compat.h:130
ULONG g_BaseHandlers
DWORD dwErr
Definition: service.c:36
int Count
Definition: noreturn.cpp:7
LONG g_DllMainCalled
#define FreeLibrary(x)
Definition: compat.h:607
#define trace
Definition: atltest.h:70
WCHAR dllpath[MAX_PATH]
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define _countof(array)
Definition: sndvol32.h:68
static char workdir[MAX_PATH]
Definition: batch.c:26
#define MAX_PATH
Definition: compat.h:34
#define WINAPI
Definition: msvc.h:6
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
static IN DWORD dwReason
#define EXCEPTION_DATATYPE_MISALIGNMENT
Definition: winbase.h:309
int ret
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
static const WCHAR L[]
Definition: oid.c:1250
int g_DLL_ATTACH
#define CREATE_ALWAYS
Definition: disk.h:72
ULONG OSMinorVersion
Definition: ntddk_ex.h:301
_SEH2_END
Definition: create.c:4400
#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:700
#define NtCurrentPeb()
Definition: FLS.c:22
#define ok(value,...)
Definition: atltest.h:57
#define _WIN32_WINNT_WS08
Definition: sdkddkver.h:26
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
DWORD WINAPI SizeofResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:568
#define CreateFileW
Definition: compat.h:600
#define skip(...)
Definition: atltest.h:64
_In_ HANDLE Handle
Definition: extypes.h:390
unsigned int ULONG
Definition: retypes.h:1
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
ULONG OSMajorVersion
Definition: ntddk_ex.h:300
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
static int mod
Definition: i386-dis.c:1281
BOOL WINAPI notify_DllMain(IN HINSTANCE hDllHandle, IN DWORD dwReason, IN LPVOID lpvReserved)
LONG g_TlsExcept
BOOL extract_resource(const WCHAR *Filename, LPCWSTR ResourceName)