Files
rea/tests/fixtures/il2cpp/metadata.ts
T
JANG BYUNGKUNandgensokyo-dev 833136507d feat(il2cpp): inventory Unity IL2CPP global-metadata images, types and methods (#3807)
* feat(il2cpp): inventory IL2CPP global-metadata images, types and methods

Add inspect_il2cpp_metadata (CLI inspect-il2cpp-metadata), a read-only
in-process decoder for unencrypted Unity global-metadata.dat version 39.
It reports header sections, serialized index widths, images, type
definitions (nesting from the nestedTypes section) and method names.

Unrecognized magic and other versions are unsupported; damaged headers,
overlapping sections, inconsistent record sizes, out-of-range indices and
invalid strings fail with a reason code and no partial success. MCP
detail summary retains the complete Evidence and pages rows through
inspect_analysis_view kind il2cpp-metadata.

Adds an opt-in verify:il2cpp:metadata -- --metadata PATH check.

* feat(il2cpp): deliver metadata summaries by default over MCP

A 112,232-method file produced a 10.5 MB complete response, above the
10 MiB stdio budget. Default MCP delivery is now the retained summary;
detail complete stays available, and the one-shot CLI requests it.

* fix(il2cpp): reject shifted strings, nesting cycles and method owner drift

Review fuzzing showed that moving the strings section offset by one byte
silently renamed every image, type and method. Name indices must now start
a string, which also bounds total decoding by the section size.

Nesting cycles and methods whose declaring type disagrees with the owning
type range are rejected instead of returned. Adds service, shutdown and
index-width coverage, and moves environment-specific measurements out of
the public guide and tool description.

* feat(il2cpp): decode global-metadata versions 29 and 31

Unity 2022.3 writes version 29 or 31 metadata: a 256-byte header of
(offset, size) pairs, fixed 4-byte indices, type records with
elementTypeIndex, and method records that gained returnParameterToken in
version 31. Record counts come from section sizes and must be whole.

Sections report a null count for these versions and index widths report
fixed-width-version. Image, type and method rows keep the same shape, and
the string, nesting and method-ownership checks apply to every version.

* fix(il2cpp): bound report size before serializing and tighten coverage

Rows share decoded names, so a 130 KB file could expand into gigabytes of
JSON before the 64 MiB budget check, ending in an internal RangeError. The
provider now refuses from a lower bound on the serialized rows first.

The decoder also rejects non-empty sections inside the header, methods
outside their declaring type's range and overlapping image type ranges, and
keeps a leading byte order mark in names. Layouts now carry one indexing
mode instead of two coupled flags, and the tool description explains
nested_type_start.

* fix(il2cpp): refuse oversized metadata before decoding rows

Address review findings on the IL2CPP metadata inventory:

- check a lower bound from header row counts before any row object is
  allocated, so a large rejected file no longer builds every row first;
- validate sealed parent Evidence and count its rows once per report
  instead of on every inspect_analysis_view page;
- decode version 39 fixtures with 2- and 4-byte type-definition,
  generic-container and parameter indices.

* docs(il2cpp): attach the decoder comment to readIl2cppMetadata

* fix(il2cpp): deliver complete rows by default like the other inspection tools

inspect_il2cpp_metadata defaulted to detail summary, the only inspection
tool to do so. Omitted detail now returns every row inline, as
inspect_artifact, decode_interface_builder and the capture tools do;
summary stays one argument away and the guide, CLI reference and skill
reference say to select it first for large games, whose complete MCP
delivery is refused at the stdio budget with retained Evidence.

* test(il2cpp): cover narrow-width -1 sentinels and fix the skill's default

An empty image stores its type start as the all-ones value of a one- or
two-byte index; the decoder reads it as -1, but no fixture exercised it,
so removing either conversion passed the suite. The fixture can now append
an empty image, and both widths decode it. The skill still described a
summary default; it now says to select summary first, as the guide does.

* docs(il2cpp): request summary explicitly in the summary example

* chore(il2cpp): hash the verifier metadata through owned file resources

* fix(il2cpp): count escaped name length without building the escaped string

* fix(il2cpp): keep the verifier's check failure and small-table last page

* feat(domain): count JSON string length without building the escaped string

* fix(il2cpp): keep descriptor cleanup owners and share the JSON length count

* test(domain): give the backslash case real backslashes

* fix(il2cpp): select the CLI path and finish the command like the other tools

Follow main's absoluteCallerPath (#4067) and pass the command signal to finishAnalysisCommand.

* fix(il2cpp): select the verifier input path like the CLI

Use absoluteCallerPath (#4067) so a path with a symlink followed by .. names the same file the CLI and MCP inspect.

* feat(il2cpp): add compact composable metadata queries

Retain source-indexed Evidence for filtered batch and field queries. Bound representation growth before serialization, yield decoding for cancellation, and share CLI Evidence publication with desktop inspection. Verify public Unity metadata against independent byte and managed-DLL oracles and exercise actual stdio SDK workflows.

* fix(il2cpp): retain the validated metadata header magic

* test(il2cpp): remove obsolete execution wording exception

* fix(artifacts): preserve export selection and completed source facts

---------

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-10-20 07:20:47 +08:00

361 lines
12 KiB
TypeScript

import { IL2CPP_METADATA_SECTION_NAMES } from "../../../src/domain/il2cpp/il2cppMetadata.js";
type SectionName = (typeof IL2CPP_METADATA_SECTION_NAMES)[number];
type Width = 1 | 2 | 4;
/** Synthetic version 39 metadata small enough to read in a hex dump. */
export interface Il2cppMetadataFixtureOptions {
/** Serialized TypeIndex width. */
readonly typeIndexWidth?: Width;
/** Omit interfaceOffsets so the TypeIndex width comes from type records. */
readonly interfaceOffsets?: boolean;
/** Raw interfaceOffsets record size; defaults to 4 + typeIndexWidth. */
readonly interfaceOffsetRecordBytes?: number;
/** Make type 1 also list type 0, so 0 and 1 nest in each other. */
readonly nestingCycle?: boolean;
/** Append an image with no types, whose start is the -1 sentinel at the narrow width. */
readonly emptyImage?: boolean;
/** Metadata version; 29 and 31 use fixed 4-byte indices and no header counts. */
readonly version?: 29 | 31 | 39;
/**
* Version 39 entry counts that widen the serialized indices: filler type
* definitions (the last owns one extra method), and generic container and
* parameter sections whose records are zero-filled and never decoded.
*/
readonly wide?: {
readonly fillerTypes: number;
readonly genericContainers: number;
readonly parameters: number;
};
}
const widthFor = (entries: number): Width =>
entries <= 0xff ? 1 : entries <= 0xffff ? 2 : 4;
/** Type and method rows a fixture holds, including any wide-index filler. */
export const fixtureRows = (options: Il2cppMetadataFixtureOptions = {}) => {
const fillers = options.wide?.fillerTypes ?? 0;
const last = FIXTURE_TYPES.length + fillers - 1;
const types = [
...FIXTURE_TYPES,
...Array.from({ length: fillers }, (_, index) => ({
name: "Filler",
namespace: "",
method_start: index === fillers - 1 ? FIXTURE_METHODS.length : -1,
method_count: index === fillers - 1 ? 1 : 0,
nested_type_start: -1,
nested_type_count: 0,
declaring_type: -1,
})),
];
const methods = [
...FIXTURE_METHODS,
...(fillers === 0 ? [] : [{ name: "Fill", declaring_type: last }]),
];
return { types, methods };
};
export const FIXTURE_IMAGES = [
{ name: "Assembly-CSharp.dll", type_start: 0, type_count: 3 },
{ name: "mscorlib.dll", type_start: 3, type_count: 1 },
] as const;
export const FIXTURE_TYPES = [
{
name: "Player",
namespace: "Game",
method_start: 0,
method_count: 2,
nested_type_start: 0,
nested_type_count: 1,
declaring_type: -1,
},
{
name: "Slot",
namespace: "",
method_start: 2,
method_count: 1,
nested_type_start: -1,
nested_type_count: 0,
declaring_type: 0,
},
{
name: "<Module>",
namespace: "",
method_start: -1,
method_count: 0,
nested_type_start: -1,
nested_type_count: 0,
declaring_type: -1,
},
{
name: "Größe",
namespace: "System",
method_start: 3,
method_count: 2,
nested_type_start: -1,
nested_type_count: 0,
declaring_type: -1,
},
] as const;
export const FIXTURE_METHODS = [
{ name: ".ctor", declaring_type: 0 },
{ name: "Update", declaring_type: 0 },
{ name: "Add", declaring_type: 1 },
{ name: ".ctor", declaring_type: 3 },
{ name: "ToString", declaring_type: 3 },
] as const;
const writeIndex = (
target: Buffer,
offset: number,
width: Width,
value: number,
) => {
if (width === 1) target.writeUInt8(value === -1 ? 0xff : value, offset);
else if (width === 2)
target.writeUInt16LE(value === -1 ? 0xffff : value, offset);
else target.writeInt32LE(value, offset);
};
/** Built bytes with helpers that address header fields and records by name. */
export interface Il2cppMetadataFixture {
readonly bytes: Buffer;
readonly typeIndexWidth: Width;
readonly recordBytes: {
readonly typeDefinitions: number;
readonly images: number;
readonly methods: number;
};
/** Byte offset of a header entry field; only version 39 records counts. */
header(name: SectionName, field: "offset" | "size" | "count"): number;
section(name: SectionName): {
offset: number;
size: number;
count: number | null;
};
record(name: "typeDefinitions" | "images" | "methods", index: number): number;
/** Absolute offset of a string's first byte in the strings section. */
stringOffset(value: string): number;
}
type SectionContent = { bytes: Buffer; count: number };
/** Write the header and lay out non-empty sections in header order. */
const assembleFile = (
version: 29 | 31 | 39,
contents: ReadonlyMap<SectionName, SectionContent>,
) => {
const fixed = version !== 39;
const entryBytes = fixed ? 8 : 12;
const headerBytes = 8 + IL2CPP_METADATA_SECTION_NAMES.length * entryBytes;
const header = Buffer.alloc(headerBytes);
header.writeUInt32LE(0xfab11baf, 0);
header.writeInt32LE(version, 4);
const body: Buffer[] = [];
let cursor = headerBytes;
IL2CPP_METADATA_SECTION_NAMES.forEach((name, index) => {
const content = contents.get(name);
const at = 8 + index * entryBytes;
header.writeInt32LE(cursor, at);
header.writeInt32LE(content?.bytes.length ?? 0, at + 4);
if (!fixed) header.writeInt32LE(content?.count ?? 0, at + 8);
if (content !== undefined) {
body.push(content.bytes);
cursor += content.bytes.length;
}
});
const bytes = Buffer.concat([header, ...body]);
const headerField = (
name: SectionName,
field: "offset" | "size" | "count",
) => {
if (fixed && field === "count")
throw new Error(`Version ${version} headers record no counts`);
return (
8 +
IL2CPP_METADATA_SECTION_NAMES.indexOf(name) * entryBytes +
{ offset: 0, size: 4, count: 8 }[field]
);
};
const section = (name: SectionName) => ({
offset: bytes.readInt32LE(headerField(name, "offset")),
size: bytes.readInt32LE(headerField(name, "size")),
count: fixed ? null : bytes.readInt32LE(headerField(name, "count")),
});
return { bytes, headerField, section };
};
/** Image rows a fixture holds, including the optional empty image. */
export const fixtureImages = (options: Il2cppMetadataFixtureOptions = {}) => [
...FIXTURE_IMAGES,
...(options.emptyImage === true
? [{ name: "Empty.dll", type_start: -1, type_count: 0 }]
: []),
];
/** Lay sections out in header order; empty sections share the next offset. */
export const createIl2cppMetadataFixture = (
options: Il2cppMetadataFixtureOptions = {},
): Il2cppMetadataFixture => {
const version = options.version ?? 39;
const fixed = version !== 39;
const T: Width = fixed ? 4 : (options.typeIndexWidth ?? 1);
const rows = fixtureRows(options);
const genericContainers = options.wide?.genericContainers ?? 0;
const parameterCount = options.wide?.parameters ?? 0;
const D: Width = fixed ? 4 : widthFor(rows.types.length);
const G: Width = fixed ? 4 : widthFor(genericContainers);
const P: Width = fixed ? 4 : widthFor(parameterCount);
// Versions 29 and 31 also store elementTypeIndex in type records.
const typeIndexFields = fixed ? 4 : 3;
// Version 31 added returnParameterToken to method records.
const returnToken = version === 29 ? 0 : 4;
const nestedTypeEntries = options.nestingCycle === true ? [1, 0] : [1];
const typeRows = rows.types.map((type, index) =>
options.nestingCycle === true && index === 1
? { ...type, nested_type_start: 1, nested_type_count: 1 }
: type,
);
const recordBytes = {
typeDefinitions: 68 + typeIndexFields * T + G,
images: 32 + 2 * D,
methods: 16 + returnToken + D + T + P + G,
};
const stringParts: Buffer[] = [];
const stringIndex = new Map<string, number>();
let stringBytes = 0;
const intern = (value: string) => {
const known = stringIndex.get(value);
if (known !== undefined) return known;
const encoded = Buffer.from(`${value}\0`, "utf8");
stringIndex.set(value, stringBytes);
stringParts.push(encoded);
stringBytes += encoded.length;
return stringIndex.get(value)!;
};
const imageRows = fixtureImages(options);
for (const image of imageRows) intern(image.name);
for (const type of FIXTURE_TYPES) {
intern(type.name);
intern(type.namespace);
}
for (const row of [...rows.types, ...rows.methods]) intern(row.name);
const strings = Buffer.concat(stringParts);
const types = Buffer.alloc(typeRows.length * recordBytes.typeDefinitions);
typeRows.forEach((type, index) => {
let at = index * recordBytes.typeDefinitions;
types.writeInt32LE(intern(type.name), at);
types.writeInt32LE(intern(type.namespace), at + 4);
at += 8;
// byval, native declaring and parent TypeIndex values are deliberately
// unrelated to nesting; the reader must use nestedTypes instead.
for (let field = 0; field < typeIndexFields; field += 1, at += T)
writeIndex(types, at, T, 0x2a);
writeIndex(types, at, G, -1);
at += G + 4;
const starts = [-1, type.method_start, -1, -1, type.nested_type_start];
starts.forEach((value, field) => types.writeInt32LE(value, at + field * 4));
types.writeInt32LE(-1, at + 20);
types.writeInt32LE(-1, at + 24);
types.writeInt32LE(-1, at + 28);
at += 32;
types.writeUInt16LE(type.method_count, at);
types.writeUInt16LE(type.nested_type_count, at + 8);
at += 16;
types.writeUInt32LE(0, at);
types.writeUInt32LE(0x02000001 + index, at + 4);
});
const images = Buffer.alloc(imageRows.length * recordBytes.images);
imageRows.forEach((image, index) => {
const at = index * recordBytes.images;
images.writeInt32LE(intern(image.name), at);
images.writeInt32LE(index, at + 4);
writeIndex(images, at + 8, D, image.type_start);
images.writeUInt32LE(image.type_count, at + 8 + D);
writeIndex(images, at + 12 + D, D, -1);
images.writeUInt32LE(0, at + 12 + 2 * D);
images.writeInt32LE(-1, at + 16 + 2 * D);
});
const methods = Buffer.alloc(rows.methods.length * recordBytes.methods);
rows.methods.forEach((method, index) => {
const at = index * recordBytes.methods;
methods.writeInt32LE(intern(method.name), at);
writeIndex(methods, at + 4, D, method.declaring_type);
writeIndex(methods, at + 4 + D, T, 0);
const parameterStart = at + 4 + D + T + returnToken;
writeIndex(methods, parameterStart, P, -1);
writeIndex(methods, parameterStart + P, G, -1);
methods.writeUInt32LE(0x06000001 + index, parameterStart + P + G);
});
const nested = Buffer.alloc(nestedTypeEntries.length * 4);
nestedTypeEntries.forEach((value, index) =>
nested.writeInt32LE(value, index * 4),
);
const interfaceOffsetBytes = options.interfaceOffsetRecordBytes ?? 4 + T;
const interfaceOffsets =
options.interfaceOffsets === false
? null
: Buffer.alloc(interfaceOffsetBytes);
const contents = new Map<SectionName, SectionContent>([
["strings", { bytes: strings, count: stringIndex.size }],
["methods", { bytes: methods, count: rows.methods.length }],
...(genericContainers === 0
? []
: [
[
"genericContainers",
{
bytes: Buffer.alloc(genericContainers * 16),
count: genericContainers,
},
] as [SectionName, SectionContent],
]),
...(parameterCount === 0
? []
: [
[
"parameters",
{
bytes: Buffer.alloc(parameterCount * (8 + T)),
count: parameterCount,
},
] as [SectionName, SectionContent],
]),
["nestedTypes", { bytes: nested, count: nestedTypeEntries.length }],
["typeDefinitions", { bytes: types, count: typeRows.length }],
["images", { bytes: images, count: imageRows.length }],
...(interfaceOffsets === null
? []
: [
["interfaceOffsets", { bytes: interfaceOffsets, count: 1 }] as [
SectionName,
SectionContent,
],
]),
]);
const { bytes, headerField, section } = assembleFile(version, contents);
return {
bytes,
typeIndexWidth: T,
recordBytes,
header: headerField,
section,
record: (name, index) => section(name).offset + index * recordBytes[name],
stringOffset: (value) => {
const index = stringIndex.get(value);
if (index === undefined) throw new Error(`Unknown string ${value}`);
return section("strings").offset + index;
},
};
};