Files
reverse-engineering-anything/src/gpu/CudaInfo.ts
T
morluto 264dcd5fee refactor(artifacts): break the artifacts-firmware and artifacts-gpu import cycles (#11331)
* refactor(domain): move BinaryContainer into src/domain

BinaryContainer imports only domain modules and is shared by artifacts, firmware, gpu, go, il2cpp, python, rust and native. Living under src/native made artifacts and firmware import native for its failure types and BinaryReader. Pure move: git mv plus import specifier updates.

* refactor(firmware): move the devicetree parser into src/domain/firmware

The FIT artifact reader in src/artifacts parses embedded devicetree blobs with the parser that lived under src/firmware, while firmware's providers import src/artifacts: the two units imported each other. The parser and its fixture are pure and now sit beside domain/firmware/devicetree.ts. Pure move: git mv plus import specifier and baseline key updates.

* refactor(artifacts): break the artifacts-gpu import cycle (#11335)

* refactor(artifacts): move the LZ4 block decoder into src/artifacts

Pure move: git mv plus import specifier updates. The next commit removes its GPU-specific budget.

* refactor(artifacts): take the LZ4 output budget from the caller

decodeLz4Block bounded every decode by the GPU object budget and threw through rejectGpu, so UnityFS decoding in src/artifacts imported src/gpu while gpu imports artifacts. Callers now pass their budget: GPU payloads keep GPU_OBJECT_BYTES; UnityFS passes its 64 MiB block bound, which its size checks already enforce before decoding. Malformed blocks still throw BinaryFormatFailure with the same messages.

* test(architecture): drop the artifacts-firmware and artifacts-gpu cycles from the layer baseline

* refactor(artifacts): move the PE resource reader into src/artifacts/pe (#11347)

refactor(artifacts): move the PE resource layout and parser into src/artifacts/pe

The Node SEA and Deno standalone readers in src/artifacts locate PE resources with the layout and parser that lived under src/native/pe, while those modules already read through src/artifacts (ArtifactReaderFailure, assertArtifactReadActive). Pure move: git mv plus import specifier, baseline key and spec path updates.
2026-11-16 03:44:53 +08:00

154 lines
4.6 KiB
TypeScript

import {
BinaryReader,
BinaryResourceFailure,
} from "../domain/binaryContainer.js";
import { GPU_OBJECT_BYTES, GPU_REPORT_BYTES, rejectGpu } from "./GpuSource.js";
import { Budget } from "../domain/budget.js";
import { jsonValueByteLength } from "../domain/jsonValueByteLength.js";
import type { JsonValue } from "../domain/jsonValue.js";
/** Shared by every metadata record, symbol and temporary index in one CUDA ELF. */
export class CudaMetadataBudget {
readonly #visits = new Budget(262144);
readonly #readBytes = new Budget(GPU_OBJECT_BYTES);
readonly #indexBytes = new Budget(GPU_REPORT_BYTES);
visit(): void {
this.#visits.charge(
1,
() =>
new BinaryResourceFailure(
"structure",
262144,
"CUDA metadata exceeds 262144 aggregate record/symbol visits",
),
);
}
read(size: number): void {
this.#readBytes.charge(
size,
() =>
new BinaryResourceFailure(
"structure",
GPU_OBJECT_BYTES,
"CUDA metadata exceeds 128 MiB aggregate section reads",
),
);
}
retain(value: JsonValue): void {
this.#indexBytes.charge(
jsonValueByteLength(value, GPU_REPORT_BYTES) + 2,
() =>
new BinaryResourceFailure(
"structure",
GPU_REPORT_BYTES,
"CUDA metadata exceeds the 16 MiB temporary index budget",
),
);
}
}
interface CudaInfoRecord {
readonly format: number;
readonly attribute: number;
readonly value: number;
readonly offset: number;
readonly length: number;
readonly data: Buffer;
}
/** CUDA nv.info is a numeric record format, not a string-keyed metadata map. */
export function* readCudaInfo(
bytes: Buffer,
little: boolean,
budget = new CudaMetadataBudget(),
): Generator<CudaInfoRecord> {
const reader = new BinaryReader(bytes, little, "cuda-nv-info");
for (let offset = 0; offset < bytes.length;) {
budget.visit();
const head = reader.range(offset, 4, "CUDA nv.info record");
const format = head[0]!,
attribute = head[1]!,
value = reader.u16(offset + 2);
if (format < 1 || format > 4)
rejectGpu("Unknown CUDA nv.info record format");
const length = format === 4 ? value : 0;
yield {
format,
attribute,
value,
offset,
length: 4 + length,
data: reader.range(offset + 4, length, "CUDA nv.info value"),
};
offset += 4 + length;
}
}
/** Kernel argument interpretation owns duplicate and range semantics at the numeric-record boundary. */
export const readCudaParameters = (
bytes: Buffer,
little: boolean,
budget = new CudaMetadataBudget(),
) => {
const parameters: import("../domain/gpu/gpuCodeObjects.js").GpuKernel["parameters"] =
[];
const byteDeclarations = new Set<number>();
const limitations: string[] = [];
let complete = true;
for (const record of readCudaInfo(bytes, little, budget)) {
if (record.format === 3 && record.attribute === 0x19)
byteDeclarations.add(record.value);
if (
record.format !== 4 ||
(record.attribute !== 0x17 && record.attribute !== 0x45)
)
continue;
if (record.data.length !== 12) {
complete = false;
continue;
}
const r = new BinaryReader(record.data, little, "cuda-nv-info");
if (r.u32(0) !== 0) complete = false;
const parameter = {
ordinal: r.u16(4),
name: null,
offset: r.u16(6),
size: record.attribute === 0x45 ? r.u32(8) : r.u32(8) >>> 18,
type: null,
hidden: null,
};
budget.retain(parameter);
parameters.push(parameter);
}
parameters.sort((a, b) => a.ordinal - b.ordinal);
if (parameters.some((row, index) => row.ordinal !== index)) complete = false;
const parameter_bytes =
byteDeclarations.size === 1 ? [...byteDeclarations][0]! : null;
if (byteDeclarations.size > 1)
limitations.push(
`Conflicting CUDA argument-area byte declarations: ${[...byteDeclarations].join(", ")}; parameter_bytes remains unknown`,
);
if (
parameter_bytes !== null &&
parameters.some(
(row) =>
row.offset! > parameter_bytes ||
row.size! > parameter_bytes - row.offset!,
)
)
limitations.push(
"CUDA parameter ranges disagree with the declared argument-area length; declarations do not establish a valid ABI layout",
);
if (!complete)
limitations.push(
"CUDA parameter records have unsupported indices/sizes, duplicate ordinals or gaps; parameter count is unknown",
);
return {
parameter_count:
complete && (parameters.length > 0 || parameter_bytes === 0)
? parameters.length
: null,
parameter_bytes,
parameters,
limitations,
};
};