• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SamboyCoding / Cpp2IL / 15173156024

21 May 2025 09:41PM UTC coverage: 34.291% (+0.2%) from 34.062%
15173156024

Pull #462

github

web-flow
Merge 01e84d727 into 5807d2b6c
Pull Request #462: Support overriding anything

1801 of 6646 branches covered (27.1%)

Branch coverage included in aggregate %.

133 of 243 new or added lines in 35 files covered. (54.73%)

5 existing lines in 5 files now uncovered.

4201 of 10857 relevant lines covered (38.69%)

186186.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.43
/Cpp2IL.Core/Model/Contexts/MethodAnalysisContext.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics.CodeAnalysis;
4
using System.Linq;
5
using System.Reflection;
6
using Cpp2IL.Core.Graphs;
7
using Cpp2IL.Core.Graphs.Processors;
8
using Cpp2IL.Core.ISIL;
9
using Cpp2IL.Core.Logging;
10
using Cpp2IL.Core.Utils;
11
using LibCpp2IL;
12
using LibCpp2IL.Metadata;
13
using StableNameDotNet.Providers;
14

15
namespace Cpp2IL.Core.Model.Contexts;
16

17
/// <summary>
18
/// Represents one method within the application. Can be analyzed to attempt to reconstruct the function body.
19
/// </summary>
20
public class MethodAnalysisContext : HasGenericParameters, IMethodInfoProvider
21
{
22
    /// <summary>
23
    /// The underlying metadata for the method.
24
    ///
25
    /// Nullable iff this is a subclass.
26
    /// </summary>
27
    public readonly Il2CppMethodDefinition? Definition;
28

29
    /// <summary>
30
    /// The analysis context for the declaring type of this method.
31
    /// </summary>
32
    public readonly TypeAnalysisContext? DeclaringType;
33

34
    /// <summary>
35
    /// The address of this method as defined in the underlying metadata.
36
    /// </summary>
37
    public virtual ulong UnderlyingPointer => Definition?.MethodPointer ?? throw new("Subclasses of MethodAnalysisContext should override UnderlyingPointer");
865,848!
38

39
    public ulong Rva => UnderlyingPointer == 0 || LibCpp2IlMain.Binary == null ? 0 : LibCpp2IlMain.Binary.GetRva(UnderlyingPointer);
×
40

41
    /// <summary>
42
    /// The raw method body as machine code in the active instruction set.
43
    /// </summary>
44
    public Memory<byte> RawBytes => rawMethodBody ??= InitRawBytes();
×
45

46
    /// <summary>
47
    /// The first-stage-analyzed Instruction-Set-Independent Language Instructions.
48
    /// </summary>
49
    public List<InstructionSetIndependentInstruction>? ConvertedIsil;
50

51
    /// <summary>
52
    /// The control flow graph for this method, if one is built.
53
    /// </summary>
54
    public ISILControlFlowGraph? ControlFlowGraph;
55

56
    public List<ParameterAnalysisContext> Parameters = [];
738,870✔
57

58
    /// <summary>
59
    /// Does this method return void?
60
    /// </summary>
NEW
61
    public bool IsVoid => ReturnType == AppContext.SystemTypes.SystemVoidType;
×
62

63
    public bool IsStatic => (Attributes & MethodAttributes.Static) != 0;
87,355✔
64

65
    protected override int CustomAttributeIndex => Definition?.customAttributeIndex ?? throw new("Subclasses of MethodAnalysisContext should override CustomAttributeIndex if they have custom attributes");
365,526!
66

67
    public override AssemblyAnalysisContext CustomAttributeAssembly => DeclaringType?.DeclaringAssembly ?? throw new("Subclasses of MethodAnalysisContext should override CustomAttributeAssembly if they have custom attributes");
968,522!
68

69
    public override string DefaultName => Definition?.Name ?? throw new("Subclasses of MethodAnalysisContext should override DefaultName");
87,698!
70

71
    public string FullName => DeclaringType == null ? Name : $"{DeclaringType.FullName}::{Name}";
33!
72

NEW
73
    public string FullNameWithSignature => $"{ReturnType.FullName} {FullName}({string.Join(", ", Parameters.Select(p => p.HumanReadableSignature))})";
×
74

75
    public virtual MethodAttributes DefaultAttributes => Definition?.Attributes ?? throw new($"Subclasses of MethodAnalysisContext should override {nameof(DefaultAttributes)}");
261,415!
76

77
    public virtual MethodAttributes? OverrideAttributes { get; set; }
261,415✔
78

79
    public MethodAttributes Attributes => OverrideAttributes ?? DefaultAttributes;
261,415!
80

81
    public virtual MethodImplAttributes DefaultImplAttributes => Definition?.MethodImplAttributes ?? throw new($"Subclasses of MethodAnalysisContext should override {nameof(DefaultImplAttributes)}");
87,030!
82

83
    public virtual MethodImplAttributes? OverrideImplAttributes { get; set; }
87,030✔
84

85
    public MethodImplAttributes ImplAttributes => OverrideImplAttributes ?? DefaultImplAttributes;
87,030!
86

87
    public MethodAttributes Visibility
88
    {
89
        get
90
        {
NEW
91
            return Attributes & MethodAttributes.MemberAccessMask;
×
92
        }
93
        set
94
        {
NEW
95
            OverrideAttributes = (Attributes & ~MethodAttributes.MemberAccessMask) | (value & MethodAttributes.MemberAccessMask);
×
NEW
96
        }
×
97
    }
98

99
    private List<GenericParameterTypeAnalysisContext>? _genericParameters;
100
    public override List<GenericParameterTypeAnalysisContext> GenericParameters
101
    {
102
        get
103
        {
104
            // Lazy load the generic parameters
105
            _genericParameters ??= Definition?.GenericContainer?.GenericParameters.Select(p => new GenericParameterTypeAnalysisContext(p, this)).ToList() ?? [];
485,803!
106
            return _genericParameters;
480,782✔
107
        }
108
    }
109

110
    private ushort Slot => Definition?.slot ?? ushort.MaxValue;
75,705!
111

112
    public virtual TypeAnalysisContext DefaultReturnType => DeclaringType?.DeclaringAssembly.ResolveIl2CppType(Definition?.RawReturnType) ?? throw new($"Subclasses of MethodAnalysisContext should override {nameof(DefaultReturnType)}");
385,993!
113

114
    public TypeAnalysisContext? OverrideReturnType { get; set; }
385,996✔
115

116
    //TODO Support custom attributes on return types (v31 feature)
117
    public TypeAnalysisContext ReturnType => OverrideReturnType ?? DefaultReturnType;
385,996✔
118
    
119
    protected Memory<byte>? rawMethodBody;
120

121
    public MethodAnalysisContext? BaseMethod => Overrides.FirstOrDefault(m => m.DeclaringType?.IsInterface is false);
8!
122

123
    /// <summary>
124
    /// The set of methods which this method overrides.
125
    /// </summary>
126
    public virtual IEnumerable<MethodAnalysisContext> Overrides
127
    {
128
        get
129
        {
130
            if (Definition == null)
18,047!
131
                return [];
×
132

133
            var declaringTypeDefinition = DeclaringType?.Definition;
18,047!
134
            if (declaringTypeDefinition == null)
18,047!
135
                return [];
×
136

137
            var vtable = declaringTypeDefinition.VTable;
18,047✔
138
            if (vtable == null)
18,047!
139
                return [];
×
140

141
            return GetOverriddenMethods(declaringTypeDefinition, vtable);
18,047✔
142

143
            bool TryGetMethodForSlot(TypeAnalysisContext declaringType, int slot, [NotNullWhen(true)] out MethodAnalysisContext? method)
144
            {
145
                if (declaringType is GenericInstanceTypeAnalysisContext genericInstanceType)
13,520✔
146
                {
147
                    var genericMethod = genericInstanceType.GenericType.Methods.FirstOrDefault(m => m.Slot == slot);
7,102✔
148
                    if (genericMethod is not null)
1,802✔
149
                    {
150
                        method = new ConcreteGenericMethodAnalysisContext(genericMethod, genericInstanceType.GenericArguments, []);
330✔
151
                        return true;
330✔
152
                    }
153
                }
154
                else
155
                {
156
                    var baseMethod = declaringType.Methods.FirstOrDefault(m => m.Slot == slot);
82,123✔
157
                    if (baseMethod is not null)
11,718✔
158
                    {
159
                        method = baseMethod;
2,830✔
160
                        return true;
2,830✔
161
                    }
162
                }
163

164
                method = null;
10,360✔
165
                return false;
10,360✔
166
            }
167

168
            IEnumerable<MethodAnalysisContext> GetOverriddenMethods(Il2CppTypeDefinition declaringTypeDefinition, MetadataUsage?[] vtable)
169
            {
170
                for (var i = 0; i < vtable.Length; ++i)
683,696✔
171
                {
172
                    var vtableEntry = vtable[i];
323,803✔
173
                    if (vtableEntry is null or { Type: not MetadataUsageType.MethodDef })
323,803✔
174
                        continue;
175

176
                    if (vtableEntry.AsMethod() != Definition)
318,193✔
177
                        continue;
178

179
                    // Normal inheritance
180
                    var baseType = DeclaringType?.BaseType;
3,125!
181
                    while (baseType is not null)
7,853✔
182
                    {
183
                        if (TryGetMethodForSlot(baseType, i, out var method))
4,766✔
184
                        {
185
                            yield return method;
38✔
186
                            break; // We only want direct overrides, not the entire inheritance chain.
36✔
187
                        }
188
                        baseType = baseType.BaseType;
4,728✔
189
                    }
190

191
                    // Interface inheritance
192
                    foreach (var interfaceOffset in declaringTypeDefinition.InterfaceOffsets)
34,564✔
193
                    {
194
                        if (i >= interfaceOffset.offset)
14,159✔
195
                        {
196
                            var interfaceTypeContext = interfaceOffset.Type.ToContext(CustomAttributeAssembly);
8,754✔
197
                            if (interfaceTypeContext != null && TryGetMethodForSlot(interfaceTypeContext, i - interfaceOffset.offset, out var method))
8,754✔
198
                            {
199
                                yield return method;
3,122✔
200
                            }
201
                        }
202
                    }
203
                }
204
            }
18,045✔
205
        }
206
    }
207

208
    private static readonly List<IBlockProcessor> blockProcessors =
×
209
    [
×
210
        new MetadataProcessor(),
×
211
        new CallProcessor()
×
212
    ];
×
213

214
    public MethodAnalysisContext(Il2CppMethodDefinition? definition, TypeAnalysisContext parent) : base(definition?.token ?? 0, parent.AppContext)
738,870✔
215
    {
216
        DeclaringType = parent;
738,870✔
217
        Definition = definition;
738,870✔
218

219
        if (Definition != null)
738,870✔
220
        {
221
            InitCustomAttributeData();
439,980✔
222

223
            for (var i = 0; i < Definition.InternalParameterData!.Length; i++)
1,890,450✔
224
            {
225
                var parameterDefinition = Definition.InternalParameterData![i];
505,245✔
226
                Parameters.Add(new(parameterDefinition, i, this));
505,245✔
227
            }
228
        }
229
        else
230
            rawMethodBody = Array.Empty<byte>();
298,890✔
231
    }
298,890✔
232

233
    [MemberNotNull(nameof(rawMethodBody))]
234
    public void EnsureRawBytes()
235
    {
236
        rawMethodBody ??= InitRawBytes();
439,980✔
237
    }
439,980✔
238

239
    private Memory<byte> InitRawBytes()
240
    {
241
        //Some abstract methods (on interfaces, no less) apparently have a body? Unity doesn't support default interface methods so idk what's going on here.
242
        //E.g. UnityEngine.Purchasing.AppleCore.dll: UnityEngine.Purchasing.INativeAppleStore::SetUnityPurchasingCallback on among us (itch.io build)
243
        if (Definition != null && Definition.MethodPointer != 0 && !Definition.Attributes.HasFlag(MethodAttributes.Abstract))
439,980✔
244
        {
245
            var ret = AppContext.InstructionSet.GetRawBytesForMethod(this, false);
425,868✔
246

247
            if (ret.Length == 0)
425,868✔
248
            {
249
                Logger.VerboseNewline("\t\t\tUnexpectedly got 0-byte method body for " + this + $". Pointer was 0x{Definition.MethodPointer:X}", "MAC");
33!
250
            }
251

252
            return ret;
425,868✔
253
        }
254
        else
255
            return Array.Empty<byte>();
14,112✔
256
    }
257

258
    protected MethodAnalysisContext(ApplicationAnalysisContext context) : base(0, context)
×
259
    {
260
        rawMethodBody = Array.Empty<byte>();
×
261
    }
×
262

263
    [MemberNotNull(nameof(ConvertedIsil))]
264
    public void Analyze()
265
    {
266
        if (ConvertedIsil != null)
×
267
            return;
×
268

269
        if (UnderlyingPointer == 0)
×
270
        {
271
            ConvertedIsil = [];
×
272
            return;
×
273
        }
274

275
        ConvertedIsil = AppContext.InstructionSet.GetIsilFromMethod(this);
×
276

277
        if (ConvertedIsil.Count == 0)
×
278
            return; //Nothing to do, empty function
×
279

280
        ControlFlowGraph = new ISILControlFlowGraph();
×
281
        ControlFlowGraph.Build(ConvertedIsil);
×
282

283
        // Post step to convert metadata usage. Ldstr Opcodes etc.
284
        foreach (var block in ControlFlowGraph.Blocks)
×
285
        {
286
            foreach (var converter in blockProcessors)
×
287
            {
288
                converter.Process(this, block);
×
289
            }
290
        }
291
    }
×
292

293
    public void ReleaseAnalysisData()
294
    {
295
        ConvertedIsil = null;
×
296
        ControlFlowGraph = null;
×
297
    }
×
298

299
    public ConcreteGenericMethodAnalysisContext MakeGenericInstanceMethod(params IEnumerable<TypeAnalysisContext> methodGenericParameters)
300
    {
NEW
301
        if (this is ConcreteGenericMethodAnalysisContext methodOnGenericInstanceType)
×
302
        {
NEW
303
            return new ConcreteGenericMethodAnalysisContext(methodOnGenericInstanceType.BaseMethodContext, methodOnGenericInstanceType.TypeGenericParameters, methodGenericParameters);
×
304
        }
305
        else
306
        {
NEW
307
            return new ConcreteGenericMethodAnalysisContext(this, [], methodGenericParameters);
×
308
        }
309
    }
310

311
    public override string ToString() => $"Method: {FullName}";
33✔
312

313
    #region StableNameDot implementation
314

315
    ITypeInfoProvider IMethodInfoProvider.ReturnType =>
316
        Definition!.RawReturnType!.ThisOrElementIsGenericParam()
×
317
            ? new GenericParameterTypeInfoProviderWrapper(Definition.RawReturnType!.GetGenericParamName())
×
318
            : TypeAnalysisContext.GetSndnProviderForType(AppContext, Definition!.RawReturnType);
×
319

320
    IEnumerable<IParameterInfoProvider> IMethodInfoProvider.ParameterInfoProviders => Parameters;
×
321

322
    string IMethodInfoProvider.MethodName => Name;
×
323

324
    MethodAttributes IMethodInfoProvider.MethodAttributes => Attributes;
×
325

326
    MethodSemantics IMethodInfoProvider.MethodSemantics
327
    {
328
        get
329
        {
330
            if (DeclaringType != null)
×
331
            {
332
                //This one is a bit trickier, as il2cpp doesn't use semantics.
333
                foreach (var prop in DeclaringType.Properties)
×
334
                {
335
                    if (prop.Getter == this)
×
336
                        return MethodSemantics.Getter;
×
337
                    if (prop.Setter == this)
×
338
                        return MethodSemantics.Setter;
×
339
                }
340

341
                foreach (var evt in DeclaringType.Events)
×
342
                {
343
                    if (evt.Adder == this)
×
344
                        return MethodSemantics.AddOn;
×
345
                    if (evt.Remover == this)
×
346
                        return MethodSemantics.RemoveOn;
×
347
                    if (evt.Invoker == this)
×
348
                        return MethodSemantics.Fire;
×
349
                }
350
            }
351

352
            return 0;
×
353
        }
×
354
    }
355

356
    #endregion
357
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc