Cpp2IL icon indicating copy to clipboard operation
Cpp2IL copied to clipboard

Null Reference Exceptions in Property Type Handling and Custom Attribute Population

Open abevol opened this issue 4 months ago • 0 comments

Two null reference exceptions were encountered while processing IL2CPP metadata:

Issue 1: Property Type Handling

A null reference exception occurs in Il2CppPropertyDefinition.get_RawPropertyType() when processing property types. This happens when either the getter method is null and the setter's parameters are not properly initialized, or when the getter's return type is null.

Stack trace points to:

LibCpp2IL.Metadata.Il2CppPropertyDefinition.get_RawPropertyType() in 
Il2CppPropertyDefinition.cs:line 44

Code:

public Il2CppType? RawPropertyType => 
LibCpp2IlMain.TheMetadata == null ? null : 
Getter == null ? Setter!.Parameters![0].RawType : Getter!.RawReturnType;

Issue 2: Custom Attribute Population

Another null reference exception occurs in AsmResolverAssemblyPopulator.PopulateCustomAttributes() when trying to copy custom attributes. The issue appears when accessing the "AsmResolverProperty" extra data which might be null for some property definitions.

Stack trace points to:

Cpp2IL.Core.Utils.AsmResolver.AsmResolverAssemblyPopulator.PopulateCustomAttributes() in 
AsmResolverAssemblyPopulator.cs:line 275

Code:

foreach (var property in type.Properties)
    CopyCustomAttributes(property, property.GetExtraData<PropertyDefinition>("AsmResolverProperty")!.CustomAttributes);

Environment

  • Unity Version: 2022.3.51f1
  • Runtime: .NET 6.0.7
  • Platform: Windows 10 64-bit
  • IL2CPP Metadata version: 31.1

Temporary Fix

A temporary fix has been implemented with two patches:

  1. First patch improves null checking in property type handling:

    • Added null checks for property definition and raw property type
    • Modified RawPropertyType getter to safely handle null cases
    • Added checks to skip properties without definition or type information
  2. Second patch adds null checking for custom attribute population:

    • Added null check before accessing AsmResolverProperty's custom attributes
    • Skips custom attribute copying when AsmResolverProperty is null The patches demonstrate the issues can be resolved by proper null checking, but a more comprehensive solution might be needed to handle these edge cases properly.

Additional Context

These issues were encountered while using BepInEx (version 6.0.0-be.735) to generate IL2CPP interop assemblies.

[Attached: Error logs and temporary fix patches] Error-1.log Error-2.log 0001-fix-metadata.patch 0002-fix.patch

abevol avatar Jun 11 '25 04:06 abevol