|
| 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 | +See [code versioning](../features/code-versioning.md) for a general overview and the definitions of *synthetic* and *explicit* nodes. |
| 44 | + |
| 45 | +Data descriptors used: |
| 46 | +| Data Descriptor Name | Field | Meaning | |
| 47 | +| --- | --- | --- | |
| 48 | +| MethodDescVersioningState | Flags | `MethodDescVersioningStateFlags` flags, see below | |
| 49 | +| MethodDescVersioningState | NativeCodeVersionNode | code version node of this method desc, if active | |
| 50 | +| NativeCodeVersionNode | Next | pointer to the next native code version | |
| 51 | +| NativeCodeVersionNode | MethodDesc | indicates a synthetic native code version node | |
| 52 | +| NativeCodeVersionNode | NativeCode | indicates an explicit native code version node | |
| 53 | +| ILCodeVersioningState | ActiveVersionKind | an `ILCodeVersionKind` value indicating which fields of the active version are value | |
| 54 | +| ILCodeVersioningState | ActiveVersionNode | if the active version is explicit, the NativeCodeVersionNode for the active version | |
| 55 | +| ILCodeVersioningState | ActiveVersionModule | if the active version is synthetic or unknown, the pointer to the Module that defines the method | |
| 56 | +| ILCodeVersioningState | ActiveVersionMethodDef | if the active version is synthetic or unknown, the MethodDef token for the method | |
| 57 | + |
| 58 | +The flag indicates that the default version of the code for a method desc is active: |
| 59 | +```csharp |
| 60 | +internal enum MethodDescVersioningStateFlags : byte |
| 61 | +{ |
| 62 | + IsDefaultVersionActiveChildFlag = 0x4 |
| 63 | +}; |
| 64 | +``` |
| 65 | + |
| 66 | +The value of the `ILCodeVersioningState::ActiveVersionKind` field is one of: |
| 67 | +```csharp |
| 68 | +private enum ILCodeVersionKind |
| 69 | +{ |
| 70 | + Unknown = 0, |
| 71 | + Explicit = 1, // means Node is set |
| 72 | + Synthetic = 2, // means Module and Token are set |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Global variables used: *none* |
| 77 | + |
| 78 | +Contracts used: |
| 79 | +| Contract Name | |
| 80 | +| --- | |
| 81 | +| ExecutionManager | |
| 82 | +| Loader | |
| 83 | +| RuntimeTypeSystem | |
| 84 | + |
| 85 | +### Finding the start of a specific native code version |
| 86 | + |
| 87 | +```csharp |
| 88 | +NativeCodeVersionHandle ICodeVersions.GetNativeCodeVersionForIP(TargetCodePointer ip) |
| 89 | +{ |
| 90 | + Contracts.IExecutionManager executionManager = _target.Contracts.ExecutionManager; |
| 91 | + EECodeInfoHandle? info = executionManager.GetEECodeInfoHandle(ip); |
| 92 | + if (!info.HasValue) |
| 93 | + { |
| 94 | + return NativeCodeVersionHandle.Invalid; |
| 95 | + } |
| 96 | + TargetPointer methodDescAddress = executionManager.GetMethodDesc(info.Value); |
| 97 | + if (methodDescAddress == TargetPointer.Null) |
| 98 | + { |
| 99 | + return NativeCodeVersionHandle.Invalid; |
| 100 | + } |
| 101 | + IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem; |
| 102 | + MethodDescHandle md = rts.GetMethodDescHandle(methodDescAddress); |
| 103 | + if (!rts.IsVersionable(md)) |
| 104 | + { |
| 105 | + return new NativeCodeVersionHandle(methodDescAddress, codeVersionNodeAddress: TargetPointer.Null); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + TargetCodePointer startAddress = executionManager.GetStartAddress(info.Value); |
| 110 | + return GetSpecificNativeCodeVersion(md, startAddress); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +NativeCodeVersionHandle GetSpecificNativeCodeVersion(MethodDescHandle md, TargetCodePointer startAddress) |
| 115 | +{ |
| 116 | + TargetPointer methodDescVersioningStateAddress = target.Contracts.RuntimeTypeSystem.GetMethodDescVersioningState(md); |
| 117 | + if (methodDescVersioningStateAddress == TargetPointer.Null) |
| 118 | + { |
| 119 | + return NativeCodeVersionHandle.Invalid; |
| 120 | + } |
| 121 | + Data.MethodDescVersioningState methodDescVersioningStateData = _target.ProcessedData.GetOrAdd<Data.MethodDescVersioningState>(methodDescVersioningStateAddress); |
| 122 | + return FindFirstCodeVersion(methodDescVersioningStateData, (codeVersion) => |
| 123 | + { |
| 124 | + return codeVersion.MethodDesc == md.Address && codeVersion.NativeCode == startAddress; |
| 125 | + }); |
| 126 | +} |
| 127 | + |
| 128 | +NativeCodeVersionHandle FindFirstCodeVersion(Data.MethodDescVersioningState versioningState, Func<Data.NativeCodeVersionNode, bool> predicate) |
| 129 | +{ |
| 130 | + TargetPointer currentAddress = versioningState.NativeCodeVersionNode; |
| 131 | + while (currentAddress != TargetPointer.Null) |
| 132 | + { |
| 133 | + Data.NativeCodeVersionNode current = _target.ProcessedData.GetOrAdd<Data.NativeCodeVersionNode>(currentAddress); |
| 134 | + if (predicate(current)) |
| 135 | + { |
| 136 | + return new NativeCodeVersionHandle(methodDescAddress: TargetPointer.Null, currentAddress); |
| 137 | + } |
| 138 | + currentAddress = current.Next; |
| 139 | + } |
| 140 | + return NativeCodeVersionHandle.Invalid; |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Finding the active native code version of a method descriptor |
| 145 | + |
| 146 | +```csharp |
| 147 | +NativeCodeVersionHandle ICodeVersions.GetActiveNativeCodeVersion(TargetPointer methodDesc) |
| 148 | +{ |
| 149 | + IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem; |
| 150 | + MethodDescHandle md = rts.GetMethodDescHandle(methodDesc); |
| 151 | + TargetPointer mtAddr = rts.GetMethodTable(md); |
| 152 | + TypeHandle typeHandle = rts.GetTypeHandle(mtAddr); |
| 153 | + TargetPointer module = rts.GetModule(typeHandle); |
| 154 | + uint methodDefToken = rts.GetMethodToken(md); |
| 155 | + ILCodeVersionHandle methodDefActiveVersion = FindActiveILCodeVersion(module, methodDefToken); |
| 156 | + if (!methodDefActiveVersion.IsValid) |
| 157 | + { |
| 158 | + return NativeCodeVersionHandle.Invalid; |
| 159 | + } |
| 160 | + return FindActiveNativeCodeVersion(methodDefActiveVersion, methodDesc); |
| 161 | +} |
| 162 | + |
| 163 | +ILCodeVersionHandle ILCodeVersionHandleFromState(Data.ILCodeVersioningState ilState) |
| 164 | +{ |
| 165 | + switch ((ILCodeVersionKind)ilState.ActiveVersionKind) |
| 166 | + { |
| 167 | + case ILCodeVersionKind.Explicit: |
| 168 | + return new ILCodeVersionHandle(module: TargetPointer.Null, methodDef: 0, ilState.ActiveVersionNode); |
| 169 | + case ILCodeVersionKind.Synthetic: |
| 170 | + case ILCodeVersionKind.Unknown: |
| 171 | + return new ILCodeVersionHandle(ilState.ActiveVersionModule, ilState.ActiveVersionMethodDef, TargetPointer.Null); |
| 172 | + default: |
| 173 | + throw new InvalidOperationException($"Unknown ILCodeVersionKind {ilState.ActiveVersionKind}"); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +ILCodeVersionHandle FindActiveILCodeVersion(TargetPointer module, uint methodDefinition) |
| 178 | +{ |
| 179 | + ModuleHandle moduleHandle = _target.Contracts.Loader.GetModuleHandle(module); |
| 180 | + TargetPointer ilCodeVersionTable = _target.Contracts.Loader.GetLookupTables(moduleHandle).MethodDefToILCodeVersioningState; |
| 181 | + TargetPointer ilVersionStateAddress = _target.Contracts.Loader.GetModuleLookupMapElement(ilCodeVersionTable, methodDefinition, out var _); |
| 182 | + if (ilVersionStateAddress == TargetPointer.Null) |
| 183 | + { |
| 184 | + return new ILCodeVersionHandle(module, methodDefinition, TargetPointer.Null); |
| 185 | + } |
| 186 | + Data.ILCodeVersioningState ilState = _target.ProcessedData.GetOrAdd<Data.ILCodeVersioningState>(ilVersionStateAddress); |
| 187 | + return ILCodeVersionHandleFromState(ilState); |
| 188 | +} |
| 189 | + |
| 190 | +bool IsActiveNativeCodeVersion(NativeCodeVersionHandle nativeCodeVersion) |
| 191 | +{ |
| 192 | + if (nativeCodeVersion.MethodDescAddress != TargetPointer.Null) |
| 193 | + { |
| 194 | + MethodDescHandle md = _target.Contracts.RuntimeTypeSystem.GetMethodDescHandle(nativeCodeVersion.MethodDescAddress); |
| 195 | + TargetPointer versioningStateAddress = _target.Contracts.RuntimeTypeSystem.GetMethodDescVersioningState(md); |
| 196 | + if (versioningStateAddress == TargetPointer.Null) |
| 197 | + { |
| 198 | + return true; |
| 199 | + } |
| 200 | + Data.MethodDescVersioningState versioningState = _target.ProcessedData.GetOrAdd<Data.MethodDescVersioningState>(versioningStateAddress); |
| 201 | + MethodDescVersioningStateFlags flags = (MethodDescVersioningStateFlags)versioningState.Flags; |
| 202 | + return flags.HasFlag(MethodDescVersioningStateFlags.IsDefaultVersionActiveChildFlag); |
| 203 | + } |
| 204 | + else if (nativeCodeVersion.CodeVersionNodeAddress != TargetPointer.Null) |
| 205 | + { |
| 206 | + throw new NotImplementedException(); // TODO[cdac]: IsActiveNativeCodeVersion - explicit |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + throw new ArgumentException("Invalid NativeCodeVersionHandle"); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +NativeCodeVersionHandle FindActiveNativeCodeVersion(ILCodeVersionHandle methodDefActiveVersion, TargetPointer methodDescAddress) |
| 215 | +{ |
| 216 | + if (methodDefActiveVersion.Module != TargetPointer.Null) |
| 217 | + { |
| 218 | + NativeCodeVersionHandle provisionalHandle = new NativeCodeVersionHandle(methodDescAddress: methodDescAddress, codeVersionNodeAddress: TargetPointer.Null); |
| 219 | + if (IsActiveNativeCodeVersion(provisionalHandle)) |
| 220 | + { |
| 221 | + return provisionalHandle; |
| 222 | + } |
| 223 | + else |
| 224 | + { |
| 225 | + throw new NotImplementedException(); // TODO[cdac]: iterate through versioning state nodes |
| 226 | + } |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + throw new NotImplementedException(); // TODO: [cdac] find explicit il code version |
| 231 | + } |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +### Determining whether a method descriptor supports code versioning |
| 236 | + |
| 237 | +```csharp |
| 238 | +bool ICodeVersions.CodeVersionManagerSupportsMethod(TargetPointer methodDescAddress) |
| 239 | +{ |
| 240 | + IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem; |
| 241 | + MethodDescHandle md = rts.GetMethodDescHandle(methodDescAddress); |
| 242 | + if (rts.IsDynamicMethod(md)) |
| 243 | + return false; |
| 244 | + if (rts.IsCollectibleMethod(md)) |
| 245 | + return false; |
| 246 | + TargetPointer mtAddr = rts.GetMethodTable(md); |
| 247 | + TypeHandle mt = rts.GetTypeHandle(mtAddr); |
| 248 | + TargetPointer modAddr = rts.GetModule(mt); |
| 249 | + ILoader loader = _target.Contracts.Loader; |
| 250 | + ModuleHandle mod = loader.GetModuleHandle(modAddr); |
| 251 | + ModuleFlags modFlags = loader.GetFlags(mod); |
| 252 | + if (modFlags.HasFlag(ModuleFlags.EditAndContinue)) |
| 253 | + return false; |
| 254 | + return true; |
| 255 | +} |
| 256 | +``` |
0 commit comments