Delphi - SEH 검토

7024 단어 Delphi
,DH . NP , .
SEH .
, .
SEH Structure Exception Handling. , MSDN.
MSDN , . .
Delphi try..except..end . .
Procedure TForm1.Button1Click( Sender: TObject );
Var
	a: TForm1;
Begin
	a := Nil;
	Try
		a.Show;
	Except
		ShowMessage( '1111' );
	End;
End;

, , ctrl + alt + c CPU .
Unit1.pas.38: a := Nil;
00452702 33C0 xor eax,eax
Unit1.pas.39: Try
00452704 33D2 xor edx,edx
00452706 55 push ebp
00452707 6821274500 push $00452721
0045270C 64FF32 push dword ptr fs:[edx]
0045270F 648922 mov fs:[edx],esp
Unit1.pas.40: a.Show;
00452712 E811B3FFFF call TCustomForm.Show
00452717 33C0 xor eax,eax
00452719 5A pop edx
0045271A 59 pop ecx
0045271B 59 pop ecx
0045271C 648910 mov fs:[eax],edx
0045271F EB14 jmp +$14
00452721 E91E12FBFF jmp @HandleAnyException
Unit1.pas.42: ShowMessage( '1111' );
00452726 B844274500 mov eax,$00452744
0045272B E85C8DFDFF call ShowMessage
00452730 E87715FBFF call @DoneExcept
Unit1.pas.44: End;


try . .

Procedure SetExceptionProc( Proc: Pointer );
Asm
	//           
	push eax
	//          
	push fs:[0]
	mov fs:[0],esp
End;

Delphi Try .
? , .
(by Jeremy Gordon):
   1. , , ,
   EXCEPTION_DEBUG_EVENT . , ?
   2. , ,
   , seh , .
   3. , ,
.
   4. , , debugger.
   5. debugger , SetUnhandledExceptionFilter
   , .
   6. , , ,
   . ,
   ExitProcess .
   7. , , .
, , , seh ,
, , stack, ,
, , ,
:pExcept:DWORD,pErr:DWORD,pContext:DWORD,pDispatch :
pExcept: --- EXCEPTION_RECORD
pErr: --- ERR
pContext: --- CONTEXT , .
pDispatch:---
Delphi , .
.

Function ExceptionProc( pExcept: PExceptionRecord;pError: Pointer;pContxt: PContext;pDispatch: Pointer ): Integer; Stdcall;
Begin
	ShowMessage( '1111' );
	Result:=0;
End;

stdcall , cdecl. .
.
Win32 —— ( TEB TIB)。
Windows NT、Windows 95、Win32s OS/2 。
TIB DWORD EXCEPTION_REGISTRATION 。
Intel Win32 ,FS TIB。
, FS:[0] , EXCEPTION_REGISTRATION 。
SetExceptionProc .
:

Procedure TestException( );
Begin
	//try
	SetExceptionProc( @ExceptionProc );
	//make a exception
	Asm
		xor edx,edx
		mov [edx],0
	End;
	//    
	Asm
		pop eax
		mov fs:[0],eax
		add esp,8
	End;
End;
Procedure TForm1.Button2Click( Sender: TObject );
Begin
	TestException;
End;


.

CSDN .

Program Project2;
{$APPTYPE CONSOLE}
Uses
	SysUtils,
	Windows;
Type
	PExecption_Handler= ^Exception_Handler;
	PException_Registration= ^Exception_Registration;
	_ExceptionHandler= Record
		ExceptionRecord: PExceptionRecord;
		SEH: PException_Registration;
		Context: PContext;
		DispatcherContext: Pointer;
	End;
	Exception_Handler= _ExceptionHandler;
	_ExceptionRegistration= Record
		Prev: PException_Registration;
		Handler: PExecption_Handler;
	End;
	Exception_Registration= _ExceptionRegistration;
Const
	EXCEPTION_CONTINUE_EXECUTION= 0; ///  CONTEXT       ,    
	EXCEPTION_CONTINUE_SEARCH= 1; ///        ,           
	EXCEPTION_NESTED_EXCEPTION= 2; ///          
	EXCEPTION_COLLIDED_UNWIND= 3; ///         
	EH_NONE= 0;
	EH_NONCONTINUABLE= 1;
	EH_UNWINDING= 2;
	EH_EXIT_UNWIND= 4;
	EH_STACK_INVALID= 8;
	EH_NESTED_CALL= 16;
	STATUS_ACCESS_VIOLATION= $C0000005; ///      
	STATUS_ARRAY_BOUNDS_EXCEEDED= $C000008C;
	STATUS_FLOAT_DENORMAL_OPERAND= $C000008D;
	STATUS_FLOAT_DIVIDE_BY_ZERO= $C000008E;
	STATUS_FLOAT_INEXACT_RESULT= $C000008F;
	STATUS_FLOAT_INVALID_OPERATION= $C0000090;
	STATUS_FLOAT_OVERFLOW= $C0000091;
	STATUS_FLOAT_STACK_CHECK= $C0000092;
	STATUS_FLOAT_UNDERFLOW= $C0000093;
	STATUS_INTEGER_DIVIDE_BY_ZERO= $C0000094; /// 0  
	STATUS_INTEGER_OVERFLOW= $C0000095;
	STATUS_PRIVILEGED_INSTRUCTION= $C0000096;
	STATUS_STACK_OVERFLOW= $C00000FD;
	STATUS_CONTROL_C_EXIT= $C000013A;
Var
	G_TEST: DWORD;
Procedure Log( LogMsg: String );
Begin
	Writeln( LogMsg );
End;
//       ,          ,           ExceptionRegistration,        
Function ExceptionHandler( ExceptionHandler: EXCEPTION_HANDLER ): LongInt; Cdecl;
Begin
	Result := EXCEPTION_CONTINUE_SEARCH;
	If ExceptionHandler.ExceptionRecord.ExceptionFlags= EH_NONE Then
	Begin
		Case ExceptionHandler.ExceptionRecord.ExceptionCode Of
			STATUS_ACCESS_VIOLATION:
			Begin
				Log( '           ,    EBX,    ' );
				ExceptionHandler.Context.Ebx := DWORD( @G_TEST );
				Result := EXCEPTION_CONTINUE_EXECUTION;
			End;
			Else
				Log( '         ,       ' );
			End;
	End
	Else
	If ExceptionHandler.ExceptionRecord.ExceptionFlags= EH_UNWINDING Then
		Log( '      ' );
	End;

Begin
	Asm
	///  SEH
		XOR EAX, EAX
		PUSH OFFSET ExceptionHandler
		PUSH FS:[EAX]
		MOV FS:[EAX], ESP
	///        
		XOR EBX, EBX
		MOV [EBX], 0
	///  SEH
		XOR EAX, EAX
	//                pop eax ..  .     
		MOV ECX, [ESP]
		MOV FS:[EAX], ECX
		ADD ESP, 8
	End;
	Readln;
End.

. CSDN .

DH. SEH .

좋은 웹페이지 즐겨찾기