Skip to content

Commit b46caa1

Browse files
committed
Refactor Il2CppBinary and related components
1 parent 72c306a commit b46caa1

26 files changed

+91
-106
lines changed

Cpp2IL.Core/Api/Cpp2IlInstructionSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class Cpp2IlInstructionSet
1515
/// <param name="context">The method to get the body for</param>
1616
/// <param name="isAttributeGenerator">True if this is an attribute generator function, false if it's a managed method</param>
1717
/// <returns>A byte array representing the method's body</returns>
18-
public abstract Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator);
18+
public abstract ReadOnlyMemory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator);
1919

2020
/// <summary>
2121
/// Returns the virtual address from which the given method starts. By default, returns the <see cref="Il2CppMethodDefinition.MethodPointer"/> property, but

Cpp2IL.Core/Extensions/EnumerableExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ public static IEnumerable<T> MaybeAppend<T>(this IEnumerable<T> enumerable, T? i
1717
return enumerable;
1818
}
1919

20-
public static MemoryEnumerable<T> AsEnumerable<T>(this Memory<T> memory) => new(memory);
20+
public static MemoryEnumerable<T> AsEnumerable<T>(this ReadOnlyMemory<T> memory) => new(memory);
2121

22-
public static MemoryEnumerator<T> GetEnumerator<T>(this Memory<T> memory) => new(memory);
22+
public static MemoryEnumerator<T> GetEnumerator<T>(this ReadOnlyMemory<T> memory) => new(memory);
2323

24-
public class MemoryEnumerable<T>(Memory<T> memory) : IEnumerable<T>
24+
public class MemoryEnumerable<T>(ReadOnlyMemory<T> memory) : IEnumerable<T>
2525
{
2626
public IEnumerator<T> GetEnumerator() => new MemoryEnumerator<T>(memory);
2727

2828
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
2929
}
3030

31-
public class MemoryEnumerator<T>(Memory<T> memory) : IEnumerator<T>
31+
public class MemoryEnumerator<T>(ReadOnlyMemory<T> memory) : IEnumerator<T>
3232
{
3333
private int _index = -1;
3434

Cpp2IL.Core/Extensions/MiscExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static IEnumerable<T> Peek<T>(this IEnumerable<T> enumerable, Action<T> a
161161
});
162162
}
163163

164-
public static unsafe uint ReadUInt(this Span<byte> span, int start)
164+
public static unsafe uint ReadUInt(this ReadOnlySpan<byte> span, int start)
165165
{
166166
if (start >= span.Length)
167167
throw new ArgumentOutOfRangeException(nameof(start), $"start=[{start}], mem.Length=[{span.Length}]");

Cpp2IL.Core/Il2CppApiFunctions/Arm64KeyFunctionAddresses.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using Cpp2IL.Core.Extensions;
@@ -47,7 +47,7 @@ protected override void Init(ApplicationAnalysisContext context)
4747
var oldLength = primaryExecutableSection.Length;
4848

4949
var toRemove = (int)(attributeGeneratorList[^1] - primaryExecutableSectionVa);
50-
primaryExecutableSection = primaryExecutableSection.Skip(toRemove).ToArray();
50+
primaryExecutableSection = primaryExecutableSection.Slice(toRemove);
5151

5252
primaryExecutableSectionVa = attributeGeneratorList[^1];
5353

@@ -80,7 +80,7 @@ protected override void Init(ApplicationAnalysisContext context)
8080
oldLength = primaryExecutableSection.Length;
8181

8282
toRemove = (int)(startFrom - primaryExecutableSectionVa);
83-
primaryExecutableSection = primaryExecutableSection.Skip(toRemove).ToArray();
83+
primaryExecutableSection = primaryExecutableSection.Slice(toRemove);
8484

8585
primaryExecutableSectionVa = startFrom;
8686

@@ -100,7 +100,7 @@ protected override void Init(ApplicationAnalysisContext context)
100100
oldLength = primaryExecutableSection.Length;
101101

102102
toRemove = (int)(startFrom - primaryExecutableSectionVa);
103-
primaryExecutableSection = primaryExecutableSection.Skip(toRemove).ToArray();
103+
primaryExecutableSection = primaryExecutableSection.Slice(toRemove);
104104

105105
primaryExecutableSectionVa = startFrom;
106106

@@ -116,7 +116,7 @@ protected override void Init(ApplicationAnalysisContext context)
116116
var oldLength = primaryExecutableSection.Length;
117117

118118
var toKeep = (int)(attributeGeneratorList[^1] - primaryExecutableSectionVa);
119-
primaryExecutableSection = primaryExecutableSection.SubArray(..toKeep);
119+
primaryExecutableSection = primaryExecutableSection[..toKeep];
120120

121121
//This doesn't change, we've trimmed the end, not the beginning
122122
// primaryExecutableSectionVa = primaryExecutableSectionVa;
@@ -125,7 +125,7 @@ protected override void Init(ApplicationAnalysisContext context)
125125
}
126126
}
127127

128-
_allInstructions = disassembler.Disassemble(primaryExecutableSection, (long)primaryExecutableSectionVa).ToList();
128+
_allInstructions = disassembler.Disassemble(primaryExecutableSection.ToArray(), (long)primaryExecutableSectionVa).ToList();
129129
}
130130

131131
protected override IEnumerable<ulong> FindAllThunkFunctions(ulong addr, uint maxBytesBack = 0, params ulong[] addressesToIgnore)

Cpp2IL.Core/InstructionSets/Arm64InstructionSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Cpp2IL.Core.InstructionSets;
1414

1515
public class Arm64InstructionSet : Cpp2IlInstructionSet
1616
{
17-
public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
17+
public override ReadOnlyMemory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
1818
{
1919
//Avoid use of capstone where possible
2020
if (true || context is not ConcreteGenericMethodAnalysisContext)
@@ -25,7 +25,7 @@ public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context,
2525
var count = startOfNextFunction - ptrAsInt;
2626

2727
if (startOfNextFunction > 0)
28-
return LibCpp2IlMain.Binary!.GetRawBinaryContent().AsMemory(ptrAsInt, count);
28+
return LibCpp2IlMain.Binary!.GetRawBinaryContent().Slice(ptrAsInt, count);
2929
}
3030

3131
var instructions = Arm64Utils.GetArm64MethodBodyAtVirtualAddress(context.UnderlyingPointer);

Cpp2IL.Core/InstructionSets/ArmV7InstructionSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Cpp2IL.Core.InstructionSets;
1313

1414
public class ArmV7InstructionSet : Cpp2IlInstructionSet
1515
{
16-
public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
16+
public override ReadOnlyMemory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
1717
{
1818
if (ArmV7Utils.TryGetMethodBodyBytesFast(context.UnderlyingPointer, context is AttributeGeneratorMethodAnalysisContext) is { } ret)
1919
return ret;

Cpp2IL.Core/InstructionSets/NewArmV8InstructionSet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class NewArmV8InstructionSet : Cpp2IlInstructionSet
1818
[ThreadStatic]
1919
private static Dictionary<Arm64Register, ulong> adrpOffsets = new();
2020

21-
public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
21+
public override ReadOnlyMemory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
2222
{
2323
if (context is not ConcreteGenericMethodAnalysisContext)
2424
{
@@ -28,7 +28,7 @@ public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context,
2828
var count = startOfNextFunction - ptrAsInt;
2929

3030
if (startOfNextFunction > 0)
31-
return LibCpp2IlMain.Binary!.GetRawBinaryContent().AsMemory(ptrAsInt, count);
31+
return LibCpp2IlMain.Binary!.GetRawBinaryContent().Slice(ptrAsInt, count);
3232
}
3333

3434
var result = NewArm64Utils.GetArm64MethodBodyAtVirtualAddress(context.UnderlyingPointer);
@@ -43,7 +43,7 @@ public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context,
4343
if (start < 0 || end < 0 || start >= context.AppContext.Binary.RawLength || end >= context.AppContext.Binary.RawLength)
4444
throw new Exception($"Failed to map virtual address 0x{context.UnderlyingPointer:X} to raw address for method {context!.DeclaringType?.FullName}/{context.Name} - start: 0x{start:X}, end: 0x{end:X} are out of bounds for length {context.AppContext.Binary.RawLength}.");
4545

46-
return context.AppContext.Binary.GetRawBinaryContent().AsMemory(start, end - start);
46+
return context.AppContext.Binary.GetRawBinaryContent().Slice(start, end - start);
4747
}
4848

4949
public override List<InstructionSetIndependentInstruction> GetIsilFromMethod(MethodAnalysisContext context)

Cpp2IL.Core/InstructionSets/WasmInstructionSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Cpp2IL.Core.InstructionSets;
1212

1313
public class WasmInstructionSet : Cpp2IlInstructionSet
1414
{
15-
public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
15+
public override ReadOnlyMemory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator)
1616
{
1717
if (context.Definition is { } methodDefinition)
1818
{

Cpp2IL.Core/InstructionSets/X86InstructionSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static string FormatInstruction(Instruction instruction)
3333
}
3434
}
3535

36-
public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator) => X86Utils.GetRawManagedOrCaCacheGenMethodBody(context.UnderlyingPointer, isAttributeGenerator);
36+
public override ReadOnlyMemory<byte> GetRawBytesForMethod(MethodAnalysisContext context, bool isAttributeGenerator) => X86Utils.GetRawManagedOrCaCacheGenMethodBody(context.UnderlyingPointer, isAttributeGenerator);
3737

3838
public override BaseKeyFunctionAddresses CreateKeyFunctionAddressesInstance() => new X86KeyFunctionAddresses();
3939

Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public abstract class HasCustomAttributes(uint token, ApplicationAnalysisContext
2525
/// <summary>
2626
/// On V29, stores the custom attribute blob. Pre-29, stores the bytes for the custom attribute generator function.
2727
/// </summary>
28-
public Memory<byte> RawIl2CppCustomAttributeData = Memory<byte>.Empty;
28+
public ReadOnlyMemory<byte> RawIl2CppCustomAttributeData = ReadOnlyMemory<byte>.Empty;
2929

3030
/// <summary>
3131
/// Stores the analyzed custom attribute data once analysis has actually run.

0 commit comments

Comments
 (0)