Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(targets): share the resolved target format vocabulary
  • Loading branch information
morluto committed Nov 19, 2026
commit 9bf53200820f1078e091931e1c5fa266429bcb34
12 changes: 6 additions & 6 deletions src/application/BinaryTargetResolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BINARY_TARGET_FORMAT_KINDS } from "../domain/targetFormats.js";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import {
artifactNameForClassification,
Expand Down Expand Up @@ -43,12 +44,11 @@ import {
cancelledBy,
carriedReason,
} from "../domain/analysisErrorCore.js";
import {
BINARY_TARGET_FORMAT_KINDS,
type BinaryTarget,
type BinaryArchitecture,
type PeCodeViewRecord,
type UniversalMachOSlice,
import type {
BinaryTarget,
BinaryArchitecture,
PeCodeViewRecord,
UniversalMachOSlice,
} from "../domain/binaryTargetTypes.js";
import { err, ok, type Result } from "../domain/result.js";
import { readMipsElfMetadata } from "./MipsElfMetadata.js";
Expand Down
10 changes: 7 additions & 3 deletions src/application/binary/TargetFormatVocabularies.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { describe, expect, expectTypeOf, it } from "vitest";

import {
analysisSnapshotSchema,
Expand All @@ -11,13 +11,13 @@ import {
createImmutableEvidenceBundle,
parseEvidenceBundle,
} from "../../domain/evidenceBundle.js";
import type { TargetFormat } from "../../domain/targetFormats.js";
import { binaryTargetSummary } from "./BinaryTargetSummary.js";

/**
* One admitted target per format. A format added to `BinaryTarget` fails
* compilation here until it has a fixture, and the cases below then require
* every vocabulary that persists or projects targets to accept it. This guards
* the failure class of #7290 until target formats have a single owner.
* every vocabulary that persists or projects targets to accept it.
*/
type TargetFixtures = {
readonly [Format in BinaryTarget["format"]]: BinaryTarget & {
Expand Down Expand Up @@ -148,6 +148,10 @@ const roundTrip = (value: unknown): unknown =>
JSON.parse(JSON.stringify(value));

describe("target format vocabularies", () => {
it("defines a target type for every canonical format", () => {
expectTypeOf<BinaryTarget["format"]>().toEqualTypeOf<TargetFormat>();
});
Comment thread
morluto marked this conversation as resolved.
Resolved

it.each(Object.values(TARGETS))(
"persists and projects $format targets",
(target) => {
Expand Down
46 changes: 5 additions & 41 deletions src/domain/analysisSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
} from "./analysisProfile.js";
import {
BINARY_ARCHITECTURES,
BINARY_TARGET_FORMAT_KINDS,
RAW_ONLY_ARCHITECTURES,
type BinaryTarget,
} from "./binaryTargetTypes.js";
Expand Down Expand Up @@ -52,48 +51,13 @@ import {
type RelationshipCoverage,
} from "./relationshipCoverage.js";
import { sha256Hex } from "../kernel/sha256Hex.js";
import {
BINARY_TARGET_FORMAT_KINDS,
BINARY_TARGET_FORMATS,
} from "./targetFormats.js";

const architectureSchema = z.enum(BINARY_ARCHITECTURES);
const formatSchema = z.enum([
"analysis-database",
"mach-o",
"mach-o-universal",
"elf",
"pe",
"dos-mz",
"dos-com",
"raw-binary",
"gamecube-dol",
"zip",
"ipa",
"apk",
"msix",
"appx",
"hap",
"hsp",
"app-pack",
"asar",
"ines",
"godot-pck",
"unityfs",
"wxapkg",
"dmg",
"pkg",
"intel-hex",
"srec",
"esp-image",
"uf2",
"dfuse",
"esp-flash-image",
"fit",
"ntfs-mft",
"plist",
"javascript",
"source-map",
"uimage",
"android-boot-image",
"android-ota-payload",
]);
const formatSchema = z.enum(BINARY_TARGET_FORMATS);
const kindSchema = z.enum(["executable", "database", "archive", "artifact"]);
type SnapshotFormat = z.infer<typeof formatSchema>;
/** The selection each format's open targets make inside their bytes. */
Expand Down
81 changes: 6 additions & 75 deletions src/domain/binaryTargetTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { DolHeader } from "./gamecubeDol.js";
import {
ZIP_PACKAGE_FORMATS,
type ZipPackageFormat,
} from "./zipPackageFormat.js";
import type { TargetFormatsOfKind } from "./targetFormats.js";

/** Provider-neutral CPU families detected from headers or explicitly selected raw loads. */
export const BINARY_ARCHITECTURES = [
Expand Down Expand Up @@ -320,25 +317,10 @@ export type BinaryTarget =
| (BinaryTargetIdentity &
NonExecutableMetadata & {
readonly kind: "archive";
readonly format:
| ZipPackageFormat
| "asar"
| "ines"
| "godot-pck"
| "unityfs"
| "wxapkg"
| "uimage"
| "android-boot-image"
| "android-ota-payload"
| "dmg"
| "pkg"
| "intel-hex"
| "srec"
| "esp-image"
| "uf2"
| "dfuse"
| "fit"
| "ntfs-mft";
readonly format: Exclude<
TargetFormatsOfKind<"archive">,
"esp-flash-image"
>;
})
| (BinaryTargetIdentity &
NonExecutableMetadata & {
Expand All @@ -350,60 +332,9 @@ export type BinaryTarget =
| (BinaryTargetIdentity &
NonExecutableMetadata & {
readonly kind: "artifact";
readonly format: "plist" | "javascript" | "source-map";
readonly format: TargetFormatsOfKind<"artifact">;
});

/** The kind of every target variant that may carry `Format`. */
type TargetKindOf<Format> = BinaryTarget extends infer Target
? Target extends { readonly format: infer Formats; readonly kind: infer Kind }
? Format extends Formats
? Kind
: never
: never
: never;

/**
* The one kind each target format has. A target, snapshot or identity whose
* kind disagrees with its format is malformed.
*/
export const BINARY_TARGET_FORMAT_KINDS = {
"analysis-database": "database",
"mach-o": "executable",
"mach-o-universal": "executable",
elf: "executable",
pe: "executable",
"dos-mz": "executable",
"dos-com": "executable",
"raw-binary": "executable",
"gamecube-dol": "executable",
...(Object.fromEntries(
ZIP_PACKAGE_FORMATS.map((format) => [format, "archive"]),
) as Record<ZipPackageFormat, "archive">),
asar: "archive",
ines: "archive",
"godot-pck": "archive",
unityfs: "archive",
wxapkg: "archive",
uimage: "archive",
"android-boot-image": "archive",
"android-ota-payload": "archive",
dmg: "archive",
pkg: "archive",
"intel-hex": "archive",
srec: "archive",
"esp-image": "archive",
uf2: "archive",
dfuse: "archive",
"esp-flash-image": "archive",
fit: "archive",
"ntfs-mft": "archive",
plist: "artifact",
javascript: "artifact",
"source-map": "artifact",
} as const satisfies {
readonly [Format in BinaryTarget["format"]]: TargetKindOf<Format>;
};

/** Caller inputs that select how an ESP flash dump is read. */
export interface EspFlashTargetSelection {
/** The caller selected the format, so the table scan runs without a bootloader. */
Expand Down
29 changes: 7 additions & 22 deletions src/domain/liveBinaryTargetSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
binaryArchitectureSchema,
rawBinaryEntryPointSchema,
} from "./rawBinary.js";
import { ZIP_PACKAGE_FORMATS } from "./zipPackageFormat.js";
import { targetFormatsOfKind } from "./targetFormats.js";

const architectureSchema = z.enum(BINARY_ARCHITECTURES);
const headerArchitectureSchema = architectureSchema.exclude(
Expand Down Expand Up @@ -73,26 +73,11 @@ export const liveBinaryTargetSummaryVariants = [
z.object({
...identity,
kind: z.literal("archive"),
format: z.enum([
...ZIP_PACKAGE_FORMATS,
"asar",
"ines",
"godot-pck",
"unityfs",
"wxapkg",
"uimage",
"android-boot-image",
"android-ota-payload",
"dmg",
"pkg",
"intel-hex",
"srec",
"esp-image",
"uf2",
"dfuse",
"fit",
"ntfs-mft",
]),
format: z.enum(
targetFormatsOfKind("archive").filter(
(format) => format !== "esp-flash-image",
),
),
architecture: z.null(),
}),
z.object({
Expand Down Expand Up @@ -122,7 +107,7 @@ export const liveBinaryTargetSummaryVariants = [
z.object({
...identity,
kind: z.literal("artifact"),
format: z.enum(["plist", "javascript", "source-map"]),
format: z.enum(targetFormatsOfKind("artifact")),
architecture: z.null(),
}),
] as const;
Expand Down
61 changes: 61 additions & 0 deletions src/domain/targetFormats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
ZIP_PACKAGE_FORMATS,
type ZipPackageFormat,
} from "./zipPackageFormat.js";

/** Canonical classification of resolved binary targets. */
export const BINARY_TARGET_FORMAT_KINDS = {
"analysis-database": "database",
"mach-o": "executable",
"mach-o-universal": "executable",
elf: "executable",
pe: "executable",
"dos-mz": "executable",
"dos-com": "executable",
"raw-binary": "executable",
"gamecube-dol": "executable",
...(Object.fromEntries(
ZIP_PACKAGE_FORMATS.map((format) => [format, "archive"]),
) as Record<ZipPackageFormat, "archive">),
asar: "archive",
ines: "archive",
"godot-pck": "archive",
unityfs: "archive",
wxapkg: "archive",
uimage: "archive",
"android-boot-image": "archive",
"android-ota-payload": "archive",
dmg: "archive",
pkg: "archive",
"intel-hex": "archive",
srec: "archive",
"esp-image": "archive",
uf2: "archive",
dfuse: "archive",
"esp-flash-image": "archive",
fit: "archive",
"ntfs-mft": "archive",
plist: "artifact",
javascript: "artifact",
"source-map": "artifact",
} as const;

export type TargetFormat = keyof typeof BINARY_TARGET_FORMAT_KINDS;
type TargetKind = (typeof BINARY_TARGET_FORMAT_KINDS)[TargetFormat];
export type TargetFormatsOfKind<Kind extends TargetKind> = {
[
Format in TargetFormat
]: (typeof BINARY_TARGET_FORMAT_KINDS)[Format] extends Kind ? Format : never;
}[TargetFormat];

export const BINARY_TARGET_FORMATS = Object.keys(
BINARY_TARGET_FORMAT_KINDS,
) as readonly TargetFormat[];

export const targetFormatsOfKind = <Kind extends TargetKind>(
kind: Kind,
): TargetFormatsOfKind<Kind>[] =>
BINARY_TARGET_FORMATS.filter(
(format): format is TargetFormatsOfKind<Kind> =>
BINARY_TARGET_FORMAT_KINDS[format] === kind,
);
Loading