Fix some crash on bad parameter conditions.
[wine.git] / if1632 / thunk.c
blob47e2bb5a44ba71c5e84312eaa8a5d79f97d83b76
1 /*
2 * Emulator thunks
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "callback.h"
11 #include "builtin16.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "stackframe.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(thunk);
20 /* List of the 16-bit callback functions. This list is used */
21 /* by the build program to generate the file if1632/callto16.S */
23 /* ### start build ### */
24 extern WORD CALLBACK THUNK_CallTo16_word_ (FARPROC16);
25 extern WORD CALLBACK THUNK_CallTo16_word_l (FARPROC16,LONG);
26 extern LONG CALLBACK THUNK_CallTo16_long_l (FARPROC16,LONG);
27 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
28 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
29 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
30 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
31 extern WORD CALLBACK THUNK_CallTo16_word_w (FARPROC16,WORD);
32 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
33 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
34 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
35 /* ### stop build ### */
37 static THUNK *firstThunk = NULL;
39 CALLOUT_TABLE Callout = {
40 /* PostMessageA */ NULL,
41 /* UserSignalProc */ NULL,
42 /* FinalUserInit16 */ NULL,
43 /* InitThreadInput16 */ NULL,
44 /* UserYield16) */ NULL,
45 /* DestroyIcon32 */ NULL,
46 /* WaitForInputIdle */ NULL
49 /***********************************************************************
50 * THUNK_Alloc
52 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
54 HANDLE16 hSeg;
55 NE_MODULE *pModule;
56 THUNK *thunk;
58 /* NULL maps to NULL */
59 if ( !func ) return NULL;
61 /*
62 * If we got an 16-bit built-in API entry point, retrieve the Wine
63 * 32-bit handler for that API routine.
65 * NOTE: For efficiency reasons, we only check whether the selector
66 * of 'func' points to the code segment of a built-in module.
67 * It might be theoretically possible that the offset is such
68 * that 'func' does not point, in fact, to an API entry point.
69 * In this case, however, the pointer is corrupt anyway.
71 hSeg = GlobalHandle16( SELECTOROF( func ) );
72 pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
74 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN)
75 && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
77 FARPROC proc = (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)func ))->target;
79 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
80 SELECTOROF( func ), OFFSETOF( func ), relay, proc );
81 return proc;
84 /* Otherwise, we need to alloc a thunk */
85 thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
86 if (thunk)
88 thunk->popl_eax = 0x58;
89 thunk->pushl_func = 0x68;
90 thunk->proc = func;
91 thunk->pushl_eax = 0x50;
92 thunk->jmp = 0xe9;
93 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
94 thunk->magic = CALLTO16_THUNK_MAGIC;
95 thunk->next = firstThunk;
96 firstThunk = thunk;
99 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
100 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
101 return (FARPROC)thunk;
104 /***********************************************************************
105 * THUNK_Free
107 void THUNK_Free( FARPROC thunk )
109 THUNK *t = (THUNK*)thunk;
110 if ( !t || IsBadReadPtr( t, sizeof(*t) )
111 || t->magic != CALLTO16_THUNK_MAGIC )
112 return;
114 if (HeapValidate( GetProcessHeap(), 0, t ))
116 THUNK **prev = &firstThunk;
117 while (*prev && (*prev != t)) prev = &(*prev)->next;
118 if (*prev)
120 *prev = t->next;
121 HeapFree( GetProcessHeap(), 0, t );
122 return;
125 ERR("invalid thunk addr %p\n", thunk );
126 return;
130 /***********************************************************************
131 * THUNK_GetCalloutThunk
133 * Retrieve API entry point with given name from given module.
134 * If module is builtin, return the 32-bit entry point, otherwise
135 * create a 32->16 thunk to the 16-bit entry point, using the
136 * given relay code.
139 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
141 FARPROC16 proc = GetProcAddress16( pModule->self, name );
142 if ( !proc ) return 0;
144 if ( pModule->flags & NE_FFLAGS_BUILTIN )
145 return (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)proc ))->target;
146 else
147 return (FARPROC)THUNK_Alloc( proc, relay );
150 /***********************************************************************
151 * THUNK_InitCallout
153 void THUNK_InitCallout(void)
155 HMODULE hModule;
156 NE_MODULE *pModule;
158 hModule = GetModuleHandleA( "user32.dll" );
159 if ( hModule )
161 #define GETADDR( name ) \
162 *(FARPROC *)&Callout.name = GetProcAddress( hModule, #name )
164 GETADDR( PostMessageA );
165 GETADDR( WaitForInputIdle );
166 #undef GETADDR
168 else WARN("no 32-bit USER\n");
170 pModule = NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
171 if ( pModule )
173 #define GETADDR( var, name, thk ) \
174 *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
175 (RELAY)THUNK_CallTo16_##thk )
177 GETADDR( FinalUserInit16, "FinalUserInit", word_ );
178 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
179 GETADDR( UserYield16, "UserYield", word_ );
180 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
181 GETADDR( UserSignalProc, "SignalProc32", word_lllw );
182 #undef GETADDR
184 else WARN("no 16-bit USER\n");