Files
reverse-engineering-anything/src/native/AppleObjcClassImportBindings.ts
T
morluto 5f718ce30e refactor(kernel): give error-message, byte-hex and SHA-256 hex helpers one owner in a new src/kernel layer (#11542)
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer

* fix(kernel): keep the regex worker's own error text inside its isolated source

* refactor(kernel): exclude copies that run in another realm from the error-message guard by name

The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them.

* refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552)

Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560)

byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out.

Refs #11548

* refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562)

sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted.

Refs #11548

* refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563)

All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567)

All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
2026-11-16 07:05:30 +08:00

145 lines
4.5 KiB
TypeScript

import type { PointerFixups } from "../domain/apple/appleMachoFixups.js";
import type { MachoLayout } from "../domain/apple/appleMachoSelection.js";
import type { MachoImportBinding } from "../domain/native/objcSwiftMetadata.js";
import { errorMessage } from "../kernel/errorMessage.js";
export interface ObjcClassImportBindingScan {
readonly bindings: readonly MachoImportBinding[];
readonly coverage: {
readonly status:
| "complete"
| "partial"
| "unknown"
| "unsupported"
| "not_requested";
readonly reason: string | null;
readonly examined: number;
readonly decoded: number;
};
}
/** Resolve caller-supplied names only at their exact selected-image addresses. */
export const inspectObjcClassImportBindings = (input: {
readonly bytes: Buffer;
readonly layout: MachoLayout;
readonly fixups: PointerFixups;
readonly addresses: readonly string[] | undefined;
/** Whether `addresses` covers every class-label row; undefined is unknown. */
readonly addressesComplete: boolean | undefined;
readonly maxRecords: number;
}): ObjcClassImportBindingScan => {
const { bytes, layout, fixups, addresses, addressesComplete, maxRecords } =
input;
// A candidate scan that is truncated, or whose completeness was not stated,
// reports that limitation even when no candidate was retained.
if (
addresses === undefined ||
(addresses.length === 0 && addressesComplete === true)
)
return {
bindings: [],
coverage: {
status: "not_requested",
reason: null,
examined: 0,
decoded: 0,
},
};
if (fixups.kind === "none")
return {
bindings: [],
coverage: {
status: "unsupported",
reason: "Selected Mach-O contains no supported pointer fixup commands",
examined: 0,
decoded: 0,
},
};
const reasons = new Set<string>();
if (addressesComplete === false)
reasons.add(
"Candidate address scan reached the existing max_records limit",
);
if (addressesComplete === undefined)
reasons.add(
"The caller did not state whether the candidate addresses include every class-label row (objc_class_import_addresses_complete)",
);
for (const failure of fixups.failures) reasons.add(failure);
const bindings: MachoImportBinding[] = [];
const examinedAddresses = new Set<string>();
const candidates = addresses.slice(0, maxRecords);
if (candidates.length < addresses.length)
reasons.add(
"Candidate address scan reached the existing max_records limit",
);
for (const requestedAddress of candidates) {
if (examinedAddresses.has(requestedAddress)) continue;
examinedAddresses.add(requestedAddress);
const parsedAddress = parseAddress(requestedAddress);
if (parsedAddress === undefined) {
reasons.add("A provider address is not a 64-bit hexadecimal address");
continue;
}
try {
const offset = layout.offset(parsedAddress, 8);
const raw = bytes.readBigUInt64LE(offset);
const decoded = fixups.decode(parsedAddress, raw);
if (decoded.kind === "unsupported") {
reasons.add(`${requestedAddress}: ${decoded.reason}`);
continue;
}
if (decoded.kind === "unknown") {
reasons.add(`${requestedAddress}: ${decoded.reason}`);
continue;
}
if (decoded.kind !== "bind" || !isObjcClassImport(decoded.symbol))
continue;
bindings.push({
requested_address: requestedAddress,
symbol: decoded.symbol,
binding_source: decoded.binding_source,
library_ordinal: decoded.library_ordinal,
library: decoded.library,
weak: decoded.weak,
addend: decoded.addend.toString(10),
});
} catch (cause: unknown) {
reasons.add(`${requestedAddress}: ${errorMessage(cause)}`);
}
}
const reason = [...reasons].join("; ") || null;
return {
bindings,
coverage: {
// Unstated candidate completeness leaves the whole scan's coverage
// unknown, even when other gaps are also known.
status:
addressesComplete === undefined
? "unknown"
: reason === null
? "complete"
: "partial",
reason,
examined: examinedAddresses.size,
decoded: bindings.length,
},
};
};
const parseAddress = (address: string): bigint | undefined => {
if (!/^0x[0-9a-f]{1,16}$/iu.test(address)) return undefined;
try {
return BigInt(address);
} catch {
return undefined;
}
};
const isObjcClassImport = (symbol: string): boolean =>
/^_?OBJC_CLASS_\$_.+$/u.test(symbol);