Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Directives Ouick Reference
NAME
.addon
.assembly
.class
.corflags
.custom
.data
.entrypoint
.event
.export
-NOTE This table is meant to serve as an aid for remembering how the various directives are used; the descriptions are brief in nature and do not explain the entire story behind their related directives (Chapter 2 covers the directives in detail).
DESCRIPTION Allows clients to register for event notifications
Defines information about the assembly (also used to reference an assembly)
Defines a new type
States the type of material contained in the PE file
Adds attribute information
Defines a data segment
Defines the first method that will be called on assembly execution
Defines an event on the class
Gives a name for an exported method
.field Defines a field for a class
.file References a file
.fire Defines a method that will raise the associated event
.get Defines the retrieval method for a property
.hash States the algorithm used to create the hash value, or contains the hash value of a referenced assembly or file
.locale Defines the targeted culture
.locals Defines local variables in a method
.maxstack States the maximal number of values on the stack
.method Adds a method to a class
.module Adds a module to the assembly
.mresource
.namespace
.other
Adds resource information
Groups logically similar classes together
Defines extra property methods
NAME
.override
.pack
.param
.permission
. permissionset
.property
.publickey
DESCRIPTION States that the method overrides a base method
States the field boundary size
Defines default values for optional arguments
Defines security access information about one resource
Defines security access information about zero or more resources
Defines a property in the class
Defines the public key used to sign the assembly
.publickeytoken Contains 8 bytes of a referenced assembly's public key
.removeon
.set
.size
.subsystem
.try
.ver
.vtentry
.vtfixup
Allows clients to unregister event notifications
Defines the modification method for a property
UDefines a block of memory in a class
States the user interaction preference
Defines a try block
Either states the current version of the assembly or the desired version of a referenced assembly
States which slot the method should be in the v-table
Defines a v-table for exported methods
CIL Programming: Under the Hoodm of .NET
JASON BOCK
CIL Programming: Under the Hood" of .NET Copyright © 2002 by Jason Bock Originally published by Apress in 2002 Softcover reprint ofthe hardcover 1st edition 2002
AU rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN 978-1-4302-5156-9 ISBN 978-1-4302-0845-7 (eBook) DOI 10.1007/978-1-4302-0845-7
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
Technical Reviewer: Dan Fergus Editorial Directors: Dan Appleman, Peter Blackburn, Gary Cornell, Jason Gilmore, Karen Watterson, John Zukowski Managing Editor: Grace Wong Copy Editor: Ami Knox Compositor: Diana Van Winkle, Van Winkle Design Indexer: Carol Burbo Artist and Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Marketing Manager: Stephanie Rodriguez
The information in this book is distributed on an "as is" hasis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor Apress shall ha ve any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.
IN MEMORANDUM
Ronald E Bock
1939-2001
For all that you did for me,
for the encouragement you gave me,
and for being my dad.
I'll miss you on the golf course.
Brief Contents
About the Author .............................................................................................. xiii
About the Technical Reviewer .................................................................... xiv
Acknowledgments ................................................................................................... xv
Introduction ....................................................................................................... xvii
Chapter 1 Language Interoperability ................................................. 1
Chapter 2 ilasm Directives and Attributes .................................. 23
Chapter 3 CIL Opcodes .............................................................................. 69
Chapter 4 ilasm and CIL in Practice ........................................... 121
Chapter 5 Debugging CIL ......................................................................... 149
Chapter 6 .NET Languages and CIL .................................................... 193
Chapter 7 Emitting Types ...................................................................... 239
Chapter 8 Dynamic Proxies in • NET ............................................... 271
Chapter 9 CIL Tips ................................................................................... 317
Appendix A The Future of CIL ............................................................ 319
Appendix 8 • NET Languages ................................................................... 327
Index ...................................................................................................................... 329
v
Contents About the Author ............................................................................................... xiii About the Technical Reviewer ..................................................................... xiv Acknowledgments ................................................................................................... xv Introduction ....................................................................................................... xvii
Chapter 1 Language Interoperability ........................................ 1
Language Interoperabili ty in Action ........................................................ 1 Language Interoperability in Theory ........................................................ 8 Language Interoperability in Practice .................................................. 12 Conclusion ............................................................................................................. 21
Chapter 2 ilasm Directives and Attributes ...................... 23
General Assembly ilasm .................................................................................. 23 Defining the Assembly ..................................................................................... 23 Defining the Modules ...................................................................................... 30 Defining the UI ................................................................................................. 30 Concurrent Assembly Execution ..................................................................... 31 Defining Data Segments .................................................................................. 31
Type-Specific Directives .............................................................................. 32 Defming the Type ............................................................................................. 32
Adding Fields to a Type ................................................................................ 41 Field Scope ........................................................................................................ 42 Field Initialization ............................................................................................ 43 serializable Redux ............................................................................................. 44
Adding Methods to Types ................................................................................ 45 Type and Instance Methods ............................................................................ 45 Method Implementation Details .................................................................... 46 Calling Conventions ......................................................................................... 46 Passing Arguments ........................................................................................... 48 Unmanaged Calls ............................................................................................. 48 Type and Instance Initializers ......................................................................... 49 beforefieldinit Redux ... , .................................................................................... 50 Exporting Methods ...................................... , .................................................... 51 Method Body Directives .................................. , ............................................... 53
Defining Properties in Types ..................................................................... 62 Adding Events to Types .................................................................................. 64 Conclusion ............................................................................................................. 67
vii
Contents
Chapter 3 CIL Opcodes .......................................................................... 69
Basic Stack Information ................................................................................ 69 Opcode Essentials ............................................................................................. 70 Basic Opcodes ...................................................................................................... 71
UsingLocalVariables ....................................................................................... 71 Conversions ...................................................................................................... 74 Using Method Arguments ................................................................................ 77 Using Type Fields .............................................................................................. 79
Basic Operations ................................................................................................ 80 Arithmetic Operations ..................................................................................... 80 Binary Operations ............................................................................................ 83 Comparison Operations .................................................................................. 85
Object Instructions ........................................................................................ 86 Creating Type Instances ................................................................................... 86 Calling Methods ............................................................................................... 88 Constructors ..................................................................................................... 91 Unmanaged Method Calls ............................................................................... 91 Method Pointers ............................................................................................... 93 Boxing, Unboxing, and Value Type Indirection .............................................. 94 Type Casting ...................................................................................................... 96 Array Manipulation .......................................................................................... 97 Memory Manipulation ..................................................................................... 99 StringManipulation ....................................................................................... 101
Controlling Code Flow ................................................................................. 102 Handling Exceptions ...................................................................................... 106 Opcode Miscellanea ........................................................................................ 114
Variable Argument Lists ................................................................................. 114 .maxstack Calculations .................................................................................. 116 Pointer Prefixes ............................................................................................. .. 117
Method Jumps ................................................................................................ 117 Conclusion ......................................................................................................... 119
Chapter 4 ilasm and CIL in Practice .................................. 121
Generating Random Numbers ......................................................................... 121
Designing the Essentials ......................................................................... 122 Implementing the Essentials .................................................................... 123
Stubbing Out the Delegate ............................................................................ 123 Field Definitions ............................................................................................. 125 Event Methods ................................................................................................ 125
viii
Contents
Constructors ................................................................................................... 127 Creating the Properties .................................................................................. 128 Implementing Generate() .............................................................................. 129 Implementing GenerateViaDelegateO ......................................................... 131 Implementing GenerateViaEventO ............................................................... 132 Generating the RIG Assembly ....................................................................... 133
Designing the Test Harness ...................................................................... 133 Implementing the Test Harness ................................................................ 134
Referencing Assemblies for UI Design ......................................................... 134 Defining UI Components .............................................................................. 135 Initializing the Form ....................................................................................... 136 Calling Generate() .......................................................................................... 138 Calling GenerateViaDelegate() ...................................................................... 141 Calling GenerateViaEvent() ........................................................................... 143 Generating the RIGTestClient Assembly ...................................................... 144
Conclusion ......................................................................................................... 147
Chapter 5 Debugging CIL .................................................................... 149
Debug Builds ..................................................................................................... 149 The Command-Line Tool: cordbg.exe ....................................................... 151
cordbg Basics .................................................................................................. 151 Watching Register Values and Native Instructions ...................................... 156 Setting Breakpoints ........................................................................................ 159 Modes .............................................................................................................. 160 Recording and Using cordbg Scripts ............................................................. 161
The GUI Tool: dbgclr.exe ........................................................................... 163 DebuggingWindows ...................................................................................... 165 Saving the Solution ......................................................................................... 170 Debugger Differences .................................................................................... 171
Debugging In-Process Assemblies ........................................................... 171 DLLs in dbgclr ................................................................................................. 171 DLLs in cordbg ............................................................................................... 176
Registry Control ............................................................................................ 176 Verifying Assemblies .................................................................................... 178 Debugging Compiled Programs .................................................................... 182
Preventing Recompilation ............................................................................. 186 Conclusion ......................................................................................................... 192
ix
Contents
Chapter 6 . NET Languages and CIL ............................................ 193
Debug and Release Builds ........................................................................... 193 The C# Implementation ................................................................................. 194 The VB .NET Implementation ....................................................................... 198 The Component Pascal Implementation ..................................................... 202 Commentary ................................................................................................... 204
Language Constructs ...................................................................................... 205 VB .NET's With Statement .............................................................................. 205 Implementing Interface Methods ................................................................ .208 On Error Resume Next, or How to Create a Lot of CIL ................................ 214
Active Objects ................................................................................................. 221 Language Interoperability: The Real Story ...................................... 224
Inheritance with Oberon .NET Types ........................................................... 224 Overloaded and Overridden Methods .......................................................... 226 The Other Property ........................................................................................ 230 Overloading Methods in CIL ......................................................................... 234
Conclusion .......................................................................................................... 237
Chapter 7 Emitting Types ................................................................. 239
Applying CIL ....................................................................................................... 239 Emitter Basics ................................................................................................... 240
Defining the Essentials .................................................................................. 240 Creating Assemblies ....................................................................................... 241 Building Types ................................................................................................ 243 Adding Fields .................................................................................................. 244
Emitting Methods .............................................................................................. 244 Adding Methods ............................................................................................. 244 Implementing Methods ................................................................................. 246 Exception Handling ........................................................................................ 250
Persisting the Results ............................................................................... 251 Baking Types ................................................................................................... 251 Transient-Oniy Assemblies ............................................................................ 252 Persistent -Oniy Assemblies ........................................................................... 252 Transient, Persistent Assemblies ................................................................... 254
Beyond the Basics .......................................................................................... 254 Setting Entry Points ........................................................................................ 254 Adding Attributes ........................................................................................... 255 Modifying Method Parameter Information ................................................. 256
X
Contents
Debugging Dynamic Assemblies .................................................................. 258 Emitting Debug Information ......................................................................... 258 Adding to the Symbol Writer ......................................................................... 258 Reloading the Assembly ................................................................................. 260 Where's the Target IL File? .............................................................................. 262 Creating the DebugFile Class ........................................................................ 263 Debugging Transient Modules ...................................................................... 269
Conclusion ......................................................................................................... 270
Chapter 8 Dynamic Proxies in • NET ....................................... 271
Separating Concerns ...................................................................................... 271 Functional Specifications ......................................................................... 274 Class Design and Method Call Conditions .......................................... 275
Receiving a Notification before Method Invocation ................................... 276 Receiving a Notification after Method Invocation ....................................... 277 When Method Invocations Go Bad ............................................................... 278 Proxy Call Flow ............................................................................................... 278
Implementing Proxy Creation .................................................................... 279 Preconditional Checks ................................................................................... 279 Caching JYpes, Modules, and Assemblies .................................................... 282 Defining the Constructors ............................................................................. 285 Implementing the Interfaces ......................................................................... 287 Invoking Target Methods ............................................................................... 287 Setting Up the Method Hooks ....................................................................... 294
Testing the Solution .................................................................................... 301 Loading the Form ........................................................................................... 302 Creating Proxies .............................................................................................. 302 Type Inspection .............................................................................................. 305
Contexts and . NET .......................................................................................... 309 Dynamic Proxies and Contexts ..................................................................... 314
Conclusion ......................................................................................................... 315
Chapter 9 CIL Tips ................................................................................ 317
Be CLS-Compliant Aware ............................................................................... 317 Use Verification Tools ............................................................................... 317 Comments and Code Regions ......................................................................... 318 Stay Location Savvy When Emitting Assemblies ............................... 318 Final Thoughts ................................................................................................. 318
xi
Contents
Appendix A The Future of CIL ....................................................... 319
Generics in CIL .............................................................................................. 319 Extended IL ....................................................................................................... 322 Inline CIL ......................................................................................................... 324
Appendix 8 • NET Languages .............................................................. 327
Index .................................................................................................................... 329
xii
About the Author
Jason Bock is a senior consultant for Magenic Technologies, a Microsoft Gold
Certified consulting firm. He has worked on a number of business applications using a diverse set of tools and technologies such as VB, COM, and Java. He is also the author of Visual Basic 6 Win32 API Tutorial and has written articles and given presentations on technical development issues regarding VB. He has a bachelor's degree and a master's degree in electrical engineering from Marquette University.
When he's not developing programs, writing books, or giving presentations, Jason enjoys spending time with his wife Liz, golfing, biking, reading, listening to and playing music, and watching The Simpsons whenever he can. Visit his Web site at http://www. jasonbock. net.
xiii
xiv
About the Technical Reviewer
Dan Fergus is the president and lead developer and architect at Forest Software Group (http: I /www. forestsoftwaregroup. com). His most recent employment was with NuMega Technologies working on several of their debugging tools. Most recently he was the development lead for the CodeReview and FailSafe products. Dan travels the world teaching and presenting sessions on a range of topics including debugging, the .NET Framework, Web services, and Windows CE programming. Dan has had many articles and white papers published over the years and continues to write for several magazines and also for Apress.
When he is not sitting at a keyboard, Dan is busy with his four young sons on the baseball fields around Fort Worth, Texas.
Acknowledgments
While this book only has one author's name on the cover, there are a number of people who have contributed in different ways to making this book better than it ever could have been without them. First, I must thank my wife, Liz, for putting up with me being in front of the computer every night until late in the evening instead of being with her as I wrote this book. Your patience and support is forever appreciated. I love you.
To my mom, Mary, thanks for everything you have done for me. You have gone through such a difficult time in your life-I can't imagine how hard it's been. You've always encouraged me whenever I take up a project like this, and I truly appreciate the support.
The Apress staff has been great. I thank Gary Cornell for talking to me about Apress a year and a half ago (it's been that long?) and letting me write a book on CIL when everyone else was writing a book on how to create .NET assemblies with C#. The road has been difficult, but rewarding, and I'm glad you gave me the opportunity to write this book. Thanks also go to Dan Appleman, Stephanie Rodriguez, Grace Wong, Ami Knox, and anyone else at Apress who has been involved with this book-you turned my writing into a far better product than what I could've done on my own. I also thank Dan Fergus for taking the time to edit the book-your suggestions were very helpful.
The technical staff at Magenic Technologies has been awesome. I have never worked for a company where the developers are so willing to share their knowledge as well as help people out with technical issues. A number of times, I ran into some weird issues with CIL, and people would dive into the discussions to try and figure the problem out. Thanks go to Wade Baron, Jeff Ferguson, Tom Fischer, Fred Johnson, Ross Johnson, Robert Knutson, Rocky Lhotka, Steve Lundeen, and anyone else who helped but I forgot to include in this list.
I also thank John Gough for helping me out with Component Pascal syntax and Jay Freeman and Joe Hummel for helping me solve one of the first problems I had with the Emitter classes.
Finally, to the rest of my friends and family, thanks for just being there.
XV
Introduction
"It is also tempting to compile several different languages into the same intermediate language and use a common back end for the different front ends, thereby obtaining several compilers for one machine. However, because of subtle differences in the viewpoints of different languages, there has been only limited success in this direction."
-Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools (Addison-Wesley Publishing Co., 1985)
When I first saw .NET, I was overwhelmed by the influx of new ideas and concepts. Web developers were getting a vastly improved framework with ASP.NET and Web services. Visual Basic was getting a major overhaul with VB .NET, adding inheritance and free threading into the mix along with removing some long-standing BASIC compatibility. A new language was introduced-C#-that looked a lot like Java with some subtle yet substantial differences. Security was markedly improved, allowing users to prevent code from running based on the code itself and not just on the current user's credentials.
Slowly but surely, bit by bit (and then byte by byte) I became familiar with .NET. However, there was one aspect that I became very interested in. One of the first presentations I saw on .NET demonstrated how a class in C# could be the base class in VB .NET. Debugging between the two languages was seamless. I had never seen anything like this before; using COM as the interop layer between C++ and VB just did not cut it. But how did it work? How could I create classes in one language that could be used in another? How could Microsoft claim that there were a large number of languages targeting the .NET platform?
Then I found out about IWasm. After I loaded my first assembly and saw my first disassembled method, it was all downhill from there.
Target Audience
If you've spent some time with C# or VB .NET and you want to know what the
results are when you press Ctrl-Shift-B (which compiles your code), or you want to know how code in one language can be used so seamlessly in another, then this book is for you. You may have heard about the Common Intermediate Language,
xvii
Introduction
xviii
or CIL, or you may have heard about the fact that in .NET you can generate code on the fly. Either way, you're not really comfortable with words like ldfld or .assembly, but you want to figure them out. I wrote this book to help you understand what these statements are doing and how you can use them in your future .NET projects.
Granted, learning CIL isn't easy. I've spent a lot of late nights trying to figure out what was going on with my CIL code. The bugs can be subtle and insidious to root out. But I think that by learning CIL, you gain tremendous insights into the inner workings of .NET. C#, Component Pascal, Mondrain ... none of these languages truly speak the .NET truth. Only CIL can fully express what can be done in.NET.
Source Code
I have created a number of applications that demonstrate CIL in action. You can download the code from Apress's Web site at http: I lwww. a press. com. I have made every attempt to ensure that the code compiles and behaves as expected, but mistakes can occur. If you find a bug in the source code, or you find an erroneous statement in the book itself, please contact me at jason@jasonbock. net and I'll make sure that updates are made accordingly.
Partition Documents
Throughout the book, I'll make references to a set of specification documents known as the Partition documents. These documents were sent to ECMA as part of the standardization process of .NET -they contain valuable information about how .NET is structured. When you install VS .NET, you should be able to find them in the following folder: Program Files \Microsoft Visual Studio .NET\FrameworkSDK\Tool Developers Guide\docs. You can also find them online at http: I lmsdn. microsoft. comlnetlecmal.