Files
reverse-engineering-anything/src/native/NativeMachoSource.ts
T
morluto 6fa58b0ec8 feat(artifacts): require every integrity reader failure to name its check (#11505)
ArtifactReaderFailure overloads require integrityCheck for a literal
"integrity" reason and an explicit (possibly undefined) check for a rebuild
whose reason is not a literal; primaryFacts always carries it. The .NET bundle
reader uses assertProducedEntry, and the PyInstaller cookie probe and owned
lease cleanup rewrap keep the check they previously dropped.
2026-11-16 08:10:08 +08:00

152 lines
4.9 KiB
TypeScript

import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
import {
withArtifactRegularFile,
assertArtifactReadActive,
artifactSelectedPathErrno,
ArtifactReaderFailure,
type ArtifactResourceScope,
hashStableRootArtifactHandle,
targetDigestChangedFailure,
} from "../resources/index.js";
import { errorMessage } from "../kernel/errorMessage.js";
/** A positional reader for one slice of the descriptor bound to the target. */
export interface NativeMachoSource {
readonly bytes: number;
readSlice(
sliceOffset: number,
sliceSize: number,
offset: number,
length: number,
): Promise<Uint8Array>;
}
/** Preserve the producer outcome when later identity validation fails. */
export class NativeMachoSourceVerificationFailure extends ArtifactReaderFailure {
constructor(
readonly outcome:
| { readonly kind: "completed"; readonly value: unknown }
| { readonly kind: "failed"; readonly cause: unknown },
readonly verificationCause: unknown,
) {
super(
verificationCause instanceof ArtifactReaderFailure
? verificationCause.reason
: "io",
errorMessage(verificationCause),
{
cause: verificationCause,
...(verificationCause instanceof ArtifactReaderFailure
? {
...verificationCause.primaryFacts(),
...(verificationCause.cancellation === undefined
? {}
: { cancellation: verificationCause.cancellation }),
}
: { integrityCheck: undefined }),
},
verificationCause instanceof ArtifactReaderFailure
? verificationCause.details
: undefined,
);
}
/** Compatibility accessor for a completed callback; failures remain in outcome. */
get completed(): unknown {
return this.outcome.kind === "completed" ? this.outcome.value : undefined;
}
}
/**
* Bind native command output and bounded slice reads to one selected target.
* Every callback outcome is checked against the same descriptor and selected
* path before it can escape, including returned and thrown failures.
* The supplied scope retains the exact descriptor owner if cleanup fails.
*/
export const withNativeMachoSource = <T>(
target: Pick<BinaryTarget, "path" | "sha256">,
resources: ArtifactResourceScope,
signal: AbortSignal | undefined,
run: (source: NativeMachoSource) => Promise<T>,
): Promise<T> =>
withArtifactRegularFile(
target.path,
resources,
signal,
async ({ handle, initial }) => {
const verify = async () => {
const digest = await hashStableRootArtifactHandle(
target.path,
handle,
initial,
signal,
).catch((cause: unknown) => {
if (artifactSelectedPathErrno(cause) !== undefined)
throw new ArtifactReaderFailure(
"integrity",
`Active Mach-O target changed: ${target.path}`,
{ cause, integrityCheck: "container-identity" },
);
throw cause;
});
if (digest.sha256 !== target.sha256)
throw targetDigestChangedFailure(
"Mach-O target",
target.sha256,
digest.sha256,
);
};
await verify();
const source: NativeMachoSource = {
bytes: initial.size,
async readSlice(sliceOffset, sliceSize, offset, length) {
if (
!Number.isSafeInteger(sliceOffset) ||
!Number.isSafeInteger(sliceSize) ||
!Number.isSafeInteger(offset) ||
!Number.isSafeInteger(length) ||
sliceOffset < 0 ||
sliceSize < 0 ||
offset < 0 ||
length < 0 ||
sliceOffset > initial.size - sliceSize ||
offset > sliceSize - length
)
throw new ArtifactReaderFailure(
"format",
"Mach-O read exceeds the selected slice.",
);
const bytes = await readFileHandleRange(
handle,
sliceOffset + offset,
length,
() => assertArtifactReadActive(signal),
);
if (bytes.byteLength !== length)
throw new ArtifactReaderFailure(
"integrity",
"Mach-O source changed during slice read.",
{ integrityCheck: "read-coverage" },
);
return bytes;
},
};
let outcome:
| { readonly kind: "completed"; readonly value: T }
| { readonly kind: "failed"; readonly cause: unknown };
try {
outcome = { kind: "completed", value: await run(source) };
} catch (cause: unknown) {
outcome = { kind: "failed", cause };
}
try {
await verify();
} catch (cause: unknown) {
throw new NativeMachoSourceVerificationFailure(outcome, cause);
}
if (outcome.kind === "failed") throw outcome.cause;
return outcome.value;
},
);