Unity - Memory Struct Note

记录一下逆出来的一些没什么用的玩意!!!加上一些网上总结的

结构

Il2CppClass结构

struct Il2CppClass {
    // ========== 总是有效的字段 ==========
    
    // +0x00 - 指向此类所属的程序集/模块
    const Il2CppImage* image;
    
    // +0x08 - GC 描述符(垃圾回收相关)
    void* gc_desc;
    
    // +0x10 - 类名(最重要!)
    const char* name;              // 例如: "GameObject", "Transform", "Player"
    
    // +0x18 - 命名空间
    const char* namespaze;         // 例如: "UnityEngine", "System", ""
    
    // +0x20 - 类型信息(值传递)
    const Il2CppType* byval_arg;
    
    // +0x28 - 类型信息(引用传递)
    const Il2CppType* this_arg;
    
    // +0x30 - 数组元素类型(如果这是数组类型)
    Il2CppClass* element_class;
    
    // +0x38 - 用于类型转换
    Il2CppClass* castClass;
    
    // +0x40 - 声明类型(嵌套类情况)
    Il2CppClass* declaringType;
    
    // +0x48 - 父类 // 超自然行动在0x58 看情况吧
    Il2CppClass* parent;           // 例如: GameObject 的 parent 是 Object
    
    // +0x50 - 泛型类信息
    Il2CppGenericClass* generic_class;
    
    // +0x58 - 类型定义
    const Il2CppTypeDefinition* typeDefinition;
    
    // +0x60 - 互操作数据
    const Il2CppInteropData* interopData;
    
    // ========== 需要初始化的字段 ==========
    
    // +0x68 - 字段数组(所有成员变量)
    FieldInfo* fields;
    
    // +0x70 - 事件数组
    const EventInfo* events;
    
    // +0x78 - 属性数组
    const PropertyInfo* properties;
    
    // +0x80 - 方法数组(所有方法)
    const MethodInfo** methods;
    
    // +0x88 - 嵌套类型
    Il2CppClass** nestedTypes;
    
    // +0x90 - 实现的接口
    Il2CppClass** implementedInterfaces;
    
    // +0x98 - 接口偏移
    Il2CppRuntimeInterfaceOffsetPair* interfaceOffsets;
    
    // +0xA0 - 静态字段(非常重要!)
    void* static_fields;           // 指向静态字段数据的指针
    
    // +0xA8 - RGCTX 数据
    const Il2CppRGCTXData* rgctx_data;
    
    // +0xB0 - 类型层次结构(用于快速 is/as 检查)
    Il2CppClass** typeHierarchy;
    
    // ========== 总是有效的剩余字段 ==========
    
    // +0xB8 - 静态构造函数状态
    uint32_t cctor_started;
    uint32_t cctor_finished;
    
    // +0xC0 - 静态构造函数线程
    uint64_t cctor_thread;
    
    // +0xC8 - 各种索引和大小
    int32_t genericContainerIndex;
    int32_t customAttributeIndex;
    
    // +0xD0 - 实例大小(非常重要!)
    uint32_t instance_size;        // 包括对象头的总大小
    
    // +0xD4 - 实际大小
    uint32_t actualSize;
    
    // +0xD8 - 元素大小(数组情况)
    uint32_t element_size;
    
    // +0xDC - Native 大小
    int32_t native_size;
    
    // +0xE0 - 静态字段大小
    uint32_t static_fields_size;
    
    // +0xE4 - 线程静态字段大小
    uint32_t thread_static_fields_size;
    
    // +0xE8 - 线程静态字段偏移
    int32_t thread_static_fields_offset;
    
    // +0xEC - 标志位
    uint32_t flags;
    
    // +0xF0 - Token
    uint32_t token;
    
    // +0xF4 - 更多属性
    uint16_t method_count;         // 方法数量
    uint16_t property_count;       // 属性数量
    uint16_t field_count;          // 字段数量
    uint16_t event_count;          // 事件数量
    
    // +0xFC - 嵌套类型数量
    uint16_t nested_type_count;
    
    // +0xFE - vtable 数量
    uint16_t vtable_count;
    
    // +0x100 - 接口数量
    uint16_t interfaces_count;
    
    // +0x102 - 接口偏移数量
    uint16_t interface_offsets_count;
    
    // +0x104 - 类型层次深度
    uint8_t typeHierarchyDepth;
    
    // +0x105 - 泛型递归深度
    uint8_t genericRecursionDepth;
    
    // +0x106 - 其他标志
    uint8_t rank;                  // 数组维度
    uint8_t minimumAlignment;
    uint8_t naturalAligment;
    uint8_t packingSize;
    
    // +0x10A - 位标志
    uint8_t initialized_and_no_error : 1;
    uint8_t valuetype : 1;              // 是否是值类型(struct)
    uint8_t initialized : 1;
    uint8_t enumtype : 1;               // 是否是枚举
    uint8_t is_generic : 1;
    uint8_t has_references : 1;
    uint8_t init_pending : 1;
    uint8_t size_inited : 1;
    
    // ... 更多标志位
};

Il2CppObject

struct Il2CppObject {
    Il2CppClass* klass;       // +0x00 - 类型元数据!
    void* monitor;            // +0x08 - 同步锁
};

UnityEngine_Object

struct UnityEngine_Object {
    Il2CppObject base;        // +0x00
    void* m_CachedPtr;        // +0x10
    int32_t m_InstanceID;     // +0x18
    uint32_t padding;         // +0x1C
    void* m_UnityRuntimeErrorString; // +0x20
};

例子

Unity IL2CPP 对象完整内存布局图
================================================================================

【场景】假设你有一个 GameObject 对象的指针 = 0x7CA2C08EA0


╔════════════════════════════════════════════════════════════════════════════╗
║                          GameObject 对象实例                                 ║
║                      (地址: 0x7CA2C08EA0)                                   ║
╠════════════════════════════════════════════════════════════════════════════╣
║ 偏移    大小   字段                  值示例            说明                 ║
╠════════════════════════════════════════════════════════════════════════════╣
║ +0x00  8字节  Il2CppClass* klass   0x12345678  ─┐  类型元数据指针          ║
║ +0x08  8字节  void* monitor        0x00000000   │  同步监视器              ║
╟────────────────────────────────────────────────────────────────────────────╢
║ ↑↑↑ 这是 Il2CppObject 头,所有托管对象都有  │                            ║
╟────────────────────────────────────────────────────────────────────────────╢
║ +0x10  8字节  IntPtr m_CachedPtr   0x7D9E084D80  │  指向 C++ Native 对象   ║
║ +0x18  4字节  int m_InstanceID     4294461790    │  实例 ID                ║
║ +0x1C  4字节  (padding)            0x00000000    │  对齐填充               ║
║ +0x20  8字节  string m_UnityRuntimeErrorString   │  错误信息字符串         ║
╟────────────────────────────────────────────────────────────────────────────╢
║ ↑↑↑ 这是 UnityEngine.Object 的字段             │                            ║
╟────────────────────────────────────────────────────────────────────────────╢
║ +0x28  ...   (GameObject 没有额外 C# 字段)     │                            ║
╚════════════════════════════════════════════════════════════════════════════╝
                                               │
                                               │  通过 klass 指针访问类型信息
                                               │
                                               ▼
╔════════════════════════════════════════════════════════════════════════════╗
║                          Il2CppClass 元数据                                 ║
║                      (地址: 0x12345678)                                     ║
║                                                                              ║
║  这个结构包含了 "UnityEngine.GameObject" 类的所有元数据                      ║
╠════════════════════════════════════════════════════════════════════════════╣
║ 偏移    大小   字段                  值示例            说明                 ║
╠════════════════════════════════════════════════════════════════════════════╣
║ +0x00  8字节  Il2CppImage* image   0x...           所属程序集              ║
║ +0x08  8字节  void* gc_desc        0x...           GC 描述符               ║
║ +0x10  8字节  const char* name  ───►"GameObject" ◄── 类名在这里!          ║
║ +0x18  8字节  const char* namespaze─►"UnityEngine"◄─ 命名空间在这里!      ║
║ +0x20  8字节  Il2CppType* byval_arg   0x...        值类型信息              ║
║ +0x28  8字节  Il2CppType* this_arg   0x...         引用类型信息            ║
║ +0x30  8字节  Il2CppClass* element_class 0x...    数组元素类型            ║
║ +0x38  8字节  Il2CppClass* castClass 0x...        转换类型                ║
║ +0x40  8字节  Il2CppClass* declaringType 0x...    声明类型                ║
║ +0x48  8字节  Il2CppClass* parent ────►0xABCDEF ◄── 父类 (Object)         ║
║ +0x50  8字节  Il2CppGenericClass* generic_class   泛型类信息              ║
║ +0x58  8字节  Il2CppTypeDefinition* typeDefinition 类型定义               ║
║ +0x60  8字节  Il2CppInteropData* interopData      互操作数据              ║
║ +0x68  8字节  FieldInfo* fields   ────►0x...    ◄── 字段数组              ║
║ +0x70  8字节  EventInfo* events                    事件数组                ║
║ +0x78  8字节  PropertyInfo* properties             属性数组                ║
║ +0x80  8字节  MethodInfo** methods ───►0x...    ◄── 方法数组              ║
║ +0x88  8字节  Il2CppClass** nestedTypes           嵌套类型                ║
║ +0x90  8字节  Il2CppClass** implementedInterfaces 实现的接口              ║
║ +0x98  8字节  void* interfaceOffsets              接口偏移                ║
║ +0xA0  8字节  void* static_fields ────►0x...    ◄── 静态字段!            ║
║ +0xA8  8字节  Il2CppRGCTXData* rgctx_data        RGCTX 数据              ║
║ +0xB0  8字节  Il2CppClass** typeHierarchy        类型层次                ║
║ +0xB8  4字节  uint32_t cctor_started              静态构造开始            ║
║ +0xBC  4字节  uint32_t cctor_finished             静态构造完成            ║
║ +0xC0  8字节  uint64_t cctor_thread               静态构造线程            ║
║ +0xC8  4字节  int32_t genericContainerIndex       泛型容器索引            ║
║ +0xCC  4字节  int32_t customAttributeIndex        自定义属性索引          ║
║ +0xD0  4字节  uint32_t instance_size  ──►0x28   ◄── 实例大小!            ║
║ +0xD4  4字节  uint32_t actualSize                 实际大小                ║
║ +0xD8  4字节  uint32_t element_size               元素大小                ║
║ +0xDC  4字节  int32_t native_size                 Native 大小             ║
║ +0xE0  4字节  uint32_t static_fields_size         静态字段大小            ║
║ +0xE4  4字节  uint32_t thread_static_fields_size  线程静态字段大小        ║
║ +0xE8  4字节  int32_t thread_static_fields_offset 线程静态字段偏移        ║
║ +0xEC  4字节  uint32_t flags                      标志位                  ║
║ +0xF0  4字节  uint32_t token                      Token                   ║
║ +0xF4  2字节  uint16_t method_count               方法数量                ║
║ +0xF6  2字节  uint16_t property_count             属性数量                ║
║ +0xF8  2字节  uint16_t field_count                字段数量                ║
║ +0xFA  2字节  uint16_t event_count                事件数量                ║
║ +0xFC  2字节  uint16_t nested_type_count          嵌套类型数量            ║
║ +0xFE  2字节  uint16_t vtable_count               虚表数量                ║
║ +0x100 2字节  uint16_t interfaces_count           接口数量                ║
║ +0x102 2字节  uint16_t interface_offsets_count    接口偏移数量            ║
║ +0x104 1字节  uint8_t typeHierarchyDepth          类型层次深度            ║
║ +0x105 1字节  uint8_t genericRecursionDepth       泛型递归深度            ║
║ +0x106 1字节  uint8_t rank                        数组维度                ║
║ +0x107 ... (更多字段)                                                       ║
╚════════════════════════════════════════════════════════════════════════════╝
          │                                            │
          │ parent 指针                                │ name 指针
          ▼                                            ▼
    ┌──────────────┐                           ┌─────────────────┐
    │   Object     │                           │ "GameObject"    │
    │  Il2CppClass │                           │ (字符串常量)    │
    └──────────────┘                           └─────────────────┘


════════════════════════════════════════════════════════════════════════════


完整访问路径示意图 ---------------------------- fuqiuluo write it!!!

获取类型名称的完整路径:

    GameObject 对象指针
         │
         │ 读取 +0x00 (klass)
         ▼
    Il2CppClass* klass
         │
         │ 读取 +0x10 (name)
         ▼
    const char* name
         │
         ▼
    "GameObject"

文献

  • https://platinmods.com/threads/understanding-il2cpp-structure-1.96921/

  • https://github.com/Jumboperson/Il2CppDumper/blob/master/il2cpp.h

  • https://sneakyevil.gitbook.io/il2cpp-resolver/utils/usage-of-field-offsets

  • https://www.jacksondunstan.com/articles/4533

  • https://platinmods.com/threads/static-fields.121149/

  • https://gameguardian.net/forum/files/file/2892-badcases-il2cpp-fields/

  • https://badcase.org/product/il2cpp-fields/

  • https://guidedhacking.com/threads/how-to-get-the-address-of-a-static-variable-in-unity-games.16246/

  • https://github.com/djkaty/Il2CppInspector/issues/25

  • https://uninomicon.com/objectlayout