fix(managed): size member lists by pointer and target widths (#12207)

Carries forward the three ECMA-335 metadata width corrections from #10126 with uncompressed metadata layout controls.

Co-authored-by: rudycelekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
morlutoandrudycelekli authored and GitHub committed 2026-11-19 05:29:13 +00:00
1 parent 823141b3d9
commit e9e3574744
2 files changed
+142 -3

No files matched your search

@@ -0,0 +1,138 @@
import { describe, expect, it } from "vitest";
import { readMetadataString } from "./managedMetadataHeaps.js";
import {
metadataRowOffset,
readManagedMetadataLayout,
} from "./managedMetadataLayout.js";
const memberLists = [
{
name: "ParamList",
owner: 6,
pointer: 7,
target: 8,
listOffset: 12,
nameOffset: 4,
},
{
name: "EventList",
owner: 18,
pointer: 19,
target: 20,
listOffset: 2,
nameOffset: 2,
},
{
name: "PropertyList",
owner: 21,
pointer: 22,
target: 23,
listOffset: 2,
nameOffset: 2,
},
] as const;
/** Write uncompressed metadata with independently specified field widths. */
const writeMemberList = (
members: (typeof memberLists)[number],
pointerRows: number,
targetRows: number,
listWidth: 2 | 4,
pointerWidth: 2 | 4,
) => {
const tableStart = 80;
const headerBytes = 24 + 4 * 4;
const ownerOffset = tableStart + headerBytes + 10; // One Module row.
const pointerOffset = ownerOffset + members.listOffset + listWidth;
const targetOffset = pointerOffset + pointerRows * pointerWidth;
const tablesEnd = targetOffset + targetRows * 6;
const strings = Buffer.from("\0subject\0", "ascii");
const stringsOffset = Math.ceil(tablesEnd / 4) * 4;
const guidOffset = Math.ceil((stringsOffset + strings.length) / 4) * 4;
const bytes = Buffer.alloc(guidOffset + 16);
bytes.writeUInt32LE(0x424a_5342, 0);
bytes.writeUInt16LE(1, 4);
bytes.writeUInt16LE(1, 6);
bytes.writeUInt32LE(12, 12);
bytes.write("v4.0.30319", 16, "ascii");
bytes.writeUInt16LE(3, 30);
let cursor = 32;
for (const [name, offset, size] of [
["#-", tableStart, tablesEnd - tableStart],
["#Strings", stringsOffset, strings.length],
["#GUID", guidOffset, 16],
] as const) {
bytes.writeUInt32LE(offset, cursor);
bytes.writeUInt32LE(size, cursor + 4);
bytes.write(name, cursor + 8, "ascii");
cursor += 8 + Math.ceil((name.length + 1) / 4) * 4;
}
bytes[tableStart + 4] = 2;
bytes[tableStart + 7] = 1;
bytes.writeBigUInt64LE(
1n |
(1n << BigInt(members.owner)) |
(1n << BigInt(members.pointer)) |
(1n << BigInt(members.target)),
tableStart + 8,
);
for (const [index, count] of [1, 1, pointerRows, targetRows].entries())
bytes.writeUInt32LE(count, tableStart + 24 + index * 4);
// The list and pointer rows both reference row 1.
bytes[ownerOffset + members.listOffset] = 1;
for (
let offset = pointerOffset;
offset < targetOffset;
offset += pointerWidth
)
bytes[offset] = 1;
bytes.writeUInt16LE(1, targetOffset + members.nameOffset);
strings.copy(bytes, stringsOffset);
return { bytes, targetOffset };
};
describe.each(memberLists)("$name metadata layout", (members) => {
it.each([
{
scenario: "only the pointer table is wide",
pointerRows: 65_536,
targetRows: 1,
listWidth: 4,
pointerWidth: 2,
},
{
scenario: "only the target table is wide",
pointerRows: 1,
targetRows: 65_536,
listWidth: 4,
pointerWidth: 4,
},
{
scenario: "neither table is wide",
pointerRows: 65_535,
targetRows: 65_535,
listWidth: 2,
pointerWidth: 2,
},
] as const)(
"preserves target row position and name when $scenario",
({ pointerRows, targetRows, listWidth, pointerWidth }) => {
const { bytes, targetOffset } = writeMemberList(
members,
pointerRows,
targetRows,
listWidth,
pointerWidth,
);
const layout = readManagedMetadataLayout(bytes, 0, bytes.length);
const offset = metadataRowOffset(layout, members.target, 1);
expect(offset).toBe(targetOffset);
const name = bytes.readUInt16LE(offset + members.nameOffset);
expect(readMetadataString(bytes, layout, name, 32)).toBe("subject");
},
);
});
@@ -19,7 +19,8 @@ const ROW_SIZE_FACTORIES: readonly RowSizeFactory[] = [
({ tableSize: t }) => t(4),
({ stringSize: s, blobSize: b }) => 2 + s + b,
({ tableSize: t }) => t(6),
({ tableSize: t, stringSize: s, blobSize: b }) => 8 + s + b + t(8),
({ tableSize: t, stringSize: s, blobSize: b }) =>
8 + s + b + Math.max(t(7), t(8)),
({ tableSize: t }) => t(8),
({ stringSize: s }) => 4 + s,
({ tableSize: t, codedSize: c }) => t(2) + c("TypeDefOrRef"),
@@ -33,10 +34,10 @@ const ROW_SIZE_FACTORIES: readonly RowSizeFactory[] = [
({ tableSize: t }) => 6 + t(2),
({ tableSize: t }) => 4 + t(4),
({ blobSize: b }) => b,
({ tableSize: t }) => t(2) + t(20),
({ tableSize: t }) => t(2) + Math.max(t(19), t(20)),
({ tableSize: t }) => t(20),
({ stringSize: s, codedSize: c }) => 2 + s + c("TypeDefOrRef"),
({ tableSize: t }) => t(2) + t(23),
({ tableSize: t }) => t(2) + Math.max(t(22), t(23)),
({ tableSize: t }) => t(23),
({ stringSize: s, blobSize: b }) => 2 + s + b,
({ tableSize: t, codedSize: c }) => 2 + t(6) + c("HasSemantics"),