mshtml: Check for null pointers in IHTMLLocation_get*.
[wine/hramrach.git] / dlls / kernel32 / relay16.c
blobb3ac234af33946a72b9fbffd29cda694408b3d53
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #ifdef __i386__
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wine/winbase16.h"
34 #include "winternl.h"
35 #include "kernel_private.h"
36 #include "kernel16_private.h"
37 #include "wine/unicode.h"
38 #include "wine/library.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(relay);
43 static const WCHAR **debug_relay_excludelist;
44 static const WCHAR **debug_relay_includelist;
45 static const WCHAR **debug_snoop_excludelist;
46 static const WCHAR **debug_snoop_includelist;
48 /* compare an ASCII and a Unicode string without depending on the current codepage */
49 static inline int strcmpiAW( const char *strA, const WCHAR *strW )
51 while (*strA && (toupperW((unsigned char)*strA) == toupperW(*strW))) { strA++; strW++; }
52 return toupperW((unsigned char)*strA) - toupperW(*strW);
55 /* compare an ASCII and a Unicode string without depending on the current codepage */
56 static inline int strncmpiAW( const char *strA, const WCHAR *strW, int n )
58 int ret = 0;
59 for ( ; n > 0; n--, strA++, strW++)
60 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
61 return ret;
64 /***********************************************************************
65 * build_list
67 * Build a function list from a ';'-separated string.
69 static const WCHAR **build_list( const WCHAR *buffer )
71 int count = 1;
72 const WCHAR *p = buffer;
73 const WCHAR **ret;
75 while ((p = strchrW( p, ';' )))
77 count++;
78 p++;
80 /* allocate count+1 pointers, plus the space for a copy of the string */
81 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
82 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
84 WCHAR *str = (WCHAR *)(ret + count + 1);
85 WCHAR *p = str;
87 strcpyW( str, buffer );
88 count = 0;
89 for (;;)
91 ret[count++] = p;
92 if (!(p = strchrW( p, ';' ))) break;
93 *p++ = 0;
95 ret[count++] = NULL;
97 return ret;
101 /***********************************************************************
102 * RELAY16_InitDebugLists
104 * Build the relay include/exclude function lists.
106 void RELAY16_InitDebugLists(void)
108 OBJECT_ATTRIBUTES attr;
109 UNICODE_STRING name;
110 char buffer[1024];
111 HANDLE root, hkey;
112 DWORD count;
113 WCHAR *str;
114 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
115 'W','i','n','e','\\',
116 'D','e','b','u','g',0};
117 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
118 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
119 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
120 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
122 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
123 attr.Length = sizeof(attr);
124 attr.RootDirectory = root;
125 attr.ObjectName = &name;
126 attr.Attributes = 0;
127 attr.SecurityDescriptor = NULL;
128 attr.SecurityQualityOfService = NULL;
129 RtlInitUnicodeString( &name, configW );
131 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
132 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
133 NtClose( root );
134 if (!hkey) return;
136 str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
137 RtlInitUnicodeString( &name, RelayIncludeW );
138 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
140 debug_relay_includelist = build_list( str );
143 RtlInitUnicodeString( &name, RelayExcludeW );
144 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
146 debug_relay_excludelist = build_list( str );
149 RtlInitUnicodeString( &name, SnoopIncludeW );
150 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
152 debug_snoop_includelist = build_list( str );
155 RtlInitUnicodeString( &name, SnoopExcludeW );
156 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
158 debug_snoop_excludelist = build_list( str );
160 NtClose( hkey );
164 /***********************************************************************
165 * check_list
167 * Check if a given module and function is in the list.
169 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR **list )
171 char ord_str[10];
173 sprintf( ord_str, "%d", ordinal );
174 for(; *list; list++)
176 const WCHAR *p = strrchrW( *list, '.' );
177 if (p && p > *list) /* check module and function */
179 int len = p - *list;
180 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
181 if (p[1] == '*' && !p[2]) return TRUE;
182 if (!strcmpiAW( ord_str, p + 1 )) return TRUE;
183 if (func && !strcmpiAW( func, p + 1 )) return TRUE;
185 else /* function only */
187 if (func && !strcmpiAW( func, *list )) return TRUE;
190 return FALSE;
194 /***********************************************************************
195 * RELAY_ShowDebugmsgRelay
197 * Simple function to decide if a particular debugging message is
198 * wanted.
200 static BOOL RELAY_ShowDebugmsgRelay(const char *module, int ordinal, const char *func)
202 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
203 return FALSE;
204 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
205 return FALSE;
206 return TRUE;
210 /***********************************************************************
211 * SNOOP16_ShowDebugmsgSnoop
213 * Simple function to decide if a particular debugging message is
214 * wanted.
216 int SNOOP16_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
218 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
219 return FALSE;
220 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
221 return FALSE;
222 return TRUE;
226 /***********************************************************************
227 * get_entry_point
229 * Return the ordinal, name, and type info corresponding to a CS:IP address.
231 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR module, LPSTR func, WORD *pOrd )
233 WORD i, max_offset;
234 register BYTE *p;
235 NE_MODULE *pModule;
236 ET_BUNDLE *bundle;
237 ET_ENTRY *entry;
239 *pOrd = 0;
240 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
241 return NULL;
243 max_offset = 0;
244 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
247 entry = (ET_ENTRY *)((BYTE *)bundle+6);
248 for (i = bundle->first + 1; i <= bundle->last; i++)
250 if ((entry->offs < frame->entry_ip)
251 && (entry->segnum == 1) /* code segment ? */
252 && (entry->offs >= max_offset))
254 max_offset = entry->offs;
255 *pOrd = i;
257 entry++;
259 } while ( (bundle->next)
260 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
262 /* Search for the name in the resident names table */
263 /* (built-in modules have no non-resident table) */
265 p = (BYTE *)pModule + pModule->ne_restab;
266 memcpy( module, p + 1, *p );
267 module[*p] = 0;
269 while (*p)
271 p += *p + 1 + sizeof(WORD);
272 if (*(WORD *)(p + *p + 1) == *pOrd) break;
274 memcpy( func, p + 1, *p );
275 func[*p] = 0;
277 /* Retrieve entry point call structure */
278 p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
279 /* p now points to lret, get the start of CALLFROM16 structure */
280 return (CALLFROM16 *)(p - FIELD_OFFSET( CALLFROM16, ret ));
284 extern int call_entry_point( void *func, int nb_args, const int *args );
285 __ASM_GLOBAL_FUNC( call_entry_point,
286 "pushl %ebp\n\t"
287 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
288 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
289 "movl %esp,%ebp\n\t"
290 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
291 "pushl %esi\n\t"
292 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
293 "pushl %edi\n\t"
294 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
295 "movl 12(%ebp),%edx\n\t"
296 "shll $2,%edx\n\t"
297 "jz 1f\n\t"
298 "subl %edx,%esp\n\t"
299 "andl $~15,%esp\n\t"
300 "movl 12(%ebp),%ecx\n\t"
301 "movl 16(%ebp),%esi\n\t"
302 "movl %esp,%edi\n\t"
303 "cld\n\t"
304 "rep; movsl\n"
305 "1:\tcall *8(%ebp)\n\t"
306 "leal -8(%ebp),%esp\n\t"
307 "popl %edi\n\t"
308 __ASM_CFI(".cfi_same_value %edi\n\t")
309 "popl %esi\n\t"
310 __ASM_CFI(".cfi_same_value %esi\n\t")
311 "popl %ebp\n\t"
312 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
313 __ASM_CFI(".cfi_same_value %ebp\n\t")
314 "ret" )
317 /***********************************************************************
318 * relay_call_from_16_no_debug
320 * Same as relay_call_from_16 but doesn't print any debug information.
322 static int relay_call_from_16_no_debug( void *entry_point, unsigned char *args16, CONTEXT86 *context,
323 const CALLFROM16 *call )
325 unsigned int i, j, nb_args = 0;
326 int args32[20];
328 /* look for the ret instruction */
329 for (j = 0; j < sizeof(call->ret)/sizeof(call->ret[0]); j++)
330 if (call->ret[j] == 0xca66 || call->ret[j] == 0xcb66) break;
332 if (call->ret[j] == 0xcb66) /* cdecl */
334 for (i = 0; i < 20; i++, nb_args++)
336 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
338 if (type == ARG_NONE) break;
339 switch(type)
341 case ARG_WORD:
342 args32[nb_args] = *(WORD *)args16;
343 args16 += sizeof(WORD);
344 break;
345 case ARG_SWORD:
346 args32[nb_args] = *(short *)args16;
347 args16 += sizeof(WORD);
348 break;
349 case ARG_LONG:
350 case ARG_SEGSTR:
351 args32[nb_args] = *(int *)args16;
352 args16 += sizeof(int);
353 break;
354 case ARG_PTR:
355 case ARG_STR:
356 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
357 args16 += sizeof(SEGPTR);
358 break;
359 case ARG_VARARG:
360 args32[nb_args] = (int)args16;
361 break;
362 default:
363 break;
367 else /* not cdecl */
369 /* Start with the last arg */
370 args16 += call->ret[j + 1];
371 for (i = 0; i < 20; i++, nb_args++)
373 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
375 if (type == ARG_NONE) break;
376 switch(type)
378 case ARG_WORD:
379 args16 -= sizeof(WORD);
380 args32[nb_args] = *(WORD *)args16;
381 break;
382 case ARG_SWORD:
383 args16 -= sizeof(WORD);
384 args32[nb_args] = *(short *)args16;
385 break;
386 case ARG_LONG:
387 case ARG_SEGSTR:
388 args16 -= sizeof(int);
389 args32[nb_args] = *(int *)args16;
390 break;
391 case ARG_PTR:
392 case ARG_STR:
393 args16 -= sizeof(SEGPTR);
394 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
395 break;
396 default:
397 break;
402 if (!j) /* register function */
403 args32[nb_args++] = (int)context;
405 SYSLEVEL_CheckNotLevel( 2 );
407 return call_entry_point( entry_point, nb_args, args32 );
411 /***********************************************************************
412 * relay_call_from_16
414 * Replacement for the 16-bit relay functions when relay debugging is on.
416 int relay_call_from_16( void *entry_point, unsigned char *args16, CONTEXT86 *context )
418 STACK16FRAME *frame;
419 WORD ordinal;
420 unsigned int i, j, nb_args = 0;
421 int ret_val, args32[20];
422 char module[10], func[64];
423 const CALLFROM16 *call;
425 frame = CURRENT_STACK16;
426 call = get_entry_point( frame, module, func, &ordinal );
427 if (!TRACE_ON(relay) || !RELAY_ShowDebugmsgRelay( module, ordinal, func ))
428 return relay_call_from_16_no_debug( entry_point, args16, context, call );
430 DPRINTF( "%04x:Call %s.%d: %s(",GetCurrentThreadId(), module, ordinal, func );
432 /* look for the ret instruction */
433 for (j = 0; j < sizeof(call->ret)/sizeof(call->ret[0]); j++)
434 if (call->ret[j] == 0xca66 || call->ret[j] == 0xcb66) break;
436 if (call->ret[j] == 0xcb66) /* cdecl */
438 for (i = 0; i < 20; i++, nb_args++)
440 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
442 if (type == ARG_NONE) break;
443 if (i) DPRINTF( "," );
444 switch(type)
446 case ARG_WORD:
447 DPRINTF( "%04x", *(WORD *)args16 );
448 args32[nb_args] = *(WORD *)args16;
449 args16 += sizeof(WORD);
450 break;
451 case ARG_SWORD:
452 DPRINTF( "%04x", *(WORD *)args16 );
453 args32[nb_args] = *(short *)args16;
454 args16 += sizeof(WORD);
455 break;
456 case ARG_LONG:
457 DPRINTF( "%08x", *(int *)args16 );
458 args32[nb_args] = *(int *)args16;
459 args16 += sizeof(int);
460 break;
461 case ARG_PTR:
462 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
463 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
464 args16 += sizeof(SEGPTR);
465 break;
466 case ARG_STR:
467 DPRINTF( "%08x %s", *(int *)args16,
468 debugstr_a( MapSL(*(SEGPTR *)args16 )));
469 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
470 args16 += sizeof(int);
471 break;
472 case ARG_SEGSTR:
473 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
474 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
475 args32[nb_args] = *(SEGPTR *)args16;
476 args16 += sizeof(SEGPTR);
477 break;
478 case ARG_VARARG:
479 DPRINTF( "..." );
480 args32[nb_args] = (int)args16;
481 break;
482 default:
483 break;
487 else /* not cdecl */
489 /* Start with the last arg */
490 args16 += call->ret[j + 1];
491 for (i = 0; i < 20; i++, nb_args++)
493 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
495 if (type == ARG_NONE) break;
496 if (i) DPRINTF( "," );
497 switch(type)
499 case ARG_WORD:
500 args16 -= sizeof(WORD);
501 args32[nb_args] = *(WORD *)args16;
502 DPRINTF( "%04x", *(WORD *)args16 );
503 break;
504 case ARG_SWORD:
505 args16 -= sizeof(WORD);
506 args32[nb_args] = *(short *)args16;
507 DPRINTF( "%04x", *(WORD *)args16 );
508 break;
509 case ARG_LONG:
510 args16 -= sizeof(int);
511 args32[nb_args] = *(int *)args16;
512 DPRINTF( "%08x", *(int *)args16 );
513 break;
514 case ARG_PTR:
515 args16 -= sizeof(SEGPTR);
516 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
517 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
518 break;
519 case ARG_STR:
520 args16 -= sizeof(int);
521 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
522 DPRINTF( "%08x %s", *(int *)args16,
523 debugstr_a( MapSL(*(SEGPTR *)args16 )));
524 break;
525 case ARG_SEGSTR:
526 args16 -= sizeof(SEGPTR);
527 args32[nb_args] = *(SEGPTR *)args16;
528 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
529 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
530 break;
531 case ARG_VARARG:
532 DPRINTF( "..." );
533 args32[nb_args] = (int)args16;
534 break;
535 default:
536 break;
541 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
543 if (!j) /* register function */
545 args32[nb_args++] = (int)context;
546 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08x\n",
547 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
548 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
549 (WORD)context->SegEs, context->EFlags );
552 SYSLEVEL_CheckNotLevel( 2 );
554 ret_val = call_entry_point( entry_point, nb_args, args32 );
556 SYSLEVEL_CheckNotLevel( 2 );
558 DPRINTF( "%04x:Ret %s.%d: %s() ",GetCurrentThreadId(), module, ordinal, func );
559 if (!j) /* register function */
561 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
562 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
563 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08x\n",
564 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
565 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
566 (WORD)context->SegEs, context->EFlags );
568 else
570 frame = CURRENT_STACK16; /* might have be changed by the entry point */
571 if (j == 1) /* 16-bit return sequence */
572 DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
573 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
574 else
575 DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
576 ret_val, frame->cs, frame->ip, frame->ds );
578 return ret_val;
581 #endif /* __i386__ */