mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
refactor(nativeaot): share mapped PE scalar reads (#12610)
refactor(nativeaot): share mapped scalar reads
This commit is contained in:
1 parent
b8e65d24af
commit
c98b956da1
2 files changed
+39
-41
No files matched your search
@@ -5,7 +5,7 @@
|
||||
"src/domain/desktop/openPgpKeys.ts": 2,
|
||||
"src/domain/managed/managedMetadataInventoryRows.ts": 2,
|
||||
"src/domain/native/elfPointerRelocations.ts": 6,
|
||||
"src/domain/native/nativeAotPe.ts": 16,
|
||||
"src/domain/native/nativeAotPe.ts": 10,
|
||||
"src/domain/native/readBinaryContainer.ts": 1,
|
||||
"src/domain/pngVisualDiff.ts": 2,
|
||||
"src/ghidra/extensions/GhidraExtensions.ts": 1,
|
||||
|
||||
@@ -236,10 +236,10 @@ type MethodTable = {
|
||||
};
|
||||
|
||||
const METHOD_TABLE_HEADER_BYTES = 24;
|
||||
const METHOD_TABLE_POINTER_BYTES = 8;
|
||||
const NATIVE_AOT_POINTER_BYTES = 8;
|
||||
const METHOD_TABLE_FORMAT = "NativeAOT MethodTable";
|
||||
const methodTableSlotOffset = (slot: number): number =>
|
||||
METHOD_TABLE_HEADER_BYTES + slot * METHOD_TABLE_POINTER_BYTES;
|
||||
METHOD_TABLE_HEADER_BYTES + slot * NATIVE_AOT_POINTER_BYTES;
|
||||
const methodTableInterfaceOffset = (
|
||||
slotsCount: number,
|
||||
index: number,
|
||||
@@ -252,15 +252,15 @@ const methodTablePointerAddress = (
|
||||
table: MethodTable,
|
||||
offset: number,
|
||||
): bigint => table.address + BigInt(offset);
|
||||
const readMethodTablePointer = (
|
||||
const readMappedPointer = (
|
||||
image: PeImage,
|
||||
address: bigint,
|
||||
budget: NativeAotParserBudget,
|
||||
): bigint | null => {
|
||||
const bytes = image.read(address, METHOD_TABLE_POINTER_BYTES, budget);
|
||||
const bytes = image.read(address, NATIVE_AOT_POINTER_BYTES, budget);
|
||||
return bytes === null
|
||||
? null
|
||||
: new BinaryReader(bytes, true, METHOD_TABLE_FORMAT).word(0, 64);
|
||||
: new BinaryReader(bytes, true, NATIVE_AOT_FORMAT).word(0, 64);
|
||||
};
|
||||
|
||||
const METHOD_TABLE_RECORD_BYTES = 32;
|
||||
@@ -366,9 +366,8 @@ class MethodTableIndex {
|
||||
include(child);
|
||||
}
|
||||
for (let index = 0; index < base.interfacesCount; index += 1) {
|
||||
if (!this.budget.consumeWork(METHOD_TABLE_POINTER_BYTES))
|
||||
return false;
|
||||
const address = readMethodTablePointer(
|
||||
if (!this.budget.consumeWork(NATIVE_AOT_POINTER_BYTES)) return false;
|
||||
const address = readMappedPointer(
|
||||
image,
|
||||
methodTablePointerAddress(
|
||||
base,
|
||||
@@ -852,14 +851,6 @@ class PeImage {
|
||||
}
|
||||
|
||||
const hex = (value: bigint): string => `0x${value.toString(16)}`;
|
||||
const readU64 = (bytes: Buffer | null, offset = 0): bigint | null =>
|
||||
bytes === null || offset > bytes.length - 8
|
||||
? null
|
||||
: bytes.readBigUInt64LE(offset);
|
||||
const readU32 = (bytes: Buffer | null, offset = 0): number | null =>
|
||||
bytes === null || offset > bytes.length - 4
|
||||
? null
|
||||
: bytes.readUInt32LE(offset);
|
||||
const notApplicable = (
|
||||
sha: string,
|
||||
reason: NativeAotReason,
|
||||
@@ -1061,13 +1052,13 @@ const parseMethodTable = (
|
||||
if (bytes === null) return null;
|
||||
const pointers = new BinaryReader(bytes, true, METHOD_TABLE_FORMAT);
|
||||
for (let slot = 0; slot < slotsCount; slot++) {
|
||||
if (!budget.consumeWork(METHOD_TABLE_POINTER_BYTES)) return null;
|
||||
if (!budget.consumeWork(NATIVE_AOT_POINTER_BYTES)) return null;
|
||||
validation.itemsExamined++;
|
||||
const target = pointers.word(methodTableSlotOffset(slot), 64);
|
||||
if (target !== 0n && !image.isExecutable(target, budget)) return null;
|
||||
}
|
||||
for (let index = 0; index < interfacesCount; index++) {
|
||||
if (!budget.consumeWork(METHOD_TABLE_POINTER_BYTES)) return null;
|
||||
if (!budget.consumeWork(NATIVE_AOT_POINTER_BYTES)) return null;
|
||||
validation.itemsExamined++;
|
||||
const iface = pointers.word(
|
||||
methodTableInterfaceOffset(slotsCount, index),
|
||||
@@ -1084,14 +1075,19 @@ const readRelativePointer = (
|
||||
budget: NativeAotParserBudget,
|
||||
): bigint | null => {
|
||||
const raw = image.read(address, 4, budget);
|
||||
return raw === null ? null : address + BigInt(raw.readInt32LE(0));
|
||||
return raw === null
|
||||
? null
|
||||
: address + BigInt(new BinaryReader(raw, true, NATIVE_AOT_FORMAT).i32(0));
|
||||
};
|
||||
|
||||
/** Exact UTF-8 length of JSON.stringify for a UTF-16 string without allocating it. */
|
||||
const jsonUtf16StringBytes = (bytes: Buffer, codeUnits: number): number => {
|
||||
const jsonUtf16StringBytes = (
|
||||
fields: BinaryReader,
|
||||
codeUnits: number,
|
||||
): number => {
|
||||
let total = 2;
|
||||
for (let index = 0; index < codeUnits; index++) {
|
||||
const value = bytes.readUInt16LE(index * 2);
|
||||
const value = fields.u16(index * 2);
|
||||
if (value === 0x22 || value === 0x5c) total += 2;
|
||||
else if (value < 0x20)
|
||||
total +=
|
||||
@@ -1103,8 +1099,7 @@ const jsonUtf16StringBytes = (bytes: Buffer, codeUnits: number): number => {
|
||||
? 2
|
||||
: 6;
|
||||
else if (value >= 0xd800 && value <= 0xdbff) {
|
||||
const next =
|
||||
index + 1 < codeUnits ? bytes.readUInt16LE((index + 1) * 2) : 0;
|
||||
const next = index + 1 < codeUnits ? fields.u16((index + 1) * 2) : 0;
|
||||
if (next >= 0xdc00 && next <= 0xdfff) {
|
||||
total += 4;
|
||||
index++;
|
||||
@@ -1427,7 +1422,7 @@ const discoverTables = (
|
||||
truncated = true;
|
||||
break scan;
|
||||
}
|
||||
const value = readU64(image.read(address, 8, budget));
|
||||
const value = readMappedPointer(image, address, budget);
|
||||
if (budget.truncationReason !== undefined) {
|
||||
truncated = true;
|
||||
break scan;
|
||||
@@ -1466,7 +1461,7 @@ const discoverTables = (
|
||||
return;
|
||||
// The root heuristic samples its three declared slots and one following
|
||||
// word. That extra word is outside the declared slot/interface arrays.
|
||||
const sampleBytes = (candidate.slotsCount + 1) * METHOD_TABLE_POINTER_BYTES;
|
||||
const sampleBytes = (candidate.slotsCount + 1) * NATIVE_AOT_POINTER_BYTES;
|
||||
if (!budget.consumeWork(sampleBytes)) {
|
||||
truncated = true;
|
||||
return;
|
||||
@@ -1480,15 +1475,12 @@ const discoverTables = (
|
||||
const sample = new BinaryReader(vtable, true, METHOD_TABLE_FORMAT);
|
||||
if (
|
||||
image.isExecutable(sample.word(0, 64), budget) &&
|
||||
image.isExecutable(sample.word(METHOD_TABLE_POINTER_BYTES, 64), budget) &&
|
||||
image.isExecutable(sample.word(NATIVE_AOT_POINTER_BYTES, 64), budget) &&
|
||||
image.isExecutable(
|
||||
sample.word(2 * METHOD_TABLE_POINTER_BYTES, 64),
|
||||
sample.word(2 * NATIVE_AOT_POINTER_BYTES, 64),
|
||||
budget,
|
||||
) &&
|
||||
!image.isExecutable(
|
||||
sample.word(3 * METHOD_TABLE_POINTER_BYTES, 64),
|
||||
budget,
|
||||
)
|
||||
!image.isExecutable(sample.word(3 * NATIVE_AOT_POINTER_BYTES, 64), budget)
|
||||
) {
|
||||
objectRoot = index;
|
||||
objectCount++;
|
||||
@@ -1840,7 +1832,7 @@ export const parseNativeAotPe = (
|
||||
frozenScanTruncated = true;
|
||||
break;
|
||||
}
|
||||
const table = readU64(image.read(address, 8, budget));
|
||||
const table = readMappedPointer(image, address, budget);
|
||||
if (budget.truncationReason !== undefined) {
|
||||
frozenScanTruncated = true;
|
||||
break;
|
||||
@@ -1856,11 +1848,15 @@ export const parseNativeAotPe = (
|
||||
continue;
|
||||
}
|
||||
frozenCandidates++;
|
||||
const length = readU32(image.read(address + 8n, 4, budget));
|
||||
const rawLength = image.read(address + 8n, 4, budget);
|
||||
if (budget.truncationReason !== undefined) {
|
||||
frozenScanTruncated = true;
|
||||
break;
|
||||
}
|
||||
const length =
|
||||
rawLength === null
|
||||
? null
|
||||
: new BinaryReader(rawLength, true, NATIVE_AOT_FORMAT).u32(0);
|
||||
if (length === null || address + 14n + BigInt(length) * 2n > frozen.end) {
|
||||
address += 8n;
|
||||
continue;
|
||||
@@ -1880,7 +1876,9 @@ export const parseNativeAotPe = (
|
||||
break;
|
||||
}
|
||||
frozenStringDataBytesExamined += stringBytes;
|
||||
if (raw === null || raw.readUInt16LE(length * 2) !== 0) {
|
||||
const fields =
|
||||
raw === null ? null : new BinaryReader(raw, true, NATIVE_AOT_FORMAT);
|
||||
if (fields === null || fields.u16(length * 2) !== 0) {
|
||||
address += 8n;
|
||||
continue;
|
||||
}
|
||||
@@ -1894,7 +1892,7 @@ export const parseNativeAotPe = (
|
||||
const rowBytes =
|
||||
rowPrefixBytes -
|
||||
2 +
|
||||
jsonUtf16StringBytes(raw, length) +
|
||||
jsonUtf16StringBytes(fields, length) +
|
||||
(frozenStrings.length > 0 ? 1 : 0);
|
||||
if (rowBytes > remainingSummaryBytes) {
|
||||
frozenStringsOmitted++;
|
||||
@@ -1903,7 +1901,7 @@ export const parseNativeAotPe = (
|
||||
}
|
||||
const row: NativeAotString = {
|
||||
address: hex(address),
|
||||
value: raw.toString("utf16le", 0, length * 2),
|
||||
value: fields.bytes.toString("utf16le", 0, length * 2),
|
||||
};
|
||||
frozenStrings.push(row);
|
||||
remainingSummaryBytes -= rowBytes;
|
||||
@@ -2046,7 +2044,7 @@ export const parseNativeAotPe = (
|
||||
interfacesOmitted += table.interfacesCount - index;
|
||||
break;
|
||||
}
|
||||
if (!detailBudget.consumeWork(METHOD_TABLE_POINTER_BYTES)) {
|
||||
if (!detailBudget.consumeWork(NATIVE_AOT_POINTER_BYTES)) {
|
||||
interfacesOmitted += table.interfacesCount - index;
|
||||
break;
|
||||
}
|
||||
@@ -2054,7 +2052,7 @@ export const parseNativeAotPe = (
|
||||
address: hex(table.address),
|
||||
type: null,
|
||||
};
|
||||
const value = readMethodTablePointer(
|
||||
const value = readMappedPointer(
|
||||
image,
|
||||
methodTablePointerAddress(
|
||||
table,
|
||||
@@ -2092,7 +2090,7 @@ export const parseNativeAotPe = (
|
||||
slotsOmitted += table.slotsCount - slot;
|
||||
break;
|
||||
}
|
||||
if (!detailBudget.consumeWork(METHOD_TABLE_POINTER_BYTES)) {
|
||||
if (!detailBudget.consumeWork(NATIVE_AOT_POINTER_BYTES)) {
|
||||
slotsOmitted += table.slotsCount - slot;
|
||||
break;
|
||||
}
|
||||
@@ -2100,7 +2098,7 @@ export const parseNativeAotPe = (
|
||||
table,
|
||||
methodTableSlotOffset(slot),
|
||||
);
|
||||
const value = readMethodTablePointer(image, slotAddress, detailBudget);
|
||||
const value = readMappedPointer(image, slotAddress, detailBudget);
|
||||
if (value === null) {
|
||||
slotsOmitted += table.slotsCount - slot;
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user