Presentation is loading. Please wait.

Presentation is loading. Please wait.

Demo 9: Phx.Morph Byte code weaving using Microsoft’s Phoenix compiler Marc Eaddy Columbia University.

Similar presentations


Presentation on theme: "Demo 9: Phx.Morph Byte code weaving using Microsoft’s Phoenix compiler Marc Eaddy Columbia University."— Presentation transcript:

1 Demo 9: Phx.Morph Byte code weaving using Microsoft’s Phoenix compiler Marc Eaddy Columbia University

2 Copyright (C) Marc Eaddy AOSD 2006 Demo2 Outline Phx.Morph overview Phoenix background Phx.Morph implementation Demos: Open Classes and AOP Statement annotations Microsoft and AOP

3 Copyright (C) Marc Eaddy AOSD 2006 Demo3 Why our work is interesting Built using Phoenix – Microsoft’s production-grade compiler, analysis and tools infrastructure –Capable of weaving very large programs –Produces debug information And soon... Distributed with Phoenix SDK Statement-level advising using statement annotations Dynamic weaving using Microsoft’s new Debugger APIs and our Wicca system –Byte code weaving –Breakpoint weaving –Source weaving

4 Copyright (C) Marc Eaddy AOSD 2006 Demo4 Background The Phoenix Project –Microsoft’s production-grade compiler, analysis, and tools infrastructure –Will become backend for all Microsoft compilers –Massive software project –Currently 1.8M LOC (318K hand-written)

5 Copyright (C) Marc Eaddy AOSD 2006 Demo5 Problem Many Phoenix requirements cannot be cleanly separated using traditional OO techniques (inheritance and aggregation) –Multiple clients and scenarios Orthogonal requirements Unanticipated requirements –“Operational” (nonfunctional) requirements Traditional OO solutions result in increased software complexity –Designs are complex and highly coupled –Code is cluttered and difficult to write and maintain Many other groups at Microsoft are also struggling with this problem

6 Copyright (C) Marc Eaddy AOSD 2006 Demo6 Our initial goal Determine if Aspect-Oriented Programming (AOP) can improve Phoenix development Our approach 1.Use Phoenix to develop an AOP solution 2.Then turn around and use the AOP solution to help develop Phoenix Phx.Morph

7 Copyright (C) Marc Eaddy AOSD 2006 Demo7 Compiler Phx.Morph Woven Program Aspect Assemblies Weaving using Phx.Morph Original Program Source Files Breakpoints Deltas Static Weaving Dynamic Weaving

8 Copyright (C) Marc Eaddy AOSD 2006 Demo8 Phx.Morph architecture Phx.Morph Editors Open Classes, binary and breakpoint weaving Phoenix-specific AOP Attribute Handlers Phoenix PEREW Assembly Re-Writer Phx.Aop AOP Joinpoints, pointcuts, … Attributes Custom AOP annotations.NET Morph Plugin

9 Copyright (C) Marc Eaddy AOSD 2006 Demo9 Open Classes Ability to split a class definition into separate modules aka “intertype declarations” Similar to partial classes in C# except –extends classes after they’ve been compiled –works on assemblies no source req’d language agnostic We support adding fields, properties, methods, base interfaces and classes

10 Copyright (C) Marc Eaddy AOSD 2006 Demo10 Original class

11 Copyright (C) Marc Eaddy AOSD 2006 Demo11 Adding the Visitor pattern: Traditional OOD Depends On OO design is tightly coupled and hard to maintain

12 Copyright (C) Marc Eaddy AOSD 2006 Demo12 Adding the Visitor pattern: Open Classes Depends On Open Classes design breaks the circular dependency and centralizes the code

13 Copyright (C) Marc Eaddy AOSD 2006 Demo13 AOP Ability to inject code at specific points in a program –profiling –logging and tracing –log field get/set –dirty bit (persistence, synchronization) –change notification (undo/redo/rollback) –enforce invariants (non-null, const, data flow, Design by Contract) –error checking/handling –fault injection –caching/memoization –proxies/delegation –asynchronous methods –design patterns (visitor, adaptor, factory, …) –Quality of Service –etc. etc.

14 Copyright (C) Marc Eaddy AOSD 2006 Demo14 Demo: Logging reflection usage Want to log a message whenever we use the Reflection API Self-weave Phx.Morph.dll

15 Copyright (C) Marc Eaddy AOSD 2006 Demo15 Reflection logging aspect using Phx.Aop; using Phx.Aop.Attributes; public class LogReflectionAspect { [Advice(AdviceType.before, "call(* System.Reflection.*.*(..))")] static public void LogReflection([Signature] string signature, [WithinSignature] string withinSignature, [FileName] string fileName, [Line] uint line) { System.Console.WriteLine("Called " + signature); System.Console.WriteLine(" inside " + withinSignature); System.Console.WriteLine(" [File: {0}, Line: {1}]", fileName, line); }

16 Copyright (C) Marc Eaddy AOSD 2006 Demo16 Statement annotations for AOP public void transferFundsTo(float amount, BankAccount destination) { [Trace] AuthorizationRequest ar = new AuthorizationRequest(this, destination);... [Trace] destination.deposit(amount);... [Log("Obtaining authorization. This could take awhile...")] bool result = ar.authorizeTransferOfFunds(amount);...

17 Copyright (C) Marc Eaddy AOSD 2006 Demo17 Using statement annotations in pointcuts [Advice(AdviceType.afterReturning, "call([Trace] *.new(..)") static void InstanceTrace([This] object o) { instances.add(o); } [Advice(AdviceType.before, "call([Trace] * *(..))") static void StatementTrace([Signature] string sig) { System.Console.WriteLine("Calling " + sig); } [Advice(AdviceType.before, "annotation([Log])") static void LogAdvice( [Attribute1] LogAttribute logAttr) { System.Console.WriteLine(logAttr.value); }

18 Copyright (C) Marc Eaddy AOSD 2006 Demo18 Current limitations Imported methods cannot access private members Cannot weave signed code Limited aspect instantiation model –Instance advice methods are imported –Static advice methods are referenced Not yet implemented –Can’t import methods with multiple return statements –Around advice –Many pointcuts not implemented (including cflow) –Only execution pointcuts can access the args and target context

19 Microsoft and AOP

20 Copyright (C) Marc Eaddy AOSD 2006 Demo20 AspectJ ™ PROSE Hyper/J JAsCo EAOP JMangler AspectC++ CeaserJ AspectC Steamloom AspectS FeatureC++ DynAOP Apostle AspectR Spring (J2EE) DemeterJ Concern Manipulation Environment Eclipse IBM WebSphere HyperProbes JBoss (J2EE) BEA JRockit JVM AspectScheme Aspects AOPHP Composition Filters Jakarta Hivemind JAML XWeaver PEAK Pythius PHPaspect AspectL AspectCocoa Loom.NET Weave.NET Meta.NET Rapier.NET Aspect# Aspect.NET AspectDNG SetPoint CLAW PostSharp Eos Compose* Encase AOP-Engine SourceWeave.NET AopDotN etAddin Phx.Morph Other Java C++.NET Products AOP technologies Italics = Microsoft-sponsored (although none are shipped) AOP.NET Wool AspectCOOL JAC Nanning JAsCo.NET No “real” products SiteVision Axon Arachne Jiazzi Poly TinyC 2 Wicca

21 Copyright (C) Marc Eaddy AOSD 2006 Demo21 Why is Microsoft waiting? Comprehensibility –Must be able to predict behavior –Must be easy to understand (Einstein, Elvis, Mort) –Integration into existing tools and software processes –Aspects in-the-large (reuse, composition, mining) Debuggability –Source-level debugging is critical –Both obliviousness and intimate AOP debugging –Debugging injected code Testability –AOP introduces new fault models Serviceability –EXE/DLL boundary no longer signifies ownership –How to isolate faults and assign blame? –Version currently linked to size/date –How to patch woven programs?

22 Copyright (C) Marc Eaddy AOSD 2006 Demo22 AOP support in.NET Static weaving –Limited byte code instrumentation tools –CodeDOM API not fully implemented Parsing not implemented Can’t access/weave byte code AST can’t represent all of C# Dynamic weaving –Discouraged in general –Can’t specify custom class loader –Edit-and-Continue API Debug only Inefficient Hard to specify patches

23 Copyright (C) Marc Eaddy AOSD 2006 Demo23 Conclusion Our goal was to determine if AOP would improve Phoenix development –Re-implemented a Phoenix plug-in to use Open Classes –Grafted adapter interfaces onto Phoenix classes –Validated the feasibility of using Phx.Morph on Phoenix itself Learned why Microsoft is timid about using AOP

24 Copyright (C) Marc Eaddy AOSD 2006 Demo24 Future work Address barriers to adoption –Comprehensibility, Debuggability, Testability, Serviceability Lobby for more AOP support in.NET Statement annotations Dynamic weaving

25 Copyright (C) Marc Eaddy AOSD 2006 Demo25 Contact Marc Eaddy eaddy@cs.columbia.edu Try it out! www.columbia.edu/~me133 (includes source) Questions?

26 Extra slides

27 Copyright (C) Marc Eaddy AOSD 2006 Demo27 Demo: Adding foreach sugar using System.Collections; using Phx.Morph.Attributes; [Extends("Node")] class NodeEnumeratorAspect : Node, IEnumerable { [Add] public IEnumerator GetEnumerator() { return new NodeEnumerator(this); } Class to extend “Add this method” “Add this iface”

28 Copyright (C) Marc Eaddy AOSD 2006 Demo28 NodeEnumerator public class NodeEnumerator : IEnumerator { Node node; int index = -1; public NodeEnumerator(Node node) { this.node = node; } public object Current { get { return node.GetChild(index); } } public bool MoveNext() { return ++index < node.ChildCount; } public void Reset() { index = -1; } }

29 Copyright (C) Marc Eaddy AOSD 2006 Demo29 foreach client static void DumpNode(Node root, string indent) { if (root == null) return; System.Console.WriteLine(indent + root); foreach (Node child in root) { DumpNode(child, indent + " "); } New capability!

30 Copyright (C) Marc Eaddy AOSD 2006 Demo30 Client wants to attach custom data to an object Example: IR-Longevity plug-in tracks compiler phase when an instruction is created Client’s extension object Client’s extension object Phoenix client extensibility: Traditional OOD

31 Copyright (C) Marc Eaddy AOSD 2006 Demo31 Empowers clients –High performance –Type safe –Don’t have to wait for RDK drop –Don’t require help from Phoenix team Phoenix client extensibility: Open Classes Weave Phx.dll To add BirthPhase field directly to Instr Client’s extension object Client’s extension object

32 Copyright (C) Marc Eaddy AOSD 2006 Demo32 Demo: Enforcing invariants Supporting const-ness at runtime Person teacher = new Person(); teacher.Salary = 25000; teacher.IsConst = true; teacher.Salary = 1000000; Caveat: Must weave all clients New! Invariant violated!

33 Copyright (C) Marc Eaddy AOSD 2006 Demo33 Invariant aspect [Extends("Person")] class InvariantAspect { [Add] public bool IsConst; [Advice(AdviceType.before, "set(* Person.*)")] void CheckIsConst( [SourceLocation.WithinSignature] string withinSignature, [This] object This) { if (IsConst) { System.Console.WriteLine( "Person '{0}' violated IsConst constraint inside {1}", This, withinSignature); }

34 Copyright (C) Marc Eaddy AOSD 2006 Demo34 CLR wish list New partial metadata keyword –Extend C# Partial Classes to post-compilation and make it language neutral –Compiler support: Class definition = class source files + partial class source files + partial classes defined in referenced assemblies –Merge partial classes at load-time CodeDOM API is incomplete –Express all C# and C++/CLI language constructs –Express complete syntax tree (including comments and whitespace) –Parsers for C# and C++/CLI Weaving at runtime –Ability to add fields, methods, etc. after load-time (ModuleLoadFinished) Statement annotations –Ability to identify statement-level join points –Ability to mark injected code

35 Copyright (C) Marc Eaddy AOSD 2006 Demo35 Before weaving IL_0065: ldarg.0 IL_0066: call class System.Reflection.Assembly System.Reflection.Assembly::Load(string)

36 Copyright (C) Marc Eaddy AOSD 2006 Demo36 Woven result IL_0065: ldarg.0 IL_0066: ldstr "System.Reflection.Assembly.Load" IL_006b: ldstr "Phx.Morph.ReflectionHelpers.LoadAssembly" IL_0070: ldstr “c:\\phx\\rdk\\samples\\Morpher\\Phx.Morph\\Refle ctionHelpers.cs" IL_0075: ldc.i4 0xfb IL_007a: call void LogReflectionExt::LogReflection(string, string, uint32) IL_007f: call class System.Reflection.Assembly System.Reflection.Assembly::Load(string) Injected code


Download ppt "Demo 9: Phx.Morph Byte code weaving using Microsoft’s Phoenix compiler Marc Eaddy Columbia University."

Similar presentations


Ads by Google