150 likes | 288 Views
The .Net Execution system. (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay Dundee. Last updated 26 th July 2006. Agenda. The Execution system IL code verification Type safety Type verification. Introduction.
E N D
The .NetExecution system (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay Dundee Last updated 26th July 2006
Agenda • The Execution system • IL code verification • Type safety • Type verification
Introduction • This presentation looks at some of the components that make up the run-time execution system for a .Net program. • It will be based around the functionality provided by the CLR. • Central to the .Net philosophy is the assurance of code safety and security.
CLR Subsystems • The type system. • manages type access & verifies type safety. • The metadata system. • manages & checks assembly manifests. • The execution system. • manages the execution of the IL code.
The Execution system • Responsible for the controlled execution of managed code. • This includes • JIT compilation • memory management • security checking. • A design goal of this system was that it should not be bound to any single hardware platform.
IL Code • An object-oriented assembly language. • Targeted for an abstract stack-based machine. • Example instruction functionality • push, pop, method call, create object. • It executes on a virtual machine with no expectations of specific registers. • Can therefore be ported to a variety of hosts that support a CLR implementation.
Some IL Instructions • box, unbox • convert stack values to object values on the heap. • callvirt • dynamic method call where the run-time type of the object on which the call is invoked determines the actual method call. • newobj • creates a new object on the managed heap.
An IL Code Example class ILexample { public static void Main () { int i = 5; MyClass obj = new MyClass(); obj.MyMethod (i); } } ldc.i4.5 stloc.0 newobj instance void ExecModel.MyClass::.ctor() stloc.1 ldloc.1 ldloc.0 callvirt instance void ExecModel.MyClass::MyMethod(int32) ret
The Method IL code class MyClass { public void MyMethod (int x) { int a; a = x + 2; } } ldarg.1 ldc.i4.2 add stloc.0 ret
Starting A Program • An executable CLR program has at least 3 components • a user defined assembly with entry point • execution system (mscorsvr.dll or mscorwks.dll) • basic type system (mscorlib.dll) • The two execution systems are for server or workstation environments.
Downloaded & Mobile Code • A system must protect itself from executable code that is downloaded. • Either allow it to execute or not. • Restrict access to local resources (Java sandbox approach). • Combine assurance of type safety with a flexible security system that defines permissions (.Net approach).
Type Safety • An IL program is type-safe if it • accesses types only according to their contract, • cannot result in stack overflow or underflow, • correctly uses the exception handling mechanism, and • initialises all objects.
Type Verification • To ensure type-safety the CLR must • verify the assembly manifest, and • verify the types within the assembly. • Verifying the metadata in the manifest ensures that all tokens and indexes are valid & there are no buffer overruns. • Verifying the assembly types ensures that all type contracts defined in the metadata are adhered to.
IL Code Categories • Illegal : JIT compiler cannot process the code, e.g. because of invalid op-codes. • Legal : code can be compiled but it may contain non-type safe instructions. • Type-safe : types interact only through published contracts. • Verifiable : can be assured to be type safe through a verification algorithm.
CLR Type Verification of ILCode … • is conservative. • Code that fails verification may still be type-safe. • occurs during JIT compilation. • requires that unmanaged code is fully trusted as it will not be verified.