The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Astro on February 25, 2011, 02:30:10 AM

Title: SEH Problem
Post by: Astro on February 25, 2011, 02:30:10 AM
Hi,

The following is strange, and I'm too tired/stressed (unrelated) to see quite what is going on with it.

I've commented the code where the problem is, but I just can't quite figure out why it is behaving the way it is. I can only presume that my code is generating a (genuine) exception, resulting in the exception handler being fired again, ad infinitum?

Seems the instruction rdmsr can only be called from Ring 0 which is useless anyway, but if it was to work, only works on certain processors. My idea was to catch the error (meaning it is not supported on that processor) and just say that the instruction is not supported (but that is a side-issue anyway - I ultimately wanted to try and read the DTR register of Intel processors but it seems I need to be in Ring 0 to do this).

Note that when it errors, it prints the error handler message once in the console.

Build as a console app.

include \masm32\include\masm32rt.inc

.686p
option casemap:none

.code

Error db "The instruction is not supported by the processor.",0

start:

    ;=================================================
    ; Configure SEH

    assume fs:nothing
    lea eax,ERR
    push eax
    push fs:[0]
    mov fs:[0],esp

    ;=================================================
    ; Protected code

    mov ecx,019ch ; select IA32_THERM_STATUS register
    rdmsr       ; read manufacturer specific data - edx:eax contains 64-bit data

    ;=================================================
    ; Restore error handler

    pop fs:[0]
    add esp,4

    xor eax,eax
    ret

ERR:
    ; Adding the next 2 lines results in exception c000001d (EXCEPTION_ILLEGAL_INSTRUCTION)
    ; Without, it loops the error message forever
    ;pop fs:[0]
    ;add esp,4

    lea eax,Error
    print eax

    xor eax,eax
    ret

end start


Best regards,
Robin.
Title: Re: SEH Problem
Post by: drizz on February 25, 2011, 02:37:38 AM
Inside your seh handler you return ExceptionContinueExecution(=0) without modifying EIP.
Title: Re: SEH Problem
Post by: Antariy on February 25, 2011, 02:43:12 AM
Change SE Handler as:


ERR:
    ; Adding the next 2 lines results in exception c000001d (EXCEPTION_ILLEGAL_INSTRUCTION)
    ; Without, it loops the error message forever
    ;pop fs:[0]
    ;add esp,4

    lea eax,Error
    print eax
   
    mov ecx,[esp+12]
    add dword ptr [ecx].CONTEXT.regEip,2 ; step over exceptionee instruction rdmsr

    xor eax,eax
    ret
Title: Re: SEH Problem
Post by: Astro on February 25, 2011, 03:14:36 AM
Hi,

Quote from: Antariy on February 25, 2011, 02:43:12 AMChange SE Handler as:

I just tried this - it loops the error handler multiple times then crashes with memory access violation.

Best regards,
Robin.
Title: Re: SEH Problem
Post by: Antariy on February 25, 2011, 03:29:20 AM
Quote from: Astro on February 25, 2011, 03:14:36 AM
I just tried this - it loops the error handler multiple times then crashes with memory access violation.

It works for me. Just the same as full

include \masm32\include\masm32rt.inc

.686p
option casemap:none

.code

Error db "The instruction is not supported by the processor.",0

start:

    ;=================================================
    ; Configure SEH

    assume fs:nothing
    lea eax,ERR
    push eax
    push fs:[0]
    mov fs:[0],esp

    ;=================================================
    ; Protected code

    mov ecx,019ch ; select IA32_THERM_STATUS register
    rdmsr       ; read manufacturer specific data - edx:eax contains 64-bit data

    ;=================================================
    ; Restore error handler

    pop fs:[0]
    add esp,4

    xor eax,eax
    ret

ERR:
    ; Adding the next 2 lines results in exception c000001d (EXCEPTION_ILLEGAL_INSTRUCTION)
    ; Without, it loops the error message forever
    ;pop fs:[0]
    ;add esp,4

    lea eax,Error
    print eax
   
    mov ecx,[esp+12]
    add dword ptr [ecx].CONTEXT.regEip,2

    xor eax,eax
    ret

end start

Title: Re: SEH Problem
Post by: Astro on February 25, 2011, 03:42:21 AM
Ahh ha! Found it. :)

ERR:
    ; Adding the next 2 lines results in exception c000001d (EXCEPTION_ILLEGAL_INSTRUCTION)
    ; Without, it loops the error message forever
    ;pop fs:[0]
    ;add esp,4

    lea eax,Error
    print eax

    mov ecx,[esp+12]
    add dword ptr [ecx].CONTEXT.regEip,2
    xor eax,eax ; Failure to set eax == 0 causes the error handler to be called again <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    ret


Best regards,
Robin.
Title: Re: SEH Problem
Post by: clive on February 26, 2011, 12:41:58 AM
To read MSR's and determine which were valid/invalid, I wrote a kernel mode device driver, and used the SEH to catch the ones that faulted. The user mode app could load the kernel driver, and call it, this probably can't be done like that these days.
Title: Re: SEH Problem
Post by: Antariy on February 26, 2011, 12:51:58 AM
Quote from: clive on February 26, 2011, 12:41:58 AM
To read MSR's and determine which were valid/invalid, I wrote a kernel mode device driver, and used the SEH to catch the ones that faulted. The user mode app could load the kernel driver, and call it, this probably can't be done like that these days.

BTW, is Win7 allows loading of the drivers by non-admin user? Is it requires to have drivers signed in general?
Title: Re: SEH Problem
Post by: clive on February 26, 2011, 12:56:08 AM
Quote from: Antariy on February 26, 2011, 12:51:58 AM
BTW, is Win7 allows loading of the drivers by non-admin user? Is it requires to have drivers signed in general?

Yeah, a whole load of inconvenience I really don't have time for any more.

http://www.masm32.com/board/index.php?topic=13752.msg108107#msg108107

http://www.masm32.com/board/index.php?topic=13686.msg107661#msg107661
Title: Re: SEH Problem
Post by: Antariy on February 26, 2011, 02:42:16 PM
Quote from: clive on February 26, 2011, 12:56:08 AM
Quote from: Antariy on February 26, 2011, 12:51:58 AM
BTW, is Win7 allows loading of the drivers by non-admin user? Is it requires to have drivers signed in general?

Yeah, a whole load of inconvenience I really don't have time for any more.

http://www.masm32.com/board/index.php?topic=13752.msg108107#msg108107

http://www.masm32.com/board/index.php?topic=13686.msg107661#msg107661

Interesting links, thank you :thumbu