flat assembler
Message board for the users of flat assembler.

Index > Windows > SEH/Hardware Breakpoint example

Author
Thread Post new topic Reply to topic
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 15 Dec 2006, 19:30
One of my submissions for the expansion of the FASM examples, I was asked to create a separate thread.

This creates an exception handler and goes through various exceptions, showing how to return to normal execution from them and displaying the exception info/register values (as a debugger would). The last and fatal exception is via a hardware breakpoint with the Debug registers.

Original thread: http://board.flatassembler.net/topic.php?p=49442#49442


Description:
Download
Filename: seh_example.zip
Filesize: 3.5 KB
Downloaded: 447 Time(s)


_________________
redghost.ca
Post 15 Dec 2006, 19:30
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav 16 Dec 2006, 10:15
here is my approach to seh, little different and formed as macro

ps. there are two types of seh: unhandled and handled (with safe place definition)
ps. fasm project can be build in winasm and in fasm
ps. resources can be compiled with microsoft resource compiler


Description:
Download
Filename: seh_vv.rar
Filesize: 10.33 KB
Downloaded: 437 Time(s)


_________________
[not enough memory]
Post 16 Dec 2006, 10:15
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 16 Dec 2006, 17:20
Vasilev Vjacheslav wrote:
here is my approach to seh, little different and formed as macro

ps. there are two types of seh: unhandled and handled (with safe place definition)
ps. fasm project can be build in winasm and in fasm
ps. resources can be compiled with microsoft resource compiler


I actually did something like this in macros a long time ago but never posted them, since my preprocessor skills aren't that good.

Here is the syntax:
Code:
__try
    ; code here is safe from crashing your programme
    ; you can use "__throw <code>" here to throw an exception with a custom exception code
    ; e.g.
    ;     __throw -1
    ;
__catch
   ; here we are handling an exception (if any)
   ;
   ; "ExceptionRecord" is defined as a pointer to an EXCEPTION_RECORD structre
   ; "ContextRecord"   is defined as a pointer to an CONTEXT structure
   ;
   ; e.g. mov eax, ExceptionRecord
   ;      mov ebx, [eax+EXCEPTION_RECORD.ExceptionAddress]

   ; you can make modifications to the CONTEXT to try and fix the exception

   ; __except_code gets the exception code in eax
   ;
   ; e.g.
   ;     __except_code
   ;     cmp eax, EXCEPTION_BREAKPOINT

   ; "__finished <return type>" is the only way to leave this handler
   ;
   ; e.g.
   ;     __finished ExceptionContinueExecution ; see the the code for all
   ;
__endtry ; here we complete the statement
    


I tested quickly with compiling and the disassembly, it seemed to work fine, but it doesn't support nested __try/__catch blocks.

Here is the code:
Code:
ExceptionContinueExecution = 0
ExceptionContinueSearch    = 1
ExceptionNestedException   = 2
ExceptionCollidedUnwind    = 3
ExceptionExecuteHandler    = 4

macro __try  {
    common

    __trying equ

    local ..handler
     __handler equ ..handler

   local ..endhandler
    __endhandler equ ..endhandler

    push   __handler
    push   dword [fs:$0]
    mov    dword [fs:$0], esp
}
;---

macro __catch {
    common

    __catching equ

    if __trying eq
        restore __trying

        mov    eax, [esp]
        mov    [fs:$0], eax
        add    esp, $8

        jmp __endhandler

        __handler:
            push  ebp
            mov   ebp, esp

            ExceptionRecord equ [ebp+$8]
            ContextRecord   equ [ebp+$10]
    else
        display '__catch without __try'
    end if
}
;---

macro __finished type {
    common

    if __catching  eq
        mov    eax, type
        mov    esp, ebp
        pop    ebp
        ret
    else
        display '__finished without __catch'
    end if
}
;---

macro __endtry {
    common

    if __catching eq
        restore __catching

        restore ExceptionRecord
        restore ContextRecord

        __endhandler:
        restore __endhandler
    else
        display '__endtry without __catch'
    end if
}
;---

macro __except_code {
    mov   eax, dword [ebp+$8]
    mov   eax, dword [eax+EXCEPTION_RECORD.ExceptionCode]
}
;---

; requires import, define CAN_THROW
if defined CAN_THROW
    macro __throw code {
        push  code
        push  $0
        push  $0
        push  $0
        call  [RaiseException]
    }
end if
;---
    

_________________
redghost.ca
Post 16 Dec 2006, 17:20
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.