Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Similar documents
Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Polymorphism. Zimmer CSCI 330

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

EECE.3170: Microprocessor Systems Design I Summer 2017

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Inheritance, Polymorphism and the Object Memory Model

Computer Systems Lecture 9

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

W4118: PC Hardware and x86. Junfeng Yang

C++ Programming: Polymorphism

Overview of Compiler. A. Introduction

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Assembly Language: Function Calls

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

Program Exploitation Intro

Assembly Language: Function Calls" Goals of this Lecture"

An Insight Into Inheritance, Object Oriented Programming, Run-Time Type Information, and Exceptions PV264 Advanced Programming in C++

Assembly Language: Function Calls" Goals of this Lecture"

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

OBJECT ORIENTED PROGRAMMING USING C++

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Module 3 Instruction Set Architecture (ISA)

16.317: Microprocessor Systems Design I Fall 2014

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

x86 assembly CS449 Fall 2017

Processes (Intro) Yannis Smaragdakis, U. Athens

G52CPP C++ Programming Lecture 13

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

x86 Assembly Crash Course Don Porter

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

September 10,

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Increases Program Structure which results in greater reliability. Polymorphism

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

CS11 Advanced C++ Fall Lecture 7

Introduction to Programming Using Java (98-388)

Subprograms: Arguments

C++ for System Developers with Design Pattern

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

CSC 8400: Computer Systems. Machine-Level Representation of Programs

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

IBM Global Services. Reversing C++ Paul Vincent Sabanal X-Force R&D Mark Vincent Yason X-Force R&D. IBM Internet Security Systems Ahead of the threat.

16.317: Microprocessor Systems Design I Fall 2015

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

CSC 8400: Computer Systems. Using the Stack for Function Calls

Multiprocessor Solution

04-24/26 Discussion Notes

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Exception Handling Alternatives (Part 2)

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

U23 - Binary Exploitation

CSE 401 Final Exam. December 16, 2010

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

Lecture 4 CIS 341: COMPILERS

Graph-Based Binary Analysis

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 11

Writing 32-Bit Applications

Short Notes of CS201

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

x86 assembly CS449 Spring 2016

System Software Assignment 1 Runtime Support for Procedures

CS201 - Introduction to Programming Glossary By

16.317: Microprocessor Systems Design I Spring 2015

CSC 2400: Computer Systems. Using the Stack for Function Calls

Exceptions and Continuations. Lecture #19: More Special Effects Exceptions and OOP. Approach II: Non-Standard Return. Approach I: Do Nothing

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

CS11 Advanced C++ Fall Lecture 3

Compiler Design Spring 2017

Java and C CSE 351 Spring

Machine and Assembly Language Principles

Procedure-Calling Conventions October 30

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Chapter 5. Object- Oriented Programming Part I

Runtime Environment. Part II May 8th Wednesday, May 8, 13

16.317: Microprocessor Systems Design I Fall 2013

Assembly Language for Intel-Based Computers, 4 th Edition. Lecture 25: Interface With High-Level Language

3. Process Management in xv6

Towards the Hardware"

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Dynamic Binding C++ Douglas C. Schmidt

Process Layout and Function Calls

Digital Forensics Lecture 3 - Reverse Engineering

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

C++ internals. Francis Visoiu Mistrih - 1 / 41

CVE EXPLOIT USING 108 BYTES AND DOWNLOADING A FILE WITH YOUR UNLIMITED CODE BY VALTHEK

Transcription:

HOW C++ WORKS

Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Motivation There are lot of myths about C++ compilers Mostly around performance A clear understanding of how a compiler implements language constructs is important when designing systems We learned a lot researching these topics

Assumptions People are familiar with the high-level behaviour of C+ + constructs Virtual functions Inheritance RTTI Exceptions Templates Etc.

Constructors Constructors are called when an object: enters scope is created with operator new What about global variables? Constructed before main() by C++ startup code Can t assume the order of creation Be careful with global constructors The system might not be fully set up at this time

Object Construction When an object is instantiated: operator new is called by the compiler to allocate memory for the class or it is allocated on the stack For each class in the inheritance hierarchy starting with the base class: the vtable pointers are initialised initialisation list is processed in the order objects are declared default constructor calls are added as needed by compiler the constructor for that class is called

Object Construction cont d Calling virtual functions in constructors is dangerous: class A A() }; foo(); // calls A::foo(), even if object is a B } virtual foo(); class B : public A B(); }; virtual foo();

Destructors Destructors are called when an object: leaves scope is destroyed with operator delete Global objects are destroyed after main() Same pitfalls as global construction operator delete[] informs the compiler to call the destructor for each object in an array The compiler has no way of knowing if a pointer refers to an array of objects, or just a single object.

Object Destruction Similar to constructors (backwards) if the destructor is virtual, if not: class A }; ~A(); class B : public A }; ~B() ImportantStuff(); } A* foo = new B; delete foo; // B s destructor isn t called!

Virtual Functions What is a vtable? Array of function pointers representing a classes virtual members Stored in the application s static data Used for virtual function dispatching virtual functions must be looked up in vtable before calling a few cycles slower than a regular function call can incur a cache miss can t be inlined

Single Inheritance Implemented by concatenating layout of base classes together except for the base class vtable pointers only one vtable pointer regardless of inheritance depth Cost of single inheritance: one global vtable per class one vtable pointer per object vtable lookup per virtual call

Single Inheritance cont d class A virtual foo1(); virtual foo2(); int data1; }; class B : public A virtual foo1(); virtual foo3(); int data2; }; A s layout A s vtable B s layout B s vtable data1 A::foo1() data1 B::foo1() vtable * A::foo2() vtable * A::foo2() data2 B::foo3()

Multiple Inheritance Implemented by concatenating layout of base classes together including vtable pointers Cost of multiple inheritance: one vtable per class one vtable pointer per object one vtable pointer per parent class per object one virtual base class pointer per use of virtual base class

Multiple Inheritance cont d Cost cont d: Normal virtual function calls are the same as single inheritance A virtual base class adds an extra level of indirection affects non-virtual and virtual calls If two functions in base classes share signatures, compiler can t always disambiguate Pointers to base classes of the same object are not always the same

Multiple Inheritance cont d class A }; class B : virtual public A }; class C : virtual public A }; class D : public B, public C }; A B C D B Data Members vtable* virtual base class* C Data Members vtable* virtual base class* D Data Members A Data Members vtable*

Run Time Type Information (RTTI) RTTI relates to two C++ operators: dynamic_cast<> typeid() How does RTTI work? Compiler inserts an extra function into a classes vtable memory hit is per class, not per instance only pay the speed hit when you use RTTI operators maximum single inheritance cost is the same as a virtual function times depth of inheritance hierarchy for that class multiple inheritance is slower

RTTI Cont d User code: class A virtual ~A(); }; class B : public A }; A* foo = SomeFoo(); B* bar = dynamic_cast<b*>(foo); void* cast(void* obj, type dest) } return mytype==dest? obj : 0; void* sicast(void* obj, type dest) } Compiler generated casting function: if(mytype == dest) else return obj; return base->cast(obj, dest);

RTTI Cont d Multiple inheritance dynamic_cast<>: void* class_type_info:: dcast (const type_info& desired, int is_public, void *objptr, const type_info *sub, void *subptr) const if (*this == desired) return objptr; void *match_found = 0; for (size_t i = 0; i < n_bases; i++) if (is_public && base_list[i].access!= PUBLIC) continue; void *p = (char *)objptr + base_list[i].offset; if (base_list[i].is_virtual) p = *(void **)p; p = base_list[i].base->dcast (desired, is_public, p, sub, subptr); if (p) if (match_found == 0) match_found = p; else if (match_found!= p) if (sub) // Perhaps we're downcasting from *sub to desired; see if // subptr is a subobject of exactly one of match_found,p}. } const user_type_info &d = static_cast <const user_type_info &> (desired); void *os = d.dcast (*sub, 1, match_found); void *ns = d.dcast (*sub, 1, p); if (os == ns) /* ambiguous -- subptr is a virtual base */; else if (os == subptr) continue; else if (ns == subptr) match_found = p; continue; } } // base found at two different pointers, // conversion is not unique return 0; } } return match_found; }

Operator Overloading Most operators in C++ can be overloaded Can't overload:.?: ::.* sizeof typeid Shouldn t overload:, && Operators have function signatures of form operator <symbol>, example : Foo& operator + (Foo& a, Foo& b); Temporary creation when returning by reference

Templates Macros on steroids Evaluated in a similar fashion to macros, but are type-safe. Can be templatized on types or values Code is generated at each template instantiation Everything must be defined inline Templatized class is parsed by compiler and held When a template class is instantiated, compiler inserts actual classes into parse tree to generate code.

Templates template <class T> class foo T Func(void) return bar; } T bar; }; foo<int> I; foo<char> C; class fooint int Func(void) return bar; } int bar; }; class foochar char Func(void) return bar; } char bar; }; fooint I; foochar C; These two examples will generate identical code

Templates (cont d) Not one, but two ways to bloat code! Because templates must be defined inline, code may be inlined unintentionally. Each instantiation of new templatized type causes the creation of a large amount of code Combating code bloat Separate non-type dependant functions into non-templatized base class. Use templates as type-safe wrappers for unsafe classes.

Templates (cont d) When templates are not inlined, duplicate symbols are generated which the linker must strip out. Templates can interact fine with derivation hierarchy and virtual functions But the specializations are not naturally related in any way Templates can not be exported from libraries because no code exists specialised template classes can

Exceptions Provides a way to handle error conditions without constant checking of return values. Problems to be solved by exception handling implementation : Finding correct exception handler Transferring control to exception handler Destroying objects on the stack

Exceptions (cont d) Finding the correct exception handler Table is of handlers is kept one per try/catch block also stores reference to the next (up the stack) try/catch frame. Global pointer to current try/catch frame is stored Passing Control At the beginning of each try/catch block the current stack state is stored (setjmp). If an exception occurs the runtime searches the try/catch frame for an appropriate handler, resets the stack frame and passes control (longjmp)

Exceptions (cont d) Destroying objects on the stack For each function an unwinding table of all stack allocated objects is kept Current initialisation state is kept for each object When an exception occurs current unwind table and all above it before next try/catch frame have all valid objects destroyed The table is created even for functions with no try/catch or throw statements Extra work per stack allocation/deallocation Extra work at start and end of a function

Exceptions (cont d) Exceptions C++ Code void Test(void) Foo a; Foo b; } No Exceptions?Test@@YAXXZ PROC NEAR push ebp mov ebp, esp sub esp, 72 push ebx push esi push edi lea ecx, DWORD PTR _f$[ebp] call??0foo@@qae@xz lea ecx, DWORD PTR _g$[ebp] call??0foo@@qae@xz lea ecx, DWORD PTR _g$[ebp] call??1foo@@qae@xz lea ecx, DWORD PTR _f$[ebp] call??1foo@@qae@xz pop edi pop esi pop ebx mov esp, ebp pop ebp ret 0?Test@@YAXXZ ENDP?Test@@YAXXZ PROC NEAR push ebp mov ebp, esp push -1 push ehhandler$?test@@yaxxz mov eax, DWORD PTR fs: except_list push eax mov DWORD PTR fs: except_list, esp sub esp, 72 push ebx push esi push edi lea ecx, DWORD PTR _f$[ebp] call??0foo@@qae@xz mov DWORD PTR $EHRec$[ebp+8], 0 lea ecx, DWORD PTR _g$[ebp] call??0foo@@qae@xz lea ecx, DWORD PTR _g$[ebp] call??1foo@@qae@xz mov DWORD PTR $EHRec$[ebp+8], -1 lea ecx, DWORD PTR _f$[ebp] call??1foo@@qae@xz mov ecx, DWORD PTR $EHRec$[ebp] mov DWORD PTR fs: except_list, ecx pop edi pop esi pop ebx mov esp, ebp pop ebp ret 0 _TEXT ENDS ; COMDAT text$x text$x SEGMENT unwindfunclet$?test@@yaxxz$0: lea ecx, DWORD PTR _f$[ebp] call??1foo@@qae@xzret 0 ehhandler$?test@@yaxxz: mov eax, OFFSET FLAT: ehfuncinfo$? Test@@YAXXZ jmp CxxFrameHandler text$x ENDS?Test@@YAXXZ ENDP

Exceptions This behaviour means that exception handling costs even when you don t actually use it. Most compilers have a flag to turn on/off stack unwinding for exception handling This makes exception handling basically useless though Exceptions are one of the few C++ constructs that fully deserves their bad reputation

Points to Take With You Most of this is covered in Effective C++,, and More Effective C++ All of it is covered in the GCC source code harder to read though Be wary of C++ folklore You don't need to know how to write a C++ compiler from scratch But it wouldn't hurt