|
| 1 | +# Contract CodeVersions |
| 2 | + |
| 3 | +This contract encapsulates support for [code versioning](../features/code-versioning.md) in the runtime. |
| 4 | + |
| 5 | +## APIs of contract |
| 6 | + |
| 7 | +```csharp |
| 8 | +internal struct NativeCodeVersionHandle |
| 9 | +{ |
| 10 | + // no public constructors |
| 11 | + internal readonly TargetPointer MethodDescAddress; |
| 12 | + internal readonly TargetPointer CodeVersionNodeAddress; |
| 13 | + internal NativeCodeVersionHandle(TargetPointer methodDescAddress, TargetPointer codeVersionNodeAddress) |
| 14 | + { |
| 15 | + if (methodDescAddress != TargetPointer.Null && codeVersionNodeAddress != TargetPointer.Null) |
| 16 | + { |
| 17 | + throw new ArgumentException("Only one of methodDescAddress and codeVersionNodeAddress can be non-null"); |
| 18 | + } |
| 19 | + MethodDescAddress = methodDescAddress; |
| 20 | + CodeVersionNodeAddress = codeVersionNodeAddress; |
| 21 | + } |
| 22 | + |
| 23 | + internal static NativeCodeVersionHandle Invalid => new(TargetPointer.Null, TargetPointer.Null); |
| 24 | + public bool Valid => MethodDescAddress != TargetPointer.Null || CodeVersionNodeAddress != TargetPointer.Null; |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +```csharp |
| 29 | + // Return a handle to the version of the native code that includes the given instruction pointer |
| 30 | + public virtual NativeCodeVersionHandle GetNativeCodeVersionForIP(TargetCodePointer ip); |
| 31 | + // Return a handle to the active version of the native code for a given method descriptor |
| 32 | + public virtual NativeCodeVersionHandle GetActiveNativeCodeVersion(TargetPointer methodDesc); |
| 33 | + |
| 34 | + // returns true if the given method descriptor supports multiple code versions |
| 35 | + public virtual bool CodeVersionManagerSupportsMethod(TargetPointer methodDesc); |
| 36 | + |
| 37 | + // Return the instruction pointer corresponding to the start of the given native code version |
| 38 | + public virtual TargetCodePointer GetNativeCode(NativeCodeVersionHandle codeVersionHandle); |
| 39 | +``` |
| 40 | + |
| 41 | +## Version 1 |
| 42 | + |
| 43 | +Data descriptors used: |
| 44 | +| Data Descriptor Name | Field | Meaning | |
| 45 | +| --- | --- | --- | |
| 46 | +| MethodDescVersioningState | ? | ? | |
| 47 | +| NativeCodeVersionNode | ? | ? | |
| 48 | +| ILCodeVersioningState | ? | ? | |
| 49 | + |
| 50 | + |
| 51 | +Global variables used: |
| 52 | +| Global Name | Type | Purpose | |
| 53 | +| --- | --- | --- | |
| 54 | + |
| 55 | +Contracts used: |
| 56 | +| Contract Name | |
| 57 | +| --- | |
| 58 | +| ExecutionManager | |
| 59 | +| Loader | |
| 60 | +| RuntimeTypeSystem | |
| 61 | + |
| 62 | +### Finding the start of a specific native code version |
| 63 | + |
| 64 | +```csharp |
| 65 | + NativeCodeVersionHandle GetNativeCodeVersionForIP(TargetCodePointer ip) |
| 66 | + { |
| 67 | + Contracts.IExecutionManager executionManager = _target.Contracts.ExecutionManager; |
| 68 | + EECodeInfoHandle? info = executionManager.GetEECodeInfoHandle(ip); |
| 69 | + if (!info.HasValue) |
| 70 | + { |
| 71 | + return NativeCodeVersionHandle.Invalid; |
| 72 | + } |
| 73 | + TargetPointer methodDescAddress = executionManager.GetMethodDesc(info.Value); |
| 74 | + if (methodDescAddress == TargetPointer.Null) |
| 75 | + { |
| 76 | + return NativeCodeVersionHandle.Invalid; |
| 77 | + } |
| 78 | + IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem; |
| 79 | + MethodDescHandle md = rts.GetMethodDescHandle(methodDescAddress); |
| 80 | + if (!rts.IsVersionable(md)) |
| 81 | + { |
| 82 | + return new NativeCodeVersionHandle(methodDescAddress, codeVersionNodeAddress: TargetPointer.Null); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + TargetCodePointer startAddress = executionManager.GetStartAddress(info.Value); |
| 87 | + return GetSpecificNativeCodeVersion(md, startAddress); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private NativeCodeVersionHandle GetSpecificNativeCodeVersion(MethodDescHandle md, TargetCodePointer startAddress) |
| 92 | + { |
| 93 | + TargetPointer methodDescVersioningStateAddress = target.Contracts.RuntimeTypeSystem.GetMethodDescVersioningState(md); |
| 94 | + if (methodDescVersioningStateAddress == TargetPointer.Null) |
| 95 | + { |
| 96 | + return NativeCodeVersionHandle.Invalid; |
| 97 | + } |
| 98 | + Data.MethodDescVersioningState methodDescVersioningStateData = _target.ProcessedData.GetOrAdd<Data.MethodDescVersioningState>(methodDescVersioningStateAddress); |
| 99 | + // CodeVersionManager::GetNativeCodeVersion(PTR_MethodDesc, PCODE startAddress) |
| 100 | + return FindFirstCodeVersion(methodDescVersioningStateData, (codeVersion) => |
| 101 | + { |
| 102 | + return codeVersion.MethodDesc == md.Address && codeVersion.NativeCode == startAddress; |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + private NativeCodeVersionHandle FindFirstCodeVersion(Data.MethodDescVersioningState versioningState, Func<Data.NativeCodeVersionNode, bool> predicate) |
| 107 | + { |
| 108 | + // NativeCodeVersion::Next, heavily inlined |
| 109 | + TargetPointer currentAddress = versioningState.NativeCodeVersionNode; |
| 110 | + while (currentAddress != TargetPointer.Null) |
| 111 | + { |
| 112 | + Data.NativeCodeVersionNode current = _target.ProcessedData.GetOrAdd<Data.NativeCodeVersionNode>(currentAddress); |
| 113 | + if (predicate(current)) |
| 114 | + { |
| 115 | + return new NativeCodeVersionHandle(methodDescAddress: TargetPointer.Null, currentAddress); |
| 116 | + } |
| 117 | + currentAddress = current.Next; |
| 118 | + } |
| 119 | + return NativeCodeVersionHandle.Invalid; |
| 120 | + } |
| 121 | +``` |
| 122 | + |
| 123 | +### Finding the active native code version of a method descriptor |
| 124 | + |
| 125 | +```csharp |
| 126 | + NativeCodeVersionHandle ICodeVersions.GetActiveNativeCodeVersion(TargetPointer methodDesc) |
| 127 | + { |
| 128 | + // CodeVersionManager::GetActiveILCodeVersion |
| 129 | + // then ILCodeVersion::GetActiveNativeCodeVersion |
| 130 | + IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem; |
| 131 | + MethodDescHandle md = rts.GetMethodDescHandle(methodDesc); |
| 132 | + TargetPointer mtAddr = rts.GetMethodTable(md); |
| 133 | + TypeHandle typeHandle = rts.GetTypeHandle(mtAddr); |
| 134 | + TargetPointer module = rts.GetModule(typeHandle); |
| 135 | + uint methodDefToken = rts.GetMethodToken(md); |
| 136 | + ILCodeVersionHandle methodDefActiveVersion = FindActiveILCodeVersion(module, methodDefToken); |
| 137 | + if (!methodDefActiveVersion.IsValid) |
| 138 | + { |
| 139 | + return NativeCodeVersionHandle.Invalid; |
| 140 | + } |
| 141 | + return FindActiveNativeCodeVersion(methodDefActiveVersion, methodDesc); |
| 142 | + } |
| 143 | +``` |
| 144 | + |
| 145 | +**FIXME** |
| 146 | + |
| 147 | +### Determining whether a method descriptor supports code versioning |
| 148 | + |
| 149 | +**TODO** |
0 commit comments