News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How do you trap a access denied error (memory) in Windows?

Started by CCurl, November 26, 2015, 02:14:56 PM

Previous topic - Next topic

CCurl

I have a program that allows the user to ask for the value at any address, valid or invalid. So sometimes it can try to retrieve the value at an address the program does not have access to. An error is thrown ... how do I catch and handle such errors?

dedndave

SEH - Structured Exception Handling

here's an example by Alex - i think Michael did one a while back, as well

http://masm32.com/board/index.php?topic=350.0


jj2007

include \masm32\MasmBasic\MasmBasic.inc
  Init tc, console      ; initialise MasmBasic with Try/Catch (aka SEH), and output errors to console
  Let esi="400000h"
  .While 1
      Let esi=Input$("Type your address: ", esi)
      .Break .if Len(esi)==0
      Try
            mov ecx, [Val(esi)]
            Print Str$("The value is %i\n", ecx)
      Catch
            PrintLine Str$("Ouch, we had an exception in source line %i at address ", LastEx(line)), Hex$(LastEx(addr)), ", code ", Hex$(LastEx(code)), CrLf$
            PrintLine "Windows says:", CrLf$, LastEx(info)
      Finally
  .Endw
  Print "bye"
EndOfCode


Output:
Type your address: 400000h
The value is 9460301
Type your address: 40000h
The value is 2
Type your address: 4000h
Ouch, we had an exception in source line 7 at address 004010AE, code C0000005

Windows says:
L'istruzione a 004010AE ha fatto riferimento alla memoria a 00004000. La memoria non poteva essere read.

Type your address: 10000h
The value is 0
Type your address: 10000h


Your Windows error message will be in the language of your OS, of course. Besides, you will see source line 0 if you launch the exe; the indication of the source line works only if you build the source with RichMasm.

TouEnMasm

fonction to test a pointer,0 access denied


isGoodPointer PROC uses ebx ecx edx pchain:DWORD
Local memory_basic_information:MEMORY_BASIC_INFORMATION
Local retour:DWORD

invoke VirtualQuery,pchain,addr memory_basic_information,sizeof memory_basic_information
.if eax == 0
mov retour,0
.else

mov eax,memory_basic_information.AllocationProtect
.if eax == 0
mov retour,0
jmp FindeisGoodPointer
.endif
and eax,PAGE_NOACCESS
.if eax != 0
mov retour,0
jmp FindeisGoodPointer
.endif
mov eax,memory_basic_information.AllocationProtect
and eax,PAGE_READONLY
.if eax != 0
mov retour,0
jmp FindeisGoodPointer
.endif
mov retour,1
.endif

FindeisGoodPointer:
mov eax,retour
ret
isGoodPointer endp



version tested on dll to see if it is possible to read the exports functions

Autorise PROC  pchain:DWORD
Local mem_basic_inf:MEMORY_BASIC_INFORMATION
Local  retour:DWORD
      mov retour,0
mov edx,pchain
invoke VirtualQuery,pchain,addr mem_basic_inf,sizeof mem_basic_inf
.if eax != 0
mov eax,mem_basic_inf.AllocationProtect
.if eax == 0
jmp fin
.endif
and eax,PAGE_NOACCESS
jnz fin
mov eax,mem_basic_inf.AllocationProtect
and eax,PAGE_EXECUTE
jnz fin
mov eax,mem_basic_inf.AllocationProtect
and eax,PAGE_GUARD
jnz fin
mov retour,1
.endif 
fin:
FindeAutorise:
         mov eax,retour
         ret
Autorise endp






Fa is a musical note to play with CL

CCurl

Thanks folks. It has been a busy thanksgiving .. i hope to try this soon.