Skip to content

Commit

Permalink
Make call analysis compatible with native method detection (SamboyCod…
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 authored Jan 1, 2024
1 parent 00b0e70 commit 6a5e46e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cpp2IL.Core/Model/Contexts/ApplicationAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private void PopulateMethodsByAddressTable()
return AssembliesByName[name];
}

public TypeAnalysisContext? ResolveContextForType(Il2CppTypeDefinition typeDefinition) => GetAssemblyByName(typeDefinition.DeclaringAssembly!.Name!)?.TypesByDefinition[typeDefinition];
public TypeAnalysisContext? ResolveContextForType(Il2CppTypeDefinition typeDefinition) => GetAssemblyByName(typeDefinition.DeclaringAssembly!.Name!)?.GetTypeByDefinition(typeDefinition);

public BaseKeyFunctionAddresses GetOrCreateKeyFunctionAddresses()
{
Expand Down
15 changes: 9 additions & 6 deletions Cpp2IL.Core/Model/Contexts/AssemblyAnalysisContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Cpp2IL.Core.Extensions;
using Cpp2IL.Core.Utils;
using LibCpp2IL.BinaryStructures;
Expand Down Expand Up @@ -40,9 +41,9 @@ public class AssemblyAnalysisContext : HasCustomAttributes

public override string CustomAttributeOwnerName => Definition.AssemblyName.Name;

private Dictionary<string, TypeAnalysisContext> TypesByName = new();
private readonly Dictionary<string, TypeAnalysisContext> TypesByName = new();

public readonly Dictionary<Il2CppTypeDefinition, TypeAnalysisContext> TypesByDefinition = new();
private readonly Dictionary<Il2CppTypeDefinition, TypeAnalysisContext> TypesByDefinition = new();

/// <summary>
/// Get assembly name without the extension and with any invalid path characters or elements removed.
Expand Down Expand Up @@ -77,14 +78,16 @@ public AssemblyAnalysisContext(Il2CppAssemblyDefinition assemblyDefinition, Appl
}
}

public TypeAnalysisContext InjectType(string ns, string name, TypeAnalysisContext? baseType)
public TypeAnalysisContext InjectType(string ns, string name, TypeAnalysisContext? baseType, TypeAttributes typeAttributes = TypeAnalysisContext.DefaultTypeAttributes)
{
var ret = new InjectedTypeAnalysisContext(this, name, ns, baseType);
var ret = new InjectedTypeAnalysisContext(this, name, ns, baseType, typeAttributes);
Types.Add(ret);
return ret;
}

public TypeAnalysisContext? GetTypeByFullName(string fullName) => TypesByName.TryGetValue(fullName, out var typeContext) ? typeContext : null;


public TypeAnalysisContext? GetTypeByDefinition(Il2CppTypeDefinition typeDefinition) => TypesByDefinition.TryGetValue(typeDefinition, out var typeContext) ? typeContext : null;

public override string ToString() => "Assembly: " + Definition.AssemblyName.Name;
}
}
9 changes: 6 additions & 3 deletions Cpp2IL.Core/Model/Contexts/InjectedTypeAnalysisContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Reflection;

namespace Cpp2IL.Core.Model.Contexts;
Expand All @@ -9,11 +9,14 @@ public class InjectedTypeAnalysisContext : TypeAnalysisContext

public override string DefaultNs { get; }

public InjectedTypeAnalysisContext(AssemblyAnalysisContext containingAssembly, string name, string ns, TypeAnalysisContext? baseType) : base(null, containingAssembly)
public override TypeAttributes TypeAttributes { get; }

public InjectedTypeAnalysisContext(AssemblyAnalysisContext containingAssembly, string name, string ns, TypeAnalysisContext? baseType, TypeAttributes typeAttributes = DefaultTypeAttributes) : base(null, containingAssembly)
{
DefaultName = name;
DefaultNs = ns;
OverrideBaseType = baseType;
TypeAttributes = typeAttributes;
}

public InjectedMethodAnalysisContext InjectMethodContext(string methodName, bool isStatic, TypeAnalysisContext returnType, MethodAttributes attributes, params TypeAnalysisContext[] args)
Expand All @@ -33,4 +36,4 @@ public InjectedFieldAnalysisContext InjectFieldContext(string fieldName, TypeAna
Fields.Add(field);
return field;
}
}
}
6 changes: 5 additions & 1 deletion Cpp2IL.Core/Model/Contexts/ReferencedTypeAnalysisContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using LibCpp2IL.BinaryStructures;
Expand Down Expand Up @@ -35,6 +35,10 @@ public abstract class ReferencedTypeAnalysisContext : TypeAnalysisContext

protected override int CustomAttributeIndex => -1;

public sealed override bool IsGenericInstance => GenericArguments.Count > 0;

public sealed override int GenericParameterCount => GenericArguments.Count;

public override AssemblyAnalysisContext CustomAttributeAssembly => DeclaringAssembly;

protected ReferencedTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext referencedFrom) : base(null, referencedFrom)
Expand Down
14 changes: 8 additions & 6 deletions Cpp2IL.Core/Model/Contexts/TypeAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Cpp2IL.Core.Model.Contexts;
/// </summary>
public class TypeAnalysisContext : HasCustomAttributesAndName, ITypeInfoProvider, ICSharpSourceToken
{
internal const TypeAttributes DefaultTypeAttributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;

/// <summary>
/// The context for the assembly this type was defined in.
/// </summary>
Expand Down Expand Up @@ -203,15 +205,15 @@ public static ITypeInfoProvider GetSndnProviderForType(ApplicationAnalysisContex
}

public IEnumerable<ITypeInfoProvider> Interfaces => Definition!.RawInterfaces!.Select(t => GetSndnProviderForType(AppContext, t));
public TypeAttributes TypeAttributes => Definition!.Attributes;
public int GenericParameterCount => Definition!.GenericContainer?.genericParameterCount ?? 0;
public virtual TypeAttributes TypeAttributes => Definition?.Attributes ?? DefaultTypeAttributes;
public virtual int GenericParameterCount => Definition!.GenericContainer?.genericParameterCount ?? 0;
public string OriginalTypeName => DefaultName;
public string RewrittenTypeName => Name;
public string TypeNamespace => Namespace;
public bool IsGenericInstance => false;
public bool IsValueType => Definition!.IsValueType;
public bool IsEnumType => Definition!.IsEnumType;
public bool IsInterface => Definition!.IsInterface;
public virtual bool IsGenericInstance => false;
public bool IsValueType => Definition?.IsValueType ?? BaseType is { Namespace: "System", Name: "ValueType" };
public bool IsEnumType => Definition?.IsEnumType ?? BaseType is { Namespace: "System", Name: "Enum" };
public bool IsInterface => Definition?.IsInterface ?? ((TypeAttributes & TypeAttributes.Interface) != default);
public IEnumerable<ITypeInfoProvider> GenericArgumentInfoProviders => Array.Empty<ITypeInfoProvider>();
public IEnumerable<IFieldInfoProvider> FieldInfoProviders => Fields;
public IEnumerable<IMethodInfoProvider> MethodInfoProviders => Methods;
Expand Down
4 changes: 1 addition & 3 deletions Cpp2IL.Core/OutputFormats/AsmResolverDummyDllOutputFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,8 @@ private static TypeDefinition BuildStubType(TypeAnalysisContext typeContext)
{
var typeDef = typeContext.Definition;

const int defaultAttributes = (int)(TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed);

//Initialize an empty type definition
var ret = new TypeDefinition(typeContext.Namespace, typeContext.Name, (TypeAttributes)(typeDef?.Flags ?? defaultAttributes));
var ret = new TypeDefinition(typeContext.Namespace, typeContext.Name, (TypeAttributes)typeContext.TypeAttributes);

//Set up its layout
if (typeDef != null && typeDef.BaseType?.ToString() != "System.Enum")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Cpp2IL.Core.Api;
using Cpp2IL.Core.ISIL;
using Cpp2IL.Core.Model.Contexts;
Expand All @@ -16,7 +17,11 @@ public class NativeMethodDetectionProcessingLayer : Cpp2IlProcessingLayer
public override void Process(ApplicationAnalysisContext appContext, Action<int, int>? progressCallback = null)
{
var nativeMethodInfoStack = new Stack<(ulong, bool)>();
var cppNativeMethodsType = appContext.AssembliesByName["mscorlib"].InjectType("Cpp2ILInjected", "CppNativeMethods", null);
var cppNativeMethodsType = appContext.AssembliesByName["mscorlib"].InjectType(
"Cpp2ILInjected",
"CppNativeMethods",
null,
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Abstract | TypeAttributes.Sealed);//public static class
foreach (var assemblyAnalysisContext in appContext.Assemblies)
{
foreach (var m in assemblyAnalysisContext.Types.SelectMany(t => t.Methods))
Expand Down

0 comments on commit 6a5e46e

Please sign in to comment.