wininet: Move freeing netconn into create_netconn_socket (Coverity).
[wine.git] / dlls / krnl386.exe16 / snoop.c
blobcef0fda73952d858dfeb486e62c09dc43a74efbd
1 /*
2 * 386-specific Win16 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnt.h"
31 #include "wine/winbase16.h"
32 #include "winternl.h"
33 #include "wine/library.h"
34 #include "kernel16_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
39 #include "pshpack1.h"
41 static void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT *context);
42 static void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT *context);
44 typedef struct tagSNOOP16_FUN {
45 /* code part */
46 BYTE lcall; /* 0x9a call absolute with segment */
47 DWORD snr;
48 /* unreached */
49 int nrofargs;
50 FARPROC16 origfun;
51 char *name;
52 } SNOOP16_FUN;
54 typedef struct tagSNOOP16_DLL {
55 HMODULE16 hmod;
56 HANDLE16 funhandle;
57 SNOOP16_FUN *funs;
58 struct tagSNOOP16_DLL *next;
59 char name[1];
60 } SNOOP16_DLL;
62 typedef struct tagSNOOP16_RETURNENTRY {
63 /* code part */
64 BYTE lcall; /* 0x9a call absolute with segment */
65 DWORD snr;
66 /* unreached */
67 FARPROC16 origreturn;
68 SNOOP16_DLL *dll;
69 DWORD ordinal;
70 WORD origSP;
71 WORD *args; /* saved args across a stdcall */
72 } SNOOP16_RETURNENTRY;
74 typedef struct tagSNOOP16_RETURNENTRIES {
75 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
76 HANDLE16 rethandle;
77 struct tagSNOOP16_RETURNENTRIES *next;
78 } SNOOP16_RETURNENTRIES;
80 typedef struct tagSNOOP16_RELAY {
81 WORD pushbp; /* 0x5566 */
82 BYTE pusheax; /* 0x50 */
83 WORD pushax; /* 0x5066 */
84 BYTE pushl; /* 0x68 */
85 DWORD realfun; /* SNOOP16_Return */
86 BYTE lcall; /* 0x9a call absolute with segment */
87 DWORD callfromregs;
88 WORD seg;
89 WORD lret; /* 0xcb66 */
90 } SNOOP16_RELAY;
92 #include "poppack.h"
94 static SNOOP16_DLL *firstdll = NULL;
95 static SNOOP16_RETURNENTRIES *firstrets = NULL;
96 static SNOOP16_RELAY *snr;
97 static HANDLE16 xsnr = 0;
99 void
100 SNOOP16_RegisterDLL(HMODULE16 hModule,LPCSTR name) {
101 SNOOP16_DLL **dll = &(firstdll);
102 const char *p;
103 char *q;
105 if (!TRACE_ON(snoop)) return;
107 TRACE("hmod=%x, name=%s\n", hModule, name);
109 if (!snr) {
110 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT);
111 snr = GlobalLock16(xsnr);
112 snr[0].pushbp = 0x5566;
113 snr[0].pusheax = 0x50;
114 snr[0].pushax = 0x5066;
115 snr[0].pushl = 0x68;
116 snr[0].realfun = (DWORD)SNOOP16_Entry;
117 snr[0].lcall = 0x9a;
118 snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
119 snr[0].seg = wine_get_cs();
120 snr[0].lret = 0xcb66;
122 snr[1].pushbp = 0x5566;
123 snr[1].pusheax = 0x50;
124 snr[1].pushax = 0x5066;
125 snr[1].pushl = 0x68;
126 snr[1].realfun = (DWORD)SNOOP16_Return;
127 snr[1].lcall = 0x9a;
128 snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
129 snr[1].seg = wine_get_cs();
130 snr[1].lret = 0xcb66;
132 while (*dll) {
133 if ((*dll)->hmod == hModule)
135 /* another dll, loaded at the same address */
136 GlobalUnlock16((*dll)->funhandle);
137 GlobalFree16((*dll)->funhandle);
138 break;
140 dll = &((*dll)->next);
143 if (*dll)
144 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
145 else
146 *dll = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SNOOP16_DLL)+strlen(name));
148 (*dll)->next = NULL;
149 (*dll)->hmod = hModule;
150 if ((p=strrchr(name,'\\')))
151 name = p+1;
152 strcpy( (*dll)->name, name );
153 if ((q=strrchr((*dll)->name,'.')))
154 *q='\0';
155 (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
156 (*dll)->funs = GlobalLock16((*dll)->funhandle);
157 if (!(*dll)->funs) {
158 HeapFree(GetProcessHeap(),0,*dll);
159 FIXME("out of memory\n");
160 return;
164 FARPROC16
165 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
166 SNOOP16_DLL *dll = firstdll;
167 SNOOP16_FUN *fun;
168 NE_MODULE *pModule = NE_GetPtr(hmod);
169 unsigned char *cpnt;
170 char name[200];
172 if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
173 return origfun;
174 if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an impossible opcode, possible dataref. */
175 return origfun;
176 while (dll) {
177 if (hmod == dll->hmod)
178 break;
179 dll=dll->next;
181 if (!dll) /* probably internal */
182 return origfun;
183 if (ordinal>65535/sizeof(SNOOP16_FUN))
184 return origfun;
185 fun = dll->funs+ordinal;
186 /* already done? */
187 fun->lcall = 0x9a;
188 fun->snr = MAKELONG(0,xsnr);
189 fun->origfun = origfun;
190 if (fun->name)
191 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
192 cpnt = (unsigned char *)pModule + pModule->ne_restab;
193 while (*cpnt) {
194 cpnt += *cpnt + 1 + sizeof(WORD);
195 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
196 sprintf(name,"%.*s",*cpnt,cpnt+1);
197 break;
200 /* Now search the non-resident names table */
202 if (!*cpnt && pModule->nrname_handle) {
203 cpnt = GlobalLock16( pModule->nrname_handle );
204 while (*cpnt) {
205 cpnt += *cpnt + 1 + sizeof(WORD);
206 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
207 sprintf(name,"%.*s",*cpnt,cpnt+1);
208 break;
212 if (*cpnt)
214 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
215 strcpy( fun->name, name );
217 else
218 fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
220 if (!SNOOP16_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
221 return origfun;
223 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
224 if (strchr(fun->name,'_')) {
225 char *s=strchr(fun->name,'_');
227 if (!strncasecmp(s,"_thunkdata",10)) {
228 HeapFree(GetProcessHeap(),0,fun->name);
229 fun->name = NULL;
230 return origfun;
233 fun->lcall = 0x9a;
234 fun->snr = MAKELONG(0,xsnr);
235 fun->origfun = origfun;
236 fun->nrofargs = -1;
237 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
240 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
241 static void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT *context) {
242 DWORD ordinal=0;
243 DWORD entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
244 WORD xcs = context->SegCs;
245 SNOOP16_DLL *dll = firstdll;
246 SNOOP16_FUN *fun = NULL;
247 SNOOP16_RETURNENTRIES **rets = &firstrets;
248 SNOOP16_RETURNENTRY *ret;
249 unsigned i=0;
250 int max;
252 while (dll) {
253 if (xcs == dll->funhandle) {
254 fun = (SNOOP16_FUN*)entry;
255 ordinal = fun-dll->funs;
256 break;
258 dll=dll->next;
260 if (!dll) {
261 FIXME("entrypoint 0x%08x not found\n",entry);
262 return; /* oops */
264 while (*rets) {
265 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
266 if (!(*rets)->entry[i].origreturn)
267 break;
268 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
269 break;
270 rets = &((*rets)->next);
272 if (!*rets) {
273 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
274 *rets = GlobalLock16(hand);
275 (*rets)->rethandle = hand;
276 i = 0; /* entry 0 is free */
278 ret = &((*rets)->entry[i]);
279 ret->lcall = 0x9a;
280 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
281 ret->origreturn = (FARPROC16)CALLER1REF;
282 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
283 ret->dll = dll;
284 ret->args = NULL;
285 ret->ordinal = ordinal;
286 ret->origSP = LOWORD(context->Esp);
288 context->Eip= LOWORD(fun->origfun);
289 context->SegCs = HIWORD(fun->origfun);
292 DPRINTF("%04x:CALL %s.%d: %s(",GetCurrentThreadId(), dll->name,ordinal,fun->name);
293 if (fun->nrofargs>0) {
294 max = fun->nrofargs;
295 if (max>16) max=16;
296 for (i=max;i--;)
297 DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
298 if (max!=fun->nrofargs)
299 DPRINTF(" ...");
300 } else if (fun->nrofargs<0) {
301 DPRINTF("<unknown, check return>");
302 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
303 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
305 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
308 static void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT *context) {
309 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
311 /* We haven't found out the nrofargs yet. If we called a cdecl
312 * function it is too late anyway and we can just set '0' (which
313 * will be the difference between orig and current SP
314 * If pascal -> everything ok.
316 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
317 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
319 context->Eip = LOWORD(ret->origreturn);
320 context->SegCs = HIWORD(ret->origreturn);
321 DPRINTF("%04x:RET %s.%d: %s(",
322 GetCurrentThreadId(),ret->dll->name,ret->ordinal,
323 ret->dll->funs[ret->ordinal].name);
324 if (ret->args) {
325 int i,max;
327 max = ret->dll->funs[ret->ordinal].nrofargs;
328 if (max>16)
329 max=16;
330 if (max<0)
331 max=0;
333 for (i=max;i--;)
334 DPRINTF("%04x%s",ret->args[i],i?",":"");
335 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
336 DPRINTF(" ...");
337 HeapFree(GetProcessHeap(),0,ret->args);
338 ret->args = NULL;
340 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
341 (WORD)context->Edx,(WORD)context->Eax,
342 HIWORD(ret->origreturn),LOWORD(ret->origreturn));
343 ret->origreturn = NULL; /* mark as empty */