Check for valid object in GDI_GetObjPtr even for MAGIC_DONTCARE.
[wine/multimedia.git] / if1632 / thunk.c
blob5e892ae1251f8e0d34ca31ac16a7e47dfb938cdd
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 "module.h"
13 #include "stackframe.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(thunk);
19 /* List of the 16-bit callback functions. This list is used */
20 /* by the build program to generate the file if1632/callto16.S */
22 /* ### start build ### */
23 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
24 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
25 /* ### stop build ### */
27 static THUNK *firstThunk = NULL;
29 CALLOUT_TABLE Callout = {
30 /* UserSignalProc */ NULL,
31 /* DestroyIcon32 */ NULL
34 /***********************************************************************
35 * THUNK_Alloc
37 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
39 HANDLE16 hSeg;
40 NE_MODULE *pModule;
41 THUNK *thunk;
43 /* NULL maps to NULL */
44 if ( !func ) return NULL;
46 /*
47 * If we got an 16-bit built-in API entry point, retrieve the Wine
48 * 32-bit handler for that API routine.
50 * NOTE: For efficiency reasons, we only check whether the selector
51 * of 'func' points to the code segment of a built-in module.
52 * It might be theoretically possible that the offset is such
53 * that 'func' does not point, in fact, to an API entry point.
54 * In this case, however, the pointer is corrupt anyway.
56 hSeg = GlobalHandle16( SELECTOROF( func ) );
57 pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
59 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN)
60 && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
62 FARPROC proc = (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)func ))->target;
64 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
65 SELECTOROF( func ), OFFSETOF( func ), relay, proc );
66 return proc;
69 /* Otherwise, we need to alloc a thunk */
70 thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
71 if (thunk)
73 thunk->popl_eax = 0x58;
74 thunk->pushl_func = 0x68;
75 thunk->proc = func;
76 thunk->pushl_eax = 0x50;
77 thunk->jmp = 0xe9;
78 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
79 thunk->magic = CALLTO16_THUNK_MAGIC;
80 thunk->next = firstThunk;
81 firstThunk = thunk;
84 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
85 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
86 return (FARPROC)thunk;
89 /***********************************************************************
90 * THUNK_Free
92 void THUNK_Free( FARPROC thunk )
94 THUNK *t = (THUNK*)thunk;
95 if ( !t || IsBadReadPtr( t, sizeof(*t) )
96 || t->magic != CALLTO16_THUNK_MAGIC )
97 return;
99 if (HeapValidate( GetProcessHeap(), 0, t ))
101 THUNK **prev = &firstThunk;
102 while (*prev && (*prev != t)) prev = &(*prev)->next;
103 if (*prev)
105 *prev = t->next;
106 HeapFree( GetProcessHeap(), 0, t );
107 return;
110 ERR("invalid thunk addr %p\n", thunk );
111 return;
115 /***********************************************************************
116 * THUNK_GetCalloutThunk
118 * Retrieve API entry point with given name from given module.
119 * If module is builtin, return the 32-bit entry point, otherwise
120 * create a 32->16 thunk to the 16-bit entry point, using the
121 * given relay code.
124 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
126 FARPROC16 proc = GetProcAddress16( pModule->self, name );
127 if ( !proc ) return 0;
129 if ( pModule->flags & NE_FFLAGS_BUILTIN )
130 return (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)proc ))->target;
131 else
132 return (FARPROC)THUNK_Alloc( proc, relay );
135 /***********************************************************************
136 * THUNK_InitCallout
138 void THUNK_InitCallout(void)
140 NE_MODULE *pModule = NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
141 if ( pModule )
143 #define GETADDR( var, name, thk ) \
144 *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
145 (RELAY)THUNK_CallTo16_##thk )
147 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
148 GETADDR( UserSignalProc, "SignalProc32", word_lllw );
149 #undef GETADDR
151 else WARN("no 16-bit USER\n");