Skip to content

Commit

Permalink
Core: Fix more test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Dec 13, 2024
1 parent c91170a commit 6eaa22d
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public abstract class HasCustomAttributes(uint token, ApplicationAnalysisContext

public abstract string CustomAttributeOwnerName { get; }

/// <summary>
/// Pre-v29, stores the index of the custom attribute range for this member. Post-v29, always 0.
/// </summary>
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"))
Expand All @@ -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)
{
Expand All @@ -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

Check warning on line 114 in Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

View workflow job for this annotation

GitHub Actions / Build - Windows .NET Framework Zip

Dereference of a possibly null reference.

Check warning on line 114 in Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

View workflow job for this annotation

GitHub Actions / Build Single-File Artifact (linux-x64, Cpp2IL)

Dereference of a possibly null reference.

Check warning on line 114 in Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

View workflow job for this annotation

GitHub Actions / Build Single-File Artifact (osx-x64, Cpp2IL)

Dereference of a possibly null reference.

Check warning on line 114 in Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

View workflow job for this annotation

GitHub Actions / Build Single-File Artifact (win-x64, Cpp2IL)

Dereference of a possibly null reference.
(
CustomAttributeAssembly.Definition.Image.customAttributeStart,
(int)CustomAttributeAssembly.Definition.Image.customAttributeCount,
target,
new TokenComparer()
);

if (caIndex < 0)
{
RawIl2CppCustomAttributeData = Array.Empty<byte>();
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
Expand All @@ -112,7 +145,7 @@ protected void InitCustomAttributeData()
{
Logger.WarnNewline("Custom attribute generator out of range for " + this, "CA Restore");
RawIl2CppCustomAttributeData = Array.Empty<byte>();
return; //Bail out
return;
}
else
{
Expand All @@ -126,38 +159,13 @@ protected void InitCustomAttributeData()
{
Logger.WarnNewline($"Supposedly had custom attributes ({string.Join(", ", AttributeTypes)}), but generator was null for " + this, "CA Restore");

Check warning on line 160 in Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

View workflow job for this annotation

GitHub Actions / Build Single-File Artifact (linux-x64, Cpp2IL)

Possible null reference argument for parameter 'values' in 'string string.Join<Il2CppType>(string? separator, IEnumerable<Il2CppType> values)'.

Check warning on line 160 in Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

View workflow job for this annotation

GitHub Actions / Build Single-File Artifact (osx-x64, Cpp2IL)

Possible null reference argument for parameter 'values' in 'string string.Join<Il2CppType>(string? separator, IEnumerable<Il2CppType> values)'.

Check warning on line 160 in Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

View workflow job for this annotation

GitHub Actions / Build Single-File Artifact (win-x64, Cpp2IL)

Possible null reference argument for parameter 'values' in 'string string.Join<Il2CppType>(string? separator, IEnumerable<Il2CppType> values)'.
RawIl2CppCustomAttributeData = Memory<byte>.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<byte>();
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);
}

/// <summary>
/// Attempt to parse the Il2CppCustomAttributeData blob into custom attributes.
/// </summary>
Expand All @@ -175,6 +183,8 @@ public void AnalyzeCustomAttributeData(bool allowAnalysis = true)
AnalyzeCustomAttributeDataV29();
return;
}

InitPre29AttributeGeneratorAnalysis(Pre29RangeIndex);

if (RawIl2CppCustomAttributeData.Length == 0)
return;
Expand Down

0 comments on commit 6eaa22d

Please sign in to comment.