Skip to content

Remove static mutable state from LibCpp2IL#541

Closed
nitrog0d wants to merge 4 commits intoSamboyCoding:developmentfrom
nitrog0d:development
Closed

Remove static mutable state from LibCpp2IL#541
nitrog0d wants to merge 4 commits intoSamboyCoding:developmentfrom
nitrog0d:development

Conversation

@nitrog0d
Copy link
Copy Markdown

@nitrog0d nitrog0d commented Apr 9, 2026

  • All metadata and binary structures now use per-instance OwningBinary/OwningMetadata (via ReadableClass base) instead of static LibCpp2IlMain fields
  • LibCpp2IlContext holds all state that was previously global: Binary, Metadata, MethodsByPtr, and global mapper collections
  • Il2CppBinary automatically propagates OwningBinary/OwningMetadata to any ReadableClass it reads via OnReadableCreated
  • Il2CppMetadata sets OwningMetadata on all structures at construction, and exposes SetOwningBinaryOnAllStructures called by the builder
  • Cpp2IL.Core utility methods now take Il2CppBinary as an explicit parameter instead of accessing statics
  • Legacy static API on LibCpp2IlMain marked [Obsolete] for backwards compatibility

nitrog0d added 2 commits April 9, 2026 07:36
Migrate all metadata/binary structures from static LibCpp2IlMain fields
to per-instance OwningBinary/OwningMetadata on ReadableClass. Move global
mapper state into LibCpp2IlContext. Update Cpp2IL.Core to pass Il2CppBinary
explicitly. Legacy static API kept as [Obsolete] wrappers.
@Alluseri
Copy link
Copy Markdown
Contributor

Alluseri commented Apr 9, 2026

Hi, did you write all of this yourself?

Copy link
Copy Markdown
Contributor

@Xpl0itR Xpl0itR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vibecoders amirite...

also needs a bit of refactoring to remove all the OwningMetadata == null and OwningBinary == null checks which should be unnessesary

var memoryOp = (IsilMemoryOperand)instruction.Operands[1].Data;
if (memoryOp.Base == null && memoryOp.Index == null && memoryOp.Scale == 0)
{
#pragma warning disable CS0618 // Deprecated static API - TODO: migrate to context-based API when available from Cpp2IL.Core
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs implementing

internal static void Init()
internal static void Init(Il2CppBinary binary)
{
_primitiveSizes = new(14)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would also need to become instanced, but im not seeing any references to _primitiveSizes so it could just be removed

disassembler.EnableInstructionDetails = true;
disassembler.EnableSkipDataMode = true;
disassembler.DisassembleSyntax = DisassembleSyntax.Intel;
_arm64Disassembler = disassembler;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to become instanced too

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boilerplate ahh... primary ctors exist

}

public static int GetPointerSizeBytes()
public static int GetPointerSizeBytes(Il2CppBinary binary)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better as an instanced method on Il2CppBinary

Comment on lines +181 to +185
#pragma warning disable CS0618 // Fallback to legacy statics for backwards compatibility
var effectiveBinary = binary ?? LibCpp2IlMain.Binary!;
var effectiveMetadata = metadata ?? LibCpp2IlMain.TheMetadata!;
#pragma warning restore CS0618

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't fallback to static state, params shouldnt be nullable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests should not use static state. maybe rull them in parallel to ensure that works

public ulong CachedClass; /* if present, the Il2CppClass corresponding to the instantiation. */

private float EffectiveMetadataVersion => OwningMetadata?.MetadataVersion ?? LibCpp2IlMain.MetadataVersion;
private float EffectiveMetadataVersion => OwningMetadata?.MetadataVersion ?? MetadataVersion;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant property

t.OwningMetadata = OwningMetadata ?? LibCpp2IlMain.TheMetadata;
t.Il2CppTypeHasNumMods5Bits ??= (OwningMetadata?.MetadataVersion ?? LibCpp2IlMain.MetadataVersion) >= 27.2f;
t.OwningMetadata = OwningMetadata;
t.Il2CppTypeHasNumMods5Bits ??= (OwningMetadata?.MetadataVersion ?? MetadataVersion) >= 27.2f;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant fallback (MetadataVersion)


public abstract class ReadableClass
{
internal float MetadataVersion { get; set; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant property

Remove unused _primitiveSizes from MiscUtils. Use primary constructor for
UnsupportedInstructionSetException. Remove redundant EffectiveMetadataVersion
from Il2CppGenericClass. Make MetadataUsage binary/metadata params non-nullable
and remove static fallbacks. Move PointerSizeBytes to Il2CppBinary instance
property. Expose LibCpp2IlContext on ApplicationAnalysisContext and migrate
MetadataProcessor. Move Arm64 disassembler from static Arm64Utils to
ApplicationAnalysisContext. Remove unnecessary OwningBinary/OwningMetadata
null checks across metadata structures.
@nitrog0d nitrog0d requested a review from Xpl0itR April 9, 2026 20:05
Remove LibCpp2IlGlobalMapper (logic already in LibCpp2IlContext). Remove
static fallbacks from LibCpp2IlUtils, Il2CppTypeReflectionData, and
MetadataUsage. Remove static VTABLE_OFFSET from Il2CppClassUsefulOffsets.
Change X86Utils to take is32Bit param instead of full binary. Move Arm64
disassembler to ApplicationAnalysisContext. Expose LibCpp2IlContext on
ApplicationAnalysisContext and migrate MetadataProcessor. Keep legacy
static field writes in LibCpp2IlContextBuilder since LibCpp2IlReflection
still depends on DefaultContext.
@lilmayofuksu
Copy link
Copy Markdown

clankers...

@nitrog0d nitrog0d closed this by deleting the head repository Apr 9, 2026
@nitrog0d
Copy link
Copy Markdown
Author

nitrog0d commented Apr 9, 2026

good attempt lets try it again in like a few months

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants