fix(targets): project invalid raw-binary loads as caller input (#2004)

* fix(targets): project invalid raw-binary loads as caller input

* fix(targets): name real load options and cover DOS COM kind pairing
This commit is contained in:
pablo
2026-10-12 09:34:45 +08:00
committed by GitHub
parent 5dfb9d050c
commit edf46248f2
8 changed files with 230 additions and 44 deletions
+39 -15
View File
@@ -60,6 +60,16 @@ export interface RawBinaryAdmission {
readonly entryPointSource: string;
}
/** Why a headerless load was refused, with its owning classification. */
export interface RawBinaryAdmissionRejection {
readonly reason: string;
/**
* `load_selection_rejected` names a caller-input selection problem;
* `file_format_rejected` names a refusal caused by the target file itself.
*/
readonly constraint: "load_selection_rejected" | "file_format_rejected";
}
const addressWidth = (architecture: BinaryArchitecture): bigint =>
architecture === "x86" || architecture === "arm"
? 0xffffffffn
@@ -70,33 +80,47 @@ export const admitRawBinary = (input: {
readonly architecture: unknown;
readonly entryPoint: unknown;
readonly fileBytes: number;
}): Result<RawBinaryAdmission, string> => {
}): Result<RawBinaryAdmission, RawBinaryAdmissionRejection> => {
const architecture = binaryArchitectureSchema.safeParse(input.architecture);
if (!architecture.success)
return err(
"raw-binary format requires architecture (x86, x86_64, arm, or arm64)",
);
return err({
reason:
"raw-binary format requires architecture (x86, x86_64, arm, or arm64)",
constraint: "load_selection_rejected",
});
if (typeof input.entryPoint !== "string")
return err(
"raw-binary format requires entry_point as a hexadecimal address of the form 0x…",
);
return err({
reason:
"raw-binary format requires entry_point as a hexadecimal address of the form 0x…",
constraint: "load_selection_rejected",
});
const entry = parseRawBinaryEntryPoint(input.entryPoint);
if (!entry.ok) return entry;
if (!entry.ok)
return err({
reason: entry.error,
constraint: "load_selection_rejected",
});
if (
(architecture.data === "arm" || architecture.data === "arm64") &&
entry.value.value % 4n !== 0n
)
return err(
`raw-binary ${architecture.data} entry_point ${entry.value.canonical} must be 4-byte aligned`,
);
return err({
reason: `raw-binary ${architecture.data} entry_point ${entry.value.canonical} must be 4-byte aligned`,
constraint: "load_selection_rejected",
});
const length = validateRawBinaryLength(input.fileBytes);
if (!length.ok) return length;
if (!length.ok)
return err({
reason: length.error,
constraint: "file_format_rejected",
});
const last = entry.value.value + BigInt(input.fileBytes) - 1n;
const max = addressWidth(architecture.data);
if (entry.value.value > max || last > max)
return err(
`raw-binary mapping from ${entry.value.canonical} must fit in the ${architecture.data} address width`,
);
return err({
reason: `raw-binary mapping from ${entry.value.canonical} must fit in the ${architecture.data} address width`,
constraint: "load_selection_rejected",
});
return ok({
architecture: architecture.data,
entryPoint: entry.value.canonical,