Handling Exceptions In C & C++ [Part B] Ver 2

12. Feb 2010
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
1 von 76

Más contenido relacionado

Was ist angesagt?

C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)Olve Maudal
Insecure coding in C (and C++)Insecure coding in C (and C++)
Insecure coding in C (and C++)Olve Maudal
Java - Generic programmingJava - Generic programming
Java - Generic programmingRiccardo Cardin
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++IRJET Journal
C tutorialC tutorial
C tutorialKhan Rahimeen
C tutorialC tutorial
C tutorialtuncay123

Destacado

Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaManoj_vasava
Exception HandlingException Handling
Exception HandlingReddhi Basu
HandlingHandling
HandlingAmit Vats
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)Mohd Effandi
Pointers in c++Pointers in c++
Pointers in c++sai tarlekar
05 c++-strings05 c++-strings
05 c++-stringsKelly Swanson

Similar a Handling Exceptions In C & C++ [Part B] Ver 2

2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
Day 1Day 1
Day 1Pat Zearfoss
.Net Garbage Collector 101.Net Garbage Collector 101
.Net Garbage Collector 101Woody Pewitt
C# 6.0 PreviewC# 6.0 Preview
C# 6.0 PreviewFujio Kojima
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CanSecWest
Scope Stack AllocationScope Stack Allocation
Scope Stack AllocationElectronic Arts / DICE

Más de ppd1961

Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel TourLand of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tour
Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tourppd1961
Science & Culture Article with Editorial & CoverScience & Culture Article with Editorial & Cover
Science & Culture Article with Editorial & Coverppd1961
NDL @ YOJANANDL @ YOJANA
NDL @ YOJANAppd1961
Unified Modeling Language (UML)Unified Modeling Language (UML)
Unified Modeling Language (UML)ppd1961
OOP in C++OOP in C++
OOP in C++ppd1961
Digital geometry  - An introductionDigital geometry  - An introduction
Digital geometry - An introductionppd1961

Handling Exceptions In C & C++ [Part B] Ver 2

Hinweis der Redaktion

  1. We should outline the various Instrumentations needed for handling Function Calls depending on the kind of exception handling being supported: Normal Flow return Exceptional Flow with Stack Cutting setjmp / longjmp Exceptional Flow with Stack Unwinding try-catch-throw Naturally, keeping with the code separation / code organization motivations in mind, we do not need to refer to any of the local (goto’s) or global (errno / exit() etc) options here. We need to highlight, as we move from #1 to #3, how we are doing in terms of the basic issues (objectives) we have started off with. This will be more a philosophical introduction.
  2. These are the items needed for the discussion on Instrumentation. We’ll elaborate them in the following slides. We need to collect information on: How the calling order for finalizers decided? In normal flow and in exceptional flow. What exactly does the EH Handler do to achieve stack unwinding? How a sample EH Frame is laid out.
  3. We discuss the stack frame structure here. Function parameters Function return address Frame pointer Local Objects Callee save registers We’ll take a small example and show the stack frame on x86. Good resource is: http://www.unixwiz.net/techtips/win32-callconv-asm.html
  4. We discuss the context of a call here. Register PC / Return Address (eip on x86) Register SP / Stack Pointer (esp on x86) Register FP / Frame Pointer or Base Pointer (ebp on x86) With respect to the example, we’ll show the context on x86. Good resources is: http://www.unixwiz.net/techtips/win32-callconv-asm.html
  5. We discuss the finalization of a call here. How are the right destructors called in the right order? This is tricky once the function has a multiple return statements before / after a number of local object constructions. We can frame a small example and illustrate.
  6. We outline the normal flow in a function call: Caller prepares the Parameters Caller calls the Callee Callee saves the Context (Function Prologue) Callee does the job Callee restores the Context (Function Epilogue) Callee returns Caller cleans up the Parameters Caller uses the return value We can use the stack frame example for illustration. Good resource is: http://www.unixwiz.net/techtips/win32-callconv-asm.html
  7. We discuss the enhanced stack frame structure with (bold item). Function parameters Function return address Frame pointer Exception Handler frame Local Objects Callee save registers
  8. We need to discuss how EH Frame is maintained. Need more information on this. A good resource is: http://www.osronline.com/custom.cfm?name=articlePrint.cfm&id=469
  9. This is the case of handling in C for setjmp / longjmp. Our discussion should follow as: Recap of the setjmp / longjmp with an example. Explanation of the jmp_buf structure for x86-32. This is from the setjmp.h header. With reference to stack frame and context, we need to explain how the information set for the buffer has been chosen. Explanation of the behavior of setjmp – low level steps. Assembly code of setjmp can says that. Explanation of the behavior of longjmp – low level steps. Assembly code of longjmp can says that. Justify why this is ‘stack cutting’ – that is finalizers are not invoked. We use the above example to illustrate. Good resource is: CS360 Lecture notes – Setjmp : http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Setjmp/lecture.html
  10. This is the case of handling in C for setjmp / longjmp. Our discussion should follow as: Recap of the setjmp / longjmp with an example. Explanation of the jmp_buf structure for x86-32. This is from the setjmp.h header. With reference to stack frame and context, we need to explain how the information set for the buffer has been chosen. Explanation of the behavior of setjmp – low level steps. Assembly code of setjmp can says that. Explanation of the behavior of longjmp – low level steps. Assembly code of longjmp can says that. Justify why this is ‘stack cutting’ – that is finalizers are not invoked. We use the above example to illustrate. Good resource is: CS360 Lecture notes – Setjmp : http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Setjmp/lecture.html
  11. We discuss the additional information that a stack frame structure may need to keep for C++ exceptions. We need to identify them beyond the following. Function parameters Function return address Frame pointer Exception Handler frame Local Objects Callee save registers
  12. What is a thunk? The word thunk has two meanings in computer science: a delayed computation (as in functional programming) a mapping of machine data from one system-specific form to another, usually for compatibility reasons. For example, running a 16-bit program on a 32-bit operating system may require a thunk from 16-bit addresses to 32-bit. Thunk in this sense may also refer to mappings from one calling convention to another or from one version of a library to another. This meaning is similar to the first—the "delayed computation" can be thought of as the "update" from the old format to the new. http://en.wikipedia.org/wiki/Thunk Why do we need a destructor thunk for the Exception object? How is it used? Good resource is: Intel presentation on: “C++ Exception Handling for IA-64 Unix”
  13. We need to discuss how the EH hook is maintained.
  14. This is the case of handling in C++ for try-catch-throw. Our discussion should follow as: Recap of the try-catch-throw with an example. It will be good to use the same example as setjmp / longjmp case. Introduce the basic notions of stack unwinding: Creation of Exception object Placement of destructor thunk for Exception object Wrapping up of the stack frame. Calling of Finalizers Matching for Handler (catch handlers are statically overloaded but dynamically dispatched). Explain why this will need RTTI. Invocation of the right handler. Exit from the handler Invocation of the thunk if no rethrow has been done. Flow / flowchart of the unwind logic. Role of the C++ Runtime system. Outline the data structures required. Stack Frame – modified RUNTIME_FUNCTION UNWIND_INFO TRY_REGION_TABLE CLEANUP_TABLE We use the example to illustrate. Good resources are: Intel presentation on: “C++ Exception Handling for IA-64 Unix” Exceptional Behavior - x64 Structured Exception Handling: http://www.osronline.com/custom.cfm?name=articlePrint.cfm&id=469
  15. This is the case of handling in C++ for try-catch-throw. Our discussion should follow as: Recap of the try-catch-throw with an example. It will be good to use the same example as setjmp / longjmp case. Introduce the basic notions of stack unwinding: Creation of Exception object Placement of destructor thunk for Exception object Wrapping up of the stack frame. Calling of Finalizers Matching for Handler (catch handlers are statically overloaded but dynamically dispatched). Explain why this will need RTTI. Invocation of the right handler. Exit from the handler Invocation of the thunk if no rethrow has been done. Flow / flowchart of the unwind logic. Role of the C++ Runtime system. Outline the data structures required. Stack Frame – modified RUNTIME_FUNCTION UNWIND_INFO TRY_REGION_TABLE CLEANUP_TABLE We use the example to illustrate. Good resources are: Intel presentation on: “C++ Exception Handling for IA-64 Unix” Exceptional Behavior - x64 Structured Exception Handling: http://www.osronline.com/custom.cfm?name=articlePrint.cfm&id=469
  16. This is the case of handling in C++ for try-catch-throw. Our discussion should follow as: Recap of the try-catch-throw with an example. It will be good to use the same example as setjmp / longjmp case. Introduce the basic notions of stack unwinding: Creation of Exception object Placement of destructor thunk for Exception object Wrapping up of the stack frame. Calling of Finalizers Matching for Handler (catch handlers are statically overloaded but dynamically dispatched). Explain why this will need RTTI. Invocation of the right handler. Exit from the handler Invocation of the thunk if no rethrow has been done. Flow / flowchart of the unwind logic. Role of the C++ Runtime system. Outline the data structures required. Stack Frame – modified RUNTIME_FUNCTION UNWIND_INFO TRY_REGION_TABLE CLEANUP_TABLE We use the example to illustrate. Good resources are: Intel presentation on: “C++ Exception Handling for IA-64 Unix” Exceptional Behavior - x64 Structured Exception Handling: http://www.osronline.com/custom.cfm?name=articlePrint.cfm&id=469
  17. Nothing in life comes for free. While Exception support in C++ makes life much easier for the designer, coder and debugger; it makes life tough for the compiler writer. The main two costs are: Overhead during a non-exception flow Loss of Optimization options for the compiler. Good resource is: How much does Exception Handling cost, really? : http://www.nwcpp.org/Downloads/2006/ehc.ppt From this presentation, we can lift a couple of slides (we’ll exclude all references to SEH).