Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ILDisassembler.cs « IL « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f11ed8cf5db8e14e39a93c4ded4a26f6f390eac6 (plain)
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
// 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.Text;

using Internal.TypeSystem;

using Debug = System.Diagnostics.Debug;

namespace Internal.IL
{
    // Known shortcomings:
    // - Escaping identifier names is missing (special characters and ILASM identifier names)
    // - Array bounds in signatures missing
    // - Custom modifiers and PINNED constraint not decoded in signatures
    // - Calling conventions in signatures not decoded
    // - Vararg signatures
    // - Floating point numbers are not represented in roundtrippable format

    /// <summary>
    /// Helper struct to disassemble IL instructions into a textual representation.
    /// </summary>
    public struct ILDisassembler
    {
        private byte[] _ilBytes;
        private MethodIL _methodIL;
        private ILTypeNameFormatter _typeNameFormatter;
        private int _currentOffset;

        public ILDisassembler(MethodIL methodIL)
        {
            _methodIL = methodIL;
            _ilBytes = methodIL.GetILBytes();
            _currentOffset = 0;
            _typeNameFormatter = null;
        }

        #region Type/member/signature name formatting
        private ILTypeNameFormatter TypeNameFormatter
        {
            get
            {
                if (_typeNameFormatter == null)
                {
                    // Find the owning module so that the type name formatter can remove
                    // redundant assembly name qualifiers in type names.
                    TypeDesc owningTypeDefinition = _methodIL.OwningMethod.OwningType;
                    ModuleDesc owningModule = owningTypeDefinition is MetadataType ?
                        ((MetadataType)owningTypeDefinition).Module : null;

                    _typeNameFormatter = new ILTypeNameFormatter(owningModule);
                }
                return _typeNameFormatter;
            }
        }

        public void AppendType(StringBuilder sb, TypeDesc type, bool forceValueClassPrefix = true)
        {
            // Types referenced from the IL show as instantiated over generic parameter.
            // E.g. "initobj !0" becomes "initobj !T"
            TypeDesc typeInContext = type.InstantiateSignature(
                _methodIL.OwningMethod.OwningType.Instantiation, _methodIL.OwningMethod.Instantiation);
            if (typeInContext.HasInstantiation || forceValueClassPrefix)
                this.TypeNameFormatter.AppendNameWithValueClassPrefix(sb, typeInContext);
            else
                this.TypeNameFormatter.AppendName(sb, typeInContext);
        }

        private void AppendOwningType(StringBuilder sb, TypeDesc type)
        {
            // Special case primitive types: we don't want to use short names here
            if (type.IsPrimitive || type.IsString || type.IsObject)
                _typeNameFormatter.AppendNameForNamespaceTypeWithoutAliases(sb, (MetadataType)type);
            else
                AppendType(sb, type, false);
        }

        private void AppendMethodSignature(StringBuilder sb, MethodDesc method)
        {
            // If this is an instantiated generic method, the formatted signature should
            // be uninstantiated (e.g. "void Foo::Bar<int>(!!0 param)", not "void Foo::Bar<int>(int param)")
            MethodSignature signature = method.GetMethodDefinition().Signature;

            AppendSignaturePrefix(sb, signature);
            sb.Append(' ');
            AppendOwningType(sb, method.OwningType);
            sb.Append("::");
            sb.Append(method.Name);

            if (method.HasInstantiation)
            {
                sb.Append('<');

                for (int i = 0; i < method.Instantiation.Length; i++)
                {
                    if (i != 0)
                        sb.Append(", ");
                    _typeNameFormatter.AppendNameWithValueClassPrefix(sb, method.Instantiation[i]);
                }

                sb.Append('>');
            }

            sb.Append('(');
            AppendSignatureArgumentList(sb, signature);
            sb.Append(')');
        }

        private void AppendMethodSignature(StringBuilder sb, MethodSignature signature)
        {
            AppendSignaturePrefix(sb, signature);
            sb.Append('(');
            AppendSignatureArgumentList(sb, signature);
            sb.Append(')');
        }

        private void AppendSignaturePrefix(StringBuilder sb, MethodSignature signature)
        {
            if (!signature.IsStatic)
                sb.Append("instance ");

            this.TypeNameFormatter.AppendNameWithValueClassPrefix(sb, signature.ReturnType);
        }

        private void AppendSignatureArgumentList(StringBuilder sb, MethodSignature signature)
        {
            for (int i = 0; i < signature.Length; i++)
            {
                if (i != 0)
                    sb.Append(", ");

                this.TypeNameFormatter.AppendNameWithValueClassPrefix(sb, signature[i]);
            }
        }

        private void AppendFieldSignature(StringBuilder sb, FieldDesc field)
        {
            this.TypeNameFormatter.AppendNameWithValueClassPrefix(sb, field.FieldType);
            sb.Append(' ');
            AppendOwningType(sb, field.OwningType);
            sb.Append("::");
            sb.Append(field.Name);
        }

        private void AppendStringLiteral(StringBuilder sb, string s)
        {
            sb.Append('"');
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '\\')
                    sb.Append("\\\\");
                else if (s[i] == '\t')
                    sb.Append("\\t");
                else if (s[i] == '"')
                    sb.Append("\\\"");
                else if (s[i] == '\n')
                    sb.Append("\\n");
                else
                    sb.Append(s[i]);
            }
            sb.Append('"');
        }

        private void AppendToken(StringBuilder sb, int token)
        {
            object obj = _methodIL.GetObject(token);
            if (obj is MethodDesc)
                AppendMethodSignature(sb, (MethodDesc)obj);
            else if (obj is FieldDesc)
                AppendFieldSignature(sb, (FieldDesc)obj);
            else if (obj is MethodSignature)
                AppendMethodSignature(sb, (MethodSignature)obj);
            else if (obj is TypeDesc)
                AppendType(sb, (TypeDesc)obj, false);
            else
            {
                Debug.Assert(obj is string, "NYI: " + obj.GetType());
                AppendStringLiteral(sb, (string)obj);
            }
        }
        #endregion

        #region Instruction decoding
        private byte ReadILByte()
        {
            return _ilBytes[_currentOffset++];
        }

        private UInt16 ReadILUInt16()
        {
            UInt16 val = (UInt16)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8));
            _currentOffset += 2;
            return val;
        }

        private UInt32 ReadILUInt32()
        {
            UInt32 val = (UInt32)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8) + (_ilBytes[_currentOffset + 2] << 16) + (_ilBytes[_currentOffset + 3] << 24));
            _currentOffset += 4;
            return val;
        }

        private int ReadILToken()
        {
            return (int)ReadILUInt32();
        }

        private ulong ReadILUInt64()
        {
            ulong value = ReadILUInt32();
            value |= (((ulong)ReadILUInt32()) << 32);
            return value;
        }

        private unsafe float ReadILFloat()
        {
            uint value = ReadILUInt32();
            return *(float*)(&value);
        }

        private unsafe double ReadILDouble()
        {
            ulong value = ReadILUInt64();
            return *(double*)(&value);
        }

        public static void AppendOffset(StringBuilder sb, int offset)
        {
            sb.Append($"IL_{offset:X4}");
        }

        private static void PadForInstructionArgument(StringBuilder sb)
        {
            if (sb.Length < 22)
                sb.Append(' ', 22 - sb.Length);
            else
                sb.Append(' ');
        }

        public bool HasNextInstruction
        {
            get
            {
                return _currentOffset < _ilBytes.Length;
            }
        }

        public int Offset
        {
            get
            {
                return _currentOffset;
            }
        }

        public int CodeSize
        {
            get
            {
                return _ilBytes.Length;
            }
        }

        public string GetNextInstruction()
        {
            StringBuilder decodedInstruction = new StringBuilder();
            AppendOffset(decodedInstruction, _currentOffset);
            decodedInstruction.Append(":  ");

        again:

            ILOpcode opCode = (ILOpcode)ReadILByte();
            if (opCode == ILOpcode.prefix1)
            {
                opCode = (ILOpcode)(0x100 + ReadILByte());
            }

            // Quick and dirty way to get the opcode name is to convert the enum value to string.
            // We need some adjustments though.
            string opCodeString = opCode.ToString().Replace("_", ".");
            if (opCodeString.EndsWith("."))
                opCodeString = opCodeString.Substring(0, opCodeString.Length - 1);

            decodedInstruction.Append(opCodeString);

            switch (opCode)
            {
                case ILOpcode.ldarg_s:
                case ILOpcode.ldarga_s:
                case ILOpcode.starg_s:
                case ILOpcode.ldloc_s:
                case ILOpcode.ldloca_s:
                case ILOpcode.stloc_s:
                case ILOpcode.ldc_i4_s:
                    PadForInstructionArgument(decodedInstruction);
                    decodedInstruction.Append(ReadILByte().ToStringInvariant());
                    return decodedInstruction.ToString();

                case ILOpcode.unaligned:
                    decodedInstruction.Append(' ');
                    decodedInstruction.Append(ReadILByte().ToStringInvariant());
                    decodedInstruction.Append(' ');
                    goto again;

                case ILOpcode.ldarg:
                case ILOpcode.ldarga:
                case ILOpcode.starg:
                case ILOpcode.ldloc:
                case ILOpcode.ldloca:
                case ILOpcode.stloc:
                    PadForInstructionArgument(decodedInstruction);
                    decodedInstruction.Append(ReadILUInt16().ToStringInvariant());
                    return decodedInstruction.ToString();

                case ILOpcode.ldc_i4:
                    PadForInstructionArgument(decodedInstruction);
                    decodedInstruction.Append(ReadILUInt32().ToStringInvariant());
                    return decodedInstruction.ToString();

                case ILOpcode.ldc_r4:
                    PadForInstructionArgument(decodedInstruction);
                    decodedInstruction.Append(ReadILFloat().ToStringInvariant());
                    return decodedInstruction.ToString();

                case ILOpcode.ldc_i8:
                    PadForInstructionArgument(decodedInstruction);
                    decodedInstruction.Append(ReadILUInt64().ToStringInvariant());
                    return decodedInstruction.ToString();

                case ILOpcode.ldc_r8:
                    PadForInstructionArgument(decodedInstruction);
                    decodedInstruction.Append(ReadILDouble().ToStringInvariant());
                    return decodedInstruction.ToString();

                case ILOpcode.jmp:
                case ILOpcode.call:
                case ILOpcode.calli:
                case ILOpcode.callvirt:
                case ILOpcode.cpobj:
                case ILOpcode.ldobj:
                case ILOpcode.ldstr:
                case ILOpcode.newobj:
                case ILOpcode.castclass:
                case ILOpcode.isinst:
                case ILOpcode.unbox:
                case ILOpcode.ldfld:
                case ILOpcode.ldflda:
                case ILOpcode.stfld:
                case ILOpcode.ldsfld:
                case ILOpcode.ldsflda:
                case ILOpcode.stsfld:
                case ILOpcode.stobj:
                case ILOpcode.box:
                case ILOpcode.newarr:
                case ILOpcode.ldelema:
                case ILOpcode.ldelem:
                case ILOpcode.stelem:
                case ILOpcode.unbox_any:
                case ILOpcode.refanyval:
                case ILOpcode.mkrefany:
                case ILOpcode.ldtoken:
                case ILOpcode.ldftn:
                case ILOpcode.ldvirtftn:
                case ILOpcode.initobj:
                case ILOpcode.constrained:
                case ILOpcode.sizeof_:
                    PadForInstructionArgument(decodedInstruction);
                    AppendToken(decodedInstruction, ReadILToken());
                    return decodedInstruction.ToString();

                case ILOpcode.br_s:
                case ILOpcode.leave_s:
                case ILOpcode.brfalse_s:
                case ILOpcode.brtrue_s:
                case ILOpcode.beq_s:
                case ILOpcode.bge_s:
                case ILOpcode.bgt_s:
                case ILOpcode.ble_s:
                case ILOpcode.blt_s:
                case ILOpcode.bne_un_s:
                case ILOpcode.bge_un_s:
                case ILOpcode.bgt_un_s:
                case ILOpcode.ble_un_s:
                case ILOpcode.blt_un_s:
                    PadForInstructionArgument(decodedInstruction);
                    AppendOffset(decodedInstruction, (sbyte)ReadILByte() + _currentOffset);
                    return decodedInstruction.ToString();

                case ILOpcode.br:
                case ILOpcode.leave:
                case ILOpcode.brfalse:
                case ILOpcode.brtrue:
                case ILOpcode.beq:
                case ILOpcode.bge:
                case ILOpcode.bgt:
                case ILOpcode.ble:
                case ILOpcode.blt:
                case ILOpcode.bne_un:
                case ILOpcode.bge_un:
                case ILOpcode.bgt_un:
                case ILOpcode.ble_un:
                case ILOpcode.blt_un:
                    PadForInstructionArgument(decodedInstruction);
                    AppendOffset(decodedInstruction, (int)ReadILUInt32() + _currentOffset);
                    return decodedInstruction.ToString();

                case ILOpcode.switch_:
                    {
                        decodedInstruction.Clear();
                        decodedInstruction.Append("switch (");
                        uint count = ReadILUInt32();
                        int jmpBase = _currentOffset + (int)(4 * count);
                        for (uint i = 0; i < count; i++)
                        {
                            if (i != 0)
                                decodedInstruction.Append(", ");
                            int delta = (int)ReadILUInt32();
                            AppendOffset(decodedInstruction, jmpBase + delta);
                        }
                        decodedInstruction.Append(")");
                        return decodedInstruction.ToString();
                    }

                default:
                    return decodedInstruction.ToString();
            }
        }
        #endregion

        #region Helpers
        public class ILTypeNameFormatter : TypeNameFormatter
        {
            private ModuleDesc _thisModule;

            public ILTypeNameFormatter(ModuleDesc thisModule)
            {
                _thisModule = thisModule;
            }

            public void AppendNameWithValueClassPrefix(StringBuilder sb, TypeDesc type)
            {
                if (!type.IsSignatureVariable
                    && type.IsDefType
                    && !type.IsPrimitive
                    && !type.IsObject
                    && !type.IsString)
                {
                    string prefix = type.IsValueType ? "valuetype " : "class ";
                    sb.Append(prefix);
                    AppendName(sb, type);
                }
                else
                {
                    AppendName(sb, type);
                }
            }

            public override void AppendName(StringBuilder sb, PointerType type)
            {
                AppendNameWithValueClassPrefix(sb, type.ParameterType);
                sb.Append('*');
            }

            public override void AppendName(StringBuilder sb, FunctionPointerType type)
            {
                MethodSignature signature = type.Signature;

                sb.Append("method ");

                if (!signature.IsStatic)
                    sb.Append("instance ");

                // TODO: rest of calling conventions

                AppendName(sb, signature.ReturnType);

                sb.Append(" *(");
                for (int i = 0; i < signature.Length; i++)
                {
                    if (i > 0)
                        sb.Append(", ");
                    AppendName(sb, signature[i]);
                }
                sb.Append(')');
            }

            public override void AppendName(StringBuilder sb, SignatureMethodVariable type)
            {
                sb.Append("!!");
                sb.Append(type.Index.ToStringInvariant());
            }

            public override void AppendName(StringBuilder sb, SignatureTypeVariable type)
            {
                sb.Append("!");
                sb.Append(type.Index.ToStringInvariant());
            }

            public override void AppendName(StringBuilder sb, GenericParameterDesc type)
            {
                string prefix = type.Kind == GenericParameterKind.Type ? "!" : "!!";
                sb.Append(prefix);
                sb.Append(type.Name);
            }

            protected override void AppendNameForInstantiatedType(StringBuilder sb, DefType type)
            {
                AppendName(sb, type.GetTypeDefinition());
                sb.Append('<');

                for (int i = 0; i < type.Instantiation.Length; i++)
                {
                    if (i > 0)
                        sb.Append(", ");
                    AppendNameWithValueClassPrefix(sb, type.Instantiation[i]);
                }   

                sb.Append('>');
            }

            public override void AppendName(StringBuilder sb, ByRefType type)
            {
                AppendNameWithValueClassPrefix(sb, type.ParameterType);
                sb.Append('&');
            }

            public override void AppendName(StringBuilder sb, ArrayType type)
            {
                AppendNameWithValueClassPrefix(sb, type.ElementType);
                sb.Append('[');
                sb.Append(',', type.Rank - 1);
                sb.Append(']');
            }

            protected override void AppendNameForNamespaceType(StringBuilder sb, DefType type)
            {
                switch (type.Category)
                {
                    case TypeFlags.Void:
                        sb.Append("void");
                        return;
                    case TypeFlags.Boolean:
                        sb.Append("bool");
                        return;
                    case TypeFlags.Char:
                        sb.Append("char");
                        return;
                    case TypeFlags.SByte:
                        sb.Append("int8");
                        return;
                    case TypeFlags.Byte:
                        sb.Append("uint8");
                        return;
                    case TypeFlags.Int16:
                        sb.Append("int16");
                        return;
                    case TypeFlags.UInt16:
                        sb.Append("uint16");
                        return;
                    case TypeFlags.Int32:
                        sb.Append("int32");
                        return;
                    case TypeFlags.UInt32:
                        sb.Append("uint32");
                        return;
                    case TypeFlags.Int64:
                        sb.Append("int64");
                        return;
                    case TypeFlags.UInt64:
                        sb.Append("uint64");
                        return;
                    case TypeFlags.IntPtr:
                        sb.Append("native int");
                        return;
                    case TypeFlags.UIntPtr:
                        sb.Append("native uint");
                        return;
                    case TypeFlags.Single:
                        sb.Append("float32");
                        return;
                    case TypeFlags.Double:
                        sb.Append("float64");
                        return;
                }

                if (type.IsString)
                {
                    sb.Append("string");
                    return;
                }

                if (type.IsObject)
                {
                    sb.Append("object");
                    return;
                }

                AppendNameForNamespaceTypeWithoutAliases(sb, type);
            }
            public void AppendNameForNamespaceTypeWithoutAliases(StringBuilder sb, DefType type)
            {
                ModuleDesc owningModule = (type as MetadataType)?.Module;
                if (owningModule != null && owningModule != _thisModule)
                {
                    Debug.Assert(owningModule is IAssemblyDesc);
                    string owningModuleName = ((IAssemblyDesc)owningModule).GetName().Name;
                    sb.Append('[');
                    sb.Append(owningModuleName);
                    sb.Append(']');
                }

                string ns = type.Namespace;
                if (ns.Length > 0)
                {
                    sb.Append(ns);
                    sb.Append('.');
                }
                sb.Append(type.Name);
            }

            protected override void AppendNameForNestedType(StringBuilder sb, DefType nestedType, DefType containingType)
            {
                AppendName(sb, containingType);
                sb.Append('/');
                sb.Append(nestedType.Name);
            }
        }
        #endregion
    }
}