Skip to content
Tcl Source Code Attachment Details
Login

Attachment Details

Overview

Artifact ID: 24f04cb267bf98c2dfb6abce8932f803e31f714b
Ticket: 525746ffffffffffffffffffffffffffffffffff
Date: 2003-01-25 03:28:38
User: mdejong
Artifact Attached: 36f659ff8745037c8b3917b499ac7bfd65c8ea99
Filename:new_handler.c
Description:New stack based SEH handler impl
Content:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <excpt.h>

#define EXCEPTION_DISPOSITION int

void *INITIAL_ESP;
void *INITIAL_EBP;
void *INITIAL_HANDLER;

void *RESTORED_ESP;
void *RESTORED_EBP;
void *RESTORED_HANDLER;

int main()
{
    __asm__ __volatile__ (
             "movl %%esp, %0" "\n\t" : "=r" (INITIAL_ESP) );
    __asm__ __volatile__ (
             "movl %%ebp, %0" "\n\t" : "=r" (INITIAL_EBP) );
    __asm__ __volatile__ (
             "movl %%fs:0, %0" "\n\t" : "=r" (INITIAL_HANDLER) );

    __asm__ __volatile__ (
             "pushl %ebp" "\n\t"
             "pushl $__except_handler" "\n\t"
             "pushl %fs:0" "\n\t"
             "movl  %esp, %fs:0");

    *(PDWORD) 0 = 0;

    __asm__ __volatile__ (
             "main_reentry:" "\n\t"
             "movl %fs:0, %eax" "\n\t"
             "movl 0x8(%eax), %esp" "\n\t"
             "movl (%esp), %eax" "\n\t"
             "movl %eax, %fs:0" "\n\t"
             "movl 0x8(%esp), %ebp" "\n\t"
             "add  $12, %esp");

    __asm__ __volatile__ (
             "movl %%esp, %0" "\n\t" : "=r" (RESTORED_ESP) );
    __asm__ __volatile__ (
             "movl %%ebp, %0" "\n\t" : "=r" (RESTORED_EBP) );
    __asm__ __volatile__ (
             "movl %%fs:0, %0" "\n\t" : "=r" (RESTORED_HANDLER) );

    if (INITIAL_ESP != RESTORED_ESP)
        exit(-1);
    if (INITIAL_EBP != RESTORED_EBP)
        exit(-1);
    if (INITIAL_HANDLER != RESTORED_HANDLER)
        exit(-1);

    printf( "Everything gonna be alright ...\n" );
    return 0;
}

__attribute__ ((cdecl))
EXCEPTION_DISPOSITION
_except_handler(
    struct _EXCEPTION_RECORD *ExceptionRecord,
    void * EstablisherFrame,
    struct _CONTEXT *ContextRecord,
    void * DispatcherContext )
{
    __asm__ __volatile__ (
             "jmp main_reentry");
    return 0; /* Function does not return */
}