My Runtime Aspect Weaver
2002-03-03
This article describes some of the implementation details of my runtime aspect weaver (RAW) for the CLR. It is designed to give folks an idea about the direction that I’m going in building this technology. It also highlights some of the challenges that lay ahead for my implementation as well.
The RAW is responsible for weaving a set of methods using code that is provided by a set of aspects. The mapping of aspects to target methods is provided by a set of weave instructions, which are generated by a separate weave compiler. Currently, all weaving is performed at runtime; it is a future goal of this technology to emit assmblies that contain the weaved code.
The aspect code is provided by a set of assemblies that are generated using any CLR-compatible compiler. Invidivdual aspects could be compiled using any CLR compatible langauge, and these aspects can be weaved with target methods that were compiled using any CLR compatible langauge. This allows us to perform true cross-language aspect weaving (CLAW), and is a key feature that will be demonstrated during my talk at the AOSD conference.[1]
The programmer defines the weave using a set of XML weave definition files that are provided as input to the weave compiler. A weave definition file contains a set of weave definitions; each weave definition describes how a join point must be mapped to an aspect. The join points are identified using an XPath-derived syntax that selects the appropriate portions of the target methods to weave. These join points are mapped to aspect code by the weave definition. The weave compiler compiles the weave definition files into a weave instruction file that is used by the RAW to perform the actual weaving at runtime.[2] At the time of the writing of this article, the weave definition file syntax is in a high state of flux; this article will be updated once the syntax has stabilized.
The RAW operates by weaving code prior to JIT compilation of a target method. To do so, the RAW is provided with a weave instruction file at application start-up time. At JIT compilation time, the RAW reads the existing CIL of the target method, constructs an annotated abstract syntax tree (AST), weaves in the appropriate code, and writes the modified CIL into memory. The JIT compiler then compiles the modified CIL, and executes it.
The RAW hooks into the CLR using the unmanaged profiling interfaces. The interfaces in question are:
- ICorProfilerCallback, which must be implemented by the runtime aspect weaver
- ICorProfilerCallbackInfo, which is implemented by the runtime and provides services to clients
These interfaces are very well documented by the Profiling document that can be found in the Tool Developers Guide directory of the Framework SDK.
Since these interfaces are unmanaged, all of the runtime aspect weaver code is implemented using C++. The reason why profilers must be unmanaged is that they may attempt to do unusual things like allocate memory during a garbage collection, which would lead to deadlock situations if the code were managed.
The profiling interfaces operate using an event-based mechanism. At initialization time, my aspect weaver informs the execution engine (EE) about what events it is interested in monitoring. At runtime, the EE calls the appropriate event methods on the ICorProfilerCallback interface that is implemented by the aspect weaver. Some of the key events that my aspect weaver is interested in are:
- Module Loads
- Assembly Loads
- JIT compilation
One of the really awesome features of the CLR is the ability to force existing compiled code to be re-JIT compiled. To make this possible, all method calls are bound using indirect x86 call instructions. This allows future versions of the RAW to re-weave existing aspects at runtime. This could be useful for adding and removing diagnostic code from an existing running application without having to shut down and restart the application!
The unmanaged metadata interfaces provide very low-level access to the CIL and metadata in a module. To support the weaving operations required by the RAW, an extensive unmanaged metadata library had to be constructed. The library largely mirrors the System.Reflection namespace wherever possible, since this is an excellent starting point. However, it adds many features that are not supported by System.Reflection, such as the ability to read, modify, and rewrite the CIL for an existing method. Currently, most of the engineering effort in RAW is devoted to building this library.
That’s the current status of the project at the time of this writing. This page will serve as the permanent location of all status reports on the implementation of my RAW.
[1] The first International Aspect Oriented Software Development Conference to be held April 22-25 at the University of Twente in Enschede, Netherlands. See the demonstration scedule for more information about when my Cross Language Aspect Weaving (CLAW) talk will be held.
[2] A future version of this technology will weave code during a post compilation step in the build process. This is required for efficiency, since some weaves are very computationally expensive.