Title: CLR: Under the Hood
1CLR Under the Hood
Brad Abrams brada_at_Microsoft.com Program
Managerhttp//blogs.msdn.com/brada
2Health Warning
- This talk dives deep!
- Examines internal data structures
- They will change!
- Internals knowledge not needed to write .NET apps
3Quick Refresher The Unmanaged World
C source
VB6 source
Compiler
Compiler
.obj
PCode/x86 .exe
Linker
VBRun
.exe
Hardware Platform
Loader
Hardware Platform
4Quick Refresher The CLR
Compilation
Code (IL)
Source Code
Metadata
At installation or the first time each method is
called
5Agenda
QA
61. Compilation
HelloAtlanta.cs
using Systemclass Output private int
year public Output(int year)
this.year year public void
SayHello() Console.WriteLine("Hello,
its the year " year) class
HelloAtlanta static void Main(string
args) new Output(2004).SayHello()
new Output(2005).SayHello()
7Agenda
QA
82. Packaging Assemblies
92. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based virtual machine
1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
Evaluation Stack
102. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based
1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0001
Evaluation Stack
112. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based
1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0002
0000 0001
Evaluation Stack
122. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based
1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0003
Evaluation Stack
132. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based
1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0003
0000 0003
Evaluation Stack
142. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based
1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0006
Evaluation Stack
152. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based
1 2 3 - 4 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0004
0000 0006
Evaluation Stack
162. MetadataIL Intermediate Language
- IL The language for execution
- Independent of CPU and platform
- Created by Microsoft, external commercial and
academic language/compiler writers - Stack based
1 2 3 4 2 IL_0001 ldc.i4.1
IL_0002 ldc.i4.2 IL_0003 add IL_0004
ldc.i4.3 IL_0005 add IL_0006
ldc.i4.4 IL_0007 sub
0000 0002
Evaluation Stack
172. MetadataIL Verification
- When processing IL, runtime verifies the IL to
make sure its safe - Every IL construct is called with the correct
amount of stack parameters - Every method is called with the correct type and
number of parameters - Helps prevents buffer overflows, underruns
- Helps prevent security holes
- Safety allows multiple managed applications in
the same process
18Agenda
QA
193. Loading and LayoutStartup logic
- OS hands the image to a CLR shim (mscoree.dll)
- Which starts the runtime
- Runtime performs the following
- Loads assembly in to memory and sets up the MD
reader - Resolves immediate dependencies
- Slurps metadata, creates internal data structures
- Starts execution at Assembly entry point
20Agenda
QA
214. ExecutionInvocation
Objects in GC heap
Private execution engine memory
78abed08 System.Object.ToString()
78a7fe50 System.Object.Equals(System.Object)
78a74de8 System.Object.GetHashCode()
78abed58 System.Object.Finalize()
03690ceb Output..ctor(Int32)
03690cfb Output.SayHello()
03790118 Output.SayHello()
MethodTable(runtime info)
2004
03690cfb Prestub Dispatch
03790118 push edipush esipush ebx SayHello()
Native Code
JIT Compiler
newobj instance void Output.ctor(int32) call
instance void OutputSayHello()
224. ExecutionInvocation JIT output
Output.SayHello()
push edi push esimov edi,ecx mov
esi,dword ptr ds01AF201Chmov
ecx,788F34E8hcall FD34FF55 //
JIT_DbgIsJustMyCodemov edx,eaxmov eax,dword
ptr edi4mov dword ptr edx4,eaxmov
ecx,esicall 7530FA52mov esi,eaxcmp dword
ptr ds01AF1070h,0jne 0000000Cmov
ecx,1call 752E8A85 // System.String.Concatmov
ecx,dword ptr ds01AF1070hmov edx,esicall
dword ptr ds037B0010h // System.Console.WriteLi
ne pop esi pop ediret
234. ExecutionJIT Optimizations
- Register Allocation
- locals, temps, evaluation stack
- Loop unroll
- Dead code elimination
- define SOMETHING 0 if (SOMETHING gt 10) a
x // dead code statement - Constant and Copy propagation
- Processor specific code generation
244. ExecutionJIT Optimizations
//Range check will be eliminatedfor (int i 0
i lt myArray.Length i) Console.WriteLine(m
yArrayi.ToString()) //Range check will NOT
be eliminatedfor (int i 0 i lt myArray.Length
- y i) Console.WriteLine(myArrayi
x.ToString())
25Agenda
QA
265. Runtime servicesA look at the Garbage
Collector
- Reference tracking (tracing) GC
- Large object heap objects over 80k
- Generational (three gen)
- Mark sweep and compact
27Agenda
QA
28More Information
- My Blog http//blogs.msdn.com/brada
Applied Microsoft .NET Framework Programming
Shared Source CLI Essentials
Compiling for the .NET CLR
Common Language Infrastructure Annotated Standard
The SLAR
Inside MS .NET IL Assembler
29Questions?
30BACKUP
31New runtime features for V2.0
- Generics
- 64 bit (Itanium and x86-64)
- ReflectionOnly context
- Delegate Relaxation
- Lightweight Code Generation
- NGen/NGen services
- Stub based dispatch
- MDbg
- Edit and Continue
- BCL Enhancements
32Generational GC
New Heap Begins with New Generation
Accessible References Keep Objects Alive
Preserves / Compacts Referenced Objects
Objects Left Merge with Older Generation
New Allocations Rebuild New Generation
33Generational GC
- Generations Dynamically Tuned
- CPU Cache Size
- Acceptable Fragmentation
- Older Generations are Larger / More Stable
- Require Collection Less Often
- Are More Expensive to Collect
- Can Have References to Newer Objects
34What is Generics?
- Type checking, no boxing, no downcasts
- Increased sharing (typed collections)
- Instantiated at run-time, not compile-time
- Work for both reference and value types
- Code shared for reference types
- Exact run-time type information
35What is Generics?
Generic Type Declaration public class ListltTgt public void Add(T item)
Generic Method public static void SwapltTgt(ref T ref1, ref T ref2)
Type Parameter public class DictionaryltK,Vgt
Type Argument Dictionaryltstring,intgt map new Dictionaryltstring,intgt()
Constraints public class ListltTgt where TIComparable, Tnew()
Open Constructed Type ListltTgt
Closed Constructed Type Listltstringgt
36C Generics
public class ListltTgt private T elements
private int count public void Add(T
element) if (count elements.Length)
Resize(count 2) elementscount
element public T thisint index
get return elementsindex set
elementsindex value public
int Count get return count
Listltintgt intList new Listltintgt() intList.Add(
1) // No boxing intList.Add(2) // No
boxing intList.Add("Three") // Compile-time
error int i intList0 // No cast required
37Bits bytes under the hood
- New metadata tables
- Flags defines variance and special constraints
- GenericParamConstraint defines type constraints
38Implementation choices
- Code sharing
- Modula 3 and ML use code sharing
- Type identity can be a problem
- Runtime type checking required
- Code specialization
- C templates uses specialization
- Code bloat can be a problem
394. ExecutionJIT Optimizations
- JIT Inlining (method example)
Class Fib Shared Sub NextFib (ByRef i As
Integer, ByRef j As Integer) Dim k As
Integer i j i j j k End
Sub Shared Sub Main() Dim fib1 As
Integer 1 Dim fib2 As Integer 1
Dim n As Integer For n 3 To 36
NextFib (fib1, fib2) Next n End
SubEnd Class
404. ExecutionJIT No inline SLOW
push ebp mov ebp,esp sub esp,0Ch
push esi
Prolog
xor eax,eax mov ebp-4,eax mov
ebp-8,eax xor esi,esi nop
Zero vars
mov dword ptr ebp-4,1 mov dword ptr
ebp-8,1 mov esi,3
Init vars
lea ecx,ebp-4 lea edx,ebp-8 call
003E50D4h gt nop
nop
Call NextFib
n
add esi,1 jno 00000009
Overflow Handler
xor ecx,ecx call 764618ED
cmp esi,24h jle FFFFFFE3 nop
Next n
nop pop esi mov esp,ebp
pop ebp ret
Epilog
414. ExecutionNextFib() method
push ebp mov ebp,esp sub esp,0Ch push
edi push esi push ebx mov edi,ecx mov
esi,edx xor ebx,ebx nop mov eax,edi add
eax,esi jno 00000009 xor ecx,ecx
call 764618B7 mov ebx,eax mov eax,esi mov
edi,eax mov esi,ebx nop pop ebx pop
esi pop edi mov esp,ebp pop ebp ret
424. ExecutionJIT Inlining FAST!
sub esp,8 push edi push esi
Prolog
xor eax,eax mov esp8,eax mov espC,eax
Zero vars
mov esp8,1 mov esp0C,1 mov edi,3
Init vars
lea esi,esp8 lea ecx,espC mov eax,esi
mov edx,ecx add eax,edx jo 16 mov
esi,edx mov ecs,eax
Calc next
add edi,1 jo 0D cmp edi,24h jle
FFFFFFE4
Next n
pop esi pop edi add esp,8 ret
Epilog