The MASM Forum

General => The Campus => Topic started by: CCurl on November 26, 2015, 02:14:56 PM

Title: How do you trap a access denied error (memory) in Windows?
Post by: CCurl on November 26, 2015, 02:14:56 PM
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?
Title: Re: How do you trap a access denied error (memory) in Windows?
Post by: dedndave on November 26, 2015, 03:40:12 PM
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 (http://masm32.com/board/index.php?topic=350.0)
Title: Re: How do you trap a access denied error (memory) in Windows?
Post by: dedndave on November 26, 2015, 03:42:02 PM
MSDN page...

https://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx (https://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx)
Title: Re: How do you trap a access denied error (memory) in Windows?
Post by: jj2007 on November 26, 2015, 08:01:02 PM
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.
Title: Re: How do you trap a access denied error (memory) in Windows?
Post by: TouEnMasm on November 27, 2015, 03:31:05 AM
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






Title: Re: How do you trap a access denied error (memory) in Windows?
Post by: CCurl on November 29, 2015, 04:28:49 PM
Thanks folks. It has been a busy thanksgiving .. i hope to try this soon.