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

SamboyCoding / Cpp2IL / 17216182984

25 Aug 2025 05:35PM UTC coverage: 30.38% (-4.0%) from 34.352%
17216182984

Pull #481

github

web-flow
Merge b82763a24 into d5260685f
Pull Request #481: Decompiler

1804 of 7561 branches covered (23.86%)

Branch coverage included in aggregate %.

100 of 1839 new or added lines in 29 files covered. (5.44%)

41 existing lines in 6 files now uncovered.

4093 of 11850 relevant lines covered (34.54%)

165575.01 hits per line

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

1.28
/Cpp2IL.Core/ProcessingLayers/NativeMethodDetectionProcessingLayer.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Reflection;
5
using Cpp2IL.Core.Api;
6
using Cpp2IL.Core.Extensions;
7
using Cpp2IL.Core.ISIL;
8
using Cpp2IL.Core.Model.Contexts;
9

10
namespace Cpp2IL.Core.ProcessingLayers;
11

12
public class NativeMethodDetectionProcessingLayer : Cpp2IlProcessingLayer
13
{
14
    public override string Name => "Native Method Detection";
×
15

16
    public override string Id => "nativemethoddetector";
1✔
17

18
    public override void Process(ApplicationAnalysisContext appContext, Action<int, int>? progressCallback = null)
19
    {
20
        var nativeMethodInfoStack = new Stack<(ulong, bool)>();
×
21
        var cppNativeMethodsType = appContext.AssembliesByName["mscorlib"].InjectType(
×
22
            "Cpp2ILInjected",
×
23
            "CppNativeMethods",
×
24
            null,
×
25
            TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Abstract | TypeAttributes.Sealed); //public static class
×
26
        foreach (var assemblyAnalysisContext in appContext.Assemblies)
×
27
        {
28
            foreach (var m in assemblyAnalysisContext.Types.SelectMany(t => t.Methods))
×
29
            {
30
                AnalyzeMethod(appContext, m, nativeMethodInfoStack);
×
31
            }
32

33
            if (Cpp2IlApi.LowMemoryMode)
×
34
                GC.Collect();
×
35
        }
36

37
        if (Cpp2IlApi.LowMemoryMode)
×
38
            GC.Collect();
×
39

40
        while (nativeMethodInfoStack.Count > 0)
×
41
        {
42
            (var address, var isVoid) = nativeMethodInfoStack.Pop();
×
43
            if (!appContext.MethodsByAddress.ContainsKey(address))
×
44
            {
45
                var m = new NativeMethodAnalysisContext(cppNativeMethodsType, address, isVoid);
×
46
                cppNativeMethodsType.Methods.Add(m);
×
47
                appContext.MethodsByAddress.Add(address, [m]);
×
48
                AnalyzeMethod(appContext, m, nativeMethodInfoStack);
×
49
            }
50
        }
51
    }
×
52

53
    private static void AnalyzeMethod(ApplicationAnalysisContext appContext, MethodAnalysisContext m, Stack<(ulong, bool)> nativeMethodInfoStack)
54
    {
55
        if (m.UnderlyingPointer == 0)
×
56
            return;
×
57

58
        var convertedIsil = appContext.InstructionSet.GetIsilFromMethod(m);
×
59

60
        if (convertedIsil is { Count: 0 })
×
61
        {
62
            return;
×
63
        }
64

65
        foreach (var instruction in convertedIsil)
×
66
        {
NEW
67
            if (instruction.OpCode == OpCode.Call)
×
68
            {
69
                if (TryGetAddressFromInstruction(instruction, out var address) && !appContext.MethodsByAddress.ContainsKey(address))
×
70
                {
71
                    nativeMethodInfoStack.Push((address, true));
×
72
                }
73
            }
NEW
74
            else if (instruction.OpCode == OpCode.CallVoid)
×
75
            {
76
                if (TryGetAddressFromInstruction(instruction, out var address) && !appContext.MethodsByAddress.ContainsKey(address))
×
77
                {
78
                    nativeMethodInfoStack.Push((address, m.IsVoid));
×
79
                }
80
            }
81
        }
82
    }
×
83

84
    private static bool TryGetAddressFromInstruction(Instruction instruction, out ulong address)
85
    {
NEW
86
        var operand = instruction.Operands[0];
×
87

NEW
88
        if (instruction.Operands.Count > 0 && instruction.Operands[0].IsNumeric())
×
89
        {
NEW
90
            address = (ulong)operand;
×
91
            return true;
×
92
        }
93

94
        address = default;
×
95
        return false;
×
96
    }
97
}
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