-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ReadyToRunMethod.cs
632 lines (548 loc) · 20.5 KB
/
ReadyToRunMethod.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
using System.Text;
using Internal.Runtime;
namespace ILCompiler.Reflection.ReadyToRun
{
/// <summary>
/// This structure represents a single precode fixup cell decoded from the
/// nibble-oriented per-method fixup blob. Each method entrypoint fixup
/// represents an array of cells that must be fixed up before the method
/// can start executing.
/// </summary>
public struct FixupCell
{
public int Index { get; }
/// <summary>
/// Zero-based index of the import table within the import tables section.
/// </summary>
public uint TableIndex { get; }
/// <summary>
/// Zero-based offset of the entry in the import table; it must be a multiple
/// of the target architecture pointer size.
/// </summary>
public uint CellOffset { get; }
/// <summary>
/// Fixup cell signature
/// </summary>
public ReadyToRunSignature Signature { get; }
public FixupCell(int index, uint tableIndex, uint cellOffset, ReadyToRunSignature signature)
{
Index = index;
TableIndex = tableIndex;
CellOffset = cellOffset;
Signature = signature;
}
}
public abstract class BaseUnwindInfo
{
public int Size { get; set; }
}
public abstract class BaseGcTransition
{
public int CodeOffset { get; set; }
public BaseGcTransition() { }
public BaseGcTransition(int codeOffset)
{
CodeOffset = codeOffset;
}
}
public abstract class BaseGcSlot
{
public abstract GcSlotFlags WriteTo(StringBuilder sb, Machine machine, GcSlotFlags prevFlags);
}
public abstract class BaseGcInfo
{
public int Size { get; set; }
public int Offset { get; set; }
public int CodeLength { get; set; }
public Dictionary<int, List<BaseGcTransition>> Transitions { get; set; }
public List<List<BaseGcSlot>> LiveSlotsAtSafepoints { get; set; }
}
/// <summary>
/// A runtime function corresponds to a contiguous fragment of code that implements a method.
/// </summary>
/// <remarks>
/// Based on <a href="https://github.com/dotnet/runtime/blob/main/src/coreclr/pal/inc/pal.h">src/pal/inc/pal.h</a> _RUNTIME_FUNCTION
/// </remarks>
public class RuntimeFunction
{
private ReadyToRunReader _readyToRunReader;
private EHInfo _ehInfo;
private DebugInfo _debugInfo;
/// <summary>
/// The index of the runtime function
/// </summary>
public int Id { get; }
/// <summary>
/// The relative virtual address to the start of the code block
/// </summary>
public int StartAddress { get; }
/// <summary>
/// The relative virtual address to the end of the code block
/// </summary>
public int EndAddress { get; }
/// <summary>
/// The size of the code block in bytes
/// </summary>
/// /// <remarks>
/// The EndAddress field in the runtime functions section is conditional on machine type
/// Size is -1 for images without the EndAddress field
/// </remarks>
public int Size
{
get
{
EnsureInitialized();
return _size;
}
}
private int _size = -1;
/// <summary>
/// The relative virtual address to the unwind info
/// </summary>
public int UnwindRVA { get; }
/// <summary>
/// The start offset of the runtime function with is non-zero for methods with multiple runtime functions
/// </summary>
public int CodeOffset { get; }
/// <summary>
/// The method that this runtime function belongs to
/// </summary>
public ReadyToRunMethod Method { get; }
public BaseUnwindInfo UnwindInfo { get; }
public EHInfo EHInfo
{
get
{
if (_ehInfo == null)
{
_readyToRunReader.RuntimeFunctionToEHInfo.TryGetValue(StartAddress, out _ehInfo);
}
return _ehInfo;
}
}
public DebugInfo DebugInfo
{
get
{
if (_debugInfo == null)
{
int offset;
if (_readyToRunReader.RuntimeFunctionToDebugInfo.TryGetValue(Id, out offset))
{
this._debugInfo = new DebugInfo(this, offset);
}
}
return _debugInfo;
}
}
internal ReadyToRunReader ReadyToRunReader
{
get
{
return _readyToRunReader;
}
}
public RuntimeFunction(
ReadyToRunReader readyToRunReader,
int id,
int startRva,
int endRva,
int unwindRva,
int codeOffset,
ReadyToRunMethod method,
BaseUnwindInfo unwindInfo)
{
_readyToRunReader = readyToRunReader;
Id = id;
StartAddress = startRva;
EndAddress = endRva;
UnwindRVA = unwindRva;
Method = method;
UnwindInfo = unwindInfo;
CodeOffset = codeOffset;
}
private void EnsureInitialized()
{
if (_size < 0)
{
_size = GetSize();
}
}
private int GetSize()
{
if (EndAddress != -1)
{
return EndAddress - StartAddress;
}
else if (UnwindInfo is x86.UnwindInfo x86Info)
{
return (int)x86Info.FunctionLength;
}
else if (UnwindInfo is Arm.UnwindInfo armInfo)
{
return (int)armInfo.FunctionLength;
}
else if (UnwindInfo is Arm64.UnwindInfo arm64Info)
{
return (int)arm64Info.FunctionLength;
}
else if (Method.GcInfo != null)
{
return Method.GcInfo.CodeLength;
}
else
{
return -1;
}
}
}
public class ReadyToRunMethod
{
private const int _mdtMethodDef = 0x06000000;
/// <summary>
/// MSIL module containing the method.
/// </summary>
public IAssemblyMetadata ComponentReader { get; private set; }
/// <summary>
/// The name of the method
/// </summary>
public string Name { get; set; }
/// <summary>
/// The signature with format: namespace.class.methodName<S, T, ...>(S, T, ...)
/// </summary>
public string SignatureString { get; set; }
public MethodSignature<string> Signature { get; }
public ImmutableArray<string> LocalSignature { get; }
/// <summary>
/// The type that the method belongs to
/// </summary>
public string DeclaringType { get; set; }
/// <summary>
/// The method metadata handle
/// </summary>
public EntityHandle MethodHandle { get; set; }
/// <summary>
/// The row id of the method
/// </summary>
public uint Rid { get; set; }
/// <summary>
/// Total method size (sum of the sizes of all runtime functions).
/// </summary>
private int _size;
/// <summary>
/// All the runtime functions of this method
/// </summary>
public IReadOnlyList<RuntimeFunction> RuntimeFunctions
{
get
{
EnsureRuntimeFunctions();
return _runtimeFunctions;
}
}
public int Size
{
get
{
EnsureRuntimeFunctions();
return _size;
}
}
private void EnsureRuntimeFunctions()
{
if (this._runtimeFunctions == null)
{
this._runtimeFunctions = new List<RuntimeFunction>();
this.ParseRuntimeFunctions(false);
}
}
/// <summary>
/// The id of the entrypoint runtime function
/// </summary>
public int EntryPointRuntimeFunctionId { get; set; }
public int GcInfoRva { get; set; }
public BaseGcInfo GcInfo
{
get
{
EnsureInitialized();
return _gcInfo;
}
}
private BaseGcInfo _gcInfo;
public PgoInfo PgoInfo
{
get
{
EnsureInitialized();
if (_pgoInfo == PgoInfo.EmptySingleton)
return null;
return _pgoInfo;
}
}
private PgoInfo _pgoInfo;
private ReadyToRunReader _readyToRunReader;
private List<FixupCell> _fixupCells;
private int? _fixupOffset;
private List<RuntimeFunction> _runtimeFunctions;
public IReadOnlyList<FixupCell> Fixups
{
get
{
EnsureFixupCells();
return _fixupCells;
}
}
public string[] InstanceArgs { get; set; }
public int RuntimeFunctionCount { get; set; }
/// <summary>
/// Extracts the method signature from the metadata by rid
/// </summary>
public ReadyToRunMethod(
ReadyToRunReader readyToRunReader,
IAssemblyMetadata componentReader,
EntityHandle methodHandle,
int entryPointId,
string owningType,
string constrainedType,
string[] instanceArgs,
int? fixupOffset)
{
InstanceArgs = (string[])instanceArgs?.Clone();
_readyToRunReader = readyToRunReader;
_fixupOffset = fixupOffset;
MethodHandle = methodHandle;
EntryPointRuntimeFunctionId = entryPointId;
ComponentReader = componentReader;
EntityHandle owningTypeHandle;
GenericParameterHandleCollection genericParams = default(GenericParameterHandleCollection);
DisassemblingGenericContext genericContext = new DisassemblingGenericContext(typeParameters: Array.Empty<string>(), methodParameters: instanceArgs);
DisassemblingTypeProvider typeProvider = new DisassemblingTypeProvider();
// get the method signature from the method handle
switch (MethodHandle.Kind)
{
case HandleKind.MethodDefinition:
{
MethodDefinition methodDef = ComponentReader.MetadataReader.GetMethodDefinition((MethodDefinitionHandle)MethodHandle);
if (methodDef.RelativeVirtualAddress != 0)
{
MethodBodyBlock mbb = ComponentReader.ImageReader.GetMethodBody(methodDef.RelativeVirtualAddress);
if (!mbb.LocalSignature.IsNil)
{
StandaloneSignature ss = ComponentReader.MetadataReader.GetStandaloneSignature(mbb.LocalSignature);
LocalSignature = ss.DecodeLocalSignature(typeProvider, genericContext);
}
}
Name = ComponentReader.MetadataReader.GetString(methodDef.Name);
Signature = methodDef.DecodeSignature<string, DisassemblingGenericContext>(typeProvider, genericContext);
owningTypeHandle = methodDef.GetDeclaringType();
genericParams = methodDef.GetGenericParameters();
}
break;
case HandleKind.MemberReference:
{
MemberReference memberRef = ComponentReader.MetadataReader.GetMemberReference((MemberReferenceHandle)MethodHandle);
Name = ComponentReader.MetadataReader.GetString(memberRef.Name);
Signature = memberRef.DecodeMethodSignature<string, DisassemblingGenericContext>(typeProvider, genericContext);
owningTypeHandle = memberRef.Parent;
}
break;
default:
throw new NotImplementedException();
}
if (owningType != null)
{
DeclaringType = owningType;
}
else
{
DeclaringType = MetadataNameFormatter.FormatHandle(ComponentReader.MetadataReader, owningTypeHandle);
}
StringBuilder sb = new StringBuilder();
sb.Append(Signature.ReturnType);
sb.Append(" ");
sb.Append(DeclaringType);
sb.Append(".");
sb.Append(Name);
if (Signature.GenericParameterCount != 0)
{
sb.Append("<");
for (int i = 0; i < Signature.GenericParameterCount; i++)
{
if (i > 0)
{
sb.Append(", ");
}
if (instanceArgs != null && instanceArgs.Length > i)
{
sb.Append(instanceArgs[i]);
}
else
{
sb.Append("!");
sb.Append(i);
}
}
sb.Append(">");
}
sb.Append("(");
for (int i = 0; i < Signature.ParameterTypes.Length; i++)
{
if (i > 0)
{
sb.Append(", ");
}
sb.AppendFormat($"{Signature.ParameterTypes[i]}");
}
sb.Append(")");
SignatureString = sb.ToString();
}
private void EnsureInitialized()
{
if (_gcInfo == null)
{
ParseRuntimeFunctions(true);
if (GcInfoRva != 0)
{
int gcInfoOffset = _readyToRunReader.CompositeReader.GetOffset(GcInfoRva);
if (_readyToRunReader.Machine == Machine.I386)
{
_gcInfo = new x86.GcInfo(_readyToRunReader.Image, gcInfoOffset, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion);
}
else
{
// Arm and Arm64 use the same GcInfo format as Amd64
_gcInfo = new Amd64.GcInfo(_readyToRunReader.Image, gcInfoOffset, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion);
}
}
}
if (_pgoInfo == null)
{
_pgoInfo = _readyToRunReader.GetPgoInfoByKey(PgoInfoKey.FromReadyToRunMethod(this));
if (_pgoInfo == null)
{
_pgoInfo = PgoInfo.EmptySingleton;
}
}
}
private void EnsureFixupCells()
{
if (_fixupCells != null)
{
return;
}
if (!_fixupOffset.HasValue)
{
return;
}
_fixupCells = new List<FixupCell>();
NibbleReader reader = new NibbleReader(_readyToRunReader.Image, _fixupOffset.Value);
// The following algorithm has been loosely ported from CoreCLR,
// src\vm\ceeload.inl, BOOL Module::FixupDelayListAux
uint curTableIndex = reader.ReadUInt();
while (true)
{
uint fixupIndex = reader.ReadUInt(); // Accumulate the real rva from the delta encoded rva
while (true)
{
ReadyToRunImportSection importSection = _readyToRunReader.ImportSections[(int)curTableIndex];
ReadyToRunImportSection.ImportSectionEntry entry = importSection.Entries[(int)fixupIndex];
_fixupCells.Add(new FixupCell(_fixupCells.Count, curTableIndex, fixupIndex, entry.Signature));
uint delta = reader.ReadUInt();
// Delta of 0 means end of entries in this table
if (delta == 0)
break;
fixupIndex += delta;
}
uint tableIndex = reader.ReadUInt();
if (tableIndex == 0)
break;
curTableIndex = curTableIndex + tableIndex;
} // Done with all entries in this table
}
/// <summary>
/// Get the RVAs of the runtime functions for each method
/// based on <a href="https://github.com/dotnet/coreclr/blob/master/src/zap/zapcode.cpp">ZapUnwindInfo::Save</a>
/// </summary>
private void ParseRuntimeFunctions(bool partial)
{
int runtimeFunctionId = EntryPointRuntimeFunctionId;
int runtimeFunctionSize = _readyToRunReader.CalculateRuntimeFunctionSize();
int runtimeFunctionOffset = _readyToRunReader.CompositeReader.GetOffset(_readyToRunReader.ReadyToRunHeader.Sections[ReadyToRunSectionType.RuntimeFunctions].RelativeVirtualAddress);
int curOffset = runtimeFunctionOffset + runtimeFunctionId * runtimeFunctionSize;
int codeOffset = 0;
for (int i = 0; i < RuntimeFunctionCount; i++)
{
int startRva = NativeReader.ReadInt32(_readyToRunReader.Image, ref curOffset);
if (_readyToRunReader.Machine == Machine.ArmThumb2)
{
// The low bit of this address is set since the function contains thumb code.
// Clear this bit in order to get the "real" RVA of the start of the function.
startRva = (int)(startRva & ~1);
}
int endRva = -1;
if (_readyToRunReader.Machine == Machine.Amd64)
{
endRva = NativeReader.ReadInt32(_readyToRunReader.Image, ref curOffset);
}
int unwindRva = NativeReader.ReadInt32(_readyToRunReader.Image, ref curOffset);
int unwindOffset = _readyToRunReader.CompositeReader.GetOffset(unwindRva);
BaseUnwindInfo unwindInfo = null;
if (_readyToRunReader.Machine == Machine.I386)
{
unwindInfo = new x86.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}
else if (_readyToRunReader.Machine == Machine.Amd64)
{
unwindInfo = new Amd64.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}
else if (_readyToRunReader.Machine == Machine.ArmThumb2)
{
unwindInfo = new Arm.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}
else if (_readyToRunReader.Machine == Machine.Arm64)
{
unwindInfo = new Arm64.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}
if (i == 0 && unwindInfo != null)
{
if (_readyToRunReader.Machine == Machine.I386)
{
GcInfoRva = unwindRva;
}
else
{
GcInfoRva = unwindRva + unwindInfo.Size;
}
}
if (partial)
{
return;
}
RuntimeFunction rtf = new RuntimeFunction(
_readyToRunReader,
runtimeFunctionId,
startRva,
endRva,
unwindRva,
codeOffset,
this,
unwindInfo);
_runtimeFunctions.Add(rtf);
runtimeFunctionId++;
codeOffset += rtf.Size;
}
_size = codeOffset;
}
}
}