Delphi异常机制与SEH(100分)

  • 主题发起人 主题发起人 yinju
  • 开始时间 开始时间
Y

yinju

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi异常机制与SEH<br>书呆子<br>介绍SEH机制的文章很多,但基本都是C++的,关于Delphi的很少。最近项目需要,仔细阅读了VCL关于异常的处理,有些心得体会,希望和大家一起分享。<br>SEH简介<br>&nbsp; &nbsp; &nbsp; &nbsp;SEH(struct exception handling)结构化异常处理是WIN32系统提供一种与语言无关的的异常处理机制。编程语言通过对SEH的包装,使程序异常处理更加简单,代码结构更加清晰。常见的如,delphi用到的 try exception end, try finally end,C++用到的_try{} _finally{} 和_try{} _except {} 结构都是对SEH的包装。<br>&nbsp; &nbsp; &nbsp; &nbsp;SEH提供了两种方式供开发者使用,一种是线程级的,通过设置线程的SEH链表结构。线程的TIB信息保存在FS:[0],而TIB的第一项就是指向SEH链表,所以,FS:[0]就是指向SEH链表,关于SEH结构后面介绍。第二种是进程级的,通过API函数SetUnhandledExceptionFilter设置过滤器函数来获取异常,注意的是,这种方式只有在前面的异常机制都不予以处理的时候才会被触发。<br>&nbsp; &nbsp; &nbsp; &nbsp;关于更详细的SEH相关内容,请参见大牛Matt Pietrek的文章:<br>A Crash Course on the Depths of Win32 Structured Exception Handling (原文)<br>A Crash Course on the Depths of Win32 Structured Exception Handling (翻译)<br>SEH链表的结构如下:<br><br><br>Delphi打造的最简单的SEH示例<br>program Project1;<br>&nbsp;<br>{$APPTYPE CONSOLE}<br>&nbsp;<br>uses<br>&nbsp;SysUtils, Windows;<br>&nbsp;<br>type<br>&nbsp;PEXCEPTION_HANDLER = ^EXCEPTION_HANDLER;<br>&nbsp;<br>&nbsp;PEXCEPTION_REGISTRATION = ^EXCEPTION_REGISTRATION;<br>&nbsp;_EXCEPTION_REGISTRATION = record<br>&nbsp; &nbsp; Prev: PEXCEPTION_REGISTRATION;<br>&nbsp; &nbsp; Handler: PEXCEPTION_HANDLER;<br>&nbsp;end;<br>&nbsp;<br>&nbsp;EXCEPTION_REGISTRATION = _EXCEPTION_REGISTRATION;<br>&nbsp;<br>&nbsp;_EXCEPTION_HANDLER = record<br>&nbsp; &nbsp; ExceptionRecord: PExceptionRecord;<br>&nbsp; &nbsp; SEH: PEXCEPTION_REGISTRATION;<br>&nbsp; &nbsp; Context: PContext;<br>&nbsp; &nbsp; DispatcherContext: Pointer;<br>&nbsp;end;<br>&nbsp;<br>&nbsp;EXCEPTION_HANDLER = _EXCEPTION_HANDLER;<br>&nbsp;<br>const<br>&nbsp;EXCEPTION_CONTINUE_EXECUTION = 0; ///恢复CONTEXT里的寄存器环境,继续执行<br>&nbsp;EXCEPTION_CONTINUE_SEARCH &nbsp; &nbsp;= 1; ///拒绝处理这个异常,请调用下个异常处理函数<br>&nbsp;EXCEPTION_NESTED_EXCEPTION &nbsp; = 2; ///函数中出发了新的异常<br>&nbsp;EXCEPTION_COLLIDED_UNWIND &nbsp; &nbsp;= 3; ///发生了嵌套展开操作<br>&nbsp;<br>&nbsp;EH_NONE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 0;<br>&nbsp;EH_NONCONTINUABLE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 1;<br>&nbsp;EH_UNWINDING &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 2;<br>&nbsp;EH_EXIT_UNWIND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 4;<br>&nbsp;EH_STACK_INVALID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 8;<br>&nbsp;EH_NESTED_CALL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 16;<br>&nbsp;<br>&nbsp;STATUS_ACCESS_VIOLATION &nbsp; &nbsp; &nbsp; &nbsp; = $C0000005; ///访问非法地址<br>&nbsp;STATUS_ARRAY_BOUNDS_EXCEEDED &nbsp; &nbsp;= $C000008C;<br>&nbsp;STATUS_FLOAT_DENORMAL_OPERAND &nbsp; = $C000008D;<br>&nbsp;STATUS_FLOAT_DIVIDE_BY_ZERO &nbsp; &nbsp; = $C000008E;<br>&nbsp;STATUS_FLOAT_INEXACT_RESULT &nbsp; &nbsp; = $C000008F;<br>&nbsp;STATUS_FLOAT_INVALID_OPERATION = $C0000090;<br>&nbsp;STATUS_FLOAT_OVERFLOW &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $C0000091;<br>&nbsp;STATUS_FLOAT_STACK_CHECK &nbsp; &nbsp; &nbsp; &nbsp;= $C0000092;<br>&nbsp;STATUS_FLOAT_UNDERFLOW &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $C0000093;<br>&nbsp;STATUS_INTEGER_DIVIDE_BY_ZERO &nbsp; = $C0000094; ///除0错误<br>&nbsp;STATUS_INTEGER_OVERFLOW &nbsp; &nbsp; &nbsp; &nbsp; = $C0000095;<br>&nbsp;STATUS_PRIVILEGED_INSTRUCTION &nbsp; = $C0000096;<br>&nbsp;STATUS_STACK_OVERFLOW &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $C00000FD;<br>&nbsp;STATUS_CONTROL_C_EXIT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $C000013A;<br>&nbsp;<br>&nbsp;<br>var<br>&nbsp;G_TEST: DWORD;<br>&nbsp;<br>procedure Log(LogMsg: string);<br>begin<br>&nbsp;Writeln(LogMsg);<br>end;<br>&nbsp;<br>function ExceptionHandler(ExceptionHandler: EXCEPTION_HANDLER): LongInt; cdecl;<br>begin<br>&nbsp;Result := EXCEPTION_CONTINUE_SEARCH;<br>&nbsp;if ExceptionHandler.ExceptionRecord.ExceptionFlags = EH_NONE then<br>&nbsp;begin<br>&nbsp; &nbsp; case ExceptionHandler.ExceptionRecord.ExceptionCode of<br>&nbsp; &nbsp; &nbsp; STATUS_ACCESS_VIOLATION:<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log('发现异常为非法内存访问,尝试修复EBX,继续执行');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExceptionHandler.Context.Ebx := DWORD(@G_TEST);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := EXCEPTION_CONTINUE_EXECUTION;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; Log('这个异常我无法处理,请让别人处理吧');<br>&nbsp; &nbsp; end;<br>&nbsp;<br>&nbsp;end else if ExceptionHandler.ExceptionRecord.ExceptionFlags = EH_UNWINDING then<br>&nbsp; &nbsp; Log('异常展开操作');<br>end;<br>&nbsp;<br>begin<br>&nbsp;asm<br>&nbsp; &nbsp; &nbsp; ///设置SEH<br>&nbsp; &nbsp; &nbsp; &nbsp;XOR &nbsp; EAX, EAX<br>&nbsp; &nbsp; &nbsp; PUSH &nbsp; OFFSET ExceptionHandler<br>&nbsp; &nbsp; &nbsp; PUSH &nbsp; FS:[EAX]<br>&nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; FS:[EAX], ESP<br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;///产生内存访问错误<br>&nbsp; &nbsp; &nbsp; &nbsp;XOR &nbsp; EBX, EBX<br>&nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; [EBX], 0<br>&nbsp;<br>&nbsp; &nbsp; &nbsp; ///取消SEH<br>&nbsp; &nbsp; &nbsp; &nbsp;XOR &nbsp; EAX, EAX<br>&nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; ECX, [ESP]<br>&nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; FS:[EAX], ECX<br>&nbsp; &nbsp; &nbsp; &nbsp;ADD &nbsp; ESP, 8<br>&nbsp;end;<br>&nbsp;<br>&nbsp;Readln;<br>end.<br>这个例子演示了最简单的异常处理,首先,通过PUSH handler 和 prev两个字段创建一个EXCEPTION_REGISTRATION结构体。再将ESP所指的新的REGISTRATION结构体赋值给FS:[0],这样就挂上了我们自己的SEH处理结构。当MOV &nbsp; [EBX], 0发生内存访问错后,系统挂起,查找SEH处理链表,通知ExceptionHandler进行处理,ExceptionHandler中,将EBX修复到一个可以访问的内存位置,再通知系统恢复环境继续执行。当处理完后恢复原来的SEH结构,再还原堆栈,处理完毕。<br>VCL对SEH的封装<br>&nbsp; &nbsp; &nbsp; &nbsp;在Delphi里我们通常使用try except end 和 try finally end 来处理异常,那么在VCL里是怎么来实现的呢?<br>&nbsp; &nbsp; &nbsp; 1、VCL的顶层异常捕获<br>在DELPHI开发的程序中,出错的时候,我们很少看到出现一个错误对话框,提示点确定结束程序,点取消调试。而在VC或VB里就很常见,这是为什么呢?这是因为VCL的理念是,只要能够继续运行,就尽量不结束程序,而VC或VB里则认为,一旦出错,而开发者又不处理的话将会导致更严重的错误,所以干脆结束了事。至于二者之间的优劣我们就不讨论了,总之,有好有坏,关键要应用得当。<br>注意:后面的代码都是以EXE程序来讨论的,DLL的原理是一样的<br>VCL的顶层异常捕获是在程序入口函数StartExe处做的:<br>procedure &nbsp; &nbsp; &nbsp; _StartExe(InitTable: PackageInfo; Module: PLibModule);<br>begin<br>&nbsp;RaiseExceptionProc := @RaiseException;<br>&nbsp;RTLUnwindProc := @RTLUnwind;<br>{$ENDIF}<br>&nbsp;InitContext.InitTable := InitTable;<br>&nbsp;InitContext.InitCount := 0;<br>&nbsp;InitContext.Module := Module;<br>&nbsp;MainInstance := Module.Instance;<br>{$IFNDEF PC_MAPPED_EXCEPTIONS}<br>&nbsp;SetExceptionHandler; ///挂上SEH<br>{$ENDIF}<br>&nbsp;IsLibrary := False;<br>&nbsp;InitUnits;<br>end;<br>也就是在工程文件的begin处做的:<br>Project1.dpr.9: begin<br>00472004 55 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push ebp<br>00472005 8BEC &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov ebp,esp<br>00472007 83C4F0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add esp,-$10 &nbsp; &nbsp;//注意这里,分配了16个字节的堆栈,其中的12个字节是用来存储顶层异常结构的SEH内容<br>0047200A B8C41D4700 &nbsp; &nbsp; &nbsp; mov eax,$00471dc4<br>0047200F E81844F9FF &nbsp; &nbsp; &nbsp; call @InitExe &nbsp; // InitExe 在Sysinit单元里,我就不贴了,InitExe 接着就是调用_StartExe<br>Project1.dpr.13: end.<br>00472044 E89F21F9FF &nbsp; &nbsp; &nbsp; call @Halt0<br>00472049 8D4000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lea eax,[eax+$00]<br>SetExceptionHandler的代码:<br>procedure &nbsp; &nbsp; &nbsp; SetExceptionHandler;<br>asm<br>&nbsp;XOR &nbsp; &nbsp; EDX,EDX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { using [EDX] saves some space over [0] }<br>&nbsp;LEA &nbsp; &nbsp; EAX,[EBP-12] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///这里就是直接将begin处分配的内存指针传给EAX,指向一个TExcFrame结构体<br>&nbsp;MOV &nbsp; &nbsp; ECX,FS:[EDX] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ ECX := head of chain &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp;MOV &nbsp; &nbsp; FS:[EDX],EAX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ head of chain := @exRegRec &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp;<br>&nbsp;MOV &nbsp; &nbsp; [EAX].TExcFrame.next,ECX<br>{$IFDEF PIC}<br>&nbsp;LEA &nbsp; &nbsp; EDX, [EBX]._ExceptionHandler<br>&nbsp;MOV &nbsp; &nbsp; [EAX].TExcFrame.desc, EDX<br>{$ELSE}<br>&nbsp;MOV &nbsp; &nbsp; [EAX].TExcFrame.desc,offset _ExceptionHandler ///异常处理函数<br>{$ENDIF}<br>&nbsp;MOV &nbsp; &nbsp; [EAX].TExcFrame.hEBP,EBP ///保存EBP寄存器,EBP寄存器是一个非常关键的寄存器,一般用来保存进入函数时候的栈顶指针,当函数执行完后用来恢复堆栈,一旦这个寄存器被修改或无法恢复,用明叔的话说就是:windows很生气,后果很严重!<br>{$IFDEF PIC}<br>&nbsp;MOV &nbsp; &nbsp; [EBX].InitContext.ExcFrame,EAX<br>{$ELSE}<br>&nbsp;MOV &nbsp; &nbsp; InitContext.ExcFrame,EAX<br>{$ENDIF}<br>end;<br>介绍一下TExcFrame:<br>&nbsp;PExcFrame = ^TExcFrame;<br>&nbsp;TExcFrame = record<br>&nbsp; &nbsp; next: PExcFrame;<br>&nbsp; &nbsp; desc: PExcDesc;<br>&nbsp; &nbsp; hEBP: Pointer;<br>&nbsp; &nbsp; case Integer of<br>&nbsp; &nbsp; 0: ( );<br>&nbsp; &nbsp; 1: ( ConstructedObject: Pointer );<br>&nbsp; &nbsp; 2: ( SelfOfMethod: Pointer );<br>&nbsp;end;<br>TExcFrame其实相当于在EXCEPTION_REGISTRATION基础上扩展了hEBP和另外一个指针,这是符合规范的,因为系统只要求前两位就行了。一般的编程语言都会扩展几个字段来保存一些关键寄存器或者其他信息方便出错后能够恢复现场。<br>当ExceptionHandler捕获到了异常时,VCL就没的选择了,弹出一个错误对话框,显示错误信息,点击确定就结束进程了。<br>2、消息处理时候的异常处理<br>大家可能有疑问了,那不是意味着程序里没有TRY EXCEPT END的话,出现异常就会直接退出?那么我在button的事件里抛出一个错误为什么没有退出呢?这是因为,DELPHI几乎在所有的消息函数处理位置加了异常保护,以controls为例子:<br>procedure TWinControl.MainWndProc(var Message: TMessage);<br>begin<br>&nbsp;try<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; WindowProc(Message);<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; FreeDeviceContexts;<br>&nbsp; &nbsp; &nbsp; FreeMemoryContexts;<br>&nbsp; &nbsp; end;<br>&nbsp;except<br>&nbsp; &nbsp; Application.HandleException(Self);<br>&nbsp;end;<br>end;<br>一旦消息处理过程中发生了异常DELPHI将跳至Application.HandleException(Self);<br>进行处理:<br>procedure TApplication.HandleException(Sender: TObject);<br>begin<br>&nbsp;if GetCapture &lt;&gt; 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);<br>&nbsp;if ExceptObject is Exception then<br>&nbsp;begin<br>&nbsp; &nbsp; if not (ExceptObject is EAbort) then<br>&nbsp; &nbsp; &nbsp; if Assigned(FOnException) then<br>&nbsp; &nbsp; &nbsp; &nbsp; FOnException(Sender, Exception(ExceptObject))<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; ShowException(Exception(ExceptObject));<br>&nbsp;end else<br>&nbsp; &nbsp; SysUtils.ShowException(ExceptObject, ExceptAddr);<br>end;<br>&nbsp; &nbsp; &nbsp; &nbsp;如果用户挂上了application.onexception事件,VCL就会将错误交给事件处理,如果没有,VCL将会弹出错误对话框警告用户,但是不会结束程序。<br>&nbsp; &nbsp; &nbsp; &nbsp;这种方式的好处就是,软件不会因为异常而直接中止,开发者可以轻松的在onexception里接管所有的异常,坏处就是它破坏了系统提供的SEH异常处理结构,使得别的模块无法获得异常。<br>3、Try except end 和try finally end做了什么<br>&nbsp; &nbsp; &nbsp; &nbsp;Try except end和try finally end在实现上其实没有本质的区别,先介绍下第一个。<br>&nbsp;<br>try except end的实现:<br>&nbsp; &nbsp; &nbsp; &nbsp;PASSCAL代码(使用3个Sleep主要是用了观看汇编代码时比较方便隔开编译器生成的代码):<br>&nbsp;try<br>&nbsp; &nbsp; Sleep(1);<br>&nbsp;except<br>&nbsp; &nbsp; Sleep(1);<br>&nbsp;end;<br>&nbsp;Sleep(1);<br>&nbsp;<br>编译后代码:<br>SEHSample.dpr.89: try<br>///挂上SEH,将异常处理函数指向到00408D0E 实际上这个地址就直接跳转到了HandleAnyException(后面再介绍这个函数)<br>&nbsp;<br>00408CEF 33C0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xor eax,eax<br>00408CF1 55 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push ebp ///保存了EBP指针<br>00408CF2 680E8D4000 &nbsp; &nbsp; &nbsp; push $00408d0e<br>00408CF7 64FF30 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push dword ptr fs:[eax]<br>00408CFA 648920 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov fs:[eax],esp<br>&nbsp;<br>SEHSample.dpr.90: Sleep(1);<br>00408CFD 6A01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push $01<br>00408CFF E8F8C1FFFF &nbsp; &nbsp; &nbsp; call Sleep<br>&nbsp;<br>///如果没有发生异常,取消SEH,恢复堆栈<br>00408D04 33C0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xor eax,eax<br>00408D06 5A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop edx<br>00408D07 59 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop ecx<br>00408D08 59 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop ecx<br>00408D09 648910 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov fs:[eax],edx<br>///没有发生异常,跳转到00408D1F继续执行下面的代码<br>00408D0C EB11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jmp +$11<br>&nbsp;<br>///如果在异常处理里用了on E:Exception 语法的话会交给另外一个函数<br>_HandleOnException处理,这里不详细介绍HandleAnyException的实现了,其中的很大一个作用就是把异常翻译成DELPHI的EXCEPTION对象交给开发者处理,这就是为什么你只是声明了个E:Exception没有构造就直接可以使用,而且也不用释放,其实是VCL帮你做了创建和释放工作。<br>00408D0E E9ADAAFFFF &nbsp; &nbsp; &nbsp; jmp @HandleAnyException<br>&nbsp;<br>///发生异常后,HandleAnyException处理完毕,交给开发者处理<br>SEHSample.dpr.92: Sleep(1);<br>00408D13 6A01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push $01<br>00408D15 E8E2C1FFFF &nbsp; &nbsp; &nbsp; call Sleep<br>&nbsp;<br>///执行清理工作,释放异常对象,取消SEH,恢复EBP<br>00408D1A E881ACFFFF &nbsp; &nbsp; &nbsp; call @DoneExcept<br>&nbsp;<br>SEHSample.dpr.94: Sleep(1);<br>00408D1F 6A01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push $01<br>00408D21 E8D6C1FFFF &nbsp; &nbsp; &nbsp; call Sleep<br>当代码进入try except end 结构时,首先挂上SEH,如果代码正常执行,在执行完毕后取消SEH,这种情况比较简单。如果出现了异常,那么代码就会跳到错误处理函数位置,首先会交给HandleAnyException处理,再返回到开发者代码,最后执行DoneExcept进行清理工作。<br>&nbsp;<br>Try finally end 的实现:<br>&nbsp; &nbsp; &nbsp; &nbsp;Passcal代码:<br>try<br>&nbsp; &nbsp; Sleep(1);<br>&nbsp;finally<br>&nbsp; &nbsp; Sleep(1);<br>&nbsp;end;<br>&nbsp;Sleep(1);<br>编译后代码:<br>SEHSample.dpr.89: try<br>///挂上SEH,将异常处理函数指向到00408D0E 实际上这个地址就直接跳转到了HandleFinally<br>00408CEC 33C0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xor eax,eax<br>00408CEE 55 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push ebp<br>00408CEF 68168D4000 &nbsp; &nbsp; &nbsp; push $00408d16<br>00408CF4 64FF30 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push dword ptr fs:[eax]<br>00408CF7 648920 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov fs:[eax],esp<br>&nbsp;<br>SEHSample.dpr.90: Sleep(1);<br>00408CFA 6A01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push $01<br>00408CFC E8FBC1FFFF &nbsp; &nbsp; &nbsp; call Sleep<br>&nbsp;<br>///如果没有发生异常,取消SEH,恢复堆栈<br>00408D01 33C0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xor eax,eax<br>00408D03 5A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop edx<br>00408D04 59 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop ecx<br>00408D05 59 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop ecx<br>00408D06 648910 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov fs:[eax],edx<br>&nbsp;<br>///将try finally end结构后的用户代码放在栈顶,为后面ret指令所作的工作<br>00408D09 681D8D4000 &nbsp; &nbsp; &nbsp; push $00408d1d<br>&nbsp;<br>SEHSample.dpr.92: Sleep(1);<br>00408D0E 6A01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push $01<br>00408D10 E8E7C1FFFF &nbsp; &nbsp; &nbsp; call Sleep<br>&nbsp;<br>///弹回到$00408d1d处,就是try finally end后的代码<br>00408D15 C3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret <br>&nbsp;<br>///处理异常HandleFinally处理完毕后,会跳转到00408D16的下一段代码,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HandleFinally: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>MOV &nbsp; &nbsp; ECX,[EDX].TExcFrame.desc ///将错误处理函数保存在ECX<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; [EDX].TExcFrame.desc,offset @@exit<br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;EBX<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;ESI<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;EDI<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;EBP<br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; EBP,[EDX].TExcFrame.hEBP<br>ADD &nbsp; &nbsp; ECX,TExcDesc.instructions ///将ECX指向下段代码<br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;NotifyExceptFinally<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;ECX &nbsp; ///调用ECX,实际上就是00408D1B<br>////////////////////////////////////<br>&nbsp;<br>00408D16 E9D1ABFFFF &nbsp; &nbsp; &nbsp; jmp @HandleFinally<br>///跳到00408D0E处,就是FINALLY内的代码处<br>00408D1B EBF1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jmp -$0f<br>&nbsp;<br>SEHSample.dpr.94: Sleep(1);<br>00408D1D 6A01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push $01<br>00408D1F E8D8C1FFFF &nbsp; &nbsp; &nbsp; call Sleep<br>&nbsp; &nbsp; &nbsp; &nbsp;当代码进入到try finally end时,首先挂上SEH,如果代码正常执行,取消SEH,将try finally end后的代码地址压入堆栈,再finally里的代码运行完毕后,ret就返回到了该地址。如果发生异常,跳到HandleFinally,HandleFinally处理完后再跳转到finally里的代码,ret返回后,回到HandleFinally,返回EXCEPTION_CONTINUE_SEARCH给系统,异常将会继续交给上层SEH结构处理。<br>&nbsp;<br>从代码可以看出,简单的try except end和try finally end背后,编译器可是做了大量的工作,这也是SEH结构化异常处理的优点,复杂的东西编译器都给你弄好了,开发者面对的东西相对简单。<br>4、VCL对象构造时的异常处理<br>&nbsp; &nbsp; &nbsp; &nbsp;在Delphi开发的时候,经常会重载构造函数constractor,构造函数是创造对象的过程,如果这个时候出现异常VCL会怎么办呢?看代码吧:<br>function _ClassCreate(AClass: TClass; Alloc: Boolean): TObject;<br>asm<br>&nbsp; &nbsp; &nbsp; &nbsp; { -&gt; &nbsp; &nbsp;EAX = pointer to VMT &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp; { &lt;- &nbsp; &nbsp;EAX = pointer to instance }<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;EDX<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;ECX<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;EBX<br>&nbsp; &nbsp; &nbsp; &nbsp; TEST &nbsp; &nbsp;DL,DL<br>&nbsp; &nbsp; &nbsp; &nbsp; JL &nbsp; &nbsp; &nbsp;@@noAlloc<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///首先通过NewInstance构造对象,分配内存<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;DWORD PTR [EAX] + VMTOFFSET TObject.NewInstance<br>@@noAlloc:<br>{$IFNDEF PC_MAPPED_EXCEPTIONS}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///挂上SEH<br>&nbsp; &nbsp; &nbsp; &nbsp; XOR &nbsp; &nbsp; EDX,EDX<br>&nbsp; &nbsp; &nbsp; &nbsp; LEA &nbsp; &nbsp; ECX,[ESP+16]<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; EBX,FS:[EDX]<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; [ECX].TExcFrame.next,EBX<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; [ECX].TExcFrame.hEBP,EBP<br>///将异常处理函数指向@desc节<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; [ECX].TExcFrame.desc,offset @desc<br>///将EAX,也就是对象实例存在在扩展字段里<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; [ECX].TexcFrame.ConstructedObject,EAX &nbsp; { trick: remember copy to instance }<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; FS:[EDX],ECX<br>{$ENDIF}<br>///返回,调用构造函数<br>&nbsp; &nbsp; &nbsp; &nbsp; POP &nbsp; &nbsp; EBX<br>&nbsp; &nbsp; &nbsp; &nbsp; POP &nbsp; &nbsp; ECX<br>&nbsp; &nbsp; &nbsp; &nbsp; POP &nbsp; &nbsp; EDX<br>&nbsp; &nbsp; &nbsp; &nbsp; RET<br>&nbsp;<br>{$IFNDEF PC_MAPPED_EXCEPTIONS}<br>@desc:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;///发生异常先交给HandleAnyException处理<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; JMP &nbsp; &nbsp; _HandleAnyException<br>&nbsp;<br>&nbsp;{ &nbsp; &nbsp; &nbsp; destroy the object &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///异常处理完毕后,获取对象<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; EAX,[ESP+8+9*4]<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; EAX,[EAX].TExcFrame.ConstructedObject<br>///判断对象是否为空<br>&nbsp; &nbsp; &nbsp; &nbsp; TEST &nbsp; &nbsp;EAX,EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; JE &nbsp; &nbsp; &nbsp;@@skip<br>///调用析构函数,释放对象<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; ECX,[EAX]<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; DL,$81<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;DWORD PTR [ECX] + VMTOFFSET TObject.Destroy<br>&nbsp; &nbsp; &nbsp; &nbsp; POP &nbsp; &nbsp; EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;_ClassDestroy<br>@@skip:<br>&nbsp;{ &nbsp; &nbsp; &nbsp; reraise the exception &nbsp; }<br>///重新抛出异常<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;_RaiseAgain<br>{$ENDIF}<br>end;<br>这也算一个VCL里比较特殊的SEH应用吧,过程大概就是,对构造函数进行保护,如果出现异常就调用析构函数释放。<br>这个地方很容易让开发者犯错误,下面举个例子:<br>type<br>&nbsp;TTest = class<br>&nbsp;private<br>&nbsp; &nbsp; a: TObject;<br>&nbsp; &nbsp; b: TObject;<br>&nbsp;public<br>&nbsp; &nbsp; constructor Create;<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp;end;<br>constructor TTest.Create;<br>begin<br>&nbsp;inherited;<br>&nbsp;a := TObject.Create;<br>&nbsp;b := TObject.Create;<br>end;<br>&nbsp;<br>destructor TTest.Destroy;<br>begin<br>&nbsp;a.Free;<br>&nbsp;b.Free;<br>&nbsp;inherited;<br>end;<br>这段代码看起来没啥问题,可实际上却不然,正常情况下,没有异常可以顺利通过,但如果a := TObject.Create;出现了异常,意味着b := TObject.Create;就不会被运行,b对象就不存在,这个时候VCL又会主动调用析构函数,结果b.free的时候就出错了。所以在析构函数里释放对象的时候,一定要注意判断对象是否存在。改正如下:<br>destructor TTest.Destroy;<br>begin<br>&nbsp; &nbsp; if a &lt;&gt; nil then<br>&nbsp;a.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp;if b &lt;&gt; nil then<br>&nbsp;b.Free;<br>&nbsp;inherited;<br>end;<br>结语<br>&nbsp; &nbsp; &nbsp; &nbsp;以上就是我所了解到delphi里关于SEH的处理了,内容基本是自己摸索出来的心得,有不当之处,欢迎指正。<br>参考资料<br>A Crash Course on the Depths of Win32 Structured Exception Handling <br>联系方式<br>Mail:heroyin@gmail.com<br><br>原文:http://blog.csdn.net/hero_yin/archive/2008/01/28/2069916.aspx
 
好文,收藏。
 
唉,大家都不再关注底层的东西了,郁闷~~~~
 
文章写得不错,
 
会用调试器的这个是不成问题的。
 
多人接受答案了。
 
后退
顶部