This specification is a detailed description of the Common Intermediate Language (CIL) instruction set, part of the specification of the Common Language Infrastructure. Partition I describes the architecture of the CLI and provides an overview of a large number of issues relating to the CIL instruction set. That overview is essential to an understanding of the instruction set as described here.
Each instruction description describes a set of related CLI machine instructions. Each instruction definition consists of five parts:
In addition, operations that have a numeric operand also specify an operand type table that describes how they operate based on the type of the operand. See Partition III, section 1.5. Note that not all instructions are included in all CLI profiles. See Partition IV for details. 1.1 Data TypesWhile the Common Type System (CTS) defines a rich type system and the Common Language Specification (CLS) specifies a subset that can be used for language interoperability, the CLI itself deals with a much simpler set of types. These types include user-defined value types and a subset of the built-in types. The subset is collectively known as the "basic CLI types":
Note that object references and pointer types may be assigned the value null. This is defined throughout the CLI to be zero (a bit pattern of all bits zero).
1.1.1 Numeric Data Types
NOTE Short integers are loaded as 4-byte numbers on all architectures, and these 4-byte numbers must always be tracked as distinct from 8-byte numbers. This helps portability of code by ensuring that the default arithmetic behavior (i.e., when no conv or conv.ovf instructions are executed) will have identical results on all implementations. Convert instructions that yield short integer values actually leave an int32 (32-bit) value on the stack, but it is guaranteed that only the low bits have meaning (i.e., the more significant bits are all zero for the unsigned conversions or a sign extension for the signed conversions). To correctly simulate the full set of short integer operations, a conversion to the short form is required before the div, rem, shr, comparison, and conditional branch instructions. In addition to the explicit conversion instructions, there are four cases where the CLI handles short integers in a special way:
In the last two cases it is up to the native calling convention to determine whether values are actually truncated or extended, as well as whether this is done in the called procedure or the calling procedure. The CIL instruction sequence is unaffected, and it is as though the CIL sequence included an appropriate conv instruction.
NOTE The above specification allows a compliant implementation to avoid rounding to the precision of the target type on intermediate computations, and thus permits the use of wider precision hardware registers, as well as the application of optimizing transformations which result in the same or greater precision, such as contractions. Where exactly reproducible behavior is required by a language or application, explicit conversions may be used. When a floating point value whose internal representation has greater range and/or precision than its nominal type is put in a storage location, it is automatically coerced to the type of the storage location. This may involve a loss of precision or the creation of an out-of-range value (NaN, +infinity, or infinity). However, the value may be retained in the internal representation for future use, if it is reloaded from the storage location without having been modified. It is the responsibility of the compiler to ensure that the memory location is still valid at the time of a subsequent load, taking into account the effects of aliasing and other execution threads (see Partition I, section 12.6). This freedom to carry extra precision is not permitted, however, following the execution of an explicit conversion (conv.r4 or conv.r8), at which time the internal representation must be exactly representable in the associated type. NOTE To detect values that cannot be converted to a particular storage type, use a conversion instruction (conv.r4, or conv.r8) and then check for an out-of-range value using ckfinite. To detect underflow when converting to a particular storage type, a comparison to zero is required before and after the conversion. NOTE This standard does not specify the behavior of arithmetic operations on denormalized floating point numbers, nor does it specify when or whether such representations should be created. This is in keeping with IEC 60559:1989. In addition, this standard does not specify how to access the exact bit pattern of NaNs that are created, nor the behavior when converting a NaN between 32-bit and 64-bit representation. All of this behavior is deliberately left implementation-specific. 1.1.2 Boolean Data TypeA CLI Boolean type occupies 1 byte in memory. A bit pattern of all zeros denotes a value of false. A bit pattern with any bit set (analogous to a non-zero integer) denotes a value of true. 1.1.3 Object ReferencesObject references (type O) are completely opaque. There are no arithmetic instructions that allow object references as operands, and the only comparison operations permitted are equality (and inequality) between two object references. There are no conversion operations defined on object references. Object references are created by certain CIL object instructions (notably newobj and newarr). Object references can be passed as arguments, stored as local variables, returned as values, and stored in arrays and as fields of objects. 1.1.4 Runtime Pointer TypesThere are two kinds of pointers: unmanaged pointers and managed pointers. For pointers into the same array or object, the following arithmetic operations are defined:
None of these operations is allowed in verifiable code. It is important to understand the impact on the garbage collector of using arithmetic on the different kinds of pointers. Since unmanaged pointers must never reference memory that is controlled by the garbage collector, performing arithmetic on them can endanger the memory safety of the system (hence it is not verifiable), but since they are not reported to the garbage collector there is no impact on its operation. Managed pointers, however, are reported to the garbage collector. As part of garbage collection both the contents of the location to which they point and the pointer itself can be modified. The garbage collector will ignore managed pointers if they point into memory that is not under its control (the evaluation stack, the call stack, static memory, or memory under the control of another allocator). If, however, a managed pointer refers to memory controlled by the garbage collector it must point to either a field of an object, an element of an array, or the address of the element just past the end of an array. If address arithmetic is used to create a managed pointer that refers to any other location (an object header or a gap in the allocated memory), the garbage collector's operation is unspecified. 1.1.4.1 Unmanaged PointersUnmanaged pointers are the traditional pointers used in languages like C and C++. There are no restrictions on their use, although for the most part they result in code that cannot be verified. While it is perfectly legal to mark locations that contain unmanaged pointers as though they were unsigned integers (and this is, in fact, how they are treated by the CLI), it is often better to mark them as unmanaged pointers to a specific type of data. This is done by using ELEMENT_TYPE_PTR in a signature for a return value, a local variable, or an argument or by using a pointer type for a field or array element. Unmanaged pointers are not reported to the garbage collector and can be used in any way that an integer can be used.
1.1.4.2 Managed Pointers (type &)Managed pointers (&) may point to a local variable, a method argument, a field of an object [(this includes an instance or static field of an object)], a field of a value type, an element of an array, or the address where an element just past the end of an array would be stored (for pointer indexes into managed arrays). Managed pointers cannot be null. (They must be reported to the garbage collector, even if they do not point to managed memory.)
Managed pointers are specified by using ELEMENT_TYPE_BYREF in a signature for a return value, a local variable, or an argument or by using a by-ref type for a field or array element.
1.2 Instruction Variant TableIn Partition III, section 3, an instruction variant table is presented for each instruction. It describes each variant of the instructions. The Format column of the table lists the opcode for the instruction variant, along with any arguments that follow the instruction in the instruction stream. For example:
The first one or two hex numbers in the Format column show how this instruction is encoded (its "opcode"). So, the ldarga instruction is encoded as a byte holding FE, followed by another holding 0A. Italicized type names represent numbers that should follow in the instruction stream. In this example a 2-byte quantity that is to be treated as an unsigned integer directly follows the FE 0A opcode. Any of the fixed-size built-in types (int8, unsigned int8, int16, unsigned int16, int32, unsigned int32, int64, unsigned in64, float32, and float64) can appear in format descriptions. These types define the number of bytes for the argument and how it should be interpreted (signed, unsigned, or floating point). In addition, a metadata token can appear, indicated as <T>. Tokens are encoded as 4-byte integers. All argument numbers are encoded least-significant-byte-at-smallest-address (a pattern commonly termed "little-endian"). Bytes for instruction opcodes and arguments are packed as tightly as possible (no alignment padding is done). The Assembly Format column defines an assembly code mnemonic for each instruction variant. For those instructions that have instruction stream arguments, this column also assigns names to each of the arguments to the instruction. For each instruction argument, there is a name in the assembly format. These names are used later in the instruction description. 1.2.1 Opcode EncodingsCIL opcodes are 1 or more bytes long; they may be followed by zero or more operand bytes. All opcodes whose first byte lies in the range 0x00 through 0xEF, or 0xFC through 0xFF, are reserved for standardization. Opcodes whose first byte lies in the range 0xF0 through 0xFB, inclusive, are available for experimental purposes. The use of experimental opcodes in any method renders the method invalid and hence unverifiable.
The currently defined encodings are specified in Table 6-1, Opcode Encodings.
1.3 Stack Transition DiagramThe stack transition diagram displays the state of the evaluation stack before and after the instruction is executed. Below is a typical stack transition diagram. …, value1, value2 …, result This diagram indicates that the stack must have at least two elements on it, and in the definition the topmost value ("top of stack" or "most recently pushed") will be called value2 and the value underneath (pushed prior to value2) will be called value1. (In diagrams like this, the stack grows to the right, along the page.) The instruction removes these values from the stack and replaces them by another value, called result in the description. 1.4 English DescriptionThe English description describes any details about the instructions that are not immediately apparent once the format and stack transition have been described. 1.5 Operand Type TableMany CIL operations take numeric operands on the stack. These operations fall into several categories, depending on how they deal with the types of the operands. The following tables summarize the valid kinds of operand types and the type of the result. Notice that the types referred to here are the types as tracked by the CLI rather than the more detailed types used by tools such as CIL verification. The types tracked by the CLI are: int32, int64, native int, F, O, and &.
1.5.1 Binary Numeric OperationsA op B (used for add, div, mul, rem, and sub). Table 6-2, Binary Numeric Operations, shows the result type, for each possible combination of operand types. Boxes holding simply a result type apply to all five instructions. Boxes marked indicate an invalid CIL instruction. Shaded boxes indicate a CIL instruction that is not verifiable. Boxes with a list of instructions are valid only for those instructions.
1.5.2 Unary Numeric OperationsUsed for the neg instruction. Boxes marked [in Table 6-3, Unary Numeric Operations,] indicate an invalid CIL instruction. All valid uses of this instruction are verifiable.
1.5.3 Binary Comparison or Branch OperationsThese return a Boolean value or branch based on the top two values on the stack. Used for beq, beq.s, bge, bge.s, bge.un, bge.un.s, bgt, bgt.s, bgt.un, bgt.un.s, ble, ble.s, ble.un, ble.un.s, blt, blt.s, blt.un, blt.un.s, bne.un, bne.un.s, ceq, cgt, cgt.un, clt, clt.un. Boxes marked [in Table 6-4, Binary Comparison or Branch Operations,] indicate that all instructions are valid for that combination of operand types. Boxes marked indicate invalid CIL sequences. Shaded boxes indicate a CIL instruction that is not verifiable. Boxes with a list of instructions are valid only for those instructions.
1.5.4 Integer OperationsThese operate only on integer types. Used for and, div.un, not, or, rem.un, xor. The div.un and rem.un instructions treat their arguments as unsigned integers and produce the bit pattern corresponding to the unsigned result. As described in the CLI specification, however, the CLI makes no distinction between signed and unsigned integers on the stack. The not instruction is unary and returns the same type as the input. The shl and shr instructions return the same type as their first operand, and their second operand must be of type native unsigned int. Boxes marked [in Table 6-5, Integer Operations,] indicate invalid CIL sequences. All other boxes denote verifiable combinations of operands.
1.5.5 Shift OperationsTable 6-6, Shift Operations, lists the legal combinations of operands and results for the shift instructions: shl, shr, shr_un. Boxes marked indicate invalid CIL sequences. All other boxes denote verifiable combinations of operand. If the "Shift-By" operand is larger than the width of the "To-Be-Shifted" operand, then the results are implementation-defined (e.g., shift an int32 integer left by 37 bits).
1.5.6 Overflow Arithmetic OperationsThese operations generate an exception if the result cannot be represented in the target data type. Used for add.ovf, add.ovf.un, mul.ovf, mul.ovf.un, sub.ovf, sub.ovf.un. The shaded uses [in Table 6-7, Overflow Arithmetic Operations,] are not verifiable, while boxes marked indicate invalid CIL sequences
1.5.7 Conversion OperationsThese operations convert the top item on the evaluation stack from one numeric type to another. The result type is guaranteed to be representable as the data type specified as part of the operation (i.e., the conv.u2 instruction returns a value that can be stored in an unsigned int16). The stack, however, can only store values that are a minimum of 4 bytes wide. Used for the conv.<to type>, conv.ovf.<to type>, and conv.ovf.<to type>.un instructions. The shaded uses [in Table 6-8, Conversion Operations,] are not verifiable, while boxes marked indicate invalid CIL sequences.
1.6 Implicit Argument CoercionWhile the CLI operates only on six types (int32, native int, int64, F, O, and &), the metadata supplies a much richer model for parameters of methods. When about to call a method, the VES performs implicit type conversions, detailed in Table 6-9, Signature Matching. (Conceptually, it inserts the appropriate conv.* instruction into the CIL stream, which may result in an information loss through truncation or rounding.) This implicit conversion occurs for boxes marked . Shaded boxes are not verifiable. Boxes marked indicate invalid CIL sequences. (A compiler is of course free to emit explicit conv.* or conv.*.ovf instructions to achieve any desired effect.) Further notes concerning this table:
1.7 Restrictions on CIL Code SequencesAs well as detailed restrictions on CIL code sequences to ensure:
there are a few further restrictions, imposed to make it easier to construct a simple CIL-to-native-code compiler. This section specifies the general restrictions that apply in addition to those listed for individual instructions.
1.7.1 The Instruction StreamThe implementation of a method is provided by a contiguous block of CIL instructions, encoded as specified below. The address of the instruction block for a method, as well as its length, is specified in the file format (see Partition II, section 24.4, Common Intermediate Language Physical Layout). The first instruction is at the first byte (lowest address) of the instruction block. Instructions are variable in size. The size of each instruction can be determined (decoded) from the content of the instruction bytes themselves. The size of and ordering of the bytes within an instruction are specified by each instruction definition. Instructions follow each other without padding in a stream of bytes that is both alignment- and byte-order-insensitive. Each instruction occupies an exact number of bytes, and until the end of the instruction block, the next instruction begins immediately at the next byte. It is invalid for the instruction block (as specified by the block's length) to end without forming a complete last instruction. Instruction prefixes extend the length of an instruction without introducing a new instruction; an instruction having one or more prefixes introduces only one instruction that begins at the first byte of the first instruction prefix. NOTE Until the end of the instruction block, the instruction following any control transfer instruction is decoded as an instruction and thus participates in locating subsequent instructions even if it is not the target of a branch. Only instructions may appear in the instruction stream, even if unreachable. There are no address-relative data addressing modes, and raw data cannot be directly embedded within the instruction stream. Certain instructions allow embedding of immediate data as part of the instruction; however, that differs from allowing raw data embedded directly in the instruction stream. Unreachable code may appear as the result of machine-generated code and is allowed, but it must always be in the form of properly formed instruction sequences. The instruction stream can be translated and the associated instruction block discarded prior to execution of the translation. Thus, even instructions that capture and manipulate code addresses, such as call, ret, etc. can be virtualized to operate on translated addresses instead of addresses in the CIL instruction stream. 1.7.2 Valid Branch TargetsThe set of addresses composed of the first byte of each instruction identified in the instruction stream defines the only valid instruction targets. Instruction targets include branch targets as specified in branch instructions, targets specified in exception tables such as protected ranges (see Partition I, section 12.4.2 and Partition II, section 18), filters, and handler targets. Branch instructions specify branch targets as either a 1-byte or a 4-byte signed relative offset; the size of the offset is differentiated by the opcode of the instruction. The offset is defined as being relative to the byte following the branch instruction. (Note: Thus, an offset value of zero targets the immediately following instruction.) The value of a 1-byte offset is computed by interpreting that byte as a signed 8-bit integer. The value of a 4-byte offset can be computed by concatenating the bytes into a signed integer in the following manner: the byte of lowest address forms the least significant byte, and the byte with the highest address forms the most significant byte of the integer. (Note: This representation is often called "a signed integer in little-endian byte-order.")
1.7.3 Exception RangesException tables describe ranges of instructions that are protected by catch, fault, or finally handlers (see Partition I, section 12.4.2 and Partition II, section 18). The starting address of a protected block, filter clause, or handler shall be a valid branch target as specified in Partition III, section 1.7.2. It is invalid for a protected block, filter clause, or handler to end without forming a complete last instruction.
1.7.4 Must Provide MaxstackEvery method specifies a maximum number of items that can be pushed onto the CIL evaluation [stack]. The value is stored in the IMAGE_COR_ILMETHOD structure that precedes the CIL body of each method. A method that specifies a maximum number of items less than the amount required by a static analysis of the method (using a traditional control flow graph without analysis of the data) is invalid (hence also unverifiable) and need not be supported by a conforming implementation of the CLI. NOTE Maxstack is related to analysis of the program, not to the size of the stack at runtime. It does not specify the maximum size in bytes of a stack frame, but rather the number of items that must be tracked by an analysis tool. RATIONALE By analyzing the CIL stream for any method, it is easy to determine how many items will be pushed onto the CIL evaluation stack. However, specifying that maximum number ahead of time helps a CIL-to-native-code compiler (especially a simple one that does only a single pass through the CIL stream) in allocating internal data structures that model the stack and/or verification algorithm.
1.7.5 Backward Branch ConstraintsIt must be possible, with a single forward-pass through the CIL instruction stream for any method, to infer the exact state of the evaluation stack at every instruction (where by "state" we mean the number and type of each item on the evaluation stack). In particular, if that single-pass analysis arrives at an instruction, call it location X, that immediately follows an unconditional branch, and where X is not the target of an earlier branch instruction, then the state of the evaluation stack at X, clearly, cannot be derived from existing information. In this case, the CLI demands that the evaluation stack at X be empty. Following on from this rule, it would clearly be invalid CIL if a later branch instruction to X were to have a non-empty evaluation stack RATIONALE This constraint ensures that CIL code can be processed by a simple CIL-to-native-code compiler. It ensures that the state of the evaluation stack at the beginning of each CIL can be inferred from a single, forward-pass analysis of the instruction stream. Note: The stack state at location X in the above can be inferred by various means: from a previous forward branch to X, because X marks the start of an exception handler, etc. See the following sections for further information:
1.7.6 Branch Verification ConstraintsThe target of all branch instructions must be a valid branch target (see Partition III, section 1.7.2) within the method holding that branch instruction. 1.8 VerifiabilityMemory safety is a property that ensures programs running in the same address space are correctly isolated from one another (see Partition I, section 8.8). Thus, it is desirable to test whether programs are memory-safe prior to running them. Unfortunately, it is provably impossible to do this with 100% accuracy. Instead, the CLI can test a stronger restriction, called verifiability. Every program that is verified is memory-safe, but some programs that are not verifiable are still memory-safe. It is perfectly acceptable to generate CIL code that is not verifiable, but that is known to be memory-safe by the compiler writer. Thus, conforming CIL may not be verifiable, even though the producing compiler may know that it is memory-safe. Several important uses of CIL instructions are not verifiable, such as the pointer arithmetic versions of add that are required for the faithful and efficient compilation of C programs. For non-verifiable code, memory safety is the responsibility of the application programmer. CIL contains a verifiable subset. The verifiability description [for each instruction] gives details of the conditions under which use of an instruction falls within the verifiable subset of CIL. Verification tracks the types of values in much finer detail than is required for the basic functioning of the CLI, because it is checking that a CIL code sequence respects not only the basic rules of the CLI with respect to the safety of garbage collection, but also the typing rules of the CTS. This helps to guarantee the sound operation of the entire CLI. The verifiability section of each operation description specifies requirements both for correct CIL generation and for verification. Correct CIL generation always requires guaranteeing that the top items on the stack correspond to the types shown in the stack transition diagram. The verifiability section specifies only requirements for correct CIL generation that are not captured in that diagram. Verification tests both the requirements for correct CIL generation and the specific verification conditions that are described with the instruction. The operation of CIL sequences that do not meet the CIL correctness requirements is unspecified. The operation of CIL sequences that meet the correctness requirements but are not verifiable may violate type safety and hence may violate security or memory access constraints. 1.8.1 Flow Control Restrictions for Verifiable CILThis section specifies a verification algorithm that, combined with information on individual CIL instructions (see Partition III, section 3) and metadata validation (see Partition II), guarantees memory integrity. The algorithm specified here creates a minimum level for all compliant implementations of the CLI in the sense that any program that is considered verifiable by this algorithm shall be considered verifiable and run correctly on all compliant implementations of the CLI. The CLI provides a security permission that controls whether or not the CLI shall run programs that may violate memory safety. Any program that is verifiable according to this specification does not violate memory safety, and a conforming implementation of the CLI shall run such programs. The implementation may also run other programs, provided it is able to show they do not violate memory safety (typically because they use a verification algorithm that makes use of specific knowledge about the implementation). NOTE While a compliant implementation is required to accept and run any program this verification algorithm states is verifiable, there may be programs that are accepted as verifiable by a given implementation but that this verification algorithm will fail to consider verifiable. Such programs will run in the given implementation but need not be considered verifiable by other implementations. For example, an implementation of the CLI may choose to correctly track full signatures on method pointers and permit programs to execute the calli instruction even though this is not permitted by the verification algorithm specified here. Implementers of the CLI are urged to provide a means for testing whether programs generated on their implementation meet this portable verifiability standard. They are also urged to specify where their verification algorithms are more permissive than this standard.
Only valid programs shall be verifiable. For ease of explanation, the verification algorithm described here assumes that the program is valid and does not explicitly call for tests of all validity conditions. Validity conditions are specified on a per-CIL instruction basis (see Partition III, section 3), and on the overall file format in Partition II, section 21. 1.8.1.1 Verification AlgorithmThe verification algorithm shall attempt to associate a valid stack state with every CIL instruction. The stack state specifies the number of slots on the CIL stack at that point in the code and, for each slot, a required type that must be present in that slot. The initial stack state is empty (there are no items on the stack). Verification assumes that the CLI zeros all memory other than the evaluation stack before it is made visible to programs. A conforming implementation of the CLI shall provide this observable behavior. Furthermore, verifiable methods shall have the "zero initialize" bit set, see Partition II, section 24.4.4. If this bit is not set, then a CLI may throw a Verification exception at any point where a local variable is accessed, and where the assembly containing that method has not been granted SecurityPermission.SkipVerification.
RATIONALE This requirement strongly enhances program portability, and a well-known technique (definite-assignment analysis) allows a compiler from CIL to native code to minimize its performance impact. Note that a CLI may optionally choose to perform definite-assignment analysis in such a case, it may confirm that a method, even without the "zero initialize" bit set, may in fact be verifiable (and therefore not throw a Verification exception). NOTE Definite-assignment analysis can be used by the CLI to determine which locations are written before they are read. Such locations needn't be zeroed, since it isn't possible to observe the contents of the memory as it was provided by the EE. Performance measurements on C++ implementations (which do not require definite-assignment analysis) indicate that adding this requirement has almost no impact, even in highly optimized code. Furthermore, customers incorrectly attribute bugs to the compiler when this zeroing is not performed, since such code often fails when small, unrelated changes are made to the program. The verification algorithm shall simulate all possible control flow paths through the code and ensures that a legal stack state exists for every reachable CIL instruction. The verification algorithm does not take advantage of any data values during its simulation (e.g., it does not perform constant propagation), but uses only type assignments. Details of the type system used for verification and the algorithm used to merge stack states are provided in Partition III, section 1.8.1.3. The verification algorithm terminates as follows:
There is a control flow path from every instruction to the subsequent instruction, with the exception of the unconditional branch instructions, throw, rethrow, and ret. Finally, there is a control flow path from each branch instruction (conditional or unconditional) to the branch target (targets, plural, for the switch instruction). Verification simulates the operation of each CIL instruction to compute the new stack state, and any type mismatch between the specified conditions on the stack state (see Partition III, section 3) and the simulated stack state shall cause the verification algorithm to fail. (Note that verification simulates only the effect on the stack state; it does not perform the actual computation.) The algorithm shall also fail if there is an existing stack state at the next instruction address (for conditional branches or instructions within a try block there may be more than one such address) that cannot be merged with the stack state just computed. For rules of this merge operation, see Partition III, section 1.8.1.3.
1.8.1.2 Verification Type SystemThe verification algorithm compresses types that are logically equivalent, since they cannot lead to memory safety violations. The types used by the verification algorithm are specified in Partition III, section 1.8.1.2.1; the type compatibility rules are specified in Partition III, section 1.8.1.2.2; and the rules for merging stack states are in Partition III, section 1.8.1.3. 1.8.1.2.1 Verification TypesThe following table specifies the mapping of types used in the CLI and those used in verification. Notice that verification compresses the CLI types to a smaller set that maintains information about the size of those types in memory, but then compresses these again to represent the fact that the CLI stack expands 1-, 2-, and 4-byte built-in types into 4-byte types on the stack. Similarly, verification treats floating point numbers on the stack as 64-bit quantities regardless of the actual representation. Arrays are objects, but with special compatibility rules. There is a special encoding for null that represents an object known to be the null value, hence with indeterminate actual type. In the following table, CLI Type is the type as it is described in metadata. The Verification Type is a corresponding type used for type compatibility rules in verification (see Partition III, section 1.8.1.2.2) when considering the types of local variables, incoming arguments, and formal parameters on methods being called. The column Verification Type (in Stack State) is used to simulate instructions that load data onto the stack, and shows the types that are actually maintained in the stack state information of the verification algorithm. The column Managed Pointer to Type shows the type tracked for managed pointers.
A method can be defined as returning a managed pointer, but calls upon such methods are not verifiable. RATIONALE Some uses of returning a managed pointer are perfectly verifiable (e.g., returning a reference to a field in an object); but some are not (e.g., returning a pointer to a local variable of the called method). Tracking this in the general case is a burden, and therefore not included in this standard. 1.8.1.2.2 Verification Type CompatibilityThe following rules define type compatibility. We use S and T to denote verification types, and the notation "S := T" to indicate that the verification type T can be used wherever the verification type S can be used, while "S !:= T" indicates that T cannot be used where S is expected. These are the verification type compatibility rules. We use T[] to denote an array (of any rank) whose elements are of type T, and T& to denote a managed pointer to type T.
1.8.1.3 Merging Stack StatesAs the verification algorithm simulates all control flow paths, it shall merge the simulated stack state with any existing stack state at the next CIL instruction in the flow. If there is no existing stack state, the simulated stack state is stored for future use. Otherwise the merge shall be computed as follows and stored to replace the existing stack state for the CIL instruction. If the merge fails, the verification algorithm shall fail. The merge shall be computed by comparing the number of slots in each stack state. If they differ, the merge shall fail. If they match, then the overall merge shall be computed by merging the states slot-by-slot as follows. Let T be the type from the slot on the newly computed state and S be the type from the corresponding slot on the previously stored state. The merged type, U, shall be computed as follows (recall that S := T is the compatibility function defined in Partition III, section 1.8.1.2.2):
1.8.1.4 Class and Object Initialization RulesThe VES ensures that all statics are initially zeroed (i.e., built-in types are 0 or false, object references are null), hence the verification algorithm does not test for definite assignment to statics. An object constructor shall not return unless a constructor for the base class or a different construct for the object's class has been called on the newly constructed object. The verification algorithm shall treat the this pointer as uninitialized unless the base class constructor has been called. No operations can be performed on an uninitialized this except for storing into and loading from the object's fields. NOTE If the constructor generates an exception, the this pointer in the corresponding catch block is still uninitialized. 1.8.1.5 Delegate ConstructorsThe verification algorithm shall require that one of the following code sequences is used for constructing delegates; no other code sequence in verifiable code shall contain a newobj instruction for a delegate type. There shall be only one instance constructor method for a delegate (overloading is not allowed). The verification algorithm shall fail if a branch target is within these instruction sequences (other than at the start of the sequence). NOTE See Partition II [section 7] for the signature of delegates and a validity requirement regarding the signature of the method used in the constructor and the signature of Invoke and other methods on the delegate class. 1.8.1.5.1 Delegating via Virtual DispatchThe following CIL instruction sequence shall be used or the verification algorithm shall fail. The sequence begins with an object on the stack. dup ldvirtftn mthd; Method shall be on the class of the object, ; or one of its parent classes, or an interface ; implemented by the object newobj delegateclass::.ctor(object, native int) RATIONALE The dup is required to ensure that it is precisely the same object stored in the delegate as was used to compute the virtual method. If another object of a subtype were used, the object and the method wouldn't match and could lead to memory violations. 1.8.1.5.2 Delegating via Instance DispatchThe following CIL instruction sequence shall be used or the verification algorithm shall fail. The sequence begins with either null or an object on the stack. ldftn mthd; Method shall either be a static method or ; a method on the class of the object on the stack or ; one of the object's parent classes newobj delegateclass::.ctor(object, native int) 1.9 Metadata TokensMany CIL instructions are followed by a "metadata token." This is a 4-byte value that specifies a row in a metadata table, or a starting byte offset in the Userstring heap. The most-significant byte of the token specifies the table or heap. For example, a value of 0x02 specifies the TypeDef table; a value of 0x70 specifies the Userstring heap. The value corresponds to the number assigned to that metadata table (see Partition II [section 21] for the full list of tables) or to 0x70 for the Userstring heap. The least-significant 3 bytes specify the target row within that metadata table, or starting byte offset within the Userstring heap. The rows within metadata tables are numbered one upward, while offsets in the heap are numbered zero upward. (So, for example, the metadata token with value 0x02000007 specifies row number 7 in the TypeDef table.) 1.10 Exceptions ThrownA CIL instruction can throw a range of exceptions. The CLI can also throw the general-purpose exception called ExecutionEngineException. See Partition I, section 12.4.2. |