Table Of ContentTwenteResearchandEducationonSoftwareEngineering,
DepartmentofComputerScience,
FacultyofElectricalEngineering,MathematicsandComputerScience,
UniversityofTwente
Performing transformations on .NET
Intermediate Language code
S.R. Boschman
Enschede,August23,2006
Prof. dr. ir. M. Aksit
Dr. ir. L.M.J. Bergmans
Ir. P.E.A. Du¨rr
Abstract
To express crosscutting concerns in a clear manner, the aspect-oriented programming (AOP)
paradigm was introduced. In AOP languages, crosscutting concerns are defined in aspects.
Theseaspectsarecomposed,orwoven,withcomponents. Thesecomponentsencapsulatefunc-
tionalityexpressedintraditionalobject-orientedlanguages.
As the aspect language and component language can be separated, an AOP solution can be
implemented independent of the component language. A suitable platform for such an AOP
solutionisthe.NETFramework,since,inprinciple,thisplatformcansupportawiderangeof
differentcomponentlanguages(i.e. programminglanguages).
This thesis will describe the development of a tool that can weave the aspects with the com-
ponents in the .NET Framework environment. The first step is to discuss the Common Inter-
mediateLanguage(CIL)inmoredetail,asallprogramminglanguagesinthe.NETFramework
maptothisCIL.ThesecondstepistocreateamappingfromAOPconstructstoCILcodestruc-
tures. The third step provides an overview of possible approaches to perform the weaving of
aspects and base components. One of the weaving approaches is based on modifying the CIL
codestoredinbinaryfiles,calledassemblies. Thefourth,andlast,stepisthediscussionofthe
creationoftheweavertool.
TheresultistheCILWeavingTool,whichconsistsoftwoparts. ThefirstpartisthePEWeaver,
responsibleforcreatingtextualILfilesfromtheassembliesandrecreatingtheassembliesafter
weaving. The second part is the IL Weaver, which performs the actual weaving on the textual
IL files. To define the weave operations that have to be performed by the IL Weaver, a weave
specification file has to be supplied. This weave specification file allows the definition of weave
points,whicharebasedonAOPconstructs.
Contents
Abstract i
ListofFigures vii
ListofTables ix
Nomenclature xi
1 IntroductiontoAOSD 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 TraditionalApproach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 AOPApproach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.1 AOPComposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.2 AspectWeaving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 AOPSolutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4.1 AspectJApproach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4.2 HyperspacesApproach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4.3 CompositionFilters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
?
2 Compose 13
2.1 EvolutionofCompositionFilters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
?
2.2 CompositionFiltersinCompose . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 DemonstratingExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.1 InitialObject-OrientedDesign . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.2 CompletingthePacmanExample . . . . . . . . . . . . . . . . . . . . . . . 18
iii
CONTENTS
?
2.4 Compose Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.4.1 IntegratedDevelopmentEnvironment . . . . . . . . . . . . . . . . . . . . . 19
2.4.2 CompileTime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4.3 Adaptation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4.4 Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.5 Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.5.1 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.5.2 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.5.3 .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
?
2.6 FeaturesSpecifictoCompose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 Introductiontothe.NETFramework 25
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2 Architectureofthe.NETFramework . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.2.1 Version2.0of.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.3 CommonLanguageRuntime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.3.1 JavaVMvs.NETCLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.4 CommonLanguageInfrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.5 FrameworkClassLibrary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.6 CommonIntermediateLanguage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4 Problemstatement 35
5 UnderstandingtheCommonIntermediateLanguage 39
5.1 Theassembly,unitofdeployment . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.1.1 PortableExecutablefiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.2 Theactivationrecordandevaluationstack . . . . . . . . . . . . . . . . . . . . . . 41
5.3 TheCommonTypeSystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.3.1 Valuetypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.3.2 Referencetypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.4 TheCILinstructionset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.4.1 Loadandstoreinstructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.4.2 Operateinstructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.4.3 Branchingandjumpinginstructions . . . . . . . . . . . . . . . . . . . . . . 46
5.4.4 Miscellaneousinstructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.5 Example: AsimpleprogramwrittenintheCIL . . . . . . . . . . . . . . . . . . . . 48
5.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
iv
CONTENTS
6 MappingAOPconstructstotheCommonIntermediateLanguage 51
6.1 Crosscuttinglocations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
6.1.1 Dynamiccrosscuttinglocations . . . . . . . . . . . . . . . . . . . . . . . . . 52
6.1.2 Staticcrosscuttinglocations . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.1.3 Concernimplementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.2 Weavepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
6.2.1 Structuralweavepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
6.2.2 Executionalweavepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.3 Fromcrosscuttinglocationstoweavepoints . . . . . . . . . . . . . . . . . . . . . . 55
6.4 Supportedweavepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
7 Towardsasolution 59
7.1 Relatedwork. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.1.1 AOPSolutionsforthe.NETFramework . . . . . . . . . . . . . . . . . . . . 59
7.1.2 Code-manipulationTools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
7.2 Approach1: Sourcecodeweaving . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
7.2.1 Advantagesanddisadvantages . . . . . . . . . . . . . . . . . . . . . . . . . 63
7.3 Approach2: Weavingatrun-timewiththeprofilingAPIs . . . . . . . . . . . . . . 63
7.3.1 TheprofilingAPIsexplained . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.3.2 Implementinganaspectprofiler . . . . . . . . . . . . . . . . . . . . . . . . 64
7.3.3 Advantagesanddisadvantages . . . . . . . . . . . . . . . . . . . . . . . . . 65
7.4 Approach3: AdaptingtheCommonLanguageRuntime . . . . . . . . . . . . . . 66
7.4.1 Advantagesanddisadvantages . . . . . . . . . . . . . . . . . . . . . . . . . 66
7.5 Approach4: Weavingaspectsinto.NETassemblies . . . . . . . . . . . . . . . . . 66
7.5.1 GettingtheMSILcodeoutoftheassembly . . . . . . . . . . . . . . . . . . 67
7.5.2 Problemswithweavingassemblies. . . . . . . . . . . . . . . . . . . . . . . 67
7.5.3 Advantagesanddisadvantages . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8 TheimplementationoftheCILWeavingTool 69
8.1 Globalstructureoftheweavertool . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
8.2 Theweavespecificationfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.2.1 Theassemblyreferenceblock . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.2.2 Themethoddefinitionblock . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.2.3 Theapplicationblock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
v
CONTENTS
8.2.4 Theclassblock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
8.3 ThePEWeaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
8.3.1 Verificationoftheassemblies . . . . . . . . . . . . . . . . . . . . . . . . . . 78
8.3.2 Disassembling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
8.3.3 Assembling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
8.4 TheILWeaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
8.4.1 Readingtheweavespecificationfile . . . . . . . . . . . . . . . . . . . . . . 80
8.4.2 ReadingtheILfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
8.4.3 Theassemblyinspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
8.4.4 Weaving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
9 IntegratingtheCILWeavingToolintoCompose* 83
9.1 Creatingtheweavespecificationfile . . . . . . . . . . . . . . . . . . . . . . . . . . 83
9.2 Invokingtheweaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
9.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
10 Conclusionandfuturework 89
10.1 FutureworkontheCILWeavingTool . . . . . . . . . . . . . . . . . . . . . . . . . 90
Bibliography 92
A TheCILInstructionSet 99
B AHelloWorldexampleintheCIL 107
C TheWeaveSpecificationfile 109
D ClassdiagramsWeaver 111
D.1 PEWeaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
D.2 ILWeaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
D.3 WeaveLibrary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
E ListingDotNETWeaveFileGenerator 117
F ListingILICIT 119
vi
List of Figures
1.1 Datesandancestryofseveralimportantlanguages . . . . . . . . . . . . . . . . . . 2
2.1 Componentsofthecompositionfiltersmodel . . . . . . . . . . . . . . . . . . . . . 15
2.2 UMLclassdiagramoftheobject-orientedPacmangame . . . . . . . . . . . . . . 17
?
2.3 OverviewoftheCompose architecture . . . . . . . . . . . . . . . . . . . . . . . . 21
3.1 Contextofthe.NETframework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2 RelationshipsintheCTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3 MaincomponentsoftheCLIandtheirrelationships . . . . . . . . . . . . . . . . . 31
3.4 Fromsourcecodetomachinecode . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.1 Single-fileandmultifileassemblylayout. . . . . . . . . . . . . . . . . . . . . . . . 40
5.2 TheCommonTypeSystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.1 ThePhoenixcompilerplatform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
7.2 TheSourceWeave.NETarchitecture. . . . . . . . . . . . . . . . . . . . . . . . . . . 61
7.3 TheWeave.NETarchitecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
7.4 ThetwoCOMinterfacesoftheprofilingAPIs. . . . . . . . . . . . . . . . . . . . . 64
8.1 Dataflowdiagramweaver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.2 DataflowdiagramPEWeaver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
8.3 DataflowdiagramILWeaver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.1 ClassdiagramDotNETWeaveFileGenerator,Compose*moduleCONE. . . . . . . 84
9.2 ClassdiagramILICIT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
9.3 TheintegrationoftheCILWeaverToolintheCompose*architecture. . . . . . . . 87
vii
LISTOFFIGURES
D.1 ClassdiagramPeWeaver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
D.2 ClassdiagramProcessManager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
D.3 ClassdiagramILWeaver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
D.4 ClassdiagraminternalILrepresentation,theIlStructure. . . . . . . . . . . . . . . 114
D.5 Classdiagraminternalrepresentationoftheweavespecification. . . . . . . . . . 116
viii
Description:CONTENTS. 6 Mapping AOP constructs to the Common Intermediate Language
7.4 Approach 3: Adapting the Common Language Runtime 66.