Files
morluto 13f618e8e3 fix(native): report non-native bytes as unrecognized, not malformed (#14936)
* fix(native): report non-native bytes as unrecognized, not malformed

A file that is not the format a reader owns was reported as a broken file
of that format. `inspect_native_initializers` called a PE file or random
bytes a malformed target, and the thin Mach-O reader called any bytes
without Mach-O magic a malformed Mach-O.

Format owners can now say "these bytes are not my format": parse failures
and format target failures gain the `unrecognized` kind that target
failures already define ("no reader for this operation recognizes the
bytes"). The owners report it once:

- the thin Mach-O reader, for bytes without Mach-O magic;
- the shared native container reader, for bytes that are not ELF, PE or
  Mach-O (previously `unsupported`);
- the initializers provider, for a file with neither ELF nor Mach-O magic.

Function starts no longer pre-checks the header itself; the shared reader
owns that decision. IL2CPP keeps mapping unknown native bytes to its
unsupported-container reason. A recognized header that is cut off or
broken stays truncated or malformed.

* fix(resources): keep every format failure kind's facts through artifact wrappers

`ArtifactReaderFailure` kept the owner's facts only for its own list of
three format kinds, and `artifactOperationReason` had a second list. With
`unrecognized` added, `artifactFormatFailure` and `artifactParseFailure`
dropped its facts, so `translateArtifactFailure` raised an internal
invariant error instead of the target failure.

The format kinds are now one list, `FORMAT_TARGET_FAILURE_KINDS`, and
`isFormatTargetFailureKind` is the one guard. `FormatTargetFailure`,
`ParseFailureKind` and `BinaryFormatFailure` derive from that list, and
the reader-failure constructor and the operation-reason check both use the
guard, so a new kind cannot be carried by one wrapper and dropped by another.

* test(conformance): expect unrecognized for foreign bytes on native readers

The Go, Rust and native-initializers readers now report bytes that are not
ELF, PE or Mach-O as unrecognized, so their foreign-bytes conformance cases
expect that kind. The case names follow the kind.
2026-11-27 23:03:26 +00:00

145 lines
4.1 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { machoArm64Initializers } from "../../src/native/initializers/NativeInitializers.fixture.js";
import { arm64FunctionStartsImage } from "../../src/native/apple/MachoFunctionStarts.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
/** The first bytes of an ELF64 little-endian header; no other ELF structure. */
const elfBytes = (): Buffer => {
const bytes = Buffer.alloc(64);
Buffer.from([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1]).copy(bytes);
return bytes;
};
/** An `MZ` DOS header with no PE image behind it. */
const peBytes = (): Buffer => {
const bytes = Buffer.alloc(64);
bytes.write("MZ", 0, "latin1");
return bytes;
};
/** Bytes with no ELF, PE or Mach-O magic. */
const randomBytes = (): Buffer =>
Buffer.from(
Array.from({ length: 64 }, (_, index) => (index * 37 + 11) & 0xff),
);
const run = async (
cli: { run: (input: { arguments: string[] }) => Promise<{ json: unknown }> },
command: string,
name: string,
bytes: Buffer,
) => {
const root = await createTestTempDirectory("rea-native-recognition-cli-");
const path = join(root, name);
await writeFile(path, bytes);
const result = await cli.run({ arguments: [command, path, "--json"] });
return { path, json: result.json };
};
for (const [name, bytes] of [
["PE", peBytes()],
["random bytes", randomBytes()],
] as const)
cliTest(
`reports a ${name} file to inspect-native-initializers as unrecognized`,
async ({ cli }) => {
const { path, json } = await run(
cli,
"inspect-native-initializers",
"target.bin",
bytes,
);
expect(json).toMatchObject({
code: "unsupported_target",
details: {
operation: "inspect_native_initializers",
path,
failure_kind: "unrecognized",
},
});
},
);
for (const [name, bytes] of [
["ELF", elfBytes()],
["PE", peBytes()],
["random bytes", randomBytes()],
] as const)
cliTest(
`reports a ${name} file to inspect-macho-function-starts as unrecognized`,
async ({ cli }) => {
const { path, json } = await run(
cli,
"inspect-macho-function-starts",
"target.bin",
bytes,
);
expect(json).toMatchObject({
code: "unsupported_target",
details: {
operation: "inspect_macho_function_starts",
path,
failure_kind: "unrecognized",
format: "macho",
},
});
},
);
for (const [command, operation, image] of [
[
"inspect-native-initializers",
"inspect_native_initializers",
machoArm64Initializers,
],
[
"inspect-macho-function-starts",
"inspect_macho_function_starts",
arm64FunctionStartsImage,
],
] as const)
cliTest(
`keeps a cut-off Mach-O header a format failure for ${command}`,
async ({ cli }) => {
// A 64-bit Mach-O magic with the header cut off after 12 bytes.
const cut = Buffer.from(image().subarray(0, 12));
const { path, json } = await run(cli, command, "program", cut);
expect(json).toMatchObject({
code: "unsupported_target",
details: { operation, path },
});
const failureKind = (json as { details: { failure_kind: string } })
.details.failure_kind;
expect(["malformed", "truncated"]).toContain(failureKind);
},
);
for (const [command, operation] of [
["inspect-go-binary", "inspect_go_binary"],
["inspect-rust-binary", "inspect_rust_binary"],
] as const)
cliTest(
`reports a non-native file to ${command} as unrecognized with its facts`,
async ({ cli }) => {
const { path, json } = await run(
cli,
command,
"target.bin",
randomBytes(),
);
expect(json).toMatchObject({
code: "unsupported_target",
details: {
operation,
path,
failure_kind: "unrecognized",
format: "native-binary",
},
});
},
);