Files
reverse-engineering-anything/tests/boundary/gpu/codeObjects.test.ts
T
38bf511a50 fix(errors)!: report unsupported and unrecognized targets on the TargetFailure model (#14281)
* feat(errors)!: add the TargetFailure model, its projection table and conformance registry

Introduce one domain-owned TargetFailure value whose kind alone derives
the public code, category, retryability, recovery and details, for CLI
and MCP alike. Selected-file malformed and truncated refusals are the
first area on it: AnalysisMalformedTargetError is replaced by
AnalysisTargetFailureError, and ParseFailure's byte-structure variants
are TargetFailure variants rather than a parallel type.

The shared stableArtifactParserFailureCli loop is replaced by a
declarative registry (tests/support/targetFailure/readers/*.ts) and one
generated suite that runs every registered case through the CLI and MCP.

BREAKING CHANGE: selected-file malformed and truncated refusals add
details.failure_kind, and a truncated target's remediation now says to
re-acquire the complete file instead of choosing another target. Codes
are unchanged. Affected operations: every operation that refuses a
selected file's malformed or truncated bytes, including the ELF, PE,
Mach-O, DWARF, Go, Rust, BEAM, pyc, devicetree, Apple IPS, kernel
module, binary hardening, managed PE/CLI and IL2CPP readers.

* test(managed): name the target failure error in the merged status test

* docs(reference): name the error CSV instead of linking a non-page file

* fix(tests): expect the path reason for non-regular dyld inputs

#14225 refuses a non-regular dyld input as ArtifactReaderFailure("path");
dylibFileLifetime still expected "format" and fails on main's shard 9.

* fix(tests): expect io, invariant and path where #14225 reclassified

stableArtifact expected "format" for an impossible descriptor byte count
(now io) and an invalid chunk size (now InternalInvariantError);
directoryArtifactReaderOpen expected "format" for a non-file entry (now
path, the same change as #14238). All three fail on main.

* fix(errors)!: raise container reader byte faults as malformed or truncated targets

Delete the ambiguous `format` reason from ArtifactReaderFailure and
ArtifactOperationError. Each container reader now raises its owner's
fact once, through malformedArtifact / truncatedArtifact, with format,
offset, field and measured extents. Short reads inside a measured extent
are read-coverage integrity failures, path-kind refusals are `path`,
foreign or recovery-requiring inputs are `unsupported`, host tool output
is `io`, and REA defects are InternalInvariantError.

The trace, NTFS journal, release exposure, web capture, native macOS and
web artifact adapters project the reader's parsed facts as
AnalysisTargetFailureError instead of invalid_request. Inventory
malformed, truncated, unsupported and cancelled reasons take their code
from the TargetFailure projection table, so a truncated container is
unsupported_target with failure_kind "truncated" on both paths.

BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact,
analyze_javascript_application, inspect_desktop_application,
inspect_keyed_archive, inspect_asset_catalog, inspect_nuget_nupkg,
trace_dylib_resolution, trace_web_module_imports and
trace_web_source_location report damaged container bytes as
unsupported_target (details.reason "malformed" or "truncated", with
failure_kind and format) instead of artifact_operation_failed with
reason "format", and a truncated container as unsupported_target instead
of truncated. inspect_playwright_trace, inspect_ntfs_journal,
audit_release_exposure, the web capture inspection operations and the
caller-selected import and source maps report damaged selected bytes as
unsupported_target instead of invalid_request. The `format` reason no
longer appears in details.reason.

* test(artifacts): expect target failure codes in compiled CLI and MCP suites

* fix(errors)!: refuse malformed SQLite, pwntools and EVM inputs as target failures

The SQLite engine's `format` reason is renamed to `malformed` at its
owner (SQLITE_INSPECTION_REASONS), and its refusal becomes an
AnalysisTargetFailureError at the selected path with format "sqlite".
The pwntools and EVM worker replies keep their wire reason "format",
which their workers define, and the adapters translate it once into a
malformed target failure with the profile's format (elf, elf-core,
evm-bytecode) instead of an invalid_format argument issue. SQLite joins
the target-failure conformance registry.

BREAKING CHANGE: inspect_sqlite_database, inspect_binary_layout,
inspect_recorded_crash and inspect_evm_interface report a selected
file's malformed bytes as unsupported_target with details.failure_kind
"malformed", format_failure, format and path, instead of invalid_request
with an invalid_format issue on path.

* fix(errors): give the integrity kind a schema-valid code and list the reference page

artifact_integrity_mismatch's projection schema requires declared and
calculated digests, which a TargetFailure of kind integrity does not
carry, so the kind now projects artifact_operation_failed with category
integrity_mismatch. The model test validates every kind's projection
against analysisErrorProjectionSchema. The reference page joins the
documentation sidebar.

* fix(artifacts): report a short carrier read as a changed file and keep unsupported desktop probes local

A Bun or Node SEA short read inside a range already checked against the
measured size meant the file changed, not that it was truncated. The
carrier range reader and Bun's readExact now raise read-coverage
integrity for it, and keep 'outside the file' as the truncated case.
DesktopFileInspection records an unsupported managed-metadata probe as a
row issue again, so later file checks still run.

* test(errors): expect target failure facts in the pwntools and EVM reply suites

The literal-replacement scenario now reads the failure's detail instead
of an issues[] message, and a malformed reply wrapped by cleanup failure
reports category unsupported_target.

* test(errors): register PE DOS-signature and truncated IPS refusals and drop their per-command copies

The non-MZ PE imports and load-config cases (PE32/PE32+, ZZ and high-bit
signatures) and the truncated Apple IPS body move into the conformance
registry, which runs them through the CLI and MCP and now also checks
that a refusal leaves the selected file unchanged. The per-command tests
keep only their valid-image controls.

* test(sqlite): drop the malformed-database provider test the conformance registry covers

inspect_sqlite_database/not-a-database in the target-failure registry runs
the same non-SQLite bytes through the CLI and MCP, asserts the malformed
target facts and checks the selected file is unchanged.

* feat(errors)!: make failure_kind the identifier and derive code and retry from it

Apply the owner review on #14239 to the TargetFailure model:
- remove details.format_failure; failure_kind is the only identifier;
- give integrity, conflict and io their own codes
  (target_integrity_mismatch, input_conflict, host_io_failed) and derive
  inventory malformed, truncated and unsupported codes from the same table;
- add a retry hint (none, backoff, after_change) derived from the kind,
  with io refined by its transient errno fact; retryable stays the
  unchanged-retry flag and is true exactly for backoff;
- rename the limit-cut result code and category truncated to partial;
- drop cancelled and resource from the target kinds and spell
  identifiers in American English (unrecognized);
- return a target failure a tool handler throws as a projected isError
  result instead of letting the SDK flatten it to text.

BREAKING CHANGE: details.format_failure is removed (read
details.failure_kind). The truncated result code and category are now
partial. Target integrity failures report target_integrity_mismatch,
host read failures host_io_failed and disagreeing inputs input_conflict.
Inventory operations (inventory_artifact, inspect_artifact,
extract_artifact and the other inventory callers) report truncated
container bytes as unsupported_target. Every target failure carries a
top-level retry hint.

* test(sqlite): expect a malformed target for non-database bytes in the CLI suite

* fix(artifacts): keep changed-file short reads, invalid offsets and EOF declarations apart

PyInstaller reads inside the measured size, and .NET bundle manifest
refills, now raise read-coverage integrity on a zero-byte read instead of
truncated. A .NET bundle field and a Godot PCK directory entry that extend
past the file are truncated (with offset and extents) instead of
malformed. An absent or invalid ASAR packed offset is malformed; only a
valid range past the archive is truncated. The .NET manifest string
offset is reported in file coordinates.

* fix(pwntools): report a short ELF header as a truncated target

The layout and recorded-crash workers raise a distinct truncated reason
when the file ends inside the ELF identification or header, and the
decoder projects it as failure_kind truncated instead of inferring it
from the message.

* fix(errors)!: report managed coverage gaps and IL2CPP pair conflicts as target failures

Managed readers interpret an issue code once, in managedIssueTargetFailure:
a coverage gap is an unsupported target, every other rule violation is a
malformed target, and the inventory metadata status is derived from that
same fact. managedInspectionTargetFailure now returns only
AnalysisTargetFailureError.

An IL2CPP binary that parses but does not link to its metadata is a
conflict target failure that lists both inputs and blames neither, in
place of an invalid_request with two issue entries. Metadata version 108
is an unsupported target. The CLI spells each conflict input's field by
the command's option name.

BREAKING CHANGE: inspect_managed_artifact, inspect_managed_members,
inspect_managed_native_boundaries and the managed comparison operations
report unsupported metadata or signature forms as an
AnalysisTargetFailureError (failure_kind unsupported, format pe-cli,
field, offset) instead of a bare unsupported target. inspect_il2cpp_metadata
reports a binary/metadata pair that does not link as code input_conflict
with failure_kind conflict and details.inputs, instead of invalid_request
with details.issues, and metadata version 108 with failure_kind
unsupported.

* fix(artifacts): verify the carrier before blaming a short read on the bytes

The NTFS $MFT decoder, positional members (FIT, uImage, Bun, Deno) and
.NET bundle members now re-verify the source before reporting a short
stream as malformed, so a file that changed during the read reports the
change. The truncation site builds the model's measured extent.

* fix(errors)!: report inventory refusals as target failures of the selected path

An inventory, inspection or extraction refused damaged or unsupported
container bytes as an ArtifactOperationError whose reason repeated the
format owner's kind. It carried no path or retry hint, so the conformance
registry could not express container cases.

translateArtifactFailure now takes the selected path and returns the
reader's TargetFailure as an AnalysisTargetFailureError. The malformed,
truncated and unsupported reasons, their classification rows and their
presentation branch are deleted from ArtifactOperationError. Readers'
unsupported refusals carry their format facts through unsupportedArtifact.

Container cases move into the conformance registry, which now opens the
target first for session tools such as inspect_artifact.

BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact and
the bundle readers report a refused container as a target failure of
details.path with a top-level retry. details.reason is the owner's
description and details.detail is removed.

* docs(il2cpp): describe pair conflicts as input_conflict with both inputs

* docs(android): name unsupported_target for truncated boot image structures

* fix(errors)!: report a target that changed during the read as failure_kind changed

Inventory integrity failures whose check means the target moved under the
read (container, member and unpacked-entry identity, read coverage,
traversal coverage) now take the model's changed projection: code
artifact_changed, retry backoff and details.failure_kind changed. A
selected target that no longer matches the one the session admitted gets
its own admitted-identity check and keeps its reopen recovery.

BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact and
the other inventory operations report a target that changed during the
read as artifact_changed (retryable, failure_kind changed) instead of
artifact_operation_failed. Admission-identity refusals report
integrity_check admitted-identity instead of container-identity.

* test(artifacts): raise the cleanup fixture's decode refusal with its format facts

* fix(errors)!: report unsupported and unrecognized targets on the TargetFailure model

AnalysisUnsupportedTargetError carried a free-text reason and remediation
beside the TargetFailure model, so unsupported refusals had no format,
failure_kind or retry hint, and their recovery was prose written at each
producer.

The class is deleted. Producers raise unsupportedTarget or
unrecognizedTarget with the owner's format, and consumers read
targetFailureOfKind. The projection table renders the recovery and names
the operations the owner lists in related.operations. The foreign-bytes
identity loop moves into the conformance registry.

BREAKING CHANGE: unsupported and unrecognized targets report
failure_kind, format and retry. Their remediation comes from the
target-failure table; producer-written remediation sentences are folded
into details.reason or replaced by details.related_operations.

* test(artifacts): expect admitted-identity for selections replaced before admission

A selected source replaced before admission fails the admitted-identity check rather than container-identity since the changed/admitted split.

* test(go): name the fixture and result when an MZ carrier is not refused through MCP

* style: format the migrated target-failure tests

* test(errors): expect the PE readers' native-binary format label for foreign bytes

* test(verify): expect target failures for damaged ELF, core and EVM inputs in real-provider verifiers

The binary-layout, recorded-crash and EVM verifiers still expected invalid_input for malformed or truncated selected bytes. The pwntools and EVM adapters now report those as unsupported_target with failure_kind; a missing path remains invalid_input.

* test(web): expect the partial code for a refused source-map admission

The limit-cut code is partial; this Windows-lane test arrived from main with the old truncated spelling.

* fix(artifacts): keep refusal facts on DMG verdicts and the selected bundle path

hdiutil's "image not recognized" verdict raised the unsupported reason
without the owner's facts, so the inventory translation treated it as a
reader defect. It now carries an unsupported dmg target failure, and the
reader-failure constructor requires facts for every target reason.

A bundle resource refusal names the caller's selected bundle path rather
than its resolved target.

The real-provider verifiers and the Android, source-location acceptance
tests expect the model's target-failure categories and codes.

* test(errors): assert unsupported refusals' kind and format, and align verifiers and the Electron guide

* test(errors): check the MCP target failure as the text clients receive

The stdio transport removes the private structured carrier from every isError result, so the conformance suite asserted a field that never reaches clients and failed every case. The projected diagnostic is checked from the JSON text instead.

* test(errors): raise main's new unsupported-target fixtures through the model

* test(artifacts): expect artifact_changed when selected bytes change after inventory

Bundle resources replaced after open and unselected JavaScript sources rewritten after inventory now fail a change check, so they report artifact_changed with retry backoff rather than artifact_operation_failed.

* fix(errors): report a session digest mismatch as admitted-identity

The digest the session admitted does not change when the same request is
repeated, so targetDigestChangedFailure was inviting a backoff retry that
fails the same way. It now names admitted-identity, which stays
artifact_operation_failed and not retryable, and tells the caller to reopen.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(artifacts): classify unused PyInstaller compressed input as layout

zlib stops at the first complete stream, so a declared span that arrives
intact but is not fully consumed is trailing junk or a second stream, not
a file that changed. A source that ends before that span stays read-coverage.
The cached-carrier test replaces the file with a different size so a shared
timestamp cannot hide the rewrite.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(applications): expect unsupported_target for a damaged trace stream

A trace member whose CRC fails is a malformed zip target. The partial
Evidence assertion now checks that code, failure_kind and format.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(artifacts): assert Android refusal kinds and drop the SEA registry case

details.reason is the owner's description, so the boot-image refusals
assert failure_kind and format. A malformed SEA blob inside an ELF stays
an outer-format inventory with a recognition limitation, which the Node
SEA reader tests already cover, so it is not an inspect_artifact refusal.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(artifacts): pass the selected path into translated target failures

translateArtifactFailure names the caller-selected path on a format
refusal. Directory inspection and a retained artifact scope both know
that path and now supply it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(artifacts): charge global pax records against the tar metadata budget

Repeated pax global headers were copied into one map without a charge, so
each header could stay inside the extended-header allowance while the map
grew without a bound. The retained metadata budget is charged for the
merged map's keys, values and entries before that map is allocated.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(dylib): expect admitted-identity when the opened target is replaced

The session keeps the digest it admitted, so replacing the executable and
repeating trace_dylib_resolution is not a retryable container change.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* style: format the inventory path translation and conformance suite

* fix(artifacts): keep the unsupported-target migration in files the inventory merge replaced

* test(errors): expect target failures for ZIP inventory refusals and input_conflict for an unlinked IL2CPP pair

* test(artifacts): expect a malformed target failure for an ISO 9660 directory cycle

* test(web): expect a malformed target failure on the captured source path

* test(artifacts): expect a malformed ZIP target failure for a corrupt Zstandard member

* test(javascript): read the owner's input facts from details.reason and recovery from the projection

* test(iso9660): assert the malformed target failure fields

details.reason is the owner's description. The compiled CLI refusal is
identified by failure_kind and format.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(errors): assert selection and unsupported view fields

A desktop root that is not a directory is a selection failure, so the
CLI reports invalid_request. An analysis view the parent cannot supply
is unsupported evidence; the tests read failure_kind, format, field and
related_operations.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(errors): keep binary plist UID facts on unsupported targets

Pass the reader's UnsupportedBinaryPlistUidError failure through instead of
rebuilding a bplist refusal that drops field and undecodedContent.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(errors): keep oversized binary plist UID facts on keyed archives

Assert the reader projects format, field, and undecoded content from the
parser failure instead of a rebuilt bplist refusal.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-24 23:25:08 -05:00

2046 lines
73 KiB
TypeScript

import { expect, it, onTestFinished } from "vitest";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { deflateSync, zstdCompressSync } from "node:zlib";
import { createHash } from "node:crypto";
import { encode } from "@msgpack/msgpack";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { runWithAnalysisOperationSnapshot } from "../../../src/domain/analysisOperationSnapshot.js";
import {
MAXIMUM_ZSTD_MAX_FRAMES,
MINIMUM_ZSTD_MAX_FRAMES,
} from "../../../src/domain/analysisResourceLimits.js";
import { pe } from "../../../src/domain/binaryTarget.fixture.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import {
gpuCodeObjectsPartialSchema,
gpuCodeObjectsSchema,
} from "../../../src/domain/gpu/gpuCodeObjects.js";
import { GpuCodeObjectsProvider } from "../../../src/gpu/GpuCodeObjectsProvider.js";
import { GpuInventory } from "../../../src/gpu/GpuInventory.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
cudaFixture,
cubinFixture,
ccobFixture,
hipFixture,
peGpuFixture,
elfFixture,
ptxFixture,
amdAbiFixture,
amdDescriptorNoteFixture,
amdFixture,
amdNoteFixture,
amdPalMetadataFixture,
llvmBitcodeFixture,
msgpackWithIntegerKeys,
} from "../../fixtures/gpu/codeObjects.js";
const inspect = async (
bytes: Buffer,
options: {
analysis?: "inventory" | "kernels";
targets?: string[];
object_ids?: string[];
expected_sha256?: string;
} = {},
) => {
const root = await createTestTempDirectory("rea-gpu-"),
path = join(root, "no-format-extension");
await writeFile(path, bytes);
const provider = new GpuCodeObjectsProvider();
onTestFinished(() => provider.close());
return provider.inspect({ path, analysis: "kernels", ...options });
};
it("reports a Zstandard frame-count refusal as an adjustable GPU limit", async () => {
const frame = (byte: number) =>
Buffer.from([0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x01, 0x09, 0x00, 0x00, byte]);
const compressed = Buffer.concat([frame(0x61), frame(0x62)]);
const entry = Buffer.alloc(64 + Math.ceil(compressed.length / 8) * 8);
entry.writeUInt16LE(1, 0);
entry.writeUInt16LE(0x101, 2);
entry.writeUInt32LE(64, 4);
entry.writeBigUInt64LE(BigInt(entry.length - 64), 8);
entry.writeUInt32LE(compressed.length, 16);
entry.writeUInt32LE(80, 28);
entry.writeBigUInt64LE(0x11n | 0x8000n, 40);
entry.writeBigUInt64LE(2n, 56);
compressed.copy(entry, 64);
const header = Buffer.alloc(16);
header.writeUInt32LE(0xba55ed50);
header.writeUInt16LE(1, 4);
header.writeUInt16LE(16, 6);
header.writeBigUInt64LE(BigInt(entry.length), 8);
const result = await runWithAnalysisOperationSnapshot(
{ revision: 1, overrides: { "zstd.max_frames": 1 } },
() => inspect(Buffer.concat([header, entry])),
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error).toMatchObject({
_tag: "AnalysisResourceConstraintError",
resource: "cpu",
limit: {
setting: "zstd.max_frames",
effective_value: 1,
source: "runtime",
minimum: MINIMUM_ZSTD_MAX_FRAMES,
maximum: MAXIMUM_ZSTD_MAX_FRAMES,
unit: "count",
},
});
});
it.each(["none", "zstd", "lz4"] as const)(
"reads selected CUDA metadata and PTX with %s compression",
async (compression) => {
const result = await inspect(cudaFixture(compression));
if (!result.ok) throw result.error;
expect(result.value.coverage).toMatchObject({
discovery: "complete",
payloads: "complete",
});
expect(
result.value.kernels.map((row) => [
row.name,
row.parameter_count,
row.parameter_bytes,
row.shared_bytes,
row.registers.physical,
]),
).toEqual([
["add_one", 2, 12, 32, null],
["add_one", 2, 12, 1056, 10],
]);
expect(result.value.ptx_artifacts[0]!.text).toBe(ptxFixture);
expect(
result.value.kernels[1]!.parameters.map((row) => [
row.ordinal,
row.offset,
row.size,
]),
).toEqual([
[0, 0, 8],
[1, 8, 4],
]);
},
);
it("keeps each kernel resource location in its own payload", async () => {
const inspected = await inspect(cudaFixture("none"));
if (!inspected.ok) throw inspected.error;
const report = inspected.value;
const kernelIndex = report.kernels.findIndex(
({ representation }) => representation === "cuda-elf-metadata",
);
const kernel = report.kernels[kernelIndex];
const sibling = report.objects.find(({ kind }) => kind === "ptx");
if (
!kernel?.sources.parameters ||
sibling === undefined ||
sibling.decoded_size === null ||
sibling.decoded_size < 1
)
throw new Error(
"Fixture needs a CUBIN parameter site and a decoded PTX sibling",
);
expect(kernel.sources.parameters.container_id).toBe(kernel.object_id);
for (const container_id of [null, sibling.id]) {
const replacement = {
container_id,
offset: 0,
size: 1,
section_index: null,
section_name: null,
};
const kernels = report.kernels.with(kernelIndex, {
...kernel,
sources: { ...kernel.sources, parameters: replacement },
});
expect(
gpuCodeObjectsSchema.safeParse({ ...report, kernels }),
).toMatchObject({
success: false,
error: {
issues: [
expect.objectContaining({
path: ["kernels", kernelIndex, "sources", "parameters"],
}),
],
},
});
}
});
it("observes cancellation during a cubin kernel directory read", async () => {
const cubin = cubinFixture();
const section = Number(cubin.readBigUInt64LE(40)) + 64;
const offset = Number(cubin.readBigUInt64LE(section + 24));
const size = Number(cubin.readBigUInt64LE(section + 32));
const names = cubin.subarray(offset, offset + size);
const compressionHeader = Buffer.alloc(24);
compressionHeader.writeUInt32LE(2);
compressionHeader.writeBigUInt64LE(BigInt(names.length), 8);
compressionHeader.writeBigUInt64LE(1n, 16);
const encodedNames = Buffer.concat([
compressionHeader,
zstdCompressSync(names),
]);
const bytes = Buffer.concat([cubin, encodedNames]);
bytes.writeBigUInt64LE(0x800n, section + 8);
bytes.writeBigUInt64LE(BigInt(cubin.length), section + 24);
bytes.writeBigUInt64LE(BigInt(encodedNames.length), section + 32);
const controller = new AbortController();
const inventory = new GpuInventory(
{ path: "/artifacts/cubin", analysis: "kernels" },
controller.signal,
);
const cancelled = new Error("kernel directory read cancelled");
let payloadReads = 0;
let pendingAbort: NodeJS.Immediate | undefined;
try {
await expect(
inventory.inspect({
size: bytes.length,
containerId: null,
read: async (at, count) => {
// The selected payload is re-read into the kernel parser's buffer.
if (at === 0 && count === bytes.length && ++payloadReads === 1)
pendingAbort = setImmediate(() => controller.abort(cancelled));
return bytes.subarray(at, at + count);
},
}),
).rejects.toMatchObject({ reason: "cancelled", cause: cancelled });
expect(payloadReads).toBe(1);
} finally {
if (pendingAbort !== undefined) clearImmediate(pendingAbort);
}
});
it("decodes a CUDA 0x2000 LZ4 entry to the exact declared payload", async () => {
const ptx = Buffer.from(ptxFixture),
bytes = cudaFixture("lz4", [{ kind: 1, target: 80, bytes: ptx }]),
// The first fatbinary header is 16 bytes, so the first entry starts there.
entry = 16;
// The fixture must really encode: a stored-profile fallback would leave the
// LZ4 selection path unexercised while this control still passed.
expect(bytes.readBigUInt64LE(entry + 40)).toBe(0x2011n);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
kind: "ptx",
compression: "lz4",
compression_id: "cuda-flags:0x2011",
decoded_size: ptx.length,
inspection: "complete",
sha256: createHash("sha256").update(ptx).digest("hex"),
});
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.ptx_artifacts.map((row) => row.text)).toEqual([
ptxFixture,
]);
});
it("rejects a CUDA LZ4 entry whose declared decoded length disagrees", async () => {
const bytes = cudaFixture("lz4", [
{ kind: 1, target: 80, bytes: Buffer.from(ptxFixture) },
]),
entry = 16;
// Grow only the declared output length; the encoded literals cannot reach it.
bytes.writeBigUInt64LE(
BigInt(Buffer.byteLength(ptxFixture) + 16),
entry + 56,
);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
compression: "lz4",
inspection: "malformed",
sha256: null,
});
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.ptx_artifacts).toEqual([]);
});
it("preserves CUDA inventory and selection across an oversized declared decoded length", async () => {
const bytes = cudaFixture("zstd", [
{ kind: 2, target: 90, bytes: cubinFixture({ architecture: 90 }) },
{ kind: 1, target: 80, bytes: Buffer.from(ptxFixture) },
]);
const firstEntry = 16;
const declared = 1n << 53n;
const encoded = bytes.readUInt32LE(firstEntry + 16);
bytes.writeBigUInt64LE(declared, firstEntry + 56);
const inventory = await inspect(bytes, { analysis: "inventory" });
if (!inventory.ok) throw inventory.error;
expect(inventory.value.artifact).toMatchObject({
path: expect.stringContaining("no-format-extension"),
file_size: bytes.length,
sha256: createHash("sha256").update(bytes).digest("hex"),
});
expect(inventory.value.objects).toMatchObject([
{
target: "sm_90",
inspection: "inventory",
encoded_size: encoded,
decoded_size: null,
location: { offset: firstEntry + 64, size: encoded },
limitations: [expect.stringContaining(String(declared))],
},
{ target: "sm_80", inspection: "inventory" },
]);
for (const [targets, inspection] of [
[undefined, "capacity"],
[["sm_80"], "not-selected"],
] as const) {
const result = await inspect(
bytes,
targets ? { targets: [...targets] } : {},
);
if (!result.ok) throw result.error;
expect(result.value.objects).toMatchObject([
{ id: inventory.value.objects[0]!.id, target: "sm_90", inspection },
{
id: inventory.value.objects[1]!.id,
target: "sm_80",
inspection: "complete",
},
]);
expect(result.value.kernels.map(({ name }) => name)).toEqual(["add_one"]);
}
});
it("retains the unsupported legacy CUDA compression declaration and extents", async () => {
const result = await inspect(
cudaFixture("legacy", [
{ kind: 1, target: 80, bytes: Buffer.from(ptxFixture), flags: 4 },
]),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
compression: "unsupported",
compression_id: expect.stringContaining("cuda-flags:0x1015"),
encoded_size: deflateSync(Buffer.from(ptxFixture)).length,
decoded_size: Buffer.byteLength(ptxFixture),
inspection: "unsupported",
});
expect(result.value.ptx_artifacts).toEqual([]);
expect(result.value.kernels).toEqual([]);
expect(result.value.objects[0]!.limitations.join(" ")).toContain(
"compression/version is not supported",
);
});
it.each([1, 2, 3] as const)(
"reads CCOB v%d zlib/Zstd and retains exact decoded provenance",
async (version) => {
for (const method of ["zlib", "zstd"] as const) {
const result = await inspect(ccobFixture(version, method));
if (!result.ok) throw result.error;
expect(result.value.kernels[0]).toMatchObject({
name: "add_one",
parameter_count: 2,
parameter_bytes: 12,
registers: { sgpr: 6, vgpr: 2 },
shared_bytes: 0,
});
expect(result.value.objects[2]!.location.container_id).toBe(
result.value.objects[0]!.id,
);
}
},
);
it("does not decode unselected payloads and keeps stable positional identities", async () => {
const bytes = cudaFixture("zstd");
const inventory = await inspect(bytes, { analysis: "inventory" }),
selected = await inspect(bytes, { targets: ["sm_90"] });
if (!inventory.ok) throw inventory.error;
if (!selected.ok) throw selected.error;
expect(inventory.value.kernels).toEqual([]);
expect(selected.value.objects.map((row) => row.id)).toEqual(
inventory.value.objects.map((row) => row.id),
);
expect(selected.value.ptx_artifacts).toEqual([]);
expect(selected.value.kernels).toHaveLength(1);
expect(selected.value.coverage.payloads).toBe("partial");
});
it.each(["cubin", "ptx"] as const)(
"retains conflicting container and %s target declarations as partial facts",
async (kind) => {
const bytes = cudaFixture("none", [
{
kind: kind === "cubin" ? 2 : 1,
target: 80,
bytes:
kind === "cubin"
? cubinFixture({ architecture: 90 })
: Buffer.from(ptxFixture.replace("sm_80", "sm_90")),
},
]);
const result = await inspect(bytes, { targets: ["sm_80"] });
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: "sm_80",
payload_target: "sm_90",
target_consistency: "conflict",
inspection: "partial",
});
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels).toHaveLength(1);
expect(result.value.kernels[0]!.limitations.join(" ")).toContain(
"inventory sm_80; payload sm_90",
);
const unexamined = await inspect(bytes, { targets: ["sm_90"] });
if (!unexamined.ok) throw unexamined.error;
expect(unexamined.value.objects[0]).toMatchObject({
target: "sm_80",
payload_target: null,
target_consistency: "unexamined",
inspection: "not-selected",
});
expect(unexamined.value.coverage.payloads).toBe("partial");
},
);
it("keeps CUBIN base-only agreement distinct from verified feature-profile agreement", async () => {
const bytes = cudaFixture("none", [
{ kind: 2, target: 90, bytes: cubinFixture({ architecture: 90 }) },
]);
bytes.writeBigUInt64LE(bytes.readBigUInt64LE(16 + 40) | 0x100000n, 16 + 40);
const result = await inspect(bytes, { targets: ["sm_90a"] });
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: "sm_90a",
payload_target: "sm_90",
target_consistency: "base-only",
inspection: "complete",
});
});
it.each([
[100, 0x06006402],
[101, 0x06006502],
[103, 0x05006702],
[120, 0x06007802],
[121, 0x06007902],
])(
"reads ABI-qualified CUDA target sm_%d from released NVIDIA header flags",
async (architecture, flags) => {
const result = await inspect(cubinFixture({ osAbi: 0x41, flags }));
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: `sm_${architecture}`,
payload_target: `sm_${architecture}`,
target_consistency: "single-source",
inspection: "complete",
cuda_target_header: {
os_abi: 0x41,
flags: `0x${flags.toString(16).padStart(8, "0")}`,
},
});
},
);
it("keeps unsupported CUDA flag layouts unknown while retaining raw header evidence", async () => {
const result = await inspect(
cubinFixture({ osAbi: 0x41, flags: 0x06016402 }),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: null,
payload_target: null,
target_consistency: "unknown",
inspection: "partial",
cuda_target_header: { os_abi: 0x41, flags: "0x06016402" },
});
expect(result.value.kernels).toHaveLength(1);
expect(result.value.kernels[0]!.limitations.join(" ")).toContain(
"target encoding is unknown",
);
const inventory = await inspect(
cubinFixture({ osAbi: 0x41, flags: 0x06016402 }),
{ analysis: "inventory" },
);
if (!inventory.ok) throw inventory.error;
expect(inventory.value.coverage.discovery).toBe("partial");
});
it("discovers standalone AMD targets before exact selection", async () => {
const bytes = amdFixture(),
target = "amdgcn-amd-amdhsa--gfx900";
const inventory = await inspect(bytes, { analysis: "inventory" });
const selected = await inspect(bytes, { targets: [target] });
const skipped = await inspect(bytes, {
targets: ["amdgcn-amd-amdhsa--gfx1100"],
});
if (!inventory.ok) throw inventory.error;
if (!selected.ok) throw selected.error;
if (!skipped.ok) throw skipped.error;
expect(inventory.value.objects[0]).toMatchObject({
target,
inspection: "inventory",
sha256: null,
});
expect(inventory.value.kernels).toEqual([]);
expect(selected.value.objects[0]).toMatchObject({
target,
inspection: "complete",
id: inventory.value.objects[0]!.id,
});
expect(selected.value.kernels).toHaveLength(1);
expect(selected.value.kernels[0]).toMatchObject({
representation: "amdgpu-metadata",
parameter_bytes: 12,
});
expect(skipped.value.objects[0]).toMatchObject({
target,
inspection: "not-selected",
sha256: null,
});
expect(skipped.value.kernels).toEqual([]);
expect(skipped.value.coverage.payloads).toBe("partial");
});
it("derives V3 AMDGPU processor identity from ELF flags when the note omits it", async () => {
const bytes = amdFixture(false, undefined, null);
// EF_AMDGPU_MACH=0x2c identifies gfx900; high bits include feature flags.
bytes.writeUInt32LE(0x12c, 48);
const inventory = await inspect(bytes, { analysis: "inventory" });
if (!inventory.ok) throw inventory.error;
expect(inventory.value.objects[0]).toMatchObject({
target: "gfx900",
target_consistency: "single-source",
inspection: "inventory",
});
const selected = await inspect(bytes, { targets: ["gfx900"] });
if (!selected.ok) throw selected.error;
expect(selected.value.objects[0]).toMatchObject({
payload_target: "gfx900",
amd_pal_metadata: null,
amd_target_metadata: {
os_abi: 64,
abi: "amdhsa",
note_coverage: "complete",
notes: [{ target: null, target_state: "absent" }],
},
});
expect(selected.value.kernels).toHaveLength(1);
expect(selected.value.kernels[0]).toMatchObject({ parameter_bytes: 12 });
});
it("distinguishes an invalid MessagePack target from legacy note coverage", async () => {
const legacy = Buffer.alloc(20);
legacy.writeUInt32LE(4, 0);
legacy.writeUInt32LE(4, 4);
legacy.writeUInt32LE(10, 8); // YAML rather than MessagePack
legacy.write("AMD\0", 12);
const declared = "amdgcn-amd-amdhsa--gfx900";
for (const [bytes, target, state, limitation] of [
[
amdFixture(false, undefined, 123),
null,
"invalid",
/target is not a string/u,
],
[amdFixture(false, legacy), declared, "declared", /Legacy AMDGPU YAML/u],
] as const) {
const result = await inspect(bytes);
if (!result.ok) throw result.error;
const row = result.value.objects[0]!;
expect(row).toMatchObject({
target,
payload_target: target,
amd_target_metadata: {
note_coverage: "partial",
notes: [{ target, target_state: state }],
},
});
expect(row.limitations.join(" ")).toMatch(limitation);
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels.map(({ name }) => name)).toEqual(["add_one"]);
}
});
it("keeps AMDGPU processor identity unknown when ELF flags are unassigned", async () => {
const bytes = amdFixture(false, undefined, null);
bytes.writeUInt32LE(0x17f, 48);
const result = await inspect(bytes, { analysis: "inventory" });
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: null,
target_consistency: "unknown",
inspection: "inventory",
});
});
const palNote = (bytes: Buffer, metadata: Buffer) => ({
container_id: "file@0",
section_index: 2,
section_name: ".note",
offset: bytes.indexOf(metadata),
size: metadata.length,
});
it("reports PAL-ABI target, ABI and declared pipeline facts without HSA kernels", async () => {
const metadata = amdPalMetadataFixture();
const bytes = amdAbiFixture(65, metadata);
const inventory = await inspect(bytes, { analysis: "inventory" });
if (!inventory.ok) throw inventory.error;
const header = {
processor: "gfx1030",
os_abi: 65,
abi: "amdpal",
note_coverage: "complete",
notes: [
{
target: null,
target_state: "absent",
location: palNote(bytes, metadata),
},
],
};
expect(inventory.value.objects[0]).toMatchObject({
target: "gfx1030",
inspection: "inventory",
amd_target_metadata: header,
amd_pal_metadata: null,
limitations: [],
});
expect(inventory.value.coverage.discovery).toBe("complete");
const selected = await inspect(bytes, { targets: ["gfx1030"] });
if (!selected.ok) throw selected.error;
expect(selected.value.objects[0]).toMatchObject({
target: "gfx1030",
inspection: "complete",
amd_target_metadata: header,
limitations: [],
});
expect(selected.value.objects[0]!.amd_pal_metadata).toEqual({
version: { major: 3, minor: 0 },
pipelines: [
{
index: 0,
name: "triangle",
type: "VsPs",
api: "Vulkan",
shaders: [
{ stage: ".vertex", hardware_mapping: [".vs"] },
{ stage: ".pixel", hardware_mapping: [".ps"] },
],
hardware_stages: [
{
stage: ".vs",
entry_point: "_amdgpu_vs_main",
entry_point_symbol: "main_vs",
sgpr_count: 20,
vgpr_count: 12,
agpr_count: null,
wavefront_size: 32,
lds_size: null,
scratch_memory_size: 0,
},
{
stage: ".ps",
entry_point: "_amdgpu_ps_main",
entry_point_symbol: null,
sgpr_count: 10,
vgpr_count: 6,
agpr_count: null,
wavefront_size: null,
lds_size: 256,
scratch_memory_size: null,
},
],
location: palNote(bytes, metadata),
},
],
});
expect(selected.value.kernels).toEqual([]);
expect(selected.value.coverage.payloads).toBe("complete");
expect(gpuCodeObjectsSchema.safeParse(selected.value).success).toBe(true);
});
it("keeps a PAL-ABI object without metadata unsupported with its target", async () => {
for (const analysis of ["inventory", "kernels"] as const) {
const result = await inspect(amdAbiFixture(65, null), { analysis });
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: "gfx1030",
inspection: "unsupported",
amd_target_metadata: { os_abi: 65, abi: "amdpal", notes: [] },
amd_pal_metadata: null,
limitations: [
"No AMDGPU PAL MessagePack pipeline metadata was available",
],
});
}
});
it("keeps the version of an LLVM PAL note that omits amdpal.version null", async () => {
// llc 23 -mtriple=amdgcn--amdpal emits only amdpal.pipelines.
const metadata = msgpackWithIntegerKeys({
"amdpal.pipelines": [
{
".hardware_stages": {
".ps": { ".entry_point_symbol": "ps_main", ".sgpr_count": 1 },
},
".registers": new Map([[0x2c0a, 0x22f0000]]),
},
],
});
const result = await inspect(amdAbiFixture(65, metadata));
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
inspection: "complete",
limitations: [],
amd_pal_metadata: {
version: null,
pipelines: [{ hardware_stages: [{ stage: ".ps", sgpr_count: 1 }] }],
},
});
});
it("keeps an unexpected PAL scalar unknown and coverage partial", async () => {
const metadata = Buffer.from(
encode({
"amdpal.version": [3, 0],
"amdpal.pipelines": [
{ ".hardware_stages": { ".cs": { ".sgpr_count": "many" } } },
],
}),
);
const result = await inspect(amdAbiFixture(65, metadata));
if (!result.ok) throw result.error;
const row = result.value.objects[0]!;
expect(row.inspection).toBe("partial");
expect(row.amd_pal_metadata?.pipelines[0]?.hardware_stages).toMatchObject([
{ stage: ".cs", sgpr_count: null },
]);
expect(row.limitations).toEqual([
"AMDGPU PAL note 0 pipeline 0 stage .cs .sgpr_count is not an unsigned integer; it remains unknown",
]);
});
it.each([
["truncated MessagePack", () => amdPalMetadataFixture().subarray(0, 40)],
[
// A MessagePack binary value is not a map, even when empty.
"a binary pipeline entry",
() =>
Buffer.from(
encode({
"amdpal.version": [3, 0],
"amdpal.pipelines": [
{ ".type": "Cs", ".hardware_stages": {} },
new Uint8Array(0),
],
}),
),
],
[
"a non-map hardware stage table",
() =>
Buffer.from(
encode({
"amdpal.version": [3, 0],
"amdpal.pipelines": [
{ ".type": "Cs", ".hardware_stages": {} },
{ ".type": "Cs", ".hardware_stages": [".cs"] },
],
}),
),
],
])("reports a PAL note with %s as malformed", async (label, metadata) => {
for (const analysis of ["inventory", "kernels"] as const) {
const result = await inspect(amdAbiFixture(65, metadata()), { analysis });
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: "gfx1030",
inspection: "malformed",
amd_target_metadata: { abi: "amdpal" },
});
}
const retained = await inspect(amdAbiFixture(65, metadata()));
if (!retained.ok) throw retained.error;
// Pipelines decoded before the failure stay as declared facts.
expect(
retained.value.objects[0]!.amd_pal_metadata?.pipelines.map(
({ index, type }) => ({ index, type }),
) ?? [],
).toEqual(
label === "truncated MessagePack" ? [] : [{ index: 0, type: "Cs" }],
);
});
it("shares one decoded-value allowance across a code object's PAL notes", async () => {
// Each note fits the per-note allowance; together they exceed it.
const note = amdDescriptorNoteFixture(
Buffer.from(
encode({ "amdpal.pipelines": Array.from({ length: 65536 }, () => ({})) }),
),
);
const bytes = elfFixture(224, [
{
name: ".note",
type: 7,
bytes: Buffer.concat([note, note, note, note, note]),
},
]);
bytes[7] = 65;
// Inventory decodes every note, so the allowance applies before selection.
const result = await inspect(bytes, { analysis: "inventory" });
if (!result.ok) throw result.error;
const row = result.value.objects[0]!;
expect(row).toMatchObject({ inspection: "capacity", amd_pal_metadata: null });
expect(row.limitations.join(" ")).toContain("262144-value");
});
it.each([
[66, "mesa3d", /Mesa3D-ABI metadata is outside/u],
[0, "unknown", /OS ABI 0 is not a recognized AMDGPU runtime ABI/u],
] as const)(
"never reads OS ABI %s notes under the HSA schema",
async (osAbi, abi, limitation) => {
// An HSA-shaped note under another ABI must not yield HSA kernels.
const note = amdNoteFixture();
const result = await inspect(
amdAbiFixture(osAbi, note.subarray(20, 20 + note.readUInt32LE(4))),
);
if (!result.ok) throw result.error;
const row = result.value.objects[0]!;
expect(row).toMatchObject({
target: "gfx1030",
inspection: "unsupported",
amd_target_metadata: {
os_abi: osAbi,
abi,
note_coverage: "partial",
notes: [],
},
amd_pal_metadata: null,
});
expect(row.limitations.join(" ")).toMatch(limitation);
expect(result.value.kernels).toEqual([]);
},
);
it.each([false, true])(
"keeps HIP bitcode representation unsupported with wrapped=%s and no declared processor",
async (wrapped) => {
const payload = llvmBitcodeFixture(wrapped),
bytes = hipFixture(payload, "hip-amdgcn-amd-amdhsa--");
for (const analysis of ["inventory", "kernels"] as const) {
const result = await inspect(bytes, { analysis });
if (!result.ok) throw result.error;
const row = result.value.objects.find(({ kind }) => kind === "amdgpu")!;
expect(row).toMatchObject({
bundle_id: "hip-amdgcn-amd-amdhsa--",
target: null,
payload_target: null,
inspection: analysis === "inventory" ? "inventory" : "unsupported",
encoded_size: payload.length,
location: { size: payload.length },
sha256:
analysis === "inventory"
? null
: createHash("sha256").update(payload).digest("hex"),
});
expect(row.limitations.join(" ")).toContain("LLVM bitcode");
expect(row.limitations.join(" ")).not.toContain("not an ELF image");
expect(result.value.coverage).toMatchObject({
discovery: "partial",
payloads: analysis === "inventory" ? "not-requested" : "partial",
});
expect(result.value.kernels).toEqual([]);
}
},
);
it.each([
["amdgcn", "gfx900:xnack+"],
["amdgcn", "gfx900+xnack"],
["amdgcn", "gfx900:sramecc-:xnack+"],
["amdgpu", "gfx900:xnack+"],
["amdgpu9", "gfx900:xnack+"],
["amdgpu9.00", "gfx900:sramecc-:xnack+"],
])(
"preserves matching AMDGPU %s note features with ELF processor flags (%s)",
async (triple, target) => {
const noteTarget = `${triple}-amd-amdhsa--${target}`;
const payload = amdFixture(false, undefined, noteTarget);
payload.writeUInt32LE(0x12c, 48);
const result = await inspect(
hipFixture(payload, `hipv4-amdgcn-amd-amdhsa--${target}`),
{ targets: [target] },
);
if (!result.ok) throw result.error;
expect(
result.value.objects.find(({ kind }) => kind === "amdgpu"),
).toMatchObject({
target,
payload_target: noteTarget,
target_consistency: "consistent",
inspection: "complete",
});
expect(result.value.kernels).toHaveLength(1);
expect(result.value.coverage.payloads).toBe("complete");
},
);
it.each(["amdgcn", "amdgpu9.00"])(
"does not choose between conflicting AMDGPU %s note and ELF processor identities",
async (triple) => {
const declared = `${triple}-amd-amdhsa--gfx900:xnack+`;
const bytes = amdFixture(false, undefined, declared);
bytes.writeUInt32LE(0x141, 48); // EF_AMDGPU_MACH gfx1100
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: null,
payload_target: null,
target_consistency: "unknown",
inspection: "partial",
amd_target_metadata: {
flags: "0x00000141",
processor: "gfx1100",
note_coverage: "complete",
notes: [{ target: declared, target_state: "declared" }],
},
});
expect(result.value.objects[0]!.limitations.join(" ")).toContain(
"processor flags conflict with target note metadata",
);
expect(result.value.kernels).toHaveLength(1);
},
);
it.each([
"producer-specific-gpu-target",
"amdgpu9.00-amd-amdhsa--gfx900:xnack",
])(
"retains the unrecognized AMDGPU note target %s without substituting ELF processor flags",
async (noteTarget) => {
const result = await inspect(
hipFixture(amdFixture(false, undefined, noteTarget)),
{ targets: ["gfx900"] },
);
if (!result.ok) throw result.error;
const row = result.value.objects.find(({ kind }) => kind === "amdgpu");
expect(row).toMatchObject({
target: "gfx900",
payload_target: noteTarget,
target_consistency: "unknown",
inspection: "complete",
});
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.kernels).toMatchObject([
{ name: "add_one", parameter_bytes: 12, registers: { sgpr: 6, vgpr: 2 } },
]);
expect(row?.limitations.join(" ")).not.toContain(
"Conflicting target declarations",
);
expect(result.value.kernels[0]?.limitations.join(" ")).not.toContain(
"Conflicting target declarations",
);
},
);
it("retains kernels from conflicting AMDGPU notes without choosing either note or ELF flags", async () => {
const first = amdNoteFixture();
const second = amdNoteFixture(false, "amdgcn-amd-amdhsa--gfx1100");
const bytes = amdFixture(false, second);
const firstDescriptor = bytes.indexOf(first) + 20;
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects).toMatchObject([
{
kind: "amdgpu",
target: null,
payload_target: null,
target_consistency: "unknown",
inspection: "partial",
amd_target_metadata: {
note_coverage: "complete",
notes: [
{
target: "amdgcn-amd-amdhsa--gfx900",
target_state: "declared",
location: { offset: firstDescriptor, size: first.readUInt32LE(4) },
},
{
target: "amdgcn-amd-amdhsa--gfx1100",
target_state: "declared",
location: {
offset: firstDescriptor + first.length,
size: second.readUInt32LE(4),
},
},
],
},
},
]);
expect(result.value.objects[0]?.limitations).toContainEqual(
expect.stringMatching(
/conflicting processor targets.*identity is unknown/iu,
),
);
expect(result.value.coverage).toMatchObject({
discovery: "partial",
payloads: "partial",
});
expect(result.value.kernels).toMatchObject([
{ name: "add_one", parameter_bytes: 12, registers: { sgpr: 6, vgpr: 2 } },
{ name: "add_one", parameter_bytes: 12, registers: { sgpr: 6, vgpr: 2 } },
]);
expect(new Set(result.value.kernels.map(({ id }) => id)).size).toBe(2);
const selected = await inspect(bytes, { targets: ["gfx900"] });
if (!selected.ok) throw selected.error;
expect(selected.value.objects[0]).toMatchObject({
target: null,
payload_target: null,
target_consistency: "unknown",
inspection: "not-selected",
});
expect(selected.value.kernels).toEqual([]);
});
it.each(
[
"amdgcn-amd-amdhsa-unknown-",
"amdgpu9.00-amd-amdhsa--",
"amdgpu9.00-amd-amdhsa-unknown-",
].flatMap((prefix) =>
(
[
["gfx900", "gfx900", "consistent"],
["gfx1100", "gfx900", "conflict"],
["gfx900", "gfx900:xnack+", "unknown"],
["gfx900", "gfx900+xnack", "unknown"],
["gfx900+xnack", "gfx900", "unknown"],
["GFX900", "gfx900", "unknown"],
["gfx900", "GFX900", "unknown"],
] as const
).map(([inventoryTarget, payloadTargetId, consistency]) => ({
inventoryTarget,
payloadTarget: `${prefix}${payloadTargetId}`,
consistency,
})),
),
)(
"compares AMDGPU inventory $inventoryTarget with note $payloadTarget as $consistency",
async ({ inventoryTarget, payloadTarget, consistency }) => {
const bytes = hipFixture(
amdFixture(false, undefined, payloadTarget),
`hipv4-amdgcn-amd-amdhsa--${inventoryTarget}`,
);
const result = await inspect(bytes, { targets: [inventoryTarget] });
if (!result.ok) throw result.error;
const row = result.value.objects.find((value) => value.kind === "amdgpu")!;
expect(row.payload_target).toBe(payloadTarget);
expect(result.value.kernels).toHaveLength(1);
if (consistency === "conflict") {
expect(row).toMatchObject({
target: inventoryTarget,
target_consistency: "conflict",
inspection: "partial",
});
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels[0]!.limitations.join(" ")).toContain(
"Conflicting target declarations",
);
} else {
expect(row).toMatchObject({
target: inventoryTarget,
target_consistency: consistency,
inspection: "complete",
});
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.kernels[0]!.limitations.join(" ")).not.toContain(
"Conflicting target declarations",
);
}
},
);
it.each([
"hipv4-amdgcn-amd-amdhsa--gfx900",
"hipv4-amdgcn-amd-amdhsa-gfx900",
"hipv4-amdgcn-amd-amdhsa-opaque",
"hipv4-amdgpu9.00-amd-amdhsa--gfx900",
])("keeps discovered HIP targets selectable for %s", async (targetId) => {
const bytes = hipFixture(amdFixture(), targetId);
const inventory = await inspect(bytes, { analysis: "inventory" });
if (!inventory.ok) throw inventory.error;
const row = inventory.value.objects.find((value) => value.kind === "amdgpu")!;
const expectedTarget = targetId.endsWith("opaque")
? "amdgcn-amd-amdhsa--gfx900"
: "gfx900";
expect(row.target).toBe(expectedTarget);
const selected = await inspect(bytes, { targets: [expectedTarget] });
if (!selected.ok) throw selected.error;
expect(selected.value.kernels).toHaveLength(1);
expect(
selected.value.objects.find((value) => value.kind === "amdgpu"),
).toMatchObject({
target: expectedTarget,
payload_target: "amdgcn-amd-amdhsa--gfx900",
target_consistency: targetId.endsWith("opaque")
? "single-source"
: "consistent",
inspection: "complete",
});
});
it.each(["gfx11-generic", "gfx9-4-generic"] as const)(
"preserves and exactly selects the generic AMDGPU processor %s",
async (processor) => {
const noteTarget = `amdgcn-amd-amdhsa--${processor}`;
const payload = amdFixture(false, undefined, noteTarget);
payload.writeUInt32LE(processor === "gfx11-generic" ? 0x54 : 0x5f, 48);
const bytes = hipFixture(payload, `hipv4-amdgcn-amd-amdhsa--${processor}`);
const result = await inspect(bytes, { targets: [processor] });
if (!result.ok) throw result.error;
expect(
result.value.objects.find(({ kind }) => kind === "amdgpu"),
).toMatchObject({
kind: "amdgpu",
target: processor,
payload_target: noteTarget,
target_consistency: "consistent",
inspection: "complete",
});
expect(result.value.kernels).toHaveLength(1);
},
);
it("recognizes a modern AMDGPU triple inside compressed CCOB v3", async () => {
const targetId = "hipv4-amdgpu9.00-amd-amdhsa--gfx900";
const bytes = ccobFixture(3, "zstd", hipFixture(amdFixture(), targetId));
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(
result.value.objects.find(({ bundle_id }) => bundle_id === targetId),
).toMatchObject({
kind: "amdgpu",
target: "gfx900",
payload_target: "amdgcn-amd-amdhsa--gfx900",
target_consistency: "consistent",
inspection: "complete",
});
expect(result.value.kernels).toHaveLength(1);
});
it("bounds standalone AMD note discovery before retaining metadata", async () => {
const result = await inspect(
elfFixture(224, [
{ name: ".note", type: 7, bytes: Buffer.alloc(8 * 1024 * 1024 + 1) },
]),
{ analysis: "inventory" },
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]).toMatchObject({
target: "gfx900",
inspection: "capacity",
});
expect(result.value.objects[0]!.limitations).toContainEqual(
expect.stringMatching(/note discovery.*8 MiB.*metadata budget/iu),
);
expect(result.value.kernels).toEqual([]);
});
it("reads PE raw-size fallback and ELF-host section locations", async () => {
for (const bytes of [
peGpuFixture(),
elfFixture(62, [{ name: ".nv_fatbin", bytes: cudaFixture() }]),
]) {
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.kernels).toHaveLength(2);
expect(result.value.objects[0]!.location.section_index).not.toBeNull();
expect(result.value.objects[1]!.location.container_id).toBe(
result.value.objects[0]!.id,
);
}
});
it("coalesces aliased CUDA metadata before expanding the temporary index", async () => {
const bytes = cubinFixture({ globalRecords: 65536, globalAliases: 1024 });
expect(bytes.length).toBeLessThan(1024 * 1024);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("complete");
expect(result.value.kernels).toHaveLength(1);
expect(result.value.kernels[0]!.registers.physical).toBe(10);
});
it("retains unknown PTX virtual counts and partial payload coverage", async () => {
const result = await inspect(
Buffer.from(ptxFixture.replace("%r<9>", "r0, r1, %r<4>, %s<8>")),
);
if (!result.ok) throw result.error;
expect(result.value.kernels[0]!.registers.virtual[0]).toEqual({
type: ".b32",
count: 14,
});
const unsupported = await inspect(
Buffer.from(ptxFixture.replace("%r<9>", "%r[4]")),
);
if (!unsupported.ok) throw unsupported.error;
expect(unsupported.value.objects[0]!.inspection).toBe("partial");
expect(unsupported.value.coverage.payloads).toBe("partial");
expect(unsupported.value.kernels[0]!.registers.virtual[0]).toEqual({
type: ".b32",
count: null,
});
});
it.each(["spaced", "compact", "mixed"])(
"counts %s PTX register keywords while retaining byte provenance",
async (spacing) => {
const directive = spacing === "spaced" ? ".reg " : ".reg";
const header = ".version 8.8\n.target sm_80\n// GPU 寄存器 🎯\n";
const entry = `.entry k() { ${directive}.b32 %r<4>; ${spacing === "mixed" ? ".reg " : directive}.b32 r0, r1, %r4; ${directive}.b64 %rd<2>; ret; }`;
const source = header + entry;
const result = await inspect(Buffer.from(source));
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("complete");
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.ptx_artifacts[0]!.text).toBe(source);
const kernel = result.value.kernels[0]!;
expect(kernel.registers.virtual).toEqual([
{ type: ".b32", count: 7 },
{ type: ".b64", count: 2 },
]);
expect(kernel.location).toMatchObject({
offset: Buffer.byteLength(header),
size: Buffer.byteLength(entry),
});
},
);
it("keeps unsupported compact PTX register types partial", async () => {
const result = await inspect(
Buffer.from(ptxFixture.replace(".reg .b32", ".reg.v2.b32")),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("partial");
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels[0]!.registers.virtual).toEqual([
{ type: ".v2 .b32", count: null },
{ type: ".b64", count: 5 },
]);
});
it.each([64, 65])(
"counts all %d scalar names in one PTX register list",
async (count) => {
const names = Array.from(
{ length: count },
(_, index) => `%r${index}`,
).join(", ");
const result = await inspect(
Buffer.from(
`.version 8.8\n.target sm_80\n.address_size 64\n.entry k() { .reg .b32 ${names}; ret; }\n`,
),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("complete");
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.kernels[0]!.registers.virtual).toEqual([
{ type: ".b32", count },
]);
},
);
it("retains PTX kernels before and after a long scalar register list", async () => {
const names = Array.from({ length: 65 }, (_, index) => `%r${index}`).join(
", ",
);
const result = await inspect(
Buffer.from(
`.version 8.8\n.target sm_80\n.address_size 64\n.entry before() { ret; }\n.entry affected() { .reg .b32 ${names}; ret; }\n.entry after() { ret; }\n`,
),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("complete");
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.kernels.map((kernel) => kernel.name)).toEqual([
"before",
"affected",
"after",
]);
expect(result.value.kernels[1]!.registers.virtual).toEqual([
{ type: ".b32", count: 65 },
]);
});
it("retains later PTX kernels when a virtual register total overflows", async () => {
const result = await inspect(
Buffer.from(
".version 8.8\n.target sm_80\n.address_size 64\n.entry before() { ret; }\n.entry affected() { .reg .b32 %r<9007199254740991>, %s; ret; }\n.entry after() { ret; }\n",
),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("partial");
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels.map((kernel) => kernel.name)).toEqual([
"before",
"affected",
"after",
]);
expect(result.value.kernels[1]!.registers.virtual).toEqual([
{ type: ".b32", count: null },
]);
});
it.each([
".entry k { ret; }",
".entry k() { ret; }",
".entry k .maxntid 1 { ret; }",
])("inspects PTX entry with optional parameters: %s", async (entry) => {
const result = await inspect(
Buffer.from(`.version 7.0\n.target sm_80\n.address_size 64\n${entry}`),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("complete");
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.kernels).toMatchObject([
{ name: "k", parameter_count: 0, parameter_bytes: 0, parameters: [] },
]);
});
it.each([
["parameter", ".u64 output", ".u64 .vendor_specific output"],
["shared", "scratch[32]", "scratch[]"],
] as const)(
"marks unknown PTX %s declarations partial while retaining known facts",
async (kind, from, to) => {
const bytes = Buffer.from(ptxFixture.replace(from, to));
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("partial");
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.ptx_artifacts[0]!.text).toBe(bytes.toString());
const kernel = result.value.kernels[0]!;
expect(kernel.registers.virtual[0]).toEqual({ type: ".b32", count: 9 });
expect(kernel.limitations.join(" ")).toContain(`PTX ${kind} declarations`);
if (kind === "parameter") {
expect(kernel.parameter_count).toBe(2);
expect(kernel.parameter_bytes).toBeNull();
expect(
kernel.parameters.map(({ size, offset }) => [size, offset]),
).toEqual([
[null, null],
[4, null],
]);
expect(kernel.shared_bytes).toBe(32);
} else {
expect(kernel.parameter_bytes).toBe(12);
expect(kernel.shared_bytes).toBeNull();
}
},
);
it.each([
".ptr .global",
".ptr .global .align 16",
".ptr.global.align 16",
".ptr .global.align 16",
".ptr.global .align 16",
])(
"keeps PTX pointer qualifiers %s complete through the provider",
async (qualifiers) => {
const bytes = Buffer.from(
ptxFixture.replace(".u64 output", `.u64 ${qualifiers} output`),
);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("complete");
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.ptx_artifacts[0]!.text).toBe(bytes.toString());
expect(result.value.kernels[0]).toMatchObject({
parameter_count: 2,
parameter_bytes: 12,
parameters: [
{ name: "output", offset: 0, size: 8 },
{ name: "value", offset: 8, size: 4 },
],
});
},
);
it.each([".align 8 .b8", ".b8 .align 8"])(
"retains PTX array parameter identity and layout with %s",
async (type) => {
const bytes = Buffer.from(
ptxFixture.replace(".u64 output", `${type} payload[8]`),
);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("complete");
expect(result.value.coverage.payloads).toBe("complete");
expect(result.value.ptx_artifacts[0]!.text).toBe(bytes.toString());
expect(result.value.kernels[0]).toMatchObject({
parameter_count: 2,
parameter_bytes: 12,
parameters: [
{ name: "payload", type: ".b8", offset: 0, size: 8 },
{ name: "value", offset: 8, size: 4 },
],
});
},
);
it.each(["9007199254740991", "0x1fffffffffffff"])(
"retains PTX parameter facts when aligning an extent of %s exceeds the exact range",
async (extent) => {
const result = await inspect(
Buffer.from(
`.version 8.0
.target sm_80
.entry wide(.param .b8 payload[${extent}], .param .u32 count, .param .u8 tail) { ret; }
.entry later(.param .u32 value) { ret; }`,
),
);
if (!result.ok) throw result.error;
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels).toMatchObject([
{
name: "wide",
parameter_bytes: null,
parameters: [
{ name: "payload", offset: 0, size: 9007199254740991 },
{ name: "count", offset: null, size: 4 },
{ name: "tail", offset: null, size: 1 },
],
},
{
name: "later",
parameter_bytes: 4,
parameters: [{ name: "value", offset: 0, size: 4 }],
},
]);
},
);
it.each([
".b8 .align 3",
".b8 .align 4503599627370495",
".align 8 .b8 .align 8",
])(
"retains PTX names and partial layout for unsupported alignment %s",
async (type) => {
const bytes = Buffer.from(
ptxFixture.replace(".u64 output", `${type} payload[8]`),
);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("partial");
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels[0]).toMatchObject({
parameter_count: 2,
parameter_bytes: null,
parameters: [
{ name: "payload", type: ".b8", offset: null, size: null },
{ name: "value", offset: null, size: 4 },
],
});
},
);
it.each(["whitespace", "comment"] as const)(
"recognizes standalone PTX after more than 64 KiB of leading %s",
async (kind) => {
const prefix =
kind === "whitespace"
? " ".repeat(70 * 1024)
: `/*${"x".repeat(70 * 1024)}*/\n`;
const bytes = Buffer.from(prefix + ptxFixture);
const inventory = await inspect(bytes, { analysis: "inventory" });
const selected = await inspect(bytes, { targets: ["sm_80"] });
if (!inventory.ok) throw inventory.error;
if (!selected.ok) throw selected.error;
expect(inventory.value.objects[0]!.target).toBe("sm_80");
expect(selected.value.kernels).toHaveLength(1);
expect(selected.value.kernels[0]!.location.offset).toBeGreaterThan(
64 * 1024,
);
},
);
it("uses a later module target when selecting standalone PTX", async () => {
const bytes = Buffer.from(
".version 8.8\n.target sm_80\n.entry first() { ret; }\n.target sm_90\n",
);
const inventory = await inspect(bytes, { analysis: "inventory" });
const selected = await inspect(bytes, { targets: ["sm_90"] });
if (!inventory.ok) throw inventory.error;
if (!selected.ok) throw selected.error;
expect(inventory.value.objects[0]!.target).toBe("sm_90");
expect(selected.value.kernels.map(({ name }) => name)).toEqual(["first"]);
});
it("reports invalid standalone PTX encoding in inventory and kernel inspection", async () => {
const header = Buffer.from(
".version 8.0\n.target sm_80\n.address_size 64\n// ",
);
const entry = Buffer.from("\n.visible .entry hello() { ret; }\n");
const malformed = Buffer.concat([header, Buffer.from([0xff]), entry]);
const valid = Buffer.concat([header, Buffer.from("café"), entry]);
for (const analysis of ["inventory", "kernels"] as const) {
const result = await inspect(malformed, { analysis });
if (!result.ok) throw result.error;
expect(result.value).toMatchObject({
format: "ptx",
artifact: {
sha256: createHash("sha256").update(malformed).digest("hex"),
},
coverage: {
discovery: "partial",
payloads: analysis === "kernels" ? "partial" : "not-requested",
},
objects: [{ kind: "ptx", target: null, inspection: "malformed" }],
kernels: [],
ptx_artifacts: [],
});
expect(result.value.objects[0]!.limitations).toContain(
"PTX is not valid UTF-8 text",
);
const control = await inspect(valid, { analysis });
if (!control.ok) throw control.error;
expect(control.value.coverage.discovery).toBe("complete");
expect(control.value.objects[0]!.target).toBe("sm_80");
if (analysis === "kernels") {
expect(control.value.kernels.map(({ name }) => name)).toEqual(["hello"]);
expect(
Buffer.from(control.value.ptx_artifacts[0]!.text).equals(valid),
).toBe(true);
}
}
});
it("keeps a UTF-8 character split by the standalone PTX scan budget unexamined", async () => {
const budget = 16 * 1024 * 1024;
const header = Buffer.from(".version 8.0\n.target sm_80\n// ");
const bytes = Buffer.concat([
header,
Buffer.alloc(budget - header.length - 1, 0x20),
Buffer.from("é\n"),
]);
const result = await inspect(bytes, { analysis: "inventory" });
if (!result.ok) throw result.error;
expect(result.value.coverage.discovery).toBe("partial");
expect(result.value.objects).toMatchObject([
{ kind: "ptx", target: null, inspection: "partial" },
]);
expect(result.value.objects[0]!.limitations).toEqual([
"PTX target scan reached the 16 MiB text budget before all module directives were examined.",
]);
});
it.each(["truncated", "invalid", "extension"] as const)(
"retains earlier AMD kernel declarations after a later %s note",
async (kind) => {
const tail = Buffer.alloc(kind === "truncated" ? 3 : 24);
if (kind !== "truncated") {
tail.writeUInt32LE(7, 0);
tail.writeUInt32LE(kind === "invalid" ? 1 : 3, 4);
tail.writeUInt32LE(32, 8);
tail.write("AMDGPU\0", 12);
tail[20] = kind === "invalid" ? 0xc1 : 0xd4;
}
const payloadTarget = "amdgcn-amd-amdhsa--gfx900";
for (const { bytes, target, consistency } of [
{
bytes: amdFixture(false, tail),
target: payloadTarget,
consistency: "single-source",
},
{
bytes: hipFixture(amdFixture(false, tail)),
target: "gfx900",
consistency: "consistent",
},
]) {
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.kernels).toHaveLength(1);
expect(result.value.kernels[0]).toMatchObject({
name: "add_one",
parameter_bytes: 12,
registers: { sgpr: 6, vgpr: 2 },
});
expect(
result.value.objects.find((row) => row.kind === "amdgpu")!.inspection,
).toBe(kind === "extension" ? "unsupported" : "malformed");
const identity = {
target,
payload_target: payloadTarget,
target_consistency: consistency,
};
expect(
result.value.objects.find((row) => row.kind === "amdgpu"),
).toMatchObject(identity);
expect(result.value.coverage.payloads).toBe("partial");
const selected = await inspect(bytes, { targets: [target] });
if (!selected.ok) throw selected.error;
expect(
selected.value.objects.find((row) => row.kind === "amdgpu"),
).toMatchObject(identity);
expect(selected.value.kernels).toEqual(result.value.kernels);
expect(selected.value.coverage.payloads).toBe("partial");
const skipped = await inspect(bytes, {
targets: [target === "gfx900" ? payloadTarget : "gfx900"],
});
if (!skipped.ok) throw skipped.error;
expect(
skipped.value.objects.find((row) => row.kind === "amdgpu"),
).toMatchObject(
target === payloadTarget
? {
...identity,
inspection: kind === "extension" ? "unsupported" : "malformed",
}
: {
target,
payload_target: null,
target_consistency: "unexamined",
inspection: "not-selected",
},
);
expect(skipped.value.kernels).toEqual([]);
}
},
);
it("bounds cumulative CUDA records across separate in-range metadata sections", async () => {
const record = Buffer.from("042f0800010000000a000000", "hex"),
records = Buffer.alloc(record.length * 131073);
for (let offset = 0; offset < records.length; offset += record.length)
record.copy(records, offset);
const result = await inspect(
elfFixture(190, [
{ name: ".nv.info", type: 0x70000000, bytes: records },
{ name: ".nv.info", type: 0x70000000, bytes: records },
]),
);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("capacity");
expect(result.value.objects[0]!.limitations.join(" ")).toContain(
"aggregate record/symbol visits",
);
});
it.each(["section-names", "symbols"] as const)(
"retains partial CUBIN coverage when %s are unavailable",
async (missing) => {
const bytes = cubinFixture();
if (missing === "section-names") bytes.writeUInt16LE(0, 62);
else bytes.writeUInt32LE(1, Number(bytes.readBigUInt64LE(40)) + 3 * 64 + 4);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.objects[0]!.inspection).toBe("partial");
expect(result.value.coverage.payloads).toBe("partial");
expect(result.value.kernels).toHaveLength(missing === "symbols" ? 0 : 1);
expect(result.value.objects[0]!.limitations.join(" ")).toContain(
missing === "symbols"
? "kernel declaration coverage is unknown"
: "named metadata coverage is partial",
);
},
);
it.each(["add_one", "kérnel_日本"])(
"decodes UTF-8 CUBIN symbol name %s without a name limitation",
async (name) => {
const result = await inspect(cubinFixture({ name }));
if (!result.ok) throw result.error;
const [kernel] = result.value.kernels;
expect(kernel).toMatchObject({ name, symbol: name });
expect(kernel!.limitations.join(" ")).not.toContain("not valid UTF-8");
},
);
it("preserves non-UTF-8 CUBIN symbol bytes in the kernel name", async () => {
const bytes = cubinFixture();
const symbolName = bytes.indexOf(Buffer.from("\0add_one\0"));
expect(symbolName).toBeGreaterThanOrEqual(0);
bytes[symbolName + 1] = 0xff;
const result = await inspect(bytes);
if (!result.ok) throw result.error;
const [kernel] = result.value.kernels;
expect(kernel?.name).toBe("ÿdd_one");
expect(kernel!.limitations).toContain(
`CUDA kernel symbol name is not valid UTF-8; name shows its bytes as Latin-1 (base64 ${Buffer.from("\xffdd_one", "latin1").toString("base64")})`,
);
});
it.each([9, 10])(
"preserves parameters and register coverage for text count %d",
async (textRegisters) => {
const name = "kernel_" + "argument".repeat(64);
const result = await inspect(
cubinFixture({ name, textRegisters, globalRegisters: 10 }),
);
if (!result.ok) throw result.error;
const conflict = textRegisters !== 10;
expect(result.value.kernels[0]).toMatchObject({
name,
parameter_count: 2,
registers: { physical: conflict ? null : 10 },
});
expect(result.value.objects[0]!.inspection).toBe(
conflict ? "partial" : "complete",
);
expect(result.value.coverage.payloads).toBe(
conflict ? "partial" : "complete",
);
expect(
result.value.kernels[0]!.limitations.some((value) =>
value.includes("Conflicting"),
),
).toBe(conflict);
},
);
it("iterates flat concatenated fatbins beyond the genuine container nesting limit", async () => {
const result = await inspect(
Buffer.concat(Array.from({ length: 20 }, () => cudaFixture())),
);
if (!result.ok) throw result.error;
expect(result.value.objects).toHaveLength(40);
expect(result.value.kernels).toHaveLength(40);
expect(result.value.coverage.discovery).toBe("complete");
});
it("reports checksum corruption and unknown compression as partial, retaining known entries", async () => {
const corrupt = ccobFixture();
corrupt[16] = corrupt[16]! ^ 1;
const result = await inspect(corrupt);
if (!result.ok) throw result.error;
expect(result.value.coverage.discovery).toBe("partial");
expect(result.value.objects[0]!.inspection).toBe("malformed");
const unknown = ccobFixture();
unknown.writeUInt16LE(99, 6);
const unsupported = await inspect(unknown);
if (!unsupported.ok) throw unsupported.error;
expect(unsupported.value.objects[0]!.inspection).toBe("unsupported");
});
it("rejects out-of-range bundle entries without guessing a payload", async () => {
const bytes = hipFixture();
bytes.writeBigUInt64LE(BigInt(bytes.length + 1), 32);
const result = await inspect(bytes);
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisTargetFailureError" },
});
});
it("preserves standalone PTX BOM and trailing NUL bytes in the retained text artifact", async () => {
const bytes = Buffer.from("\uFEFF" + ptxFixture + "\0");
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(
Buffer.compare(Buffer.from(result.value.ptx_artifacts[0]!.text), bytes),
).toBe(0);
expect(result.value.ptx_artifacts[0]!.byte_length).toBe(bytes.length);
});
it("rejects a changed full-file identity before applying positional object selection", async () => {
const bytes = cudaFixture();
const result = await inspect(bytes, {
expected_sha256: "0".repeat(64),
object_ids: ["file@10"],
});
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisArtifactChangedError" },
});
});
it("iterates mixed plain/CCOB bundles without counting flat records as nesting", async () => {
const records = Array.from({ length: 20 }, (_, index) =>
index % 2 === 0 ? hipFixture() : ccobFixture(),
);
const result = await inspect(Buffer.concat(records));
if (!result.ok) throw result.error;
expect(result.value.kernels).toHaveLength(20);
expect(result.value.coverage.discovery).toBe("complete");
});
it("does not recognize a CCOB signature after its first byte is corrupted", async () => {
const bytes = ccobFixture(3, "zlib");
bytes[0] = bytes[0]! | 0x80;
const result = await inspect(bytes);
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisTargetFailureError" },
});
});
it("requires the PTX version directive before any module content", async () => {
const result = await inspect(Buffer.from(`unexpected-prefix ${ptxFixture}`));
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisTargetFailureError" },
});
});
it("retains an embedded CCOB with a corrupted signature as unsupported", async () => {
const bytes = ccobFixture(3, "zlib");
bytes[0] = bytes[0]! | 0x80;
const result = await inspect(
elfFixture(62, [{ name: ".hip_fatbin", bytes }]),
);
if (!result.ok) throw result.error;
expect(result.value.coverage.discovery).toBe("partial");
expect(result.value.kernels).toEqual([]);
expect(result.value.objects).toContainEqual(
expect.objectContaining({
kind: "native-section",
inspection: "unsupported",
limitations: expect.arrayContaining([
"No supported CUDA/HIP container or PTX declarations were identified",
]),
}),
);
});
it.each([
["unknown COFF machine", 0xffff],
["x86 machine with a PE32+ optional header", 0x14c],
] as const)(
"discovers PE GPU sections with %s",
async (_description, machine) => {
const bytes = peGpuFixture();
const pe = bytes.readUInt32LE(60);
bytes.writeUInt16LE(machine, pe + 4);
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.format).toBe("pe");
expect(result.value.artifact.sha256).toBe(
createHash("sha256").update(bytes).digest("hex"),
);
expect(result.value.coverage).toMatchObject({
discovery: "complete",
payloads: "complete",
});
expect(result.value.kernels.map(({ name }) => name)).toEqual([
"add_one",
"add_one",
]);
expect(result.value.ptx_artifacts[0]!.text).toBe(ptxFixture);
},
);
it("does not dispatch a high-bit-corrupted DOS signature to the PE parser", async () => {
const bytes = peGpuFixture(Buffer.from(ptxFixture));
bytes[0] = bytes[0]! | 0x80;
const result = await inspect(bytes);
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisTargetFailureError" },
});
});
it("preserves the PE layout refusal at the GPU provider boundary", async () => {
const result = await inspect(pe(0x014c, 4));
expect(result).toMatchObject({
ok: false,
error: {
_tag: "AnalysisTargetFailureError",
message: expect.stringContaining("overlapping PE header"),
},
});
});
it.each(["zlib", "zstd"] as const)(
"uses each legacy CCOB v1 %s stream boundary across alignment padding",
async (codec) => {
const first = ccobFixture(1, codec),
second = ccobFixture(2, "zlib"),
padding = Buffer.alloc((8 - (first.length % 8)) % 8);
const result = await inspect(Buffer.concat([first, padding, second]));
if (!result.ok) throw result.error;
const bundles = result.value.objects.filter(
({ kind }) => kind === "hip-bundle",
);
expect(bundles).toHaveLength(2);
expect(
bundles.map(({ inspection, decoded_size }) => [inspection, decoded_size]),
).toEqual([
["complete", expect.any(Number)],
["complete", expect.any(Number)],
]);
expect(bundles[0]!.sha256).toBe(bundles[1]!.sha256);
expect(bundles[0]!.encoded_size).toBe(first.length - 20);
expect(bundles[0]!.location.size).toBe(first.length - 20);
expect(bundles[1]!.metadata_location.offset).toBe(
first.length + padding.length,
);
expect(result.value.kernels).toHaveLength(2);
expect(result.value.coverage.discovery).toBe("complete");
},
);
it("keeps a later CCOB bundle after a truncated legacy v1 stream", async () => {
const truncated = ccobFixture(1, "zlib").subarray(0, -1),
later = ccobFixture(2, "zlib");
const result = await inspect(Buffer.concat([truncated, later]));
if (!result.ok) throw result.error;
const bundles = result.value.objects.filter(
({ kind }) => kind === "hip-bundle",
);
expect(bundles).toHaveLength(2);
expect(bundles.map(({ inspection }) => inspection)).toEqual([
"malformed",
"complete",
]);
expect(result.value.kernels).toHaveLength(1);
expect(result.value.coverage.discovery).toBe("partial");
expect(bundles[0]!.limitations.join(" ")).toContain(
"uncertain recovery candidate",
);
});
it.each(["checksum", "decoded-size"] as const)(
"preserves a completed v1 codec extent after a bad %s declaration",
async (declaration) => {
const bundle = hipFixture(
Buffer.from("ordinary CCOB marker"),
"host-other",
),
compressed = deflateSync(bundle, { level: 0 }),
header = Buffer.from(ccobFixture(1, "zlib", bundle).subarray(0, 20));
if (declaration === "checksum") header[12] = header[12]! ^ 1;
else header.writeUInt32LE(bundle.length + 1, 8);
const first = Buffer.concat([header, compressed]);
const result = await inspect(
Buffer.concat([first, ccobFixture(2, "zlib")]),
);
if (!result.ok) throw result.error;
const wrappers = result.value.objects.filter(
({ kind }) => kind === "hip-bundle",
);
expect(wrappers.map(({ inspection }) => inspection)).toEqual([
"malformed",
"complete",
]);
expect(wrappers[0]).toMatchObject({
encoded_size: compressed.length,
location: { size: compressed.length },
});
expect(wrappers[0]!.limitations.join(" ")).not.toContain("recovery");
expect(wrappers[1]!.metadata_location.offset).toBe(first.length);
expect(result.value.coverage.discovery).toBe("partial");
},
);
it("continues at the declared CCOB boundary after an unsupported codec", async () => {
const unknown = ccobFixture();
unknown.writeUInt16LE(99, 6);
const result = await inspect(Buffer.concat([unknown, ccobFixture()]));
if (!result.ok) throw result.error;
expect(result.value.kernels).toHaveLength(1);
expect(result.value.coverage.discovery).toBe("partial");
});
it.each([
{ label: "exact JSON integer", decoded: 1n << 28n, projected: 1 << 28 },
{
label: "outside exact JSON integer range",
decoded: 1n << 53n,
projected: null,
},
])(
"keeps later CCOB v3 bundles after a decoded size $label exceeds capacity",
async ({ decoded, projected }) => {
const first = ccobFixture(3, "zlib");
first.writeBigUInt64LE(decoded, 16);
const result = await inspect(
Buffer.concat([first, ccobFixture(3, "zlib")]),
);
if (!result.ok) throw result.error;
const wrappers = result.value.objects.filter(
({ kind }) => kind === "hip-bundle",
);
expect(wrappers).toMatchObject([
{
inspection: "capacity",
encoded_size: first.length - 32,
decoded_size: projected,
location: { offset: 32, size: first.length - 32 },
},
{ inspection: "complete", metadata_location: { offset: first.length } },
]);
if (projected === null)
expect(wrappers[0]?.limitations).toContain(
`Declared decoded size is ${String(decoded)} bytes, outside the exact integer range of decoded_size`,
);
expect(result.value.coverage.discovery).toBe("partial");
expect(result.value.kernels.map(({ name }) => name)).toEqual(["add_one"]);
},
);
it("refuses escaped PTX report expansion while preserving bounded interrupted facts", async () => {
const text = ptxFixture + "\n//" + "\u0001".repeat(3 * 1024 * 1024);
const result = await inspect(Buffer.from(text));
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisResourceConstraintError" },
});
if (result.ok) throw new Error("expected representation capacity refusal");
expect(projectAnalysisError(result.error)).toMatchObject({
details: {
reported_limits: {
boundary: "gpu-retained-report-bytes",
maximum: 16_777_216,
},
},
});
const partial = gpuCodeObjectsPartialSchema.parse(
parseEvidence(result.error.partialObservation).normalized_result,
);
expect(partial.ptx_artifacts).toEqual([]);
expect(partial.objects).toHaveLength(1);
expect(partial.artifact.sha256).toBeNull();
});
it("distinguishes impossible Clang bundle counts from a framed over-capacity directory", async () => {
const empty = Buffer.alloc(32);
Buffer.from("__CLANG_OFFLOAD_BUNDLE__").copy(empty);
const emptyResult = await inspect(empty, { analysis: "inventory" });
if (!emptyResult.ok) throw emptyResult.error;
expect(emptyResult.value.objects).toEqual([]);
const truncated = hipFixture().subarray(0, 32);
for (const count of [65537n, 0xffffffffffffffffn]) {
const bytes = Buffer.from(truncated);
bytes.writeBigUInt64LE(count, 24);
const result = await inspect(bytes, { analysis: "inventory" });
if (result.ok) throw new Error("Expected malformed directory");
expect(result.error).toMatchObject({
_tag: "AnalysisTargetFailureError",
});
}
const framed = Buffer.alloc(32 + 65537 * 25);
Buffer.from("__CLANG_OFFLOAD_BUNDLE__").copy(framed);
framed.writeBigUInt64LE(65537n, 24);
for (let index = 0; index < 65537; index++) {
const row = 32 + index * 25;
framed.writeBigUInt64LE(BigInt(framed.length), row);
framed.writeBigUInt64LE(1n, row + 16);
framed[row + 24] = 0x78;
}
const result = await inspect(framed, { analysis: "inventory" });
if (result.ok) throw new Error("Expected directory capacity failure");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "resource_constraint",
retryable: false,
details: {
resource: "memory",
reported_limits: { boundary: "hip-bundle-entries", maximum: 65_536 },
},
});
});
it("checks Clang bundle ID bounds before the retained directory allowance", async () => {
const name = "host-" + "x".repeat(65_537 - 5);
const header = Buffer.alloc(56);
header.write("__CLANG_OFFLOAD_BUNDLE__");
header.writeBigUInt64LE(1n, 24);
header.writeBigUInt64LE(BigInt(header.length + name.length), 32);
header.writeBigUInt64LE(1n, 40);
header.writeBigUInt64LE(BigInt(name.length), 48);
const framed = Buffer.concat([header, Buffer.from(name), Buffer.alloc(1)]);
const limited = await inspect(framed, { analysis: "inventory" });
if (limited.ok) throw new Error("Expected a directory capacity failure");
expect(projectAnalysisError(limited.error)).toMatchObject({
code: "resource_constraint",
retryable: false,
details: {
resource: "memory",
reported_limits: {
boundary: "hip-bundle-id-bytes",
maximum: 65_536,
maximum_object_bytes: 134_217_728,
maximum_report_bytes: 16_777_216,
},
},
});
const short = Buffer.from(header);
short.writeBigUInt64LE(BigInt(short.length), 32);
short.writeBigUInt64LE(0n, 40);
const overlaps = Buffer.from(framed);
overlaps.writeBigUInt64LE(BigInt(header.length + 1), 32);
for (const bytes of [short, overlaps]) {
const result = await inspect(bytes, { analysis: "inventory" });
if (result.ok) throw new Error("Expected a malformed bundle directory");
expect(result.error).toMatchObject({
_tag: "AnalysisTargetFailureError",
});
}
});
it("reports aggregate Clang directory capacity when every ID fits", async () => {
const count = 256;
const idBytes = 65_536;
const entryBytes = 24 + idBytes;
const directoryEnd = 32 + count * entryBytes;
const framed = Buffer.alloc(directoryEnd + 1, 0x78);
framed.write("__CLANG_OFFLOAD_BUNDLE__");
framed.writeBigUInt64LE(BigInt(count), 24);
for (let index = 0; index < count; index++) {
const row = 32 + index * entryBytes;
framed.writeBigUInt64LE(BigInt(directoryEnd), row);
framed.writeBigUInt64LE(1n, row + 8);
framed.writeBigUInt64LE(BigInt(idBytes), row + 16);
}
const result = await inspect(framed, { analysis: "inventory" });
if (result.ok) throw new Error("Expected directory capacity failure");
expect(projectAnalysisError(result.error)).toMatchObject({
details: {
reported_limits: {
boundary: "hip-bundle-directory-bytes",
maximum: 16_777_216,
},
},
});
});