Ah you didn't mention you were mucking around patching drivers, I shoulda known Elenil
I don't know if you can use simple SEH for that. The WDK states that you must wrap a try/except block around driver code which might generate an exception. From the sounds of it, an
access violation, such as writing to a read-only page, will still generate a BSOD, even with an exception handler in place. I think that's what the guy in OSR was getting at.
Take a look at Four-F's MASM KMD docs, he may have written some try/except macros you can use.
Here is what a basic try/except block looks like as generated by VC6++. It is of a scopetable type like mentioned above.
Basic try/except block wrapped around a simple inline _asm{nop}:
Code:
void TestSEH()
{
__try {
_asm{nop}
} __except (EXCEPTION_EXECUTE_HANDLER) {
DbgPrint ("ERROR ExceptionCode: %x\n", GetExceptionCode() );
} // end __try{
}
And here is the disassembly with the ms_ehseh.idc IDC script by igorsk applied and the scopetable functions named:
Code:
:00011000 TestSEH proc near ; CODE XREF: DeviceControl+AC
:00011000
:00011000 var_1C = dword ptr -1Ch
:00011000 __$SEHRec$ = SEHRegistrationNode ptr -18h
:00011000
:00011000 push ebp
:00011001 mov ebp, esp
:00011003 push 0FFFFFFFFh
:00011005 push offset _func1_scopetable
:0001100A push offset __except_handler3
:0001100F mov eax, large fs:0
:00011015 push eax
:00011016 mov large fs:0, esp
:0001101D sub esp, 0Ch
:00011020 push ebx
:00011021 push esi
:00011022 push edi
:00011023 mov [ebp+__$SEHRec$.SavedESP], esp
:00011026 and [ebp+__$SEHRec$.TryLevel], 0
:0001102A nop
:0001102B jmp short _endoftry0
:0001102D ; ---------------------------------------------------------------------------
:0001102D ; __except() filter for try block 0
:0001102D
:0001102D FilterFunc: ; DATA XREF: .rdata:0001206C
:0001102D mov eax, [ebp+__$SEHRec$.ExceptionPointers]
:00011030 mov eax, [eax]
:00011032 mov eax, [eax]
:00011034 mov [ebp+var_1C], eax
:00011037 xor eax, eax
:00011039 inc eax
:0001103A retn
:0001103B ; ---------------------------------------------------------------------------
:0001103B ; __except {} handler for try block 0
:0001103B
:0001103B HandlerFunc: ; DATA XREF: .rdata:00012070
:0001103B mov esp, [ebp+__$SEHRec$.SavedESP]
:0001103E push [ebp+var_1C]
:00011041 push offset aErrorExceptioncodeX ; "ERROR ExceptionCode: %x\n"
:00011046 call _DbgPrint
:0001104B pop ecx
:0001104C pop ecx
:0001104D
:0001104D _endoftry0: ; CODE XREF: TestSEH+2B
:0001104D or [ebp+__$SEHRec$.TryLevel], 0FFFFFFFFh
:00011051 mov ecx, [ebp+__$SEHRec$.Next]
:00011054 mov large fs:0, ecx
:0001105B pop edi
:0001105C pop esi
:0001105D pop ebx
:0001105E leave
:0001105F retn
:0001105F TestSEH endp
Here is what the scopetable looks like:
Code:
.rdata:00012068 _func1_scopetable dd 0FFFFFFFFh ; DATA XREF: TestSEH+5
.rdata:00012068 ; try block 0, enclosed by -1
.rdata:00012068 ; EnclosingLevel
.rdata:0001206C dd offset FilterFunc ; FilterFunc
.rdata:00012070 dd offset HandlerFunc ; HandlerFunc
So, you've got a bit of work to do if you want to emulate a try/except block in a driver patch.
Kayaker