@henols/c64-re-tools 2.6.0-rc.7 → 2.6.0-rc.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/skills/c64-knowledge/SKILL.md +1 -1
- package/skills/c64-knowledge/scripts/knowledge.ts +2 -0
- package/skills/c64-knowledge/scripts/lib/listing.ts +18 -3
- package/skills/c64-reverse-engineering/SKILL.md +1 -1
- package/skills/c64-source/SKILL.md +5 -5
- package/skills/c64-source/scripts/source.ts +22 -20
- package/skills/c64-static-analysis/SKILL.md +3 -3
- package/skills/c64-static-analysis/scripts/lib/instructions.ts +22 -20
- package/skills/c64-static-analysis/scripts/static-analysis.ts +1 -8
- package/skills/c64-testing/SKILL.md +1 -0
- package/skills/c64-testing/references/recording-format.md +8 -0
- package/skills/c64-testing/scripts/lib/keys.ts +36 -0
- package/skills/c64-testing/scripts/lib/play.ts +2 -6
- package/skills/c64-testing/scripts/lib/recording.ts +13 -4
- package/skills/emulator/SKILL.md +1 -1
- package/src/cli/main.ts +1 -1
- package/src/host-client/tools.ts +1 -1
- package/src/knowledge/read.ts +16 -1
- package/src/native/ghidra/analyze.ts +74 -63
- package/src/native/ghidra/c64rt-wasm.ts +4 -0
- package/src/native/ghidra/language/c64rt_6510.sla +0 -0
- package/src/native/ghidra/pcode.ts +222 -0
- package/src/native/ghidra/walk.ts +205 -0
- package/src/native/ghidra/wasm.ts +98 -0
- package/src/native/processes.ts +1 -1
- package/src/native/types.ts +2 -2
- package/src/project.ts +4 -3
- package/src/script.ts +1 -1
- package/tools/install-container.sh +1 -2
- package/src/native/ghidra/index.ts +0 -170
- package/src/native/ghidra/scripts/C64Export.java +0 -274
- package/src/native/ghidra/scripts/C64Prepare.java +0 -66
- package/tools/install-ghidra.sh +0 -40
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
// The Ghidra analysis:
|
|
2
|
-
// 6510 language,
|
|
3
|
-
//
|
|
1
|
+
// The Ghidra analysis: the Ghidra SLEIGH decoder and decompiler with the NMOS
|
|
2
|
+
// 6510 language, in this process as a WebAssembly module. Knowledge seeds go
|
|
3
|
+
// in, the structural result comes out, and the result is checked completely
|
|
4
|
+
// before the script imports it.
|
|
4
5
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import { Refusal } from "../../errors.ts";
|
|
8
|
-
import { describeError, isObject, WireFailure } from "../../protocol/messages.ts";
|
|
6
|
+
import { describeError, WireFailure } from "../../protocol/messages.ts";
|
|
9
7
|
import { checkGhidraResult, checkRequestBounds, GHIDRA_LIMITS, imageRange, seedsInside, type GhidraParams, type GhidraResult } from "../types.ts";
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
8
|
+
import type { ToolContext } from "../run.ts";
|
|
9
|
+
import { walk } from "./walk.ts";
|
|
10
|
+
import { decompile, loadImage, setName, SLEIGH_VERSION } from "./wasm.ts";
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const AFTER_DECOMPILE_MS = 30_000;
|
|
12
|
+
/** The longest reason that the result check accepts. */
|
|
13
|
+
const MAX_REASON = 1000;
|
|
17
14
|
|
|
18
15
|
export async function analyze(
|
|
19
16
|
params: GhidraParams,
|
|
@@ -21,67 +18,81 @@ export async function analyze(
|
|
|
21
18
|
context: ToolContext,
|
|
22
19
|
): Promise<{ result: GhidraResult }> {
|
|
23
20
|
checkRequestBounds(params);
|
|
24
|
-
const ghidra = findGhidra(context.env);
|
|
25
21
|
const { start, end, body } = imageRange(params.imageKind, image);
|
|
26
|
-
const base = start;
|
|
27
22
|
|
|
28
|
-
//
|
|
23
|
+
// The decoder sees only the loaded bytes; labels may name any address.
|
|
29
24
|
const { entryPoints, codeStarts, dataRanges } = seedsInside(params, start, end);
|
|
30
25
|
if (entryPoints.length === 0) {
|
|
31
26
|
throw new WireFailure("invalid-input", "Give at least one entry point inside the program, for example the SYS address of its BASIC loader.");
|
|
32
27
|
}
|
|
28
|
+
context.signal.throwIfAborted();
|
|
33
29
|
|
|
34
|
-
|
|
30
|
+
let walked: ReturnType<typeof walk>;
|
|
35
31
|
try {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
32
|
+
loadImage(body, start);
|
|
33
|
+
walked = walk(body, start, end, { entryPoints, codeStarts, dataRanges, labels: params.labels });
|
|
34
|
+
} catch (error) {
|
|
35
|
+
context.log?.(`The Ghidra decoder failed: ${describeError(error)}`);
|
|
36
|
+
throw new WireFailure("operation-failed", `The Ghidra decoder failed: ${describeError(error)}. Nothing was imported.`);
|
|
37
|
+
}
|
|
38
|
+
const { decompilations, notDecompiled } = decompileRoutines(params, walked.functions, body, start, context);
|
|
39
|
+
|
|
40
|
+
let result: GhidraResult;
|
|
41
|
+
try {
|
|
42
|
+
result = checkGhidraResult({ ...walked, decompilations, notDecompiled, completeness: { functions: true, regions: true, references: true }, toolVersion: SLEIGH_VERSION });
|
|
43
|
+
} catch (error) {
|
|
44
|
+
context.log?.(`Ghidra result rejected: ${describeError(error)}`);
|
|
45
|
+
throw new WireFailure("operation-failed", `Ghidra returned an incomplete or inconsistent result. Nothing was imported. The check found: ${describeError(error)}.`);
|
|
46
|
+
}
|
|
47
|
+
return { result };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The requested routines as C text, within the character bounds; the labels name routines and data. */
|
|
51
|
+
function decompileRoutines(params: GhidraParams, functions: GhidraResult["functions"], body: Uint8Array, start: number, context: ToolContext): Pick<GhidraResult, "decompilations" | "notDecompiled"> {
|
|
52
|
+
const decompilations: GhidraResult["decompilations"] = [];
|
|
53
|
+
const notDecompiled: NonNullable<GhidraResult["notDecompiled"]> = [];
|
|
54
|
+
if (params.decompile.length === 0) return { decompilations, notDecompiled };
|
|
55
|
+
const entries = new Set(functions.map((fn) => fn.entry));
|
|
56
|
+
// A trap in the decompiler drops its image: the next routine loads it again.
|
|
57
|
+
let prepared = false;
|
|
58
|
+
const prepare = () => {
|
|
59
|
+
if (prepared) return;
|
|
60
|
+
loadImage(body, start);
|
|
61
|
+
for (const label of params.labels) {
|
|
62
|
+
try {
|
|
63
|
+
setName(label.address, label.name, entries.has(label.address) ? "function" : "data");
|
|
64
|
+
} catch (error) {
|
|
65
|
+
// The decompiler then prints its own name for the address.
|
|
66
|
+
context.log?.(`The Ghidra decompiler refused the name ${label.name}: ${describeError(error)}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
prepared = true;
|
|
70
|
+
};
|
|
71
|
+
let remaining = GHIDRA_LIMITS.decompiledTotalChars;
|
|
72
|
+
for (const entry of params.decompile) {
|
|
73
|
+
if (!entries.has(entry)) {
|
|
74
|
+
notDecompiled.push({ entry, reason: "no function starts at this address" });
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (remaining <= 0) {
|
|
78
|
+
notDecompiled.push({ entry, reason: "the decompiled text of the other routines used the whole limit" });
|
|
79
|
+
continue;
|
|
71
80
|
}
|
|
72
|
-
let
|
|
81
|
+
let text: string;
|
|
73
82
|
try {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
result = checkGhidraResult(isObject(exported) ? { ...exported, toolVersion: ghidraVersion(ghidra) } : exported);
|
|
77
|
-
const [range] = result.coverage;
|
|
78
|
-
if (result.coverage.length !== 1 || range === undefined || range.start !== start || range.end !== end) throw new Refusal("operation-failed", "the coverage is not the loaded program");
|
|
83
|
+
prepare();
|
|
84
|
+
text = decompile(entry);
|
|
79
85
|
} catch (error) {
|
|
80
|
-
|
|
81
|
-
|
|
86
|
+
prepared = false;
|
|
87
|
+
context.log?.(`The Ghidra decompiler failed at ${entry}: ${describeError(error)}`);
|
|
88
|
+
notDecompiled.push({ entry, reason: describeError(error).slice(0, MAX_REASON) || "the decompiler gave no reason" });
|
|
89
|
+
continue;
|
|
82
90
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
91
|
+
const limit = Math.min(GHIDRA_LIMITS.decompiledChars, remaining);
|
|
92
|
+
const truncated = text.length > limit;
|
|
93
|
+
if (truncated) text = text.slice(0, limit);
|
|
94
|
+
remaining -= text.length;
|
|
95
|
+
decompilations.push({ entry, text, truncated });
|
|
86
96
|
}
|
|
97
|
+
return { decompilations, notDecompiled };
|
|
87
98
|
}
|