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