News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

SEH

Started by ecube, March 18, 2009, 08:59:54 PM

Previous topic - Next topic

ecube

can I use seh macros through my program to prevent any crashes? and are there any big disadvantages?

ToutEnMasm


Hello,
Quote
there any big disadvantages?
Not,you don't lose time or other things.They are more useful when writing the code.
When the code is finish and tested they aren't very usefull.

Tedd

There's a small overhead for setting up each handler (one for each procedure) but that's about all.
But if you care enough to catch the exceptions, then it shouldn't be an issue.

They are still useful after development, unless you assume no-one will ever give your program bad input, or nothing will ever go wrong.
No snowflake in an avalanche feels responsible.

BlackVortex

Are there any macros like that in the masm32 package ?  Couldn't find any. Or some examples ?

I'm looking for something hassle-free, like the try/except/finally directives of some HLL languages.

ecube

seh.inc


sSEH STRUCT
OrgEsp            DD ?
OrgEbp            DD ?
SaveEip           DD ?
sSEH ENDS

;---- MACROs ----
InstSehFrame MACRO ContinueAddr
ASSUME FS : NOTHING

   IFNDEF  SehStruct
        SehStruct EQU 1
      .DATA
      SEH    sSEH <>
   ENDIF
   
.CODE
MOV  SEH.SaveEip, ContinueAddr
MOV  SEH.OrgEbp, EBP
PUSH OFFSET SehHandler
PUSH FS:[0]
MOV  SEH.OrgEsp, ESP
MOV  FS:[0], ESP
ENDM

KillSehFrame MACRO
POP  FS:[0]
ADD  ESP, 4
ENDM

.CODE
SehHandler PROC C pExcept:DWORD,pFrame:DWORD,pContext:DWORD,pDispatch:DWORD

MOV  EAX, pContext
ASSUME EAX : PTR CONTEXT

PUSH SEH.SaveEip
POP  [EAX].regEip
PUSH SEH.OrgEsp
POP  [EAX].regEsp
PUSH SEH.OrgEbp
POP [EAX].regEbp

MOV  EAX, ExceptionContinueExecution

RET
SehHandler ENDP



usage
InstSehFrame <OFFSET not_under_VMware>
push   edx
      push   ecx
      push   ebx

      mov    eax, 'VMXh'
      mov    ebx, 0         ; any value but not the MAGIC VALUE
      mov    ecx, 10        ; get VMWare version
      mov    edx, 'VX'      ; port number

      in     eax, dx        ; read port
                            ; on return EAX returns the VERSION
      cmp    ebx, 'VMXh'    ; is it a reply from VMWare?
      jne  not_under_VMware ; set return value

      pop    ebx
      pop    ecx
      pop    edx
KillSehFrame
    ;Vmware found

not_under_VMware:       
KillSehFrame


also http://www.rohitab.com/sourcecode/seh.html

the code I posted above I forgot where i got

BlackVortex

Awesome cube, nice finds there. Just used the first one and worked great the first time. Also, no registers are changed after setting a new handler !