News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

How to detect exception?

Started by x64Core, June 20, 2012, 05:35:29 PM

Previous topic - Next topic

x64Core

Hello guys, I would like know how to do a exception detect, for example:

pointer dd 20
.code
start:

mov eax,pointer
mov edx,50
mov [eax],edx


this code would give a error, in C/C++ is very simple ( with __try statement ) but in assembler?
thanks :D

jj2007

In assembler, it is also very simple:

include \masm32\MasmBasic\MasmBasic.inc   ; download
.data
   pointer dd 20

   Init tc   ; Init with Try/Catch
   mov eax, pointer
   mov edx, 50
  Try
   mov [eax], edx
  Catch only   ; pass here only if there was an exception
   Print "That was illegal, my friend - "
  Finally
   Inkey "ok?"
   Exit
   TryCatchEnd   ; last instruction
end start

NoCforMe

Ah, yes, but how does one do it if one doesn't want to use MASM Basic? (No offense, you understand, JJ: I know it's a marvelous creation, I just have no interest in learning it.)

x64Core

wow
I it disassemble and is much code lol also it's detected as virus :P
any alternative , mate? :P

Farabi

Looks simple, but I never tried it.



ASSUME FS:NOTHING;MASM assumes the use of this register to be ERROR by default
  PUSH  OFFSET YourExceptionHandler
  PUSH  FS:[0]
  MOV  FS:[0], ESP

; Guarded code here.


;Here is how to uninstall it
;------------------------------------------------------------------------------
;UNINSTALL PER-THREAD EH
;------------------------------------------------------------------------------

  POP FS:[0]
  ADD ESP,4


http://www.winasm.net/forum/index.php?showtopic=2082
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

jj2007

Quote from: RHL on June 20, 2012, 06:13:07 PM
wow
I it disassemble and is much code lol also it's detected as virus :P
any alternative , mate? :P

The alternative is to study EXCEPTION_POINTERS and EXCEPTION_RECORD and SetUnhandledException and the like.

Re virus: submit the exe to virusscan.jotti.org/
IMHO any decent self-rolled assembly proggie should trigger at least one false positive there, but I am afraid Jotti found nothing for my executable :redface:

Quote from: NoCforMe on June 20, 2012, 06:03:50 PM
Ah, yes, but how does one do it if one doesn't want to use MASM Basic? (No offense, you understand, JJ: I know it's a marvelous creation, I just have no interest in learning it.)

Strange ::)
Y'know, MasmBasic is made explicitly for people whose credo is "no C for me" :greensml:

dedndave

Farabi left out an important part of that code - the handler, itself   :P
  .CODE
;------------------------------------------------------------------------------
;INSTALL PER-THREAD EH
;------------------------------------------------------------------------------

  ASSUME FS:NOTHING;MASM assumes the use of this register to be ERROR by default
  PUSH  OFFSET PTExceptionHandler
  PUSH  FS:[0]
  MOV  FS:[0], ESP

;------------------------------------------------------------------------------
;THE CODE BETWEEN INSTALLATION & DE-INSTALLATION IS GUARDED BY THE  PER-THREAD EH
;------------------------------------------------------------------------------

;MORE GUARDED CODE HERE

;Generate an exception inside the guarded area
  INT 3;EXCEPTION BREAKPOINT

SafeOffset:

;MORE GUARDED CODE HERE

;------------------------------------------------------------------------------
;UNINSTALL PER-THREAD EH
;------------------------------------------------------------------------------

  POP FS:[0]
  ADD ESP,4

;------------------------------------------------------------------------------
;THE CODE HERE IS NOT GUARDED BY THE PER-THREAD EH
;------------------------------------------------------------------------------

;MORE UNGUARDED CODE HERE

  INVOKE ExitProcess, NULL

;------------------------------------------------------------------------------
PTExceptionHandler PROC C pExcept:DWORD, pFrame:DWORD, pContext:DWORD, pDispatch:DWORD
  MOV EAX, pContext
  MOV [EAX].CONTEXT.regEip, OFFSET SafeOffset
  MOV EAX,ExceptionContinueExecution
  RET
PTExceptionHandler ENDP
;------------------------------------------------------------------------------

x64Core

thank you very much, guys  is very simple - also  :biggrin:

ed:

I can't: :( lol
start:

  ASSUME FS:NOTHING
  PUSH  OFFSET SafeOffset
  PUSH  FS:[0]
  MOV  FS:[0], ESP

mov eax,0
mov edx,5

  mov [eax],edx <--- no jump
mov eax,1
SafeOffset:

  POP FS:[0]
  ADD ESP,4

  INVOKE ExitProcess, NULL
end start

NoCforMe

Quote from: jj2007 on June 21, 2012, 01:10:26 AM
Quote from: NoCforMe on June 20, 2012, 06:03:50 PM
Ah, yes, but how does one do it if one doesn't want to use MASM Basic? (No offense, you understand, JJ: I know it's a marvelous creation, I just have no interest in learning it.)

Strange ::)
Y'know, MasmBasic is made explicitly for people whose credo is "no C for me" :greensml:

Well, since you brought it up, no, not strange at all: I don't like using C, and I don't like using BASIC either. (Funny, BASIC was the first programming language I learned, followed by COBOL.) My preference is (more or less) "pure" '86 assembly language. (I don't like using macros either, although I used to use them back in the good old 16-bit DOS days.)

As I said, I don't mean to take anything away from your marvelous creation, and I'm glad it's there for folks who want to use it. It's just not for me (different strokes for different folks and all that). Which is why I'd like to know the underlying lower-level implementation of "catch" and "try", etc.

[If anyone wants to continue this tangential discussion, what forum here would be the appropriate one for discussing such "religious" topics?]

To return back on-topic, I'm guessing that some answers to the OP's question lie beneath this MSDN page on structured exception handling.

dedndave

MasmBasic is very fast - Jochen is a good coder, and has done a lot of work on it
i don't use it much at the moment, because i am trying to learn the win32 API
MasmBasic is a layer of abstraction from that goal
but - i intend to use it more in the future, after i have learned what i want

NoCforMe

Getting back to the original question, it looks as if handing exceptions can be easily done using WinAPI functions. (I haven't actually used them, so if someone knows that I'm wrong about this, please let us know.)

It looks as if you need simply to use AddVectoredExceptionHandler to add your own exception handler (defined as a callback function). This function will then be called in the event of an exception  (hardware or software).

Is it really that simple?

jj2007

Quote from: NoCforMe on June 21, 2012, 05:55:19 AM
Is it really that simple?

Well, yes and no. You might have a look at \masm32\MasmBasic\MasmBasic.inc - search for TryCatchEnd MACRO and Init MACRO.

The handler itself is not that difficult, it's more the decisions what to do in case of an exception, where to jump etc etc - all that is "under the hood" of that simple Try/Catch sequence. Which, by the way, looks like C but came to my knowledge with GfaBasic around 1987.

Re MasmBasic: I am just teasing you a little bit ;)
Although one useful bit to know is that most proggies that start with
  include \masm32\include\masm32rt.inc
work just fine if you replace that line with
  include \masm32\MasmBasic\MasmBasic.inc
... with the difference that you have the deb macro at your fingertips ;-)

dedndave

Jeremy Gordon has a very impressive write-up with examples on his GoTools site   :t

NoCforMe

Quote from: jj2007 on June 21, 2012, 06:25:02 AM
Quote from: NoCforMe on June 21, 2012, 05:55:19 AM
Is it really that simple?

Well, yes and no. You might have a look at \masm32\MasmBasic\MasmBasic.inc - search for TryCatchEnd MACRO and Init MACRO.

Which raises an interesting question: why don't I have masm32\MasmBasic\* on my computah? I just recently upgraded my MASM32 installation: how come I didn't get your Basic stuff? Is that an option I didn't check or something?

jj2007

Quote from: NoCforMe on June 21, 2012, 11:13:03 AMwhy don't I have masm32\MasmBasic\* on my computah? I just recently upgraded my MASM32...

It's a shame, really :eusa_boohoo:
Masm32 v17 will come with Masmbasic and Windows RG preinstalled. Right, Hutch?