DEV490 .NET Framework: CLR – Under The Hood

Slides:



Advertisements
Similar presentations
Ahead of Time Dynamic Translation PreJit/NGEN by any other name George Bosworth Microsoft MRE04 March 21, 2004.
Advertisements

By Sam Nasr September 28, 2004 Understanding MSIL.
© 2003 School of Computing, University of Leeds SY32 Secure Computing, Lecture 16 Secure Coding in Java and.NET Part 1: Fundamentals.
.NET Framework Overview
1 CHAPTER 8 BUFFER OVERFLOW. 2 Introduction One of the more advanced attack techniques is the buffer overflow attack Buffer Overflows occurs when software.
Debugging Production SharePoint Applications Wouter van Vugt.
.NET Framework Overview Pingping Ma Nov 16 th, 2006.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Introducing the Common Language Runtime for.NET. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution.
Introducing the Common Language Runtime. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution engine.
2. Developing in.NET and C#. 2 Microsoft Objectives “Microsoft.NET development is based on an underlying framework of tools and classes. These tools and.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Creating and Running Your First C# Program Svetlin Nakov Telerik Corporation
Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004.
Intro to dot Net Dr. John Abraham UTPA – Fall 09 CSCI 3327.
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
A Free sample background from © 2001 By Default!Slide 1.NET Overview BY: Pinkesh Desai.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
.NET Framework Introduction: Metadata
Introduction to .Net Framework
1 8/29/05CS360 Windows Programming Professor Shereen Khoja.
.NET Overview. 2 Objectives Introduce.NET –overview –languages –libraries –development and execution model Examine simple C# program.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
CIS NET Applications1 Chapter 2 –.NET Component- Oriented Programming Essentials.
.NET Framework & C#.
Introduction to .NET Framework
Understanding Code Compilation and Deployment Lesson 4.
Creating and Running Your First C# Program Svetlin Nakov Telerik Corporation
Compiling and Executing Code in.Net Microsoft Intermediate Language and Common Language Runtime.
C# Overview and Features. Content I.History of C# II.Architecture III.How to install IV.Features V.Code Sample VI.Microsoft.NET Platform VII.Why use C#
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Lecture 10 : Introduction to Java Virtual Machine
CS795/895: Introduction. Topics Distributed Systems –Availability –Performance –Web Services Security –Authentication –Authorization –Confidentiality.
Component-Based Software Engineering Introduction to.NET Paul Krause.
.NET Framework Danish Sami UG Lead.NetFoundry
MSIL & CLI Kun-Yuan Shieh.
Self-Contained CLI Assemblies Dipl.-Inf. Bernhard Rabe Operating Systems & Middleware Group Hasso-Plattner-Institute University of Potsdam.
tom perkins1 XML Web Services -.NET FRAMEWORK – Part 1 CHAPTER 1.1 – 1.3.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
Chapter 4 Memory Management Virtual Memory.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Buffer Overflow Proofing of Code Binaries By Ramya Reguramalingam Graduate Student, Computer Science Advisor: Dr. Gopal Gupta.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
PRIOR TO WEB SERVICES THE OTHER TECHNOLOGIES ARE:.
MSIL C#.NET Software Development. MSIL AKA CIL What all.NET languages compile to What all.NET languages compile to Binary Intermediate Language Binary.
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
Object Oriented Software Development 4. C# data types, objects and references.
Programming Languages and Paradigms Activation Records in Java.
Wel come To Seminar On C#.
Bruno Cabral “Reflection, Code Generation and Instrumentation in the.NET platform” University of Coimbra.
The Execution System1. 2 Introduction Managed code and managed data qualify code or data that executes in cooperation with the execution engine The execution.
Text Introduction to.NET Framework. CONFIDENTIAL Agenda .NET Training – Purpose  What is.NET?  Why.NET?  Advantages  Architecture  Components: CLR,
Overview CNS 3260 C#.NET Software Development. 2.NET Framework Began in 2000 Developed in three years (2000 to 2003) Operating System Hardware.NET Framework.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
RealTimeSystems Lab Jong-Koo, Lim
Just-In-Time Compilation. Introduction Just-in-time compilation (JIT), also known as dynamic translation, is a method to improve the runtime performance.
Design issues for Object-Oriented Languages
.NET and .NET Core 2. .NET Runtimes Pan Wuming 2017.
CS360 Windows Programming
Introduction to C# AKEEL AHMED.
Programming in C# CHAPTER 1
.NET and .NET Core: Languages, Cloud, Mobile and AI
Lecture Topics: 11/1 General Operating System Concepts Processes
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
IS 135 Business Programming
Presentation transcript:

DEV490 .NET Framework: CLR – Under The Hood 4/21/2017 5:36 PM DEV490 .NET Framework: CLR – Under The Hood Jeffrey Richter Author / Consultant / Trainer Wintellect © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Jeffrey Richter Author of several .NET Framework/Win32 Books 4/21/2017 5:36 PM Jeffrey Richter Author of several .NET Framework/Win32 Books Cofounder of Wintellect: a company dedicated to helping clients ship better software faster Services: Consulting, Debugging, Security Reviews, Training Consultant on Microsoft’s .NET Framework team since October 1999 MSDN Magazine Contributing Editor/.NET Columnist © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Topics Execution Model How Things Relate at Runtime Garbage Collection 4/21/2017 5:36 PM Topics Execution Model Intermediate Language (IL), verification, JIT compilation, metadata, and assembly loading How Things Relate at Runtime Code, Types, Objects, a thread’s stack, and the heap Garbage Collection How a reference-tracking GC works © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Topics Execution Model How Things Relate at Runtime Garbage Collection 4/21/2017 5:36 PM Topics Execution Model Intermediate Language (IL), verification, JIT compilation, metadata, and assembly loading How Things Relate at Runtime Code, Types, Objects, a thread’s stack, and the heap Garbage Collection How a reference-tracking GC works © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Compiling Source Code Into Assemblies C# Source Code File(s) Basic Source Code File(s) C++ Source Code File(s) Fortran Source Code File(s) C# Compiler Basic Compiler C++ Compiler Fortran Compiler Managed Assembly (IL and Metadata) Managed Assembly (IL and Metadata) Managed Assembly (IL and Metadata) Managed Assembly (IL and Metadata)

An Assembly An Assembly is the managed equivalent of an EXE/DLL Implements and optionally exports a collection of types It is the unit of versioning, security, and deployment Parts of an Assembly file Windows PE header CLR header (Information interpreted by the CLR and utilities) Metadata (Type definition and reference tables) Intermediate Language (code emitted by compiler)

ILDasm.exe

Intermediate Language All .NET compilers produce IL code IL is CPU-independent machine language Created by Microsoft with input from external commercial and academic language/compiler writers IL is higher-level than most CPU machine languages Some sample IL instructions Create and initialize objects (including arrays) Call virtual methods Throw and catch exceptions Store/load values to/from fields, parameters, and local variables Developers can write in IL assembler (ILAsm.exe) Many compilers produce IL source code and compile it by spawning ILAsm.exe

ILDasm.exe

Benefits Of IL IL is not tied to any specific CPU Managed modules can run on any CPU (x86, Itanium, Opteron, etc), as long as the OS on that CPU supports the CLR Many believe that “write once, run everywhere” is the biggest benefit I disagree, security and verification of code is the really BIG win!

Real Benefit Of IL: Security And Verification When processing IL, CLR verifies it to ensure that everything it does is “safe” Every method is called with correct number and type of parameters Every method’s return value is used properly Every method has a return statement Metadata includes all the type/method info used for verification

Benefits Of “Safe” Code Multiple managed applications can run in 1 Windows’ process Applications can’t corrupt each other (or themselves) Reduces OS resource usage, improves performance Administrators can trust apps ISPs forbidding ISAPI DLLs SQL Server running IL for stored procedures Internet downloaded code (with Code Access Security) Note: Administrator can turn off verification

Executing Managed IL Code When loaded, the runtime creates method stubs When a method is called, the stub jumps to runtime Runtime loads IL and compiles it IL is compiled into native CPU code Just like compiler back-end Method stub is removed and points to compiled code Compiled code is executed In future, when method is called, it just runs

Native CPU Instructions Managed EXE Console static void Main() { Console.WriteLine(“Hello”); Console.WriteLine(“Goodbye”); } static void WriteLine() JITCompiler static void WriteLine(String) JITCompiler NativeMethod Native CPU Instructions (remaining members) … MSCorEE.dll JITCompiler function { 1. In the assembly that implements the type (Console), look up the method (WriteLine) being called in the metadata. 2. From the metadata, get the IL for this method and verify it. 3. Allocate a block of memory. 4. Compile the IL into native CPU instructions; the native code is saved in the memory allocated in step #3. 5. Modify the method’s entry in the Type’s table so that it now points to the memory block allocated in step #3. 6. Jump to the native code contained inside the memory block. }

All Types/Modules Are Self-Describing 4/21/2017 5:36 PM All Types/Modules Are Self-Describing public class App { public static void Main() { System.Console.WriteLine("Hi"); } } 1 TypeDef entry for “App” Entry refers to MethodDef entry for “Main” 2 TypeRef entries for “System.Object” and “System.Console” Both entries refer to AssemblyRef entry for MSCorLib © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Metadata Definition Tables (Partial List) TypeDef: 1 entry for each type defined Type’s name, base type, flags (i.e. public, private, etc.) and index into MethodDef & FieldDef tables MethodDef: 1 entry for each method defined Method’s name, flags (private, public, virtual, static, etc), IL offset, and index to ParamDef table FieldDef: 1 entry for each field defined Name, flags (i.e. private, public, etc.), and type ParamDef: 1 entry for each parameter def’d Name, and flags (in, out, retval, etc.)

Metadata Reference Tables (Partial List) AssemblyRef: 1 entry for each assembly ref’d Name, version, culture, public key token TypeRef: 1 entry for each type ref’d Type’s name, and index into AssemblyRef table MemberRef: 1 entry for each member ref’d Name, signature, and index into TypeRef table

ILDasm.exe Metadata And IL 4/21/2017 5:36 PM ILDasm.exe Metadata And IL © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Code Attempts To Access A Type/Method .method /*06000001*/ public hidebysig static void Main(class System.String[] args) il managed { .entrypoint // Code size 11 (0xb) .maxstack 8 IL_0000: ldstr "Hi“ IL_0005: call void ['mscorlib'/* 23000001 */] System.Console/* 01000003 */::WriteLine(class System.String) IL_000a: ret } // end of method 'App::Main' 23000001: AssemblyRef entry for MSCorLib 01000003: TypeRef entry to System.Console 06000001: MethodDef for Main (FYI)

How The CLR Resolves An Assembly Reference IL Call with Metadata token MemberRef  TypeRef TypeRef  AssemblyRef Load Assembly MemberRef MethodDef MethodDef  TypeDef Create internal type structure Look-up TypeDef Emit Native Call

Topics Execution Model How Things Relate at Runtime Garbage Collection 4/21/2017 5:36 PM Topics Execution Model Intermediate Language (IL), verification, JIT compilation, metadata, and assembly loading How Things Relate at Runtime Code, Types, Objects, a thread’s stack, and the heap Garbage Collection How a reference-tracking GC works © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

CLR (Thread Pool & Managed Heap) A Thread’s Stack Windows’ Process void M1() { String name = “Joe”; M2(name); ... return; } . . . name (String) M1 Locals M2 Params s (String) [return address] M2 Locals length (Int32) tally (Int32) void M2(String s) { Int32 length = s.Length; Int32 tally; ... return; } CLR (Thread Pool & Managed Heap)

Simple Class Hierarchy class Employee { public Int32 GetYearsEmployed() { ... } public virtual String GenProgressReport() { ... } public static Employee Lookup(String name) { ... } } class Manager : Employee { public override String GenProgressReport() { ... } }

Instance Method Mapping Using this public Int32 GetYearsEmployed(); public (static) Int32 GetYearsEmployed(Employee this); public virtual String GenProgressReport(); public (static) String GenProgressReport(Employee this); public static Employee Lookup(String name); Employee e = new Employee(); e.GetYearsEmployed(); e.GenProgressReport(); Employee e = new Employee(); Employee.GetYearsEmployed(e); Employee.GenProgressReport(e); this is what makes instance data available to instance methods

IL Instructions To Call A Method Is usable for static, instance, and virtual instance methods No null check for the this pointer (for instance methods) Used to call virtual methods non-polymorphically base.OnPaint(); Callvirt Usable for instance and virtual methods only Slower perf Null check for all instance methods Polymorphs for virtual methods No polymorphic behavior for non-virtual methods C# and VB use callvirt to perform a null check when calling instance methods

Object o = new Object(); o.GetHashCode(); // Virtual class App { static void Main() { Object o = new Object(); o.GetHashCode(); // Virtual o.GetType(); // Non-virtual instance Console.WriteLine(1); // Static } .method private hidebysig static void Main() cil managed { .entrypoint // Code size 27 (0x1b) .maxstack 1 .locals init (object V_0) IL_0000: newobj instance void System.Object::.ctor() IL_0005: stloc.0 IL_0006: ldloc.0 IL_0007: callvirt instance int32 System.Object::GetHashCode() IL_000c: pop IL_000d: ldloc.0 IL_000e: callvirt instance class System.Type System.Object::GetType() IL_0013: pop IL_0014: ldc.i4.1 IL_0015: call void System.Console::WriteLine(int32) IL_001a: ret } // end of method App::Main

Memory: Code, Types, Objects Windows’ Process Stack Heap . . . e (Employee) year (Int32) Mthd Tbl Ptr Sync Blk Indx Instance Fields Manager Object null . . . Method Table Ptr Sync Block Index Static Fields GenProgressReport Manager Type Jitted Code = 5 = 0 Mthd Tbl Ptr Sync Blk Indx Instance Fields Manager Object Jitted Code Method Table Ptr Sync Block Index Static Fields GetYearsEmployed GenProgressReport Lookup Employee Type void M3() { Employee e; Int32 year; e = new Manager(); e = Employee.Lookup(“Joe”); year = e.GetYearsEmployed(); e.GenProgressReport(); } Jitted Code Jitted Code CLR (Thread Pool & Managed Heap)

Topics Execution Model How Things Relate at Runtime Garbage Collection 4/21/2017 5:36 PM Topics Execution Model Intermediate Language (IL), verification, JIT compilation, metadata, and assembly loading How Things Relate at Runtime Code, Types, Objects, a thread’s stack, and the heap Garbage Collection How a reference-tracking GC works © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

The Managed Heap NextObjPtr All reference types are allocated on the managed heap Your code never frees an object The GC frees objects when they are no longer reachable Each process gets its own managed heap Virtual address space region, sparsely allocated The new operator always allocates objects at the end If heap is full, a GC occurs Reality: GC occurs when generation 0 is full A B C NextObjPtr

Roots And GC Preparation Every application has a set of Roots A Root is a memory location that can refer to an object Or, the memory location can contain null Roots can be any of the following Global & static fields, local parameters, local variables, CPU registers When a method is JIT compiled, the JIT compiler creates a table indicating the method’s roots The GC uses this table The table looks something like this... Start Offset End Offset Roots________________ 0x00000000 0x00000020 this, arg1, arg2, ECX, EDX 0x00000021 0x00000122 this, arg2, fs, EBX 0x00000123 0x00000145 fs

When A GC Starts... All objects in heap are considered garbage The GC assumes that no roots refer to objects GC examines roots and marks each reachable object If a GC starts and the CPU’s IP is at 0x00000100, the objects pointed to by the this parameter, arg2 parameter, fs local variable, and the EBX register are roots; these objects are marked as “in use” As reachable objects are found, GC uses metadata to checks each object’s fields for references to other objects These objects are marked “in use” too, and so on GC walks up the thread’s call stack determining roots for the calling methods by accessing each method’s table For objects already “in use”, fields aren’t checked Improves performance Prevents infinite loops due to circular references The GC uses other means to obtain the set of roots stored in global and static variables

ROOTS (strong references) Globals Statics Locals CPU Registers Before A Collection Managed Heap A B C D E F G H I J ROOTS (strong references) Globals Statics Locals CPU Registers NextObjPtr

Compacting The Heap After all roots have been checked and all objects have been marked “in use”... The GC walks linearly through heap for free gaps and shifts reachable objects down (simple memory copy) As objects are shifted down in memory, roots are updated to point to the object’s new memory address After all objects have been shifted... The NextObjPtr pointer is positioned after last object The ‘new’ operation that caused the GC is retried This time, there should be available memory in the heap and the object construction should succeed If not, an OutOfMemoryException is thrown

ROOTS (strong references) Globals Statics Locals CPU Registers After A Collection Managed Heap A C D F H ROOTS (strong references) Globals Statics Locals CPU Registers NextObjPtr

Root Example class App { public static void Main() { // ArrayList object created in heap, a is now a root ArrayList a = new ArrayList(); // Create 10000 objects in the heap for (int x = 0; x < 10000; x++) { a.Add(new Object()); } // Local a is a root that refers to 10000 objects Console.WriteLine(a.Length); // After line above, a is not a root and all 10001 // objects may be collected. // NOTE: Method doesn’t have to return Console.WriteLine(“End of method”); } }

Microsoft Products And Services For Lifelong Learning www. microsoft Microsoft Products And Services For Lifelong Learning www.microsoft.com/learning Microsoft Products and Services for Lifelong Learning Assessments http://www.microsoft.com/assessment/ Courses 2415: Programming with the Microsoft .NET Framework (Microsoft Visual Basic .NET) 2349: Programming with the Microsoft .NET Framework (Microsoft Visual C# .NET) Books The Microsoft Platform Ahead, ISBN: 0-7356-2064-4 Network Programming for the Microsoft .NET Framework, ISBN: 0-7356-1959-X Programming Microsoft .NET, ISBN: 0-7356-1376-1 Applied Microsoft Windows .NET Framework Programming, ISBN: 0-7356-1422-9 Applied Microsoft Windows .NET Framework Programming in Microsoft Visual Basic .NET, ISBN: 0-7356-1787-2 Microsoft Windows .NET Framework 1.1 Class Library References Performance Testing Microsoft .NET Web Applications, ISBN: 0-7356-1538-1