Bruno Cabral “Reflection, Code Generation and Instrumentation in the.NET platform” University of Coimbra.

Slides:



Advertisements
Similar presentations
Operating Systems Components of OS
Advertisements

Paulo Marques, Bruno Cabral Dependable Systems Group University of Coimbra, Portugal RAIL: Code Instrumentation for.NET.
Ahead of Time Dynamic Translation PreJit/NGEN by any other name George Bosworth Microsoft MRE04 March 21, 2004.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
.NET Framework Overview
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
CS 31003: Compilers ANIRUDDHA GUPTA 11CS10004 G2 CLASS DATE : 24/07/2013.
Generating Data Access Assemblies with IronRuby Rob Rowe Blog: rob-rowe.blogspot.com.
History of.Net Introduced by Microsoft Earlier technology was VC++ and VB VC++ comes with so many library and VB was so easy to use and not flexible to.
An overview of the SSCLI Distribution Mark Lewin Microsoft Research
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
Programming Our First Java Program Yingcai Xiao. What to Do Set up for Java Programming Write our first Java Program with IDE Write our first Java Program.
Data Structure and Algorithm 1 Yingcai Xiao. You Me The Course (
Introducing the Common Language Runtime for.NET. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution.
Assembly Where it all gets physical. Objectives Introduce concepts of assemblies Discuss elements of assemblies Show how to build assemblies Runtime aspects.
Introducing the Common Language Runtime. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution engine.
Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004.
A Free sample background from © 2001 By Default!Slide 1.NET Overview BY: Pinkesh Desai.
Java Beans.
.NET Framework Introduction: Metadata
Introduction to .Net Framework
The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++
Session 1 - Introduction and Data Access Layer
Bruno Cabral Summary Vision Statement Process and Methodology Key Features Application scenarios What can we really do with RAIL?
Understanding Code Compilation and Deployment Lesson 4.
Importing outside DLLs into.Net platform and using them By Anupama Atmakur.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
UNIVERSITI TENAGA NASIONAL “Generates Professionals” CHAPTER 4 : Part 2 INTRODUCTION TO SOFTWARE DEVELOPMENT: PROGRAMMING & LANGUAGES.
Bacon A Penetration and Auditing Framework Hernan Gips
Presenter: PhuongNQK. Goals Provide you insights into core concepts of.NET framework  Assembly  Application domain  MSIL.
CS795/895: Introduction. Topics Distributed Systems –Availability –Performance –Web Services Security –Authentication –Authorization –Confidentiality.
Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual.
.NET Framework Danish Sami UG Lead.NetFoundry
Programming With C.
Self-Contained CLI Assemblies Dipl.-Inf. Bernhard Rabe Operating Systems & Middleware Group Hasso-Plattner-Institute University of Potsdam.
Random Logic l Forum.NET l NET reflection Forum.NET 3 rd Meeting ● February 15, 2006 By Damián Laufer.
ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types.
tom perkins1 XML Web Services -.NET FRAMEWORK – Part 1 CHAPTER 1.1 – 1.3.
Topic 2d High-Level languages and Systems Software
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
.NET Ying Chen Junwei Chen. What is Microsoft.NET. NET is a development platform Incorporated into.NET COM+ component services ASP web development framework.
Attributes C#.Net Software Development Version 1.0.
Object Oriented Software Development 4. C# data types, objects and references.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Reflection Programming under the hood SoftUni Team Technical Trainers Software University
The Execution System1. 2 Introduction Managed code and managed data qualify code or data that executes in cooperation with the execution engine The execution.
.Net Reflection Taipan Tamsare. Overview Reflection core concepts Exploring metadata Detail information Attributes Building Types at runtime.
Assemblies. 2 Objectives Introduce assemblies –concept –creation –naming Discuss deployment –main.exe –dependent assemblies.
Integrating and Extending Workflow 8 AA301 Carl Sykes Ed Heaney.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
Just-In-Time Compilation. Introduction Just-in-time compilation (JIT), also known as dynamic translation, is a method to improve the runtime performance.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
What is .NET.
Computer Architecture & Operations I
Advanced .NET Programming I 8th Lecture
C# and the .NET Framework
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
.NET and .NET Core 2. .NET Runtimes Pan Wuming 2017.
CS360 Windows Programming
Chapter 3 The .NET Framework Class Library (FCL)
Introduction to C# AKEEL AHMED.
Programming in C# CHAPTER 1
.NET and .NET Core: Languages, Cloud, Mobile and AI
Lecture 22 Inheritance Richard Gesick.
.NET Base Type (CTS Data Type) Managed Extensions for C++ Keyword
Advanced .NET Programming I 9th Lecture
Presentation transcript:

Bruno Cabral “Reflection, Code Generation and Instrumentation in the.NET platform” University of Coimbra

21 Jan /33 Summary PE File Metadata Introspection Assembly and Code generation Using RAIL

21 Jan /33 PE File PE = Portable Executable Marked as Code and Execute Read for the OS loader and the CLR _CorExeMain (mscoree.dll)

21 Jan /33 PE File PE = Portable Executable Marked as Code and Execute Read for the OS loader and the CLR _CorExeMain (mscoree.dll)

21 Jan /33 PE File Multiple Modules Module = file

21 Jan /33 PE File - Metadata "data about data" Describes types Member fields, methods, properties, events Describes signatures Fields, properties, methods, delegates, local vars Describes types and references Describes miscellaneous entities Files, modules, assemblies

21 Jan /33 PE File - Metadata CIL instructions reference metadata via TOKENS First byte of token indicates type Last three bytes of token are either row # (tables) or offsets (heaps) Tokens are stored compressed in signatures (binary file viewers not very useful)

21 Jan /33 PE File - Metadata Tokens (and types) in listing MethodDef : (0x ) User string: (0x ) AssemblyRef : (0x ) TypeRef : (0x ) MemberRef : (0x0A000002) Only two are directly referenced by CIL in this example: example: String (by ldstr instruction) MemberRef (by call instruction)

21 Jan /33 PE File - Metadata

21 Jan /33 PE File - Metadata

21 Jan /33 Introspection Two major metadata APIs available Managed: System.Reflection Unmanaged: IMetaDataImport Both are limited in different ways System.Reflection does not permit access to CIL System.Reflection does not reveal token values IMetaDataImport cannot resolve *Ref tokens

21 Jan /33 Introspection What can we do with System.Reflection? See loaded assemblies See referenced assemblies See the types defined in the assembly See the methods defined in a type See attributes …

21 Jan /33 Introspection See loaded assemblies Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach( Assembly assembly in assemblies ) Console.WriteLine( assembly.FullName );

21 Jan /33 Introspection See referenced assemblies AssemblyName[] referencedAssemblies = assembly.GetReferencedAssemblies(); foreach( AssemblyName assemblyName in referencedAssemblies ) Console.WriteLine( "--> {0}", assemblyName.FullName );

21 Jan /33 Introspection See the types defined in the assembly Assembly a = Assembly.LoadFrom("Teste.exe"); Module[] m = a.GetModules( ); Console.WriteLine("\n" + a.FullName ); Type[] types = m[0].GetTypes( ); foreach( Type type in types ) Console.WriteLine( "==> {0}", type.FullName );

21 Jan /33 Introspection See the methods defined in a type Assembly a = Assembly.LoadFrom("HelloWorld.exe"); Module[] m = a.GetModules( ); Type[] types = m[0].GetTypes( ); Type type = types[0]; MethodInfo[] mInfo = type.GetMethods( ); foreach ( MethodInfo mi in mInfo ) Console.WriteLine(" {0}", mi);

21 Jan /33 Assembly and Code Generation How to use System.Reflection.Emit to generate assemblies in run-time? AssemblyBuilder, ModuleBuilder, TypeBuilder, MethodBuilder, … ILGenerator

21 Jan /33 Assembly and Code Generation AssemblyName myAssemblyName = new AssemblyName(); myAssemblyName.Name = "MyAssembly"; myAssemblyName.Version = new Version(" ");

21 Jan /33 Assembly and Code Generation AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName); TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");

21 Jan /33 Assembly and Code Generation MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |MethodAttributes.Static, typeof(void), null); ILGenerator myILGenerator = myMethodBuilder.GetILGenerator(); myILGenerator.EmitWriteLine("Hello World!"); myILGenerator.Emit(OpCodes.Ret);

21 Jan /33 Assembly and Code Generation myTypeBuilder.CreateType(); myAssemblyBuilder.SetEntryPoint(myMethodBuilder); myAssemblyBuilder.Save(fileName);

21 Jan /33 What is missing? Reflection Generation …

21 Jan /33 What is missing? Reflection Generation INTRUMENTATION… Structural Reflection Behavioural Reflection HOW TO DO IT?

21 Jan /33 …use RAIL

21 Jan /33 Vision Statement “Create an API that allows CLR assemblies to be manipulated and instrumented before they are loaded and executed“ The reflection capabilities of the CLR are extremely powerful. Query the existing types at runtime Define new assemblies in memory and use Reflection.Emit to generate MSIL on-the-fly. Our plan is to fill the gap between these two concepts.

21 Jan /33 Process and Methodology Operating System program program.exe/dll PE Header MetadataIL ILx86 Source Code Compile Assembly JIT-compile RAIL

21 Jan /33 Process and Methodology

21 Jan /33 Key features ‘Rapid’ assembly instrumentation library High level of abstraction, no need for handling all the internal details Convenient object model for representation of all assembly modules Flexible MSIL instruction handling Use of design patterns

21 Jan /33 Application scenarios Runtime analysis tools Security verification MSIL optimization Application invisible proxy substitution Software fault injection Aspect Oriented Programming Others!

21 Jan /33 What can we really do with RAIL? Replace References Add epilogues and prologues to methods Redirect methods access/call Redirect field access Redirect field access to property Redirect field read and write access to methods Redirect property access Replace create new instructions with a static method Create, Run and Save assemblies

21 Jan /33 What can we really do with RAIL? private void Replace(MSIL.Code code, int pos) { MSIL.ILMethod ilm = new MSIL.ILMethod(OpCodes.Call, this.newMethod); code.Remove(pos); code.Insert(pos,ilm); } Play with MSIL code!!

21 Jan /33 Assembly Loading Interception ResolveEventHandler currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler); Load the assembly ourselves Modify the CLR (SSCLI)??? Others??

21 Jan /33 Conclusion Code instrumentation is not a simple task With RAIL it’s easy to develop high level functionalities Assembly creation mechanisms can be very slow The design patterns are very useful