Author Topic: my SEH doesn't work  (Read 12917 times)

Antariy

  • Member
  • ****
  • Posts: 551
Re: my SEH doesn't work
« Reply #15 on: December 03, 2012, 12:42:51 PM »
You could easily use those macroses in AxEH.inc in your programs :t

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: my SEH doesn't work
« Reply #16 on: December 03, 2012, 12:46:34 PM »
I would be surprised if the method used here would not work under Windows Vista/7.
Code: [Select]
;==============================================================================
; try.asm
;==============================================================================

LPEXCEPTION_POINTERS TYPEDEF PTR EXCEPTION_POINTERS

;-----------------------------------------------------------------
; This macro sets the top-level exception filter for the calling
; process to our try_filter procedure, so when an unhandled
; exception reaches the UnhandledExceptionFilter function, and
; the process is not being debugged, the function will call our
; procedure. The return_address parameter specifies the address
; of a label were execution will continue if an exception occurs.
; Use this macro to begin a guarded section of code.
;-----------------------------------------------------------------

try_begin MACRO return_address
    push return_address
    pop _exception_return_address_
    invoke SetUnhandledExceptionFilter, try_filter
    mov _previous_exception_filter_, eax
ENDM

;----------------------------------------------------------------
; This macro sets the top-level exception filter for the calling
; process to what it was before the try_begin macro changed it.
; Use this macro to end a guarded section of code.
;----------------------------------------------------------------

try_end MACRO
    invoke SetUnhandledExceptionFilter, _previous_exception_filter_
ENDM

;==============================================================================
    .data
        _exception_return_address_  dd 0
        _previous_exception_filter_ dd 0
        exception_record            EXCEPTION_RECORD <>
    .code
;==============================================================================

PEXCEPTION_RECORD TYPEDEF PTR EXCEPTION_RECORD

;-----------------------------------
; These not defined in windows.inc.
;-----------------------------------

EXCEPTION_NONCONTINUABLE_EXCEPTION  equ 0C0000025h
EXCEPTION_STACK_OVERFLOW            equ 0C00000FDh
EXCEPTION_INVALID_DISPOSITION       equ 0C0000026h
EXCEPTION_GUARD_PAGE                equ 080000001h
EXCEPTION_INVALID_HANDLE            equ 0C0000008h

ShowException proc uses ebx per:PEXCEPTION_RECORD
    mov ebx, per
    SWITCH [ebx].EXCEPTION_RECORD.ExceptionCode
        CASE EXCEPTION_ACCESS_VIOLATION
            printf("EXCEPTION_ACCESS_VIOLATION")
        CASE EXCEPTION_DATATYPE_MISALIGNMENT
            printf("EXCEPTION_DATATYPE_MISALIGNMENT")
        CASE EXCEPTION_BREAKPOINT
            printf("EXCEPTION_BREAKPOINT")
        CASE EXCEPTION_SINGLE_STEP
            printf("EXCEPTION_SINGLE_STEP")
        CASE EXCEPTION_ARRAY_BOUNDS_EXCEEDED
            printf("EXCEPTION_ARRAY_BOUNDS_EXCEEDED")
        CASE EXCEPTION_FLT_DENORMAL_OPERAND
            printf("EXCEPTION_FLT_DENORMAL_OPERAND")
        CASE EXCEPTION_FLT_DIVIDE_BY_ZERO
            printf("EXCEPTION_FLT_DIVIDE_BY_ZERO")
        CASE EXCEPTION_FLT_INEXACT_RESULT
            printf("EXCEPTION_FLT_INEXACT_RESULT")
        CASE EXCEPTION_FLT_INVALID_OPERATION
            printf("EXCEPTION_FLT_INVALID_OPERATION")
        CASE EXCEPTION_FLT_OVERFLOW
            printf("EXCEPTION_FLT_OVERFLOW")
        CASE EXCEPTION_FLT_STACK_CHECK
            printf("EXCEPTION_FLT_STACK_CHECK")
        CASE EXCEPTION_FLT_UNDERFLOW
            printf("EXCEPTION_FLT_UNDERFLOW")
        CASE EXCEPTION_INT_DIVIDE_BY_ZERO
            printf("EXCEPTION_INT_DIVIDE_BY_ZERO")
        CASE EXCEPTION_INT_OVERFLOW
            printf("EXCEPTION_INT_OVERFLOW")
        CASE EXCEPTION_PRIV_INSTRUCTION
            printf("EXCEPTION_PRIV_INSTRUCTION")
        CASE EXCEPTION_IN_PAGE_ERROR
            printf("EXCEPTION_IN_PAGE_ERROR")
        CASE EXCEPTION_ILLEGAL_INSTRUCTION
            printf("EXCEPTION_ILLEGAL_INSTRUCTION")
        CASE EXCEPTION_NONCONTINUABLE_EXCEPTION
            printf("EXCEPTION_NONCONTINUABLE_EXCEPTION")
        CASE EXCEPTION_STACK_OVERFLOW
            printf("EXCEPTION_STACK_OVERFLOW")
        CASE EXCEPTION_INVALID_DISPOSITION
            printf("EXCEPTION_INVALID_DISPOSITION")
        CASE EXCEPTION_GUARD_PAGE
            printf("EXCEPTION_GUARD_PAGE")
        CASE EXCEPTION_INVALID_HANDLE
            printf("EXCEPTION_INVALID_HANDLE")
        DEFAULT
            printf("exception code ")
            printf("%X",[ebx].EXCEPTION_RECORD.ExceptionCode)
    ENDSW
    printf(" @ %Xh\n",[ebx].EXCEPTION_RECORD.ExceptionAddress)
    ret
ShowException endp

;==============================================================================

;---------------------------------------------------
; This procedure is our top-level exception filter.
;---------------------------------------------------

try_filter proc lpexception_pointers:LPEXCEPTION_POINTERS

    ;-------------------------------------------------------------------------
    ; Copy the entire EXCEPTION_RECORD structure (80 bytes) to global memory.
    ;-------------------------------------------------------------------------

    mov edx, lpexception_pointers
    mov ecx, [edx].EXCEPTION_POINTERS.pExceptionRecord
    mov edx, OFFSET exception_record
    DISP=0
    REPEAT (SIZEOF EXCEPTION_RECORD) / 4
        mov eax, [ecx+DISP]
        mov [edx+DISP], eax
        DISP=DISP+4
    ENDM

    ;------------------------------------------------------------------
    ; Modify the EIP member of the CONTEXT structure so execution will
    ; continue at the address passed to the try_begin macro, instead
    ; of continuing at the point of the exception (which would cause
    ; the exception to repeat).
    ;------------------------------------------------------------------

    mov edx, lpexception_pointers
    mov eax, [edx].EXCEPTION_POINTERS.ContextRecord
    push _exception_return_address_
    pop [eax].CONTEXT.regEip

    invoke ShowException, ADDR exception_record

    ;------------------------------------------------------------------------
    ; Returning EXCEPTION_CONTINUE_EXECUTION to the UnhandledExceptionFilter
    ; function will cause it to return and continue execution from the point
    ; specified in the EIP member of the CONTEXT structure.
    ;------------------------------------------------------------------------

    return EXCEPTION_CONTINUE_EXECUTION

try_filter endp

;==============================================================================
Code: [Select]
;==============================================================================
include \masm32\include\masm32rt.inc
.686
include try.asm
;==============================================================================
.data
.code
;==============================================================================
start:
;==============================================================================

    ;--------------------------------------------------------------
    ; The try_ labels and the printf statements were added here to
    ; to verify that ExceptionAddress is being reported correctly,
    ; and the error handlers were placed inline as a convenience.
    ;--------------------------------------------------------------


    try_begin eh0
  try0:
    int 3
  eh0:
    printf("%Xh\n",try0)
  @@:
    try_end

    try_begin eh1
    mov ecx, 1
    mov edx, 10
  try1:
    div ecx
    jmp @F
  eh1:
    printf("%Xh\n",try1)
  @@:
    try_end

    try_begin eh2
    xor ecx, ecx
  try2:
    div ecx
    jmp @F
  eh2:
    printf("%Xh\n",try2)
  @@:
    try_end

    try_begin eh3
  try3:
    mov cr0, eax
    div ecx
    jmp @F
  eh3:
    printf("%Xh\n",try3)
  @@:
    try_end

    try_begin eh4
    xor eax, eax
  try4:
    mov DWORD PTR [eax], 1
    jmp @F
  eh4:
    printf("%Xh\n\n",try4)
  @@:
    try_end

    inkey
    exit
;==============================================================================
end start

Well Microsoft, here’s another nice mess you’ve gotten us into.

Antariy

  • Member
  • ****
  • Posts: 551
Re: my SEH doesn't work
« Reply #17 on: December 03, 2012, 01:12:51 PM »
Michael, I guess the C example you provided is "not working on Win7" just because it has some incompatibility with modern VC compilers that use many default flags which were not available at 1997, so it is not the flaws of the example/code but the build environemnt assumptions (LINK.EXE likes to make a choice instead of a programmer :biggrin:).

jj2007

  • Member
  • *****
  • Posts: 10094
  • Assembler is fun ;-)
    • MasmBasic
Re: my SEH doesn't work
« Reply #18 on: December 03, 2012, 06:54:59 PM »
Michael, I guess the C example you provided is "not working on Win7" just because it has some incompatibility with modern VC compilers that use many default flags which were not available at 1997, so it is not the flaws of the example/code but the build environemnt assumptions (LINK.EXE likes to make a choice instead of a programmer :biggrin:).

There are two SEH examples in the MasmBasic library (using plain old techniques) at
\Masm32\RichMasm\RES\SkelTryCatchDetailed.asc
\Masm32\RichMasm\RES\SkelTryCatch.asc
They both work fine on Win7-32 and Win7-64, with no particular linker settings, and tested with both polink and the old linker that comes with Masm32. So apparently, the problem is not Win7.

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: my SEH doesn't work
« Reply #19 on: December 03, 2012, 08:16:34 PM »
Yes, Alex is right.

Good to see you back here, Alex.  :t

Gunther
Get your facts first, and then you can distort them.

Antariy

  • Member
  • ****
  • Posts: 551
Re: my SEH doesn't work
« Reply #20 on: December 04, 2012, 05:29:40 PM »
Thank you, Gunther! :biggrin: :t

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: my SEH doesn't work
« Reply #21 on: December 04, 2012, 08:41:36 PM »
Alex, you're always welcome.

Gunther
Get your facts first, and then you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8825
  • Still using Abacus 2.0
    • DednDave
Re: my SEH doesn't work
« Reply #22 on: December 05, 2012, 07:24:00 AM »
yes - good to see you, Alex   :t
i had forgotten about your SEH macros   :P