From 6eaa22d34adc0eb9833526f20ae5933922ac7505 Mon Sep 17 00:00:00 2001 From: Sam Byass Date: Fri, 13 Dec 2024 22:46:41 +0000 Subject: [PATCH] Core: Fix more test failures --- .../Model/Contexts/HasCustomAttributes.cs | 66 +++++++++++-------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs b/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs index c58257be..e9b84e45 100644 --- a/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs +++ b/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs @@ -64,6 +64,11 @@ public abstract class HasCustomAttributes(uint token, ApplicationAnalysisContext public abstract string CustomAttributeOwnerName { get; } + /// + /// Pre-v29, stores the index of the custom attribute range for this member. Post-v29, always 0. + /// + private int Pre29RangeIndex; + public bool IsCompilerGeneratedBasedOnCustomAttributes => CustomAttributes?.Any(a => a.Constructor.DeclaringType!.FullName.Contains("CompilerGeneratedAttribute")) ?? AttributeTypes?.Any(t => t.Type == Il2CppTypeEnum.IL2CPP_TYPE_CLASS && t.AsClass().FullName!.Contains("CompilerGeneratedAttribute")) @@ -88,7 +93,7 @@ protected void InitCustomAttributeData() return; } - AttributeTypeRange = AppContext.Metadata.GetCustomAttributeData(CustomAttributeAssembly.Definition.Image, CustomAttributeIndex, Token, out var rangeIndex); + AttributeTypeRange = AppContext.Metadata.GetCustomAttributeData(CustomAttributeAssembly.Definition.Image, CustomAttributeIndex, Token, out Pre29RangeIndex); if (AttributeTypeRange == null || AttributeTypeRange.count == 0) { @@ -101,7 +106,35 @@ protected void InitCustomAttributeData() .Select(attrIdx => AppContext.Metadata!.attributeTypes![attrIdx]) //Not null because we've checked we're not on v29 .Select(typeIdx => AppContext.Binary!.GetType(typeIdx)) .ToList(); + } + + private (long blobStart, long blobEnd)? GetV29BlobOffsets() + { + var target = new Il2CppCustomAttributeDataRange() { token = Token }; + var caIndex = AppContext.Metadata.AttributeDataRanges.BinarySearch + ( + CustomAttributeAssembly.Definition.Image.customAttributeStart, + (int)CustomAttributeAssembly.Definition.Image.customAttributeCount, + target, + new TokenComparer() + ); + + if (caIndex < 0) + { + RawIl2CppCustomAttributeData = Array.Empty(); + return null; + } + + var attributeDataRange = AppContext.Metadata.AttributeDataRanges[caIndex]; + var next = AppContext.Metadata.AttributeDataRanges[caIndex + 1]; + + var blobStart = AppContext.Metadata.metadataHeader.attributeDataOffset + attributeDataRange.startOffset; + var blobEnd = AppContext.Metadata.metadataHeader.attributeDataOffset + next.startOffset; + return (blobStart, blobEnd); + } + private void InitPre29AttributeGeneratorAnalysis(int rangeIndex) + { ulong generatorPtr; if (AppContext.MetadataVersion < 27) try @@ -112,7 +145,7 @@ protected void InitCustomAttributeData() { Logger.WarnNewline("Custom attribute generator out of range for " + this, "CA Restore"); RawIl2CppCustomAttributeData = Array.Empty(); - return; //Bail out + return; } else { @@ -126,38 +159,13 @@ protected void InitCustomAttributeData() { Logger.WarnNewline($"Supposedly had custom attributes ({string.Join(", ", AttributeTypes)}), but generator was null for " + this, "CA Restore"); RawIl2CppCustomAttributeData = Memory.Empty; - return; //Possibly no attributes with params? + return; } CaCacheGeneratorAnalysis = new(generatorPtr, AppContext, this); RawIl2CppCustomAttributeData = CaCacheGeneratorAnalysis.RawBytes; } - private (long blobStart, long blobEnd)? GetV29BlobOffsets() - { - var target = new Il2CppCustomAttributeDataRange() { token = Token }; - var caIndex = AppContext.Metadata.AttributeDataRanges.BinarySearch - ( - CustomAttributeAssembly.Definition.Image.customAttributeStart, - (int)CustomAttributeAssembly.Definition.Image.customAttributeCount, - target, - new TokenComparer() - ); - - if (caIndex < 0) - { - RawIl2CppCustomAttributeData = Array.Empty(); - return null; - } - - var attributeDataRange = AppContext.Metadata.AttributeDataRanges[caIndex]; - var next = AppContext.Metadata.AttributeDataRanges[caIndex + 1]; - - var blobStart = AppContext.Metadata.metadataHeader.attributeDataOffset + attributeDataRange.startOffset; - var blobEnd = AppContext.Metadata.metadataHeader.attributeDataOffset + next.startOffset; - return (blobStart, blobEnd); - } - /// /// Attempt to parse the Il2CppCustomAttributeData blob into custom attributes. /// @@ -175,6 +183,8 @@ public void AnalyzeCustomAttributeData(bool allowAnalysis = true) AnalyzeCustomAttributeDataV29(); return; } + + InitPre29AttributeGeneratorAnalysis(Pre29RangeIndex); if (RawIl2CppCustomAttributeData.Length == 0) return;