fix(dotnet): validate managed metadata and CIL boundaries (#938)
* perf(dotnet): reuse decoded metadata rows * test(dotnet): preserve expanded fixture metadata * style(dotnet): format metadata inventory * fix(dotnet): bound method bodies to PE section data * fix(dotnet): derive fixture metadata index widths * fix(dotnet): validate managed metadata bounds * test(dotnet): cover large metadata ReadyToRun layout * fix(dotnet): validate exception instruction boundaries * fix(dotnet): report invalid metadata coded indexes * fix(dotnet): clarify MemberForwarded null diagnostics * fix(dotnet): honor complete CIL and EH boundaries * fix(dotnet): validate metadata identity rows and offsets
This commit is contained in:
+558
-102
@@ -5,22 +5,31 @@ import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
interface ManagedPeFixtureOptions {
|
||||
readonly cliFlags?: number;
|
||||
readonly corruptMetadataSignature?: boolean;
|
||||
readonly customAttributeTypeRaw?: number;
|
||||
readonly extendsRaw?: number;
|
||||
readonly fieldName?: string;
|
||||
readonly fieldSignature?: Buffer;
|
||||
readonly ilBody?: Buffer;
|
||||
readonly malformedAssemblyReferenceRows?: readonly number[];
|
||||
readonly malformedCustomAttributeRows?: readonly number[];
|
||||
readonly metadataValidMaskExtra?: bigint;
|
||||
readonly methodName?: string;
|
||||
readonly methodSignature?: Buffer;
|
||||
readonly moduleRowCount?: number;
|
||||
readonly assemblyRowCount?: number;
|
||||
readonly memberRefParentRaw?: number;
|
||||
readonly mvid?: Buffer;
|
||||
readonly pinvoke?: {
|
||||
readonly importName?: string;
|
||||
readonly memberForwardedRaw?: number;
|
||||
readonly mappingFlags?: number;
|
||||
readonly moduleName?: string;
|
||||
};
|
||||
readonly readyToRun?: boolean;
|
||||
readonly references?: readonly string[];
|
||||
readonly resourceImplementationRaw?: number;
|
||||
readonly resourceData?: Buffer;
|
||||
readonly targetFramework?: string;
|
||||
readonly targetFramework?: string | null;
|
||||
readonly typeName?: string;
|
||||
readonly typeNamespace?: string;
|
||||
}
|
||||
@@ -35,6 +44,10 @@ class StringHeap {
|
||||
readonly #chunks: Buffer[] = [Buffer.from([0])];
|
||||
#size = 1;
|
||||
|
||||
get size(): number {
|
||||
return this.#size;
|
||||
}
|
||||
|
||||
add(value: string): number {
|
||||
if (value.length === 0) return 0;
|
||||
const index = this.#size;
|
||||
@@ -53,13 +66,26 @@ class BlobHeap {
|
||||
readonly #chunks: Buffer[] = [Buffer.from([0])];
|
||||
#size = 1;
|
||||
|
||||
get size(): number {
|
||||
return this.#size;
|
||||
}
|
||||
|
||||
add(value: Buffer): number {
|
||||
if (value.length === 0) return 0;
|
||||
if (value.length > 0x7f)
|
||||
throw new RangeError("Managed PE fixture blob is too large");
|
||||
const index = this.#size;
|
||||
this.#chunks.push(Buffer.from([value.length]), value);
|
||||
this.#size += value.length + 1;
|
||||
const prefix =
|
||||
value.length <= 0x7f
|
||||
? Buffer.from([value.length])
|
||||
: value.length <= 0x3fff
|
||||
? Buffer.from([0x80 | (value.length >> 8), value.length & 0xff])
|
||||
: Buffer.from([
|
||||
0xc0 | (value.length >> 24),
|
||||
(value.length >> 16) & 0xff,
|
||||
(value.length >> 8) & 0xff,
|
||||
value.length & 0xff,
|
||||
]);
|
||||
this.#chunks.push(prefix, value);
|
||||
this.#size += value.length + prefix.length;
|
||||
return index;
|
||||
}
|
||||
|
||||
@@ -106,33 +132,92 @@ const u32 = (value: number): Buffer => {
|
||||
return bytes;
|
||||
};
|
||||
|
||||
const moduleRow = (name: number): Buffer =>
|
||||
Buffer.concat([u16(0), u16(name), u16(1), u16(0), u16(0)]);
|
||||
type MetadataIndexSize = 2 | 4;
|
||||
|
||||
const typeRefRow = (name: number, namespace: number): Buffer =>
|
||||
Buffer.concat([u16(0), u16(name), u16(namespace)]);
|
||||
const indexSize = (rows: number, codedTagBits = 0): MetadataIndexSize =>
|
||||
rows < 2 ** (16 - codedTagBits) ? 2 : 4;
|
||||
|
||||
const typeDefRow = (
|
||||
const metadataIndex = (value: number, size: MetadataIndexSize): Buffer =>
|
||||
size === 2 ? u16(value) : u32(value);
|
||||
|
||||
const codedIndexSize = (
|
||||
rows: readonly number[],
|
||||
tagBits: number,
|
||||
): MetadataIndexSize => indexSize(Math.max(...rows), tagBits);
|
||||
|
||||
const moduleRow = (
|
||||
name: number,
|
||||
namespace: number,
|
||||
fieldList: number,
|
||||
methodList: number,
|
||||
stringSize: MetadataIndexSize,
|
||||
guidSize: MetadataIndexSize,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
u32(0x0010_0001),
|
||||
u16(name),
|
||||
u16(namespace),
|
||||
u16((1 << 2) | 1),
|
||||
u16(fieldList),
|
||||
u16(methodList),
|
||||
u16(0),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(1, guidSize),
|
||||
metadataIndex(0, guidSize),
|
||||
metadataIndex(0, guidSize),
|
||||
]);
|
||||
|
||||
const fieldRow = (name: number, signature: number): Buffer =>
|
||||
Buffer.concat([u16(0x0001), u16(name), u16(signature)]);
|
||||
const typeRefRow = (
|
||||
name: number,
|
||||
namespace: number,
|
||||
stringSize: MetadataIndexSize,
|
||||
resolutionScopeSize: MetadataIndexSize,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
metadataIndex(0, resolutionScopeSize),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(namespace, stringSize),
|
||||
]);
|
||||
|
||||
const typeDefRow = ({
|
||||
name,
|
||||
namespace,
|
||||
fieldList,
|
||||
methodList,
|
||||
stringSize,
|
||||
typeDefOrRefSize,
|
||||
fieldListSize,
|
||||
methodListSize,
|
||||
extendsRaw = (1 << 2) | 1,
|
||||
}: {
|
||||
readonly name: number;
|
||||
readonly namespace: number;
|
||||
readonly fieldList: number;
|
||||
readonly methodList: number;
|
||||
readonly stringSize: MetadataIndexSize;
|
||||
readonly typeDefOrRefSize: MetadataIndexSize;
|
||||
readonly fieldListSize: MetadataIndexSize;
|
||||
readonly methodListSize: MetadataIndexSize;
|
||||
readonly extendsRaw?: number;
|
||||
}): Buffer =>
|
||||
Buffer.concat([
|
||||
u32(0x0010_0001),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(namespace, stringSize),
|
||||
metadataIndex(extendsRaw, typeDefOrRefSize),
|
||||
metadataIndex(fieldList, fieldListSize),
|
||||
metadataIndex(methodList, methodListSize),
|
||||
]);
|
||||
|
||||
const fieldRow = (
|
||||
name: number,
|
||||
signature: number,
|
||||
stringSize: MetadataIndexSize,
|
||||
blobSize: MetadataIndexSize,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
u16(0x0001),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(signature, blobSize),
|
||||
]);
|
||||
|
||||
interface MethodDefRowInput {
|
||||
readonly name: number;
|
||||
readonly signature: number;
|
||||
readonly stringSize: MetadataIndexSize;
|
||||
readonly blobSize: MetadataIndexSize;
|
||||
readonly parameterSize: MetadataIndexSize;
|
||||
readonly rva?: number;
|
||||
readonly flags?: number;
|
||||
readonly implFlags?: number;
|
||||
@@ -141,6 +226,9 @@ interface MethodDefRowInput {
|
||||
const methodDefRow = ({
|
||||
name,
|
||||
signature,
|
||||
stringSize,
|
||||
blobSize,
|
||||
parameterSize,
|
||||
rva = 0,
|
||||
flags = 0x0016,
|
||||
implFlags = 0,
|
||||
@@ -149,29 +237,56 @@ const methodDefRow = ({
|
||||
u32(rva),
|
||||
u16(implFlags),
|
||||
u16(flags),
|
||||
u16(name),
|
||||
u16(signature),
|
||||
u16(0),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(signature, blobSize),
|
||||
metadataIndex(0, parameterSize),
|
||||
]);
|
||||
|
||||
const memberRefRow = (name: number, signature: number): Buffer =>
|
||||
Buffer.concat([u16((1 << 3) | 1), u16(name), u16(signature)]);
|
||||
const memberRefRow = ({
|
||||
name,
|
||||
signature,
|
||||
stringSize,
|
||||
blobSize,
|
||||
parentSize,
|
||||
parentRaw = (1 << 3) | 1,
|
||||
}: {
|
||||
readonly name: number;
|
||||
readonly signature: number;
|
||||
readonly stringSize: MetadataIndexSize;
|
||||
readonly blobSize: MetadataIndexSize;
|
||||
readonly parentSize: MetadataIndexSize;
|
||||
readonly parentRaw?: number;
|
||||
}): Buffer =>
|
||||
Buffer.concat([
|
||||
metadataIndex(parentRaw, parentSize),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(signature, blobSize),
|
||||
]);
|
||||
|
||||
const moduleRefRow = (name: number): Buffer => Buffer.concat([u16(name)]);
|
||||
const moduleRefRow = (name: number, stringSize: MetadataIndexSize): Buffer =>
|
||||
Buffer.concat([metadataIndex(name, stringSize)]);
|
||||
|
||||
const implMapRow = (
|
||||
importName: number,
|
||||
importScope: number,
|
||||
mappingFlags: number,
|
||||
stringSize: MetadataIndexSize,
|
||||
memberForwardedSize: MetadataIndexSize,
|
||||
moduleRefSize: MetadataIndexSize,
|
||||
memberForwardedRaw: number,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
u16(mappingFlags),
|
||||
u16((1 << 1) | 1),
|
||||
u16(importName),
|
||||
u16(importScope),
|
||||
metadataIndex(memberForwardedRaw, memberForwardedSize),
|
||||
metadataIndex(importName, stringSize),
|
||||
metadataIndex(importScope, moduleRefSize),
|
||||
]);
|
||||
|
||||
const assemblyRow = (name: number): Buffer =>
|
||||
const assemblyRow = (
|
||||
name: number,
|
||||
stringSize: MetadataIndexSize,
|
||||
blobSize: MetadataIndexSize,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
u32(0x0000_8004),
|
||||
u16(1),
|
||||
@@ -179,29 +294,71 @@ const assemblyRow = (name: number): Buffer =>
|
||||
u16(3),
|
||||
u16(4),
|
||||
u32(0),
|
||||
u16(0),
|
||||
u16(name),
|
||||
u16(0),
|
||||
metadataIndex(0, blobSize),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(0, stringSize),
|
||||
]);
|
||||
|
||||
const assemblyRefRow = (name: number, keyOrToken: number): Buffer =>
|
||||
const assemblyRefRow = (
|
||||
name: number,
|
||||
keyOrToken: number,
|
||||
malformedName: boolean,
|
||||
stringSize: MetadataIndexSize,
|
||||
blobSize: MetadataIndexSize,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
u16(8),
|
||||
u16(0),
|
||||
u16(0),
|
||||
u16(0),
|
||||
u32(0),
|
||||
u16(keyOrToken),
|
||||
u16(name),
|
||||
u16(0),
|
||||
u16(0),
|
||||
metadataIndex(keyOrToken, blobSize),
|
||||
metadataIndex(
|
||||
malformedName ? (stringSize === 2 ? 0xffff : 0xffff_ffff) : name,
|
||||
stringSize,
|
||||
),
|
||||
metadataIndex(0, stringSize),
|
||||
metadataIndex(0, blobSize),
|
||||
]);
|
||||
|
||||
const customAttributeRow = (value: number): Buffer =>
|
||||
Buffer.concat([u16((1 << 5) | 14), u16((1 << 3) | 3), u16(value)]);
|
||||
const customAttributeRow = (
|
||||
value: number,
|
||||
options: ManagedPeFixtureOptions,
|
||||
parentIndexSize: 2 | 4,
|
||||
typeIndexSize: MetadataIndexSize,
|
||||
blobSize: MetadataIndexSize,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
parentIndexSize === 2
|
||||
? u16(
|
||||
options.malformedCustomAttributeRows?.includes(1)
|
||||
? 0xffff
|
||||
: (1 << 5) | 14,
|
||||
)
|
||||
: u32(
|
||||
options.malformedCustomAttributeRows?.includes(1)
|
||||
? 0xffff_ffff
|
||||
: (1 << 5) | 14,
|
||||
),
|
||||
metadataIndex(
|
||||
options.customAttributeTypeRaw ?? (1 << 3) | 3,
|
||||
typeIndexSize,
|
||||
),
|
||||
metadataIndex(value, blobSize),
|
||||
]);
|
||||
|
||||
const manifestResourceRow = (name: number): Buffer =>
|
||||
Buffer.concat([u32(0), u32(2), u16(name), u16(0)]);
|
||||
const manifestResourceRow = (
|
||||
name: number,
|
||||
stringSize: MetadataIndexSize,
|
||||
implementationSize: MetadataIndexSize,
|
||||
implementationRaw: number,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
u32(0),
|
||||
u32(2),
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(implementationRaw, implementationSize),
|
||||
]);
|
||||
|
||||
const fixedStringAttributeBlob = (value: string): Buffer => {
|
||||
const bytes = Buffer.from(textEncoder.encode(value));
|
||||
@@ -270,11 +427,13 @@ const metadataRoot = (
|
||||
const tablesStream = (
|
||||
rows: ReadonlyMap<number, readonly Buffer[]>,
|
||||
extraValidMask: bigint,
|
||||
heapSizes: number,
|
||||
): Buffer => {
|
||||
let valid = extraValidMask;
|
||||
for (const table of rows.keys()) valid |= 1n << BigInt(table);
|
||||
const header = Buffer.alloc(24);
|
||||
header.writeUInt8(2, 4);
|
||||
header.writeUInt8(heapSizes, 6);
|
||||
header.writeUInt8(1, 7);
|
||||
header.writeBigUInt64LE(valid, 8);
|
||||
const counts: Buffer[] = [];
|
||||
@@ -292,8 +451,269 @@ const tablesStream = (
|
||||
return Buffer.concat([header, ...counts, ...data]);
|
||||
};
|
||||
|
||||
interface FixtureMetadataRowContext {
|
||||
readonly options: ManagedPeFixtureOptions;
|
||||
readonly methodRva: number;
|
||||
readonly referenceStringIndexes: readonly number[];
|
||||
readonly indexes: {
|
||||
readonly moduleName: number;
|
||||
readonly assemblyName: number;
|
||||
readonly attributeName: number;
|
||||
readonly attributeNamespace: number;
|
||||
readonly constructorName: number;
|
||||
readonly typeName: number;
|
||||
readonly typeNamespace: number;
|
||||
readonly fieldName: number;
|
||||
readonly methodName: number;
|
||||
readonly pinvokeModuleName: number | null;
|
||||
readonly pinvokeImportName: number | null;
|
||||
readonly resourceName: number;
|
||||
readonly tokenBlob: number;
|
||||
readonly fieldSignature: number;
|
||||
readonly methodSignature: number;
|
||||
readonly constructorSignature: number;
|
||||
readonly attributeBlob: number;
|
||||
};
|
||||
readonly heapSizes: {
|
||||
readonly strings: MetadataIndexSize;
|
||||
readonly blobs: MetadataIndexSize;
|
||||
readonly guids: MetadataIndexSize;
|
||||
};
|
||||
}
|
||||
|
||||
interface FixtureIndexWidths {
|
||||
readonly table: (index: number) => MetadataIndexSize;
|
||||
readonly coded: (
|
||||
tables: readonly number[],
|
||||
tagBits: number,
|
||||
) => MetadataIndexSize;
|
||||
readonly resolutionScope: MetadataIndexSize;
|
||||
readonly typeDefOrRef: MetadataIndexSize;
|
||||
readonly memberRefParent: MetadataIndexSize;
|
||||
readonly hasCustomAttribute: MetadataIndexSize;
|
||||
readonly customAttributeType: MetadataIndexSize;
|
||||
readonly implementation: MetadataIndexSize;
|
||||
}
|
||||
|
||||
const repeatedRows = (
|
||||
count: number,
|
||||
createRow: () => Buffer,
|
||||
): readonly Buffer[] => Array.from({ length: count }, createRow);
|
||||
|
||||
const customAttributeRows = (
|
||||
{
|
||||
options,
|
||||
indexes,
|
||||
heapSizes,
|
||||
}: Pick<FixtureMetadataRowContext, "options" | "indexes" | "heapSizes">,
|
||||
widths: FixtureIndexWidths,
|
||||
): readonly Buffer[] =>
|
||||
options.targetFramework === null
|
||||
? []
|
||||
: [
|
||||
customAttributeRow(
|
||||
indexes.attributeBlob,
|
||||
options,
|
||||
widths.hasCustomAttribute,
|
||||
widths.customAttributeType,
|
||||
heapSizes.blobs,
|
||||
),
|
||||
];
|
||||
|
||||
const fixtureIndexWidths = ({
|
||||
options,
|
||||
referenceStringIndexes,
|
||||
indexes,
|
||||
}: FixtureMetadataRowContext): FixtureIndexWidths => {
|
||||
const rowCounts = new Map<number, number>([
|
||||
[0, options.moduleRowCount ?? 1],
|
||||
[1, 1],
|
||||
[2, 1],
|
||||
[4, 1],
|
||||
[6, 1],
|
||||
[8, 0],
|
||||
[10, 1],
|
||||
[12, options.targetFramework === null ? 0 : 1],
|
||||
...(indexes.pinvokeModuleName === null ? [] : ([[26, 1]] as const)),
|
||||
...(indexes.pinvokeImportName === null ? [] : ([[28, 1]] as const)),
|
||||
[32, options.assemblyRowCount ?? 1],
|
||||
[35, referenceStringIndexes.length],
|
||||
[40, 1],
|
||||
]);
|
||||
const count = (table: number): number => rowCounts.get(table) ?? 0;
|
||||
const table = (index: number): MetadataIndexSize => indexSize(count(index));
|
||||
const coded = (
|
||||
tables: readonly number[],
|
||||
tagBits: number,
|
||||
): MetadataIndexSize => codedIndexSize(tables.map(count), tagBits);
|
||||
return {
|
||||
table,
|
||||
coded,
|
||||
resolutionScope: coded([0, 26, 35, 1], 2),
|
||||
typeDefOrRef: coded([2, 1, 27], 2),
|
||||
memberRefParent: coded([2, 1, 26, 6, 27], 3),
|
||||
hasCustomAttribute: coded(
|
||||
[
|
||||
6, 4, 1, 2, 8, 9, 10, 0, 14, 23, 20, 17, 26, 27, 32, 35, 38, 39, 40, 42,
|
||||
44, 43,
|
||||
],
|
||||
5,
|
||||
),
|
||||
customAttributeType: coded([6, 10], 3),
|
||||
implementation: coded([38, 35, 39], 2),
|
||||
};
|
||||
};
|
||||
|
||||
const fixtureMetadataRows = ({
|
||||
options,
|
||||
methodRva,
|
||||
referenceStringIndexes,
|
||||
indexes,
|
||||
heapSizes,
|
||||
}: FixtureMetadataRowContext): ReadonlyMap<number, readonly Buffer[]> => {
|
||||
const widths = fixtureIndexWidths({
|
||||
options,
|
||||
methodRva,
|
||||
referenceStringIndexes,
|
||||
indexes,
|
||||
heapSizes,
|
||||
});
|
||||
const rows = new Map<number, readonly Buffer[]>([
|
||||
[
|
||||
0,
|
||||
repeatedRows(options.moduleRowCount ?? 1, () =>
|
||||
moduleRow(indexes.moduleName, heapSizes.strings, heapSizes.guids),
|
||||
),
|
||||
],
|
||||
[
|
||||
1,
|
||||
[
|
||||
typeRefRow(
|
||||
indexes.attributeName,
|
||||
indexes.attributeNamespace,
|
||||
heapSizes.strings,
|
||||
widths.resolutionScope,
|
||||
),
|
||||
],
|
||||
],
|
||||
[
|
||||
2,
|
||||
[
|
||||
typeDefRow({
|
||||
name: indexes.typeName,
|
||||
namespace: indexes.typeNamespace,
|
||||
fieldList: 1,
|
||||
methodList: 1,
|
||||
stringSize: heapSizes.strings,
|
||||
typeDefOrRefSize: widths.typeDefOrRef,
|
||||
fieldListSize: widths.table(4),
|
||||
methodListSize: widths.table(6),
|
||||
...(options.extendsRaw === undefined
|
||||
? {}
|
||||
: { extendsRaw: options.extendsRaw }),
|
||||
}),
|
||||
],
|
||||
],
|
||||
[
|
||||
4,
|
||||
[
|
||||
fieldRow(
|
||||
indexes.fieldName,
|
||||
indexes.fieldSignature,
|
||||
heapSizes.strings,
|
||||
heapSizes.blobs,
|
||||
),
|
||||
],
|
||||
],
|
||||
[
|
||||
6,
|
||||
[
|
||||
methodDefRow({
|
||||
name: indexes.methodName,
|
||||
signature: indexes.methodSignature,
|
||||
stringSize: heapSizes.strings,
|
||||
blobSize: heapSizes.blobs,
|
||||
parameterSize: widths.table(8),
|
||||
rva: methodRva,
|
||||
flags: options.pinvoke === undefined ? 0x0016 : 0x2016,
|
||||
}),
|
||||
],
|
||||
],
|
||||
[
|
||||
10,
|
||||
[
|
||||
memberRefRow({
|
||||
name: indexes.constructorName,
|
||||
signature: indexes.constructorSignature,
|
||||
stringSize: heapSizes.strings,
|
||||
blobSize: heapSizes.blobs,
|
||||
parentSize: widths.memberRefParent,
|
||||
...(options.memberRefParentRaw === undefined
|
||||
? {}
|
||||
: { parentRaw: options.memberRefParentRaw }),
|
||||
}),
|
||||
],
|
||||
],
|
||||
[12, customAttributeRows({ options, indexes, heapSizes }, widths)],
|
||||
...(indexes.pinvokeModuleName === null
|
||||
? []
|
||||
: ([
|
||||
[26, [moduleRefRow(indexes.pinvokeModuleName, heapSizes.strings)]],
|
||||
] as const)),
|
||||
...(indexes.pinvokeImportName === null
|
||||
? []
|
||||
: ([
|
||||
[
|
||||
28,
|
||||
[
|
||||
implMapRow(
|
||||
indexes.pinvokeImportName,
|
||||
1,
|
||||
options.pinvoke?.mappingFlags ?? 0x0344,
|
||||
heapSizes.strings,
|
||||
widths.coded([4, 6], 1),
|
||||
widths.table(26),
|
||||
options.pinvoke?.memberForwardedRaw ?? (1 << 1) | 1,
|
||||
),
|
||||
],
|
||||
],
|
||||
] as const)),
|
||||
[
|
||||
32,
|
||||
repeatedRows(options.assemblyRowCount ?? 1, () =>
|
||||
assemblyRow(indexes.assemblyName, heapSizes.strings, heapSizes.blobs),
|
||||
),
|
||||
],
|
||||
[
|
||||
35,
|
||||
referenceStringIndexes.map((name, index) =>
|
||||
assemblyRefRow(
|
||||
name,
|
||||
indexes.tokenBlob,
|
||||
options.malformedAssemblyReferenceRows?.includes(index + 1) ?? false,
|
||||
heapSizes.strings,
|
||||
heapSizes.blobs,
|
||||
),
|
||||
),
|
||||
],
|
||||
[
|
||||
40,
|
||||
[
|
||||
manifestResourceRow(
|
||||
indexes.resourceName,
|
||||
heapSizes.strings,
|
||||
widths.implementation,
|
||||
options.resourceImplementationRaw ?? 0,
|
||||
),
|
||||
],
|
||||
],
|
||||
]);
|
||||
return rows;
|
||||
};
|
||||
|
||||
const buildManagedFixtureMetadata = (
|
||||
options: ManagedPeFixtureOptions,
|
||||
methodRva: number,
|
||||
): Buffer => {
|
||||
const strings = new StringHeap();
|
||||
const blobs = new BlobHeap();
|
||||
@@ -329,50 +749,45 @@ const buildManagedFixtureMetadata = (
|
||||
const constructorSignature = blobs.add(Buffer.from([0x20, 0x01, 0x01, 0x0e]));
|
||||
const attributeBlob = blobs.add(
|
||||
fixedStringAttributeBlob(
|
||||
options.targetFramework ?? ".NETCoreApp,Version=v8.0",
|
||||
options.targetFramework === null
|
||||
? ".NETCoreApp,Version=v8.0"
|
||||
: (options.targetFramework ?? ".NETCoreApp,Version=v8.0"),
|
||||
),
|
||||
);
|
||||
const rows = new Map<number, readonly Buffer[]>([
|
||||
[0, [moduleRow(moduleName)]],
|
||||
[1, [typeRefRow(attributeName, attributeNamespace)]],
|
||||
[2, [typeDefRow(typeName, typeNamespace, 1, 1)]],
|
||||
[4, [fieldRow(fieldName, fieldSignature)]],
|
||||
[
|
||||
6,
|
||||
[
|
||||
methodDefRow({
|
||||
name: methodName,
|
||||
signature: methodSignature,
|
||||
rva: 0x2800,
|
||||
flags: options.pinvoke === undefined ? 0x0016 : 0x2016,
|
||||
}),
|
||||
],
|
||||
],
|
||||
[10, [memberRefRow(constructorName, constructorSignature)]],
|
||||
[12, [customAttributeRow(attributeBlob)]],
|
||||
...(pinvokeModuleName === null
|
||||
? []
|
||||
: ([[26, [moduleRefRow(pinvokeModuleName)]]] as const)),
|
||||
...(pinvokeImportName === null
|
||||
? []
|
||||
: ([
|
||||
[
|
||||
28,
|
||||
[
|
||||
implMapRow(
|
||||
pinvokeImportName,
|
||||
1,
|
||||
options.pinvoke?.mappingFlags ?? 0x0344,
|
||||
),
|
||||
],
|
||||
],
|
||||
] as const)),
|
||||
[32, [assemblyRow(assemblyName)]],
|
||||
[35, referenceStringIndexes.map((name) => assemblyRefRow(name, tokenBlob))],
|
||||
[40, [manifestResourceRow(resourceName)]],
|
||||
]);
|
||||
const stringSize = indexSize(strings.size);
|
||||
const blobSize = indexSize(blobs.size);
|
||||
const guidSize = indexSize(guidHeap(options.mvid).length);
|
||||
const rows = fixtureMetadataRows({
|
||||
options,
|
||||
methodRva,
|
||||
referenceStringIndexes,
|
||||
indexes: {
|
||||
moduleName,
|
||||
assemblyName,
|
||||
attributeName,
|
||||
attributeNamespace,
|
||||
constructorName,
|
||||
typeName,
|
||||
typeNamespace,
|
||||
fieldName,
|
||||
methodName,
|
||||
pinvokeModuleName,
|
||||
pinvokeImportName,
|
||||
resourceName,
|
||||
tokenBlob,
|
||||
fieldSignature,
|
||||
methodSignature,
|
||||
constructorSignature,
|
||||
attributeBlob,
|
||||
},
|
||||
heapSizes: { strings: stringSize, blobs: blobSize, guids: guidSize },
|
||||
});
|
||||
const heapFlags =
|
||||
(stringSize === 4 ? 0x01 : 0) |
|
||||
(guidSize === 4 ? 0x02 : 0) |
|
||||
(blobSize === 4 ? 0x04 : 0);
|
||||
const metadata = metadataRoot(
|
||||
tablesStream(rows, options.metadataValidMaskExtra ?? 0n),
|
||||
tablesStream(rows, options.metadataValidMaskExtra ?? 0n, heapFlags),
|
||||
strings.toBuffer(),
|
||||
guidHeap(options.mvid),
|
||||
blobs.toBuffer(),
|
||||
@@ -383,14 +798,56 @@ const buildManagedFixtureMetadata = (
|
||||
|
||||
const buildManagedFixtureImage = (
|
||||
options: ManagedPeFixtureOptions,
|
||||
metadata: Buffer,
|
||||
buildMetadata: (methodRva: number) => Buffer,
|
||||
): Buffer => {
|
||||
const resourceData = options.resourceData ?? Buffer.from("resource-data");
|
||||
const resourceDirectory = Buffer.concat([
|
||||
u32(resourceData.length),
|
||||
resourceData,
|
||||
]);
|
||||
const image = Buffer.alloc(0x1000);
|
||||
const initialMetadata = buildMetadata(0);
|
||||
const resourceOffset = Math.max(
|
||||
0x0800,
|
||||
Math.ceil((0x0300 + initialMetadata.length) / 0x0200) * 0x0200,
|
||||
);
|
||||
const resourceRva = 0x2000 + resourceOffset - 0x0200;
|
||||
const bodyOffset = Math.max(
|
||||
0x0a00,
|
||||
Math.ceil((resourceOffset + resourceDirectory.length) / 0x0200) * 0x0200,
|
||||
);
|
||||
const methodRva = 0x2000 + bodyOffset - 0x0200;
|
||||
const metadata = buildMetadata(methodRva);
|
||||
const body =
|
||||
options.ilBody ??
|
||||
Buffer.from([
|
||||
0x32, 0x02, 0x7b, 0x01, 0x00, 0x00, 0x04, 0x28, 0x01, 0x00, 0x00, 0x0a,
|
||||
0x2a,
|
||||
]);
|
||||
const legacyReadyToRunOffset = 0x0900;
|
||||
const metadataEnd = 0x0300 + metadata.length;
|
||||
const resourceEnd = resourceOffset + resourceDirectory.length;
|
||||
const bodyEnd = bodyOffset + body.length;
|
||||
const overlapsReadyToRun = (start: number, end: number): boolean =>
|
||||
legacyReadyToRunOffset < end && legacyReadyToRunOffset + 4 > start;
|
||||
const readyToRunOffset =
|
||||
options.readyToRun === true
|
||||
? overlapsReadyToRun(0x0300, metadataEnd) ||
|
||||
overlapsReadyToRun(resourceOffset, resourceEnd) ||
|
||||
overlapsReadyToRun(bodyOffset, bodyEnd)
|
||||
? Math.ceil(Math.max(metadataEnd, resourceEnd, bodyEnd) / 4) * 4
|
||||
: legacyReadyToRunOffset
|
||||
: null;
|
||||
const rawSectionSize =
|
||||
Math.ceil(
|
||||
Math.max(
|
||||
0x0e00,
|
||||
0x0100 + metadata.length,
|
||||
bodyOffset - 0x0200 + body.length,
|
||||
readyToRunOffset === null ? 0 : readyToRunOffset - 0x0200 + 4,
|
||||
) / 0x0200,
|
||||
) * 0x0200;
|
||||
const virtualSectionSize = Math.max(0x1000, rawSectionSize);
|
||||
const image = Buffer.alloc(0x0200 + rawSectionSize);
|
||||
image.write("MZ", 0, "ascii");
|
||||
image.writeUInt32LE(0x80, 0x3c);
|
||||
image.writeUInt32LE(0x0000_4550, 0x80);
|
||||
@@ -405,16 +862,19 @@ const buildManagedFixtureImage = (
|
||||
image.writeUInt32LE(0x0040_0000, optional + 28);
|
||||
image.writeUInt32LE(0x1000, optional + 32);
|
||||
image.writeUInt32LE(0x200, optional + 36);
|
||||
image.writeUInt32LE(0x3000, optional + 56);
|
||||
image.writeUInt32LE(
|
||||
Math.ceil((0x2000 + virtualSectionSize) / 0x1000) * 0x1000,
|
||||
optional + 56,
|
||||
);
|
||||
image.writeUInt32LE(0x200, optional + 60);
|
||||
image.writeUInt32LE(16, optional + 92);
|
||||
image.writeUInt32LE(0x2000, optional + 96 + 14 * 8);
|
||||
image.writeUInt32LE(72, optional + 96 + 14 * 8 + 4);
|
||||
const section = optional + 0x00e0;
|
||||
image.write(".text\0\0\0", section, "ascii");
|
||||
image.writeUInt32LE(0x1000, section + 8);
|
||||
image.writeUInt32LE(virtualSectionSize, section + 8);
|
||||
image.writeUInt32LE(0x2000, section + 12);
|
||||
image.writeUInt32LE(0x0e00, section + 16);
|
||||
image.writeUInt32LE(rawSectionSize, section + 16);
|
||||
image.writeUInt32LE(0x0200, section + 20);
|
||||
image.writeUInt32LE(0x6000_0020, section + 36);
|
||||
const cli = 0x0200;
|
||||
@@ -425,22 +885,16 @@ const buildManagedFixtureImage = (
|
||||
image.writeUInt32LE(metadata.length, cli + 12);
|
||||
image.writeUInt32LE(options.cliFlags ?? 1, cli + 16);
|
||||
image.writeUInt32LE(0x0600_0001, cli + 20);
|
||||
image.writeUInt32LE(0x2600, cli + 24);
|
||||
image.writeUInt32LE(resourceRva, cli + 24);
|
||||
image.writeUInt32LE(resourceDirectory.length, cli + 28);
|
||||
if (options.readyToRun === true) {
|
||||
image.writeUInt32LE(0x2700, cli + 64);
|
||||
if (readyToRunOffset !== null) {
|
||||
image.writeUInt32LE(0x2000 + readyToRunOffset - 0x0200, cli + 64);
|
||||
image.writeUInt32LE(4, cli + 68);
|
||||
image.write("RTR\0", 0x0900, "ascii");
|
||||
image.write("RTR\0", readyToRunOffset, "ascii");
|
||||
}
|
||||
(
|
||||
options.ilBody ??
|
||||
Buffer.from([
|
||||
0x32, 0x02, 0x7b, 0x01, 0x00, 0x00, 0x04, 0x28, 0x01, 0x00, 0x00, 0x0a,
|
||||
0x2a,
|
||||
])
|
||||
).copy(image, 0x0a00);
|
||||
body.copy(image, bodyOffset);
|
||||
metadata.copy(image, 0x0300);
|
||||
resourceDirectory.copy(image, 0x0800);
|
||||
resourceDirectory.copy(image, resourceOffset);
|
||||
return image;
|
||||
};
|
||||
|
||||
@@ -448,7 +902,9 @@ const buildManagedFixtureImage = (
|
||||
export const buildManagedPeFixture = (
|
||||
options: ManagedPeFixtureOptions = {},
|
||||
): Buffer =>
|
||||
buildManagedFixtureImage(options, buildManagedFixtureMetadata(options));
|
||||
buildManagedFixtureImage(options, (methodRva) =>
|
||||
buildManagedFixtureMetadata(options, methodRva),
|
||||
);
|
||||
|
||||
/** Build a syntactically valid PE32 fixture with no CLI directory. */
|
||||
export const buildNativePeFixture = (): Buffer => {
|
||||
|
||||
Reference in New Issue
Block a user