fix(hopper): load verified FAT64 slices with native Mach-O semantics

This commit is contained in:
morluto
2026-10-09 05:12:36 +08:00
parent 458771cb58
commit 0a49d5bfb9
10 changed files with 630 additions and 54 deletions
+40 -3
View File
@@ -23,6 +23,11 @@ import {
} from "./LinuxPrivateDisplayProbe.js";
import writeFileAtomic from "write-file-atomic";
import {
prepareHopperMachOImage,
type HopperMachOImage,
} from "./HopperMachOImage.js";
import { acquireHopperTargetLease } from "./HopperTargetLease.js";
import type { HopperTargetLease } from "./HopperTargetLease.js";
@@ -41,11 +46,13 @@ export interface BridgeSession {
export type BridgeLaunch =
| (ProviderProcessLaunch & {
readonly shutdownMode: "bridge-request";
readonly preparedImagePath?: string;
/** The owned helper may hand off to an external GUI application. */
readonly providerLifetime: "launcher-process" | "external-application";
})
| (Extract<ProviderProcessLaunch, { readonly ownsProcessLifetime: true }> & {
readonly shutdownMode: "process-cleanup";
readonly preparedImagePath?: string;
readonly providerLifetime: "launcher-process";
readonly cleanup: NonNullable<ProviderProcessLaunch["cleanup"]>;
});
@@ -69,6 +76,10 @@ interface SharedHopperApplicationLauncherOptions {
readonly targetKind: "executable" | "database";
readonly loaderArgs: readonly string[];
readonly bridgeScriptPath: string;
readonly preparedImage?: {
readonly image: HopperMachOImage;
readonly sourceSha256: string;
};
}
/** Explicit launcher contract; the Linux adapter verifies its pinned Hopper build before execution. */
@@ -125,16 +136,30 @@ export class HopperApplicationLauncher implements BridgeLauncher {
const lease = leaseResult.value;
let leaseTransferred = false;
try {
let loadedPath = this.options.targetPath;
if (this.options.preparedImage !== undefined) {
const prepared = await prepareHopperMachOImage(
this.options.targetPath,
this.options.preparedImage.sourceSha256,
this.options.preparedImage.image,
join(session.directory, "image.macho"),
options.signal,
);
if (!prepared.ok) return prepared;
loadedPath = prepared.value;
}
const ownsProcessLifetime = usesLinuxDemo(this.options);
const bootstrap = await this.#writeBootstrap(
session,
ownsProcessLifetime,
loadedPath,
);
if (!bootstrap.ok) return bootstrap;
const launched = await this.#launchPreparedTarget({
session,
signal: options.signal,
bootstrapPath: bootstrap.value,
loadedPath,
ownsProcessLifetime,
lease,
});
@@ -149,6 +174,7 @@ export class HopperApplicationLauncher implements BridgeLauncher {
readonly session: BridgeSession;
readonly signal: AbortSignal | undefined;
readonly bootstrapPath: string;
readonly loadedPath: string;
readonly ownsProcessLifetime: boolean;
readonly lease: HopperTargetLease | undefined;
}): Promise<
@@ -167,7 +193,7 @@ export class HopperApplicationLauncher implements BridgeLauncher {
"-Y",
bootstrapPath,
action,
this.options.targetPath,
input.loadedPath,
];
try {
if (signal?.aborted === true) return err(new HopperCancelledError());
@@ -217,6 +243,9 @@ export class HopperApplicationLauncher implements BridgeLauncher {
return err(new HopperStartError({ cause }));
}
const ownedLauncher = {
...(this.options.preparedImage === undefined
? {}
: { preparedImagePath: input.loadedPath }),
process: started.process,
ownsProcessLifetime: true as const,
ownership: started.ownership,
@@ -270,12 +299,18 @@ export class HopperApplicationLauncher implements BridgeLauncher {
async #writeBootstrap(
session: BridgeSession,
ownsProcessLifetime: boolean,
loadedPath: string,
): Promise<Result<string, HopperStartError>> {
const bootstrapPath = `${session.directory}/bootstrap.py`;
try {
await writeFile(
bootstrapPath,
bridgeBootstrapSource(session, this.options, ownsProcessLifetime),
bridgeBootstrapSource(
session,
this.options,
ownsProcessLifetime,
loadedPath,
),
{ encoding: "utf8", mode: 0o600 },
);
await chmod(bootstrapPath, 0o600);
@@ -321,12 +356,14 @@ const bridgeBootstrapSource = (
session: BridgeSession,
options: SharedHopperApplicationLauncherOptions,
ownsProcessLifetime: boolean,
loadedPath: string,
): string =>
[
`REA_SOCKET = ${JSON.stringify(session.socketPath)}`,
`REA_TOKEN = ${JSON.stringify(session.token)}`,
`REA_RUN_ID = ${JSON.stringify(session.runId)}`,
`REA_TARGET_PATH = ${JSON.stringify(options.targetPath)}`,
`REA_TARGET_PATH = ${JSON.stringify(loadedPath)}`,
`REA_ORIGINAL_EXECUTABLE_PATH = ${options.targetKind === "executable" ? JSON.stringify(options.targetPath) : "None"}`,
`REA_OWNS_PROCESS_LIFETIME = ${ownsProcessLifetime ? "True" : "False"}`,
`exec(compile(open(${JSON.stringify(options.bridgeScriptPath)}, 'rb').read(), ${JSON.stringify(options.bridgeScriptPath)}, 'exec'))`,
"",