@henols/c64-re-tools 2.8.0-rc.3 → 2.8.0-rc.4
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
CHANGED
|
@@ -102,7 +102,7 @@ The script records only structure: code and data regions, analyzer names, and th
|
|
|
102
102
|
|
|
103
103
|
- `analyzer`: `dxa` or `decompiler`. `analyzerVersion` gives the version of the analyzer when the analyzer tells it. `space` is in `body` only for a space that is not `c64`.
|
|
104
104
|
- `revision`: the revision of the findings, or `null` when the knowledge did not change.
|
|
105
|
-
- `changes`: for analyzer names (`names`), `regions` and `references`, the counts `added`, `changed`, `retired`, `unchanged` and `suppressed`. `suppressed` counts the findings that the script did not record, because the user or the LLM removed a fact at that place. `references.inData` counts the references that start in a data region of the user, the LLM or an idiom. The analyzer decoded these data bytes as code.
|
|
105
|
+
- `changes`: for analyzer names (`names`), `regions` and `references`, the counts `added`, `changed`, `retired`, `unchanged` and `suppressed`. `suppressed` counts the findings that the script did not record, because the user or the LLM removed a fact at that place. `references.inData` counts the references that start in a data region of the user, the LLM or an idiom. The analyzer decoded these data bytes as code. `regions.onExecutedCode` counts the data findings on code that a run executed and that the project knowledge holds as code. The script does not record these findings, and they are not conflicts.
|
|
106
106
|
- `conflicts`: each place where the analyzer and the project knowledge disagree. `problem` tells the disagreement. A conflict of code and data also gives these facts about its range:
|
|
107
107
|
- `access`: only with coverage. `executed` counts the instructions in the range that a run executed. `read` and `written` count the bytes that a run read and wrote.
|
|
108
108
|
- `references`: the references of the project knowledge into the range from outside it, each with `from` and `kind`.
|
|
@@ -174,7 +174,7 @@ The script does not record the candidates. Examine each candidate and decide.
|
|
|
174
174
|
|
|
175
175
|
#### What the analysis replaces
|
|
176
176
|
|
|
177
|
-
- The analysis never replaces a name or a region of the user or the LLM. Each disagreement shows in `conflicts`. A conflict with `executed`
|
|
177
|
+
- The analysis never replaces a name or a region of the user or the LLM. Each disagreement shows in `conflicts`. A conflict with `executed` shows data where a run executed code that the project knowledge does not hold as code. A conflict with `knowledge` shows data where the user or the LLM named code.
|
|
178
178
|
- Put all coverage maps into the project knowledge first. Then run DXA, then the decompiler.
|
|
179
179
|
- An idiom table can be too long or too short. Correct its region in the project knowledge. Your region replaces it, and the table stops at the end of your region. The next analysis removes the references and the entry points of the idiom after that end.
|
|
180
180
|
- The idioms obey the regions of the user and the LLM. An example is a `code` region over the bytes of a BIT skip. Where your region has a different type, the idiom gives no region, no reference and no entry point. A reference that you removed gives no entry point.
|
|
@@ -193,7 +193,8 @@ Traps:
|
|
|
193
193
|
- `captured`: true when the CPU got to `--until`. `pc` is that address.
|
|
194
194
|
- `files`: each file that the command wrote, as `{out, bytes, sha256}`. `removed` names the segment files of an earlier capture that the new `image.json` does not list.
|
|
195
195
|
- `segments` and `excluded`: the ranges of the segment files, and the ranges that `--exclude` removed. `loaded: true` marks a segment with the bytes that the program loaded.
|
|
196
|
-
- `
|
|
196
|
+
- `notExecutedUnderIo`: the segments in the RAM under the I/O area in which the program executed no code in the 250 frames after the entry. The memory map shows a read of this RAM as an I/O read. Thus the script cannot tell if the program reads these bytes. When no segment is of this type, or with `--range`, the body has no `notExecutedUnderIo`.
|
|
197
|
+
- `dropped`: the ranges from `$0400` that the stages wrote and that no segment holds. The ranges of `excluded` are not in `dropped`. Each range is in a block that is not a segment because it holds the entry of a later stage. `writtenBy` is the last stage that wrote the range. `stageEntry` is the entry of the later stage. When the capture dropped no byte, or with `--range`, the body has no `dropped`.
|
|
197
198
|
- `stages`: one item for each stage, as `stages` gives it.
|
|
198
199
|
- `differsFromReset`: the count of bytes that are not as a clean C64 has them at READY.
|
|
199
200
|
|
|
@@ -204,7 +205,7 @@ The folder `--out` gets these files:
|
|
|
204
205
|
- One PRG for each segment.
|
|
205
206
|
- `entry_state.json`: the registers, `$00` and `$01`, pages 0 to 3, the VIC, SID and CIA registers, the colour RAM, and the vectors. `io.colour` holds the 1000 colours at $d800. `cia` gives the CIA timer latches and interrupt masks. `differsFromReset` lists the bytes that are not as a clean C64 has them at READY. A rebuild must make these bytes the same, because the program can read them.
|
|
206
207
|
|
|
207
|
-
A segment is a block that a stage wrote, also a small block. The script cuts each block at the start and at the end of the I/O area, `$d000`-`$dfff`. These blocks are not segments:
|
|
208
|
+
A segment is a block that a stage wrote, also a small block. The script cuts each block at the start and at the end of the I/O area, `$d000`-`$dfff`. A segment does not cross the start or the end of the I/O area. Thus the RAM under the I/O area is always in segments of its own. These blocks are not segments:
|
|
208
209
|
|
|
209
210
|
- A block that holds the entry of a later stage before the program, when no stage wrote over that entry.
|
|
210
211
|
- A block that a stage before the program wrote in such a block, for example a variable of a loader.
|
|
@@ -218,7 +219,7 @@ If no stage wrote the capture entry, the program runs as it loaded. Then the ran
|
|
|
218
219
|
|
|
219
220
|
A capture into a folder with an earlier capture removes only the segment files that the new `image.json` does not list. It keeps all other files.
|
|
220
221
|
|
|
221
|
-
On `PASS`, `hint` tells you to disassemble the code at `pc`, and then to put `<out>/image.json` into the project knowledge. When the body has `dropped`, `hint` also gives the count of dropped bytes.
|
|
222
|
+
On `PASS`, `hint` tells you to disassemble the code at `pc`, and then to put `<out>/image.json` into the project knowledge. When the body has `dropped`, `hint` also gives the count of dropped bytes. When the body has `notExecutedUnderIo`, `hint` also names these segments.
|
|
222
223
|
|
|
223
224
|
Traps:
|
|
224
225
|
|
|
@@ -111,6 +111,9 @@ type Stages = ReadonlyArray<{ entry: number; wrote: readonly AddressRange[] }>;
|
|
|
111
111
|
type WrittenBlock = AddressRange & { writtenBy: number };
|
|
112
112
|
type DroppedRange = WrittenBlock & { stageEntry: number };
|
|
113
113
|
|
|
114
|
+
/** 0 below the I/O area, 1 in it, 2 above it. */
|
|
115
|
+
const ioSide = (address: number) => (address < IO_RAM.start ? 0 : address <= IO_RAM.end ? 1 : 2);
|
|
116
|
+
|
|
114
117
|
/** A range cut at the bounds of the I/O area: the RAM there that a stage changed never joins the blocks on its two sides. */
|
|
115
118
|
function cutAtIoArea(range: AddressRange): AddressRange[] {
|
|
116
119
|
if (!rangesOverlap(range, IO_RAM)) return [range];
|
|
@@ -151,7 +154,8 @@ function stageBlocks(stages: Stages, entry: number): Array<WrittenBlock & { stag
|
|
|
151
154
|
* The segments of a capture: the blocks that the stages wrote, without loader
|
|
152
155
|
* leftovers (stageBlocks) and without blocks that are all in pages 0-3
|
|
153
156
|
* (the entry state holds them), joined into ranges that do not overlap or
|
|
154
|
-
* touch.
|
|
157
|
+
* touch. A join never goes past a bound of the I/O area, so the RAM there is
|
|
158
|
+
* a segment of its own. The filter comes before the join: a join first would drop the
|
|
155
159
|
* program with a leftover. `used` tells the addresses that the program used
|
|
156
160
|
* after its entry: a part of a leftover that no later stage wrote is a segment
|
|
157
161
|
* when the program used an address in it, as a stage can copy code of the
|
|
@@ -175,7 +179,7 @@ export function captureSegments(
|
|
|
175
179
|
const segments: Array<AddressRange & { writtenBy: number }> = [];
|
|
176
180
|
for (const block of blocks) {
|
|
177
181
|
const previous = segments[segments.length - 1];
|
|
178
|
-
if (previous !== undefined && block.start <= previous.end + 1) {
|
|
182
|
+
if (previous !== undefined && block.start <= previous.end + 1 && ioSide(block.start) === ioSide(previous.end)) {
|
|
179
183
|
previous.end = Math.max(previous.end, block.end);
|
|
180
184
|
previous.writtenBy = Math.max(previous.writtenBy, block.writtenBy);
|
|
181
185
|
} else segments.push({ ...block });
|
|
@@ -196,13 +200,13 @@ export function captureSegments(
|
|
|
196
200
|
* leftover blocks (stageBlocks) that no range of `kept`, the segments and
|
|
197
201
|
* the excluded ranges, holds. `writtenBy` is the stage that wrote the block,
|
|
198
202
|
* and `stageEntry` the entry of the later stage that makes it a leftover. A
|
|
199
|
-
* byte in the blocks of more than one stage is given once, with the
|
|
200
|
-
* stage that wrote it.
|
|
203
|
+
* byte in the blocks of more than one stage is given once, with the last
|
|
204
|
+
* stage that wrote it, as `writtenBy` of a segment.
|
|
201
205
|
*/
|
|
202
206
|
export function droppedRanges(stages: Stages, entry: number, kept: readonly AddressRange[]): DroppedRange[] {
|
|
203
207
|
const parts: DroppedRange[] = [];
|
|
204
208
|
const given: AddressRange[] = [];
|
|
205
|
-
for (const block of stageBlocks(stages, entry)) {
|
|
209
|
+
for (const block of stageBlocks(stages, entry).sort((a, b) => b.writtenBy - a.writtenBy)) {
|
|
206
210
|
if (block.stageEntry === undefined || block.end < FIRST_SEGMENT_ADDRESS) continue;
|
|
207
211
|
const range = { start: Math.max(block.start, FIRST_SEGMENT_ADDRESS), end: block.end };
|
|
208
212
|
for (const piece of subtract(range, [...kept, ...given])) parts.push({ ...piece, writtenBy: block.writtenBy, stageEntry: block.stageEntry });
|
|
@@ -211,6 +215,19 @@ export function droppedRanges(stages: Stages, entry: number, kept: readonly Addr
|
|
|
211
215
|
return parts.sort((a, b) => a.start - b.start);
|
|
212
216
|
}
|
|
213
217
|
|
|
218
|
+
/**
|
|
219
|
+
* The segments in the RAM under the I/O area in which the program executed
|
|
220
|
+
* no code after its entry. The map gives a read there as an I/O read, so only
|
|
221
|
+
* code that ran shows that the program uses that RAM.
|
|
222
|
+
*/
|
|
223
|
+
export function notExecutedUnderIo<T extends AddressRange>(segments: readonly T[], used: (address: number) => boolean): T[] {
|
|
224
|
+
return segments.filter((segment) => {
|
|
225
|
+
if (ioSide(segment.start) !== 1 || ioSide(segment.end) !== 1) return false;
|
|
226
|
+
for (let address = segment.start; address <= segment.end; address++) if (used(address)) return false;
|
|
227
|
+
return true;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
214
231
|
/** For each address, the last stage that wrote it, or -1. */
|
|
215
232
|
function lastWriters(stages: ReadonlyArray<{ wrote: readonly AddressRange[] }>): Int8Array {
|
|
216
233
|
const writers = new Int8Array(0x10000).fill(-1);
|
|
@@ -18,7 +18,7 @@ import { PAL_FRAME_CYCLES } from "#src/native/c64run.ts";
|
|
|
18
18
|
import { listed, OPTIONS, reply, runScript, UsageError, type Call, type CommandSpec, type OptionSpec } from "#src/script.ts";
|
|
19
19
|
import { basicStart, inspect, writtenThenExecuted } from "./lib/evidence.ts";
|
|
20
20
|
import { loads, readLoadSteps } from "./lib/loads.ts";
|
|
21
|
-
import { callReturn, captureSegments, droppedRanges, executedIn, mergeMaps, stageCandidates, stagePoints, usedAfterEntry, withoutRanges } from "./lib/stages.ts";
|
|
21
|
+
import { callReturn, captureSegments, droppedRanges, executedIn, mergeMaps, notExecutedUnderIo, stageCandidates, stagePoints, usedAfterEntry, withoutRanges } from "./lib/stages.ts";
|
|
22
22
|
import { entryStateFile, machineState, readyState, vectorsOf, writeCapture } from "./lib/machine.ts";
|
|
23
23
|
|
|
24
24
|
/** Written-then-executed bytes from this many on are unpacking, decrypting or relocating, not self-changing code. */
|
|
@@ -555,11 +555,10 @@ async function capture(
|
|
|
555
555
|
const state = await machineState(client);
|
|
556
556
|
// The segments: what the stages wrote, without loader leftovers, with no overlap.
|
|
557
557
|
const load = bytes[0]! | (bytes[1]! << 8);
|
|
558
|
-
const
|
|
559
|
-
|
|
560
|
-
? [options.range]
|
|
561
|
-
: captureSegments(followed.stages, registers.pc, { loaded: { start: load, end: Math.min(0xffff, load + bytes.length - 3) }, used: await usedAfter(client) });
|
|
558
|
+
const used = options.range === undefined ? await usedAfter(client) : undefined;
|
|
559
|
+
const found = used === undefined ? [options.range!] : captureSegments(followed.stages, registers.pc, { loaded: { start: load, end: Math.min(0xffff, load + bytes.length - 3) }, used });
|
|
562
560
|
const segments = withoutRanges(found, options.exclude);
|
|
561
|
+
const underIo = used === undefined ? [] : notExecutedUnderIo(segments, used).map(formatC64RangeFields);
|
|
563
562
|
// The written ranges that the leftover rule took out of the segments. A --range takes no rule.
|
|
564
563
|
const dropped = (options.range === undefined ? droppedRanges(followed.stages, registers.pc, [...found, ...options.exclude]) : []).map((range) => ({
|
|
565
564
|
...formatC64RangeFields(range),
|
|
@@ -595,6 +594,7 @@ async function capture(
|
|
|
595
594
|
files: written,
|
|
596
595
|
...(removed.length === 0 ? {} : { removed }),
|
|
597
596
|
segments: segments.map((segment) => ({ ...formatC64RangeFields(segment), ...("loaded" in segment ? { loaded: true } : {}) })),
|
|
597
|
+
...(underIo.length === 0 ? {} : { notExecutedUnderIo: underIo }),
|
|
598
598
|
...(options.exclude.length === 0 ? {} : { excluded: options.exclude.map(formatC64RangeFields) }),
|
|
599
599
|
...(dropped.length === 0 ? {} : { dropped }),
|
|
600
600
|
stages: showStages(followed.stages),
|
|
@@ -673,10 +673,14 @@ function captured(call: Call, { output, failed }: { output: Record<string, unkno
|
|
|
673
673
|
const result = outcome({ output, failed });
|
|
674
674
|
if (failed) return result;
|
|
675
675
|
const dropped = ((output.dropped ?? []) as Array<{ start: string; end: string }>).reduce((sum, range) => sum + parseC64Address(range.end) - parseC64Address(range.start) + 1, 0);
|
|
676
|
+
const underIo = ((output.notExecutedUnderIo ?? []) as Array<{ start: string; end: string }>).map((range) => `${range.start}-${range.end}`);
|
|
676
677
|
return reply(result, {
|
|
677
678
|
hint: joined([
|
|
678
679
|
`Disassemble the code at ${output.pc as string}. Make sure that it is the program and not the unpacker again.`,
|
|
679
680
|
dropped === 0 ? undefined : `The segments do not hold ${dropped} bytes that the stages wrote, because they are in blocks with the entry of a later stage. dropped gives their ranges. Make sure that the program does not use these bytes.`,
|
|
681
|
+
underIo.length === 0
|
|
682
|
+
? undefined
|
|
683
|
+
: `In the RAM under the I/O area, the program executed no code in ${underIo.join(", ")} after the entry. The memory map cannot show reads of this RAM. Make sure that the program uses these bytes. If it does not, give each of these ranges as --exclude and capture again.`,
|
|
680
684
|
`Then put the capture ${call.string("out")!}/image.json into the project knowledge.`,
|
|
681
685
|
]),
|
|
682
686
|
});
|
package/src/knowledge/import.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import type { DatabaseSync } from "node:sqlite";
|
|
14
14
|
|
|
15
|
-
import { type AddressRange, formatC64Address, formatC64Range, mergeRanges, rangesOverlap, type ReferenceKind, REFERENCE_KINDS, subtract, SYMBOL_NAME } from "../c64.ts";
|
|
15
|
+
import { type AddressRange, formatC64Address, formatC64Range, inRanges, mergeRanges, rangesOverlap, type ReferenceKind, REFERENCE_KINDS, subtract, SYMBOL_NAME } from "../c64.ts";
|
|
16
16
|
import { Refusal } from "../errors.ts";
|
|
17
17
|
import { transaction } from "./database.ts";
|
|
18
18
|
import { analyzerNamed, FACTS, referenceSuppressed, regionsOverlapping, suppressionsOverlapping, symbolAt, type ReferenceRow, type RegionRow, type SymbolRow } from "./read.ts";
|
|
@@ -96,7 +96,10 @@ export interface ImportResult {
|
|
|
96
96
|
revision: string | null;
|
|
97
97
|
/** The analyzer names. */
|
|
98
98
|
names: ImportCounts;
|
|
99
|
-
regions: ImportCounts
|
|
99
|
+
regions: ImportCounts & {
|
|
100
|
+
/** Data findings on executed code that the knowledge holds as code: not written, and no conflict. */
|
|
101
|
+
onExecutedCode: number;
|
|
102
|
+
};
|
|
100
103
|
references: ImportCounts & {
|
|
101
104
|
/** Findings from an address in a data region of the user, the LLM or a heuristic: false code, not written. */
|
|
102
105
|
inData: number;
|
|
@@ -270,12 +273,21 @@ function contradictedData(db: DatabaseSync, findings: NormalizedFindings, space:
|
|
|
270
273
|
return found;
|
|
271
274
|
}
|
|
272
275
|
|
|
276
|
+
/** True when the knowledge holds each executed instruction start in the range in a code or dual-use region. */
|
|
277
|
+
function executedIsCode(db: DatabaseSync, findings: NormalizedFindings, range: AddressRange, space: Space): boolean {
|
|
278
|
+
const code = regionsOverlapping(db, range.start, range.end, space).filter((row) => isCode(row.type) || row.type === "dual-use");
|
|
279
|
+
for (let address = range.start; address <= range.end; address++) if (findings.executed!(address) && !inRanges(code, address)) return false;
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
|
|
273
283
|
function importRegions(db: DatabaseSync, change: Change, given: NormalizedFindings, coverage: AddressRange[], result: ImportResult): void {
|
|
274
284
|
const { analyzer } = given;
|
|
275
285
|
const space = given.space ?? DEFAULT_SPACE;
|
|
276
|
-
// Data findings over code evidence are conflicts, not facts.
|
|
286
|
+
// Data findings over code evidence are conflicts, not facts. Where a run executed the code and the knowledge holds it as code, nothing is left to decide: the finding is only counted.
|
|
277
287
|
const contradicted = contradictedData(db, given, space);
|
|
278
|
-
|
|
288
|
+
const decided = contradicted.filter((item) => item.executed !== undefined && executedIsCode(db, given, item, space));
|
|
289
|
+
result.regions.onExecutedCode = decided.length;
|
|
290
|
+
result.conflicts.push(...contradicted.filter((item) => !decided.includes(item)));
|
|
279
291
|
const findings = { ...given, regions: given.regions.filter((region) => !contradicted.some((item) => item.start === region.start && item.end === region.end)) };
|
|
280
292
|
if (analyzer === "heuristic") {
|
|
281
293
|
// An idiom is specific evidence about its bytes: the DXA or decompiler region there gives way, its parts outside stay.
|
|
@@ -450,7 +462,7 @@ export function importFindings(db: DatabaseSync, findings: NormalizedFindings, c
|
|
|
450
462
|
const coverage = validateFindings(findings);
|
|
451
463
|
return transaction(db, () => {
|
|
452
464
|
const change = new Change(db, { origin: findings.analyzer, ...context }, `import-${findings.analyzer}`);
|
|
453
|
-
const result: ImportResult = { revision: null, names: counts(), regions: counts(), references: { ...counts(), inData: 0 }, conflicts: [] };
|
|
465
|
+
const result: ImportResult = { revision: null, names: counts(), regions: { ...counts(), onExecutedCode: 0 }, references: { ...counts(), inData: 0 }, conflicts: [] };
|
|
454
466
|
importNames(db, change, findings, coverage, result);
|
|
455
467
|
importRegions(db, change, findings, coverage, result);
|
|
456
468
|
importReferences(db, change, findings, coverage, result);
|