Fix snooping of 16-bit dlls being loaded at the same address.
[wine] / if1632 / snoop.c
1 /*
2  * 386-specific Win16 dll<->dll snooping functions
3  *
4  * Copyright 1998 Marcus Meissner
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <assert.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include "winbase.h"
25 #include "windef.h"
26 #include "winnt.h"
27 #include "wine/winbase16.h"
28 #include "wine/library.h"
29 #include "global.h"
30 #include "stackframe.h"
31 #include "builtin16.h"
32 #include "toolhelp.h"
33 #include "snoop.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
37
38 #ifdef __i386__
39
40 #include "pshpack1.h"
41
42 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
43 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
44
45 typedef struct tagSNOOP16_FUN {
46         /* code part */
47         BYTE            lcall;          /* 0x9a call absolute with segment */
48         DWORD           snr;
49         /* unreached */
50         int             nrofargs;
51         FARPROC16       origfun;
52         char            *name;
53 } SNOOP16_FUN;
54
55 typedef struct tagSNOOP16_DLL {
56         HMODULE16       hmod;
57         HANDLE16        funhandle;
58         SNOOP16_FUN     *funs;
59         struct tagSNOOP16_DLL   *next;
60         char name[1];
61 } SNOOP16_DLL;
62
63 typedef struct tagSNOOP16_RETURNENTRY {
64         /* code part */
65         BYTE            lcall;          /* 0x9a call absolute with segment */
66         DWORD           snr;
67         /* unreached */
68         FARPROC16       origreturn;
69         SNOOP16_DLL     *dll;
70         DWORD           ordinal;
71         WORD            origSP;
72         WORD            *args;          /* saved args across a stdcall */
73 } SNOOP16_RETURNENTRY;
74
75 typedef struct tagSNOOP16_RETURNENTRIES {
76         SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
77         HANDLE16        rethandle;
78         struct tagSNOOP16_RETURNENTRIES *next;
79 } SNOOP16_RETURNENTRIES;
80
81 typedef struct tagSNOOP16_RELAY {
82         WORD            pushbp;         /* 0x5566 */
83         BYTE            pusheax;        /* 0x50 */
84         WORD            pushax;         /* 0x5066 */
85         BYTE            pushl;          /* 0x68 */
86         DWORD           realfun;        /* SNOOP16_Return */
87         BYTE            lcall;          /* 0x9a call absolute with segment */
88         DWORD           callfromregs;
89         WORD            seg;
90         WORD            lret;           /* 0xcb66 */
91 } SNOOP16_RELAY;
92
93 #include "poppack.h"
94
95 static  SNOOP16_DLL             *firstdll = NULL;
96 static  SNOOP16_RETURNENTRIES   *firstrets = NULL;
97 static  SNOOP16_RELAY           *snr;
98 static  HANDLE16                xsnr = 0;
99
100 void
101 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
102         SNOOP16_DLL     **dll = &(firstdll);
103         char            *s;
104
105         if (!TRACE_ON(snoop)) return;
106
107         TRACE("hmod=%x, name=%s\n", pModule->self, name);
108
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;
121
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;
131         }
132         while (*dll) {
133                 if ((*dll)->hmod == pModule->self)
134                 {
135                     /* another dll, loaded at the same address */
136                     GlobalUnlock16((*dll)->funhandle);
137                     GlobalFree16((*dll)->funhandle);
138                     break;
139                 }
140                 dll = &((*dll)->next);
141         }
142         *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
143         (*dll)->next    = NULL;
144         (*dll)->hmod    = pModule->self;
145         if ((s=strrchr(name,'\\')))
146                 name = s+1;
147         strcpy( (*dll)->name, name );
148         if ((s=strrchr((*dll)->name,'.')))
149                 *s='\0';
150         (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
151         (*dll)->funs = GlobalLock16((*dll)->funhandle);
152         if (!(*dll)->funs) {
153                 HeapFree(GetProcessHeap(),0,*dll);
154                 FIXME("out of memory\n");
155                 return;
156         }
157 }
158
159 FARPROC16
160 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
161         SNOOP16_DLL                     *dll = firstdll;
162         SNOOP16_FUN                     *fun;
163         NE_MODULE                       *pModule = NE_GetPtr(hmod);
164         unsigned char                   *cpnt;
165         char                            name[200];
166
167         if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
168                 return origfun;
169         if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
170                 return origfun;
171         while (dll) {
172                 if (hmod == dll->hmod)
173                         break;
174                 dll=dll->next;
175         }
176         if (!dll)       /* probably internal */
177                 return origfun;
178         if (ordinal>65535/sizeof(SNOOP16_FUN))
179                 return origfun;
180         fun = dll->funs+ordinal;
181         /* already done? */
182         fun->lcall      = 0x9a;
183         fun->snr        = MAKELONG(0,xsnr);
184         fun->origfun    = origfun;
185         if (fun->name)
186                 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
187         cpnt = (unsigned char *)pModule + pModule->name_table;
188         while (*cpnt) {
189                 cpnt += *cpnt + 1 + sizeof(WORD);
190                 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
191                         sprintf(name,"%.*s",*cpnt,cpnt+1);
192                         break;
193                 }
194         }
195         /* Now search the non-resident names table */
196
197         if (!*cpnt && pModule->nrname_handle) {
198                 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
199                 while (*cpnt) {
200                         cpnt += *cpnt + 1 + sizeof(WORD);
201                         if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
202                                     sprintf(name,"%.*s",*cpnt,cpnt+1);
203                                     break;
204                         }
205                 }
206         }
207         if (*cpnt)
208         {
209             fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
210             strcpy( fun->name, name );
211         }
212         else
213             fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
214
215         if (!SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
216                 return origfun;
217
218         /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
219         if (strchr(fun->name,'_')) {
220                 char *s=strchr(fun->name,'_');
221
222                 if (!strncasecmp(s,"_thunkdata",10)) {
223                         HeapFree(GetProcessHeap(),0,fun->name);
224                         fun->name = NULL;
225                         return origfun;
226                 }
227         }
228         fun->lcall      = 0x9a;
229         fun->snr        = MAKELONG(0,xsnr);
230         fun->origfun    = origfun;
231         fun->nrofargs   = -1;
232         return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
233 }
234
235 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
236 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
237         DWORD           ordinal=0;
238         DWORD           entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
239         WORD            xcs = context->SegCs;
240         SNOOP16_DLL     *dll = firstdll;
241         SNOOP16_FUN     *fun = NULL;
242         SNOOP16_RETURNENTRIES   **rets = &firstrets;
243         SNOOP16_RETURNENTRY     *ret;
244         int             i=0, max;
245
246         while (dll) {
247                 if (xcs == dll->funhandle) {
248                         fun = (SNOOP16_FUN*)entry;
249                         ordinal = fun-dll->funs;
250                         break;
251                 }
252                 dll=dll->next;
253         }
254         if (!dll) {
255                 FIXME("entrypoint 0x%08lx not found\n",entry);
256                 return; /* oops */
257         }
258         while (*rets) {
259                 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
260                         if (!(*rets)->entry[i].origreturn)
261                                 break;
262                 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
263                         break;
264                 rets = &((*rets)->next);
265         }
266         if (!*rets) {
267                 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
268                 *rets = GlobalLock16(hand);
269                 (*rets)->rethandle = hand;
270                 i = 0;  /* entry 0 is free */
271         }
272         ret = &((*rets)->entry[i]);
273         ret->lcall      = 0x9a;
274         ret->snr        = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
275         ret->origreturn = (FARPROC16)CALLER1REF;
276         CALLER1REF      = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
277         ret->dll        = dll;
278         ret->args       = NULL;
279         ret->ordinal    = ordinal;
280         ret->origSP     = LOWORD(context->Esp);
281
282         context->Eip= LOWORD(fun->origfun);
283         context->SegCs = HIWORD(fun->origfun);
284
285
286         DPRINTF("CALL %s.%ld: %s(",dll->name,ordinal,fun->name);
287         if (fun->nrofargs>0) {
288                 max = fun->nrofargs;
289                 if (max>16) max=16;
290                 for (i=max;i--;)
291                         DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
292                 if (max!=fun->nrofargs)
293                         DPRINTF(" ...");
294         } else if (fun->nrofargs<0) {
295                 DPRINTF("<unknown, check return>");
296                 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
297                 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
298         }
299         DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
300 }
301
302 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
303         SNOOP16_RETURNENTRY     *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
304
305         /* We haven't found out the nrofargs yet. If we called a cdecl
306          * function it is too late anyway and we can just set '0' (which
307          * will be the difference between orig and current SP
308          * If pascal -> everything ok.
309          */
310         if (ret->dll->funs[ret->ordinal].nrofargs<0) {
311                 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
312         }
313         context->Eip = LOWORD(ret->origreturn);
314         context->SegCs  = HIWORD(ret->origreturn);
315         if (ret->args) {
316                 int     i,max;
317
318                 DPRINTF("RET  %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
319                 max = ret->dll->funs[ret->ordinal].nrofargs;
320                 if (max>16)
321                         max=16;
322                 if (max<0)
323                         max=0;
324
325                 for (i=max;i--;)
326                         DPRINTF("%04x%s",ret->args[i],i?",":"");
327                 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
328                         DPRINTF(" ...");
329                 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
330                         DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
331                 );
332                 HeapFree(GetProcessHeap(),0,ret->args);
333                 ret->args = NULL;
334         } else
335                 DPRINTF("RET  %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
336                         ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
337                         DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
338                 );
339         ret->origreturn = NULL; /* mark as empty */
340 }
341 #else   /* !__i386__ */
342 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
343         if (!TRACE_ON(snoop)) return;
344         FIXME("snooping works only on i386 for now.\n");
345 }
346
347 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
348         return origfun;
349 }
350 #endif  /* !__i386__ */