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

SamboyCoding / Cpp2IL / 15159531087

21 May 2025 10:13AM UTC coverage: 34.289% (+0.2%) from 34.047%
15159531087

Pull #462

github

web-flow
Merge c760cddbe into 5807d2b6c
Pull Request #462: Support overriding member types

1801 of 6650 branches covered (27.08%)

Branch coverage included in aggregate %.

121 of 213 new or added lines in 33 files covered. (56.81%)

22 existing lines in 6 files now uncovered.

4201 of 10854 relevant lines covered (38.7%)

186268.63 hits per line

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

0.86
/Cpp2IL.Core/OutputFormats/WasmNameSectionOutputFormat.cs
1
using System;
2
using System.IO;
3
using System.Linq;
4
using System.Text;
5
using Cpp2IL.Core.Api;
6
using Cpp2IL.Core.Logging;
7
using Cpp2IL.Core.Model.Contexts;
8
using Cpp2IL.Core.Utils;
9
using LibCpp2IL;
10
using LibCpp2IL.Wasm;
11

12
namespace Cpp2IL.Core.OutputFormats;
13

14
public class WasmNameSectionOutputFormat : Cpp2IlOutputFormat
15
{
16
    public override string OutputFormatId => "wasm_name_section";
1✔
17
    public override string OutputFormatName => "Webassembly name section";
×
18

19
    public override void DoOutput(ApplicationAnalysisContext context, string outputRoot)
20
    {
21
        // Outputs a binary file matching the standardized "name" WebAssembly custom section.
22
        // If the game's binary doesn't already have the same type of section (which is practically always),
23
        // all that's needed to combine the two is a simple `cat main.wasm section.dat > out.wasm`.
24
        // Using this modified binary in place of the original one provides extra information for debuggers.
25

26
        // The spec can be found here: https://webassembly.github.io/spec/core/appendix/custom.html#name-section
27
        // Some additional subsections have been proposed (already implemented in v8 & spidermonkey), but they don't seem to be helpful as of now.
28
        // The proposal overview can be found here: https://github.com/WebAssembly/extended-name-section/blob/main/proposals/extended-name-section/Overview.md
29

30
        if (context.Binary.GetType() != typeof(WasmFile))
×
31
            throw new Exception("This output format only works with WebAssembly files");
×
32

33
        if (!Directory.Exists(outputRoot))
×
34
            Directory.CreateDirectory(outputRoot);
×
35

36
        var outputFile = File.Create(Path.Combine(outputRoot, "namesection.dat"));
×
37

38
        var section =
×
39
            new MemoryStream(); // This stream is separate from outputFile because we need to know the size of the section before writing it
×
40
        section.WriteName("name"); // The section's name (`name`)
×
41

42
        var moduleNameSubsection = new MemoryStream();
×
43
        moduleNameSubsection.WriteName("Unity");
×
44
        section.WriteSizedData(moduleNameSubsection, 0x0); // Subsection id 0
×
45
        moduleNameSubsection.Dispose();
×
46

47
        var paramSuccessCount = 0;
×
48
        var paramFailCount = 0;
×
49

50
        var functionData = context.AllTypes.SelectMany(t => t.Methods)
×
51
            .Select(method => (method, definition: WasmUtils.TryGetWasmDefinition(method)))
×
52
            .Where(v => v.definition is not null)
×
53
            .Select(v =>
×
54
            {
×
55
                var trueParamCount = v.definition!.GetType((WasmFile)LibCpp2IlMain.Binary!).ParamTypes.Length;
×
56

×
57
                // Also see WasmUtils.BuildSignature
×
58
                var parameters = v.method.Parameters.Select(param => param.Name).ToList();
×
59

×
60
                if (!v.method.IsStatic)
×
61
                    parameters.Insert(0, "this");
×
62

×
NEW
63
                if (v.method.ReturnType is { IsValueType: true, Definition: null or { Size: > 8 } } rt && rt.IsWasmPrimitive()) //TODO Check - I think this IsWasmPrimitive check is inverted but I just ported what it was
×
64
                    parameters.Insert(0, "out");
×
65

×
66
                parameters.Add("methodInfo"); // Only for some methods...?
×
67

×
68
                if (trueParamCount != parameters.Count)
×
69
                {
×
70
                    // Logger.WarnNewline($"Failed param matching for {v.method.FullNameWithSignature}, calculated {parameters.Count} with there actually being {trueParamCount} ({string.Join(" ", parameters)})");
×
71
                    parameters.Clear();
×
72
                    paramFailCount++;
×
73
                }
×
74
                else
×
75
                {
×
76
                    paramSuccessCount++;
×
77
                }
×
78

×
79
                return (
×
80
                    Index: v.definition!.FunctionTableIndex,
×
81
                    Name: v.method.FullName,
×
82
                    Parameters: parameters
×
83
                );
×
84
            })
×
85
            .GroupBy(v => v.Index)
×
86
            .OrderBy(grouping => grouping.Key)
×
87
            .ToDictionary(
×
88
                grouping => grouping.Key,
×
89
                grouping => grouping.Select(v => (v.Name, v.Parameters)).ToList()
×
90
            );
×
91
        
92
        Logger.InfoNewline(
×
93
            $"Estimated parameter naming success rate: {(float)paramSuccessCount / (paramSuccessCount + paramFailCount) * 100:N2}%");
×
94

95
        var functionNameSubsection = new MemoryStream();
×
96
        functionNameSubsection.WriteLEB128Unsigned((ulong)functionData.Count); // vector length
×
97

98
        var localNameSubsection = new MemoryStream();
×
99
        localNameSubsection.WriteLEB128Unsigned((ulong)functionData.Count); // vector length
×
100

101
        foreach (var (idx, data) in functionData)
×
102
        {
103
            functionNameSubsection.WriteLEB128Unsigned((ulong)idx);
×
104
            localNameSubsection.WriteLEB128Unsigned((ulong)idx);
×
105

106
            functionNameSubsection.WriteName(data.Count == 1 ? data[0].Name : $"multiple_{data.Count}_{data[0].Name}");
×
107

108
            localNameSubsection.WriteLEB128Unsigned((ulong)data[0].Parameters.Count); // vector length
×
109
            for (var i = 0; i < data[0].Parameters.Count; i++)
×
110
            {
111
                localNameSubsection.WriteLEB128Unsigned((ulong)i);
×
112
                // Possible to include type here, but may make names excessively long
113
                localNameSubsection.WriteName(data.Count == 1
×
114
                    ? data[0].Parameters[i]
×
115
                    : $"multiple_{data.Count}_{data[0].Parameters[i]}");
×
116
            }
117
        }
118

119
        section.WriteSizedData(functionNameSubsection, 0x1); // Subsection id 1
×
120
        section.WriteSizedData(localNameSubsection, 0x2); // Subsection id 2
×
121

122
        outputFile.WriteSizedData(section, 0x0); // Section id 0 - custom
×
123
    }
×
124
}
125

126
public static class Extensions
127
{
128
    public static void WriteName(this Stream memoryStream, string name)
129
    {
130
        var bytes = Encoding.Default.GetBytes(name);
×
131
        memoryStream.WriteLEB128Unsigned((ulong)bytes.Length);
×
132
        memoryStream.Write(bytes, 0, bytes.Length);
×
133
    }
×
134

135
    public static void WriteSizedData(this Stream memoryStream, MemoryStream data, byte? prependByte = null)
136
    {
137
        if (prependByte.HasValue)
×
138
        {
139
            memoryStream.WriteByte(prependByte.Value);
×
140
        }
141

142
        memoryStream.WriteLEB128Unsigned((ulong)data.Length);
×
143
        data.WriteTo(memoryStream);
×
144
    }
×
145
}
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