Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cpp2IL.Core/CorePlugin/AttributeInjectorProcessingLayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -80,14 +80,14 @@ private static void InjectAddressAttribute(ApplicationAnalysisContext appContext

foreach (var m in assemblyAnalysisContext.Types.SelectMany(t => t.Methods))
{
if (m.CustomAttributes == null || m.Definition == null)
if (m.CustomAttributes == null || m.UnderlyingPointer == 0)
continue;

var newAttribute = new AnalyzedCustomAttribute(addressConstructor);

if (!_useEzDiffMode)
{
newAttribute.Fields.Add(new(rvaField, new CustomAttributePrimitiveParameter($"0x{m.Definition.Rva:X}", newAttribute, CustomAttributeParameterKind.Field, 0)));
newAttribute.Fields.Add(new(rvaField, new CustomAttributePrimitiveParameter($"0x{m.Rva:X}", newAttribute, CustomAttributeParameterKind.Field, 0)));
if (appContext.Binary.TryMapVirtualAddressToRaw(m.UnderlyingPointer, out var offset))
newAttribute.Fields.Add(new(offsetField, new CustomAttributePrimitiveParameter($"0x{offset:X}", newAttribute, CustomAttributeParameterKind.Field, 1)));
}
Expand Down
7 changes: 4 additions & 3 deletions Cpp2IL.Core/CorePlugin/CallAnalysisProcessingLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static void InjectAttribute(ApplicationAnalysisContext appContext)
foreach (var m in assemblyAnalysisContext.Types.SelectMany(t => t.Methods))
{
m.AnalyzeCustomAttributeData();
if (m.CustomAttributes == null || m.Definition == null)
if (m.CustomAttributes == null || m.UnderlyingPointer == 0)
continue;

if (appContext.MethodsByAddress.TryGetValue(m.UnderlyingPointer, out var methodsWithThatAddress) && methodsWithThatAddress.Count > 1)
Expand All @@ -73,11 +73,12 @@ private static void InjectAttribute(ApplicationAnalysisContext appContext)
}
catch
{
m.ConvertedIsil = null;
AttributeInjectionUtils.AddZeroParameterAttribute(m, analysisFailedConstructor);
continue;
}

if (m.ConvertedIsil is null or { Count: 0 })
if (m.ConvertedIsil is { Count: 0 })
{
if ((m.MethodAttributes & MethodAttributes.Abstract) == 0)
{
Expand Down Expand Up @@ -135,7 +136,7 @@ private static void InjectAttribute(ApplicationAnalysisContext appContext)

foreach (var m in assemblyAnalysisContext.Types.SelectMany(t => t.Methods))
{
if (m.CustomAttributes == null || m.Definition == null)
if (m.CustomAttributes == null || m.UnderlyingPointer == 0)
continue;

var unknownCallCount = unknownCalls.GetOrDefault(m, 0);
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL.Core/Model/Contexts/ApplicationAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public BaseKeyFunctionAddresses GetOrCreateKeyFunctionAddresses()
return _keyFunctionAddresses;
}

public MultiAssemblyInjectedType InjectTypeIntoAllAssemblies(string ns, string name, TypeAnalysisContext baseType)
public MultiAssemblyInjectedType InjectTypeIntoAllAssemblies(string ns, string name, TypeAnalysisContext? baseType)
{
var types = Assemblies.Select(a => (InjectedTypeAnalysisContext)a.InjectType(ns, name, baseType)).ToArray();

Expand Down
6 changes: 4 additions & 2 deletions Cpp2IL.Core/Model/Contexts/InjectedMethodAnalysisContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Reflection;
using System.Reflection;

namespace Cpp2IL.Core.Model.Contexts;

public class InjectedMethodAnalysisContext : MethodAnalysisContext
{
public override ulong UnderlyingPointer => 0;

public override string DefaultName { get; }

public override bool IsStatic { get; }
Expand All @@ -25,4 +27,4 @@ public InjectedMethodAnalysisContext(TypeAnalysisContext parent, string name, bo
Parameters.Add(new InjectedParameterAnalysisContext(injectedParameterName, injectedParameterType, this));
}
}
}
}
18 changes: 16 additions & 2 deletions Cpp2IL.Core/Model/Contexts/MethodAnalysisContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Cpp2IL.Core.Graphs;
using Cpp2IL.Core.ISIL;
using Cpp2IL.Core.Utils;
using LibCpp2IL;
using LibCpp2IL.BinaryStructures;
using LibCpp2IL.Metadata;
using StableNameDotNet.Providers;
Expand Down Expand Up @@ -35,6 +37,8 @@ public class MethodAnalysisContext : HasCustomAttributesAndName, IMethodInfoProv
/// </summary>
public virtual ulong UnderlyingPointer => Definition?.MethodPointer ?? throw new("Subclasses of MethodAnalysisContext should override UnderlyingPointer");

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

/// <summary>
/// The raw method body as machine code in the active instruction set.
/// </summary>
Expand All @@ -52,6 +56,9 @@ public class MethodAnalysisContext : HasCustomAttributesAndName, IMethodInfoProv

public List<ParameterAnalysisContext> Parameters = new();

/// <summary>
/// Does this method return void?
/// </summary>
public virtual bool IsVoid => (Definition?.ReturnType?.ToString() ?? throw new("Subclasses of MethodAnalysisContext should override IsVoid")) == "System.Void";

public virtual bool IsStatic => Definition?.IsStatic ?? throw new("Subclasses of MethodAnalysisContext should override IsStatic");
Expand Down Expand Up @@ -101,11 +108,18 @@ protected MethodAnalysisContext(ApplicationAnalysisContext context) : base(0, co
RawBytes = Array.Empty<byte>();
}

[MemberNotNull(nameof(ConvertedIsil))]
public void Analyze()
{
if(UnderlyingPointer == 0)
if (ConvertedIsil != null)
return;

if (UnderlyingPointer == 0)
{
ConvertedIsil = new(0);
return;

}

ConvertedIsil = AppContext.InstructionSet.GetIsilFromMethod(this);

if (ConvertedIsil.Count == 0)
Expand Down