mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(kuna): reject lossy text in retained project exports (#12171)
This commit is contained in:
1 parent
a6469719ce
commit
f361ee1222
6 files changed
+179
-16
No files matched your search
@@ -32,6 +32,7 @@ const KUNA_DOCUMENT_CODES = [
|
||||
"xref_function_hex",
|
||||
"xref_target",
|
||||
"span_short",
|
||||
"invalid_utf8",
|
||||
] as const;
|
||||
|
||||
export type KunaDocumentCode = (typeof KUNA_DOCUMENT_CODES)[number];
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
|
||||
import { KunaDocumentError } from "./KunaDocumentError.js";
|
||||
|
||||
/**
|
||||
* One function block in a `kuna decompile-project` `.c` file. The span covers
|
||||
@@ -50,6 +52,19 @@ interface KeptLine {
|
||||
readonly text: string;
|
||||
}
|
||||
|
||||
/** Decode only the selected source bytes and retain their starting offset. */
|
||||
export const decodeKunaProjectText = (
|
||||
bytes: Uint8Array,
|
||||
offset: number,
|
||||
): Result<string, KunaDocumentError> => {
|
||||
try {
|
||||
return ok(decodeStrictUtf8(bytes, "preserve"));
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof TypeError)) throw cause;
|
||||
return err(new KunaDocumentError("invalid_utf8", { byte_offset: offset }));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Stream a file's lines, keeping the text of only the lines whose first
|
||||
* `headBytes` bytes `keep` accepts. Every other line is skipped without being
|
||||
@@ -72,6 +87,13 @@ const scanLines = async (
|
||||
let line: Buffer[] | undefined = [];
|
||||
let lineBytes = 0;
|
||||
let decided = false;
|
||||
const recordLine = (bytes: Buffer): string | undefined => {
|
||||
const decoded = decodeKunaProjectText(bytes, lineStart);
|
||||
if (!decoded.ok)
|
||||
return `the structural line at byte ${String(lineStart)} is not valid UTF-8`;
|
||||
lines.push({ offset: lineStart, text: decoded.value });
|
||||
return undefined;
|
||||
};
|
||||
for await (const chunk of chunks) {
|
||||
let position = 0;
|
||||
while (position < chunk.length) {
|
||||
@@ -90,11 +112,10 @@ const scanLines = async (
|
||||
return err(`the line at byte ${String(lineStart)} is too long`);
|
||||
}
|
||||
if (newline === -1) break;
|
||||
if (line !== undefined)
|
||||
lines.push({
|
||||
offset: lineStart,
|
||||
text: Buffer.concat(line).toString("utf8"),
|
||||
});
|
||||
if (line !== undefined) {
|
||||
const failure = recordLine(Buffer.concat(line));
|
||||
if (failure !== undefined) return err(failure);
|
||||
}
|
||||
position = newline + 1;
|
||||
lineStart = offset + position;
|
||||
line = [];
|
||||
@@ -105,8 +126,10 @@ const scanLines = async (
|
||||
}
|
||||
if (line !== undefined && lineBytes > 0) {
|
||||
const head = Buffer.concat(line);
|
||||
if (decided || keep(head.subarray(0, headBytes)))
|
||||
lines.push({ offset: lineStart, text: head.toString("utf8") });
|
||||
if (decided || keep(head.subarray(0, headBytes))) {
|
||||
const failure = recordLine(head);
|
||||
if (failure !== undefined) return err(failure);
|
||||
}
|
||||
}
|
||||
return ok({ lines, bytes: offset });
|
||||
};
|
||||
|
||||
@@ -14,6 +14,7 @@ import { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
AnalysisOutputError,
|
||||
AnalysisResourceConstraintError,
|
||||
cancelledBy,
|
||||
carriedReason,
|
||||
@@ -33,6 +34,7 @@ import {
|
||||
import { AbortedWait, waitForAbortable } from "../process/AbortablePromise.js";
|
||||
import type { KunaProject } from "./KunaProjectCache.js";
|
||||
import {
|
||||
decodeKunaProjectText,
|
||||
scanKunaAssembly,
|
||||
type KunaAssemblyIndex,
|
||||
type KunaProjectFunction,
|
||||
@@ -79,6 +81,29 @@ interface KunaSpanOwnership {
|
||||
readonly finishSpan: <T>(finish: () => Promise<T>) => Promise<T>;
|
||||
}
|
||||
|
||||
/** Project a refused text span without treating it as a provider-wide failure. */
|
||||
const invalidSpanText = (
|
||||
cause: unknown,
|
||||
operation: string,
|
||||
path: string,
|
||||
offset: number,
|
||||
length: number,
|
||||
): AnalysisOutputError | undefined =>
|
||||
cause instanceof KunaDocumentError && cause.code === "invalid_utf8"
|
||||
? new AnalysisOutputError(
|
||||
operation,
|
||||
"Kuna's cached project text is not valid UTF-8",
|
||||
{
|
||||
cause,
|
||||
diagnostics: {
|
||||
file: path,
|
||||
byte_offset: offset,
|
||||
byte_length: length,
|
||||
},
|
||||
},
|
||||
)
|
||||
: undefined;
|
||||
|
||||
const readSpan = async (
|
||||
operation: string,
|
||||
path: string,
|
||||
@@ -142,7 +167,7 @@ const readSpan = async (
|
||||
if (!sameRegularFileState(expected, await owner.handle.stat()))
|
||||
throw new RegularFileChangedError(path);
|
||||
await assertKunaFileState(path, expected);
|
||||
read = ok(buffer.toString("utf8"));
|
||||
read = decodeKunaProjectText(buffer, offset);
|
||||
} catch (cause: unknown) {
|
||||
read = err(cause);
|
||||
}
|
||||
@@ -178,9 +203,16 @@ const readSpan = async (
|
||||
if (cause.operationCause instanceof RegularFileChangedError)
|
||||
throw cause.operationCause;
|
||||
return err(
|
||||
new ProviderAdapterError(KUNA_PROVIDER_ID, operation, {
|
||||
cause: cause.operationCause,
|
||||
}),
|
||||
invalidSpanText(
|
||||
cause.operationCause,
|
||||
operation,
|
||||
path,
|
||||
offset,
|
||||
length,
|
||||
) ??
|
||||
new ProviderAdapterError(KUNA_PROVIDER_ID, operation, {
|
||||
cause: cause.operationCause,
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (cause instanceof RegularFileCleanupFailure)
|
||||
@@ -273,10 +305,11 @@ const spanCloseFailure = (
|
||||
failure.operationFailure === undefined &&
|
||||
primaryCause instanceof AnalysisError
|
||||
? primaryCause
|
||||
: new ProviderAdapterError(KUNA_PROVIDER_ID, operation, {
|
||||
: (invalidSpanText(primaryCause, operation, path, offset, length) ??
|
||||
new ProviderAdapterError(KUNA_PROVIDER_ID, operation, {
|
||||
cause: primaryCause,
|
||||
userMessage: errorMessage(primaryCause),
|
||||
});
|
||||
}));
|
||||
return analysisErrorWithCleanupDependencies(primary, marker, operation);
|
||||
};
|
||||
|
||||
|
||||
@@ -53,6 +53,8 @@ export interface FakeKunaBehavior {
|
||||
readonly asmPadding?: number;
|
||||
/** Extra C comment bytes inside each exported body, for buffered-read capacity. */
|
||||
readonly cPadding?: number;
|
||||
/** Exact bytes inside the first exported C body, for text-admission tests. */
|
||||
readonly cBodyComment?: readonly number[];
|
||||
readonly decompile?: "ok" | "hang";
|
||||
readonly standaloneFunctions?: readonly FakeKunaFunction[];
|
||||
/** Hold successful work until releaseFakeKunaCommand acknowledges the test phase. */
|
||||
@@ -212,7 +214,15 @@ if (args[0] === "--version") {
|
||||
? "// Function: " + fn.name + " @ " + fn.addr + "\nint " + fn.name + "(void)\n{\n return " + index + ";\n" + (behavior.cPadding ? " /*" + "x".repeat(behavior.cPadding) + "*/\n" : "") + "}\n\n"
|
||||
: "// Function: " + fn.name + " @ " + fn.addr + " (error: " + fn.error + ")\n\n";
|
||||
});
|
||||
fs.writeFileSync(path.join(output, stem + ".c"), c);
|
||||
if (behavior.cBodyComment === undefined) fs.writeFileSync(path.join(output, stem + ".c"), c);
|
||||
else {
|
||||
const comment = c.indexOf(" return 0;");
|
||||
if (comment === -1) throw new Error("fixture requires a successful first C body");
|
||||
fs.writeFileSync(path.join(output, stem + ".c"), Buffer.concat([
|
||||
Buffer.from(c.slice(0, comment) + " /*"), Buffer.from(behavior.cBodyComment),
|
||||
Buffer.from("*/\n" + c.slice(comment)),
|
||||
]));
|
||||
}
|
||||
fs.writeFileSync(path.join(output, stem + ".h"), "#pragma once\n");
|
||||
let asm = "; kuna decompile-project — " + stem + "\n\n; --- code section 0x1000..0x2000 ---\n\n";
|
||||
functions.forEach((fn) => { asm += fn.name + ": ; " + fn.addr + "\n " + fn.addr.slice(2).padStart(8, "0") + ": c3 RET\n\n"; });
|
||||
|
||||
@@ -25,8 +25,11 @@ const SOURCE = [
|
||||
].join("\n");
|
||||
|
||||
/** Feed the text in fixed-size pieces so headers straddle chunk boundaries. */
|
||||
async function* chunked(text: string, size: number): AsyncGenerator<Buffer> {
|
||||
const bytes = Buffer.from(text);
|
||||
async function* chunked(
|
||||
text: string | Buffer,
|
||||
size: number,
|
||||
): AsyncGenerator<Buffer> {
|
||||
const bytes = typeof text === "string" ? Buffer.from(text) : text;
|
||||
for (let offset = 0; offset < bytes.length; offset += size)
|
||||
yield bytes.subarray(offset, offset + size);
|
||||
}
|
||||
@@ -205,3 +208,41 @@ it("indexes code listings and data labels in Kuna's .asm", async () => {
|
||||
await scanKunaAssembly(chunked("a: ; 0x10\n\nb: ; 0x10\n", 64)),
|
||||
).toEqual({ ok: false, error: "the .asm repeats code address 0x10" });
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
scan: (bytes: Buffer) =>
|
||||
scanKunaProjectSource(chunked(bytes, 1), { decompiled: 1, failed: 0 }),
|
||||
prefix: '#include "sample.h"\n\n',
|
||||
head: "// Function: ",
|
||||
tail: " @ 0x1000\n",
|
||||
},
|
||||
{
|
||||
scan: (bytes: Buffer) => scanKunaAssembly(chunked(bytes, 1)),
|
||||
prefix: "; code\n",
|
||||
head: "",
|
||||
tail: ": ; 0x1000",
|
||||
},
|
||||
])(
|
||||
"validates complete structural lines before indexing them",
|
||||
async ({ scan, prefix, head, tail }) => {
|
||||
const bytesWithName = (name: Buffer) =>
|
||||
Buffer.concat([Buffer.from(prefix + head), name, Buffer.from(tail)]);
|
||||
const invalid = await scan(bytesWithName(Buffer.from([0xff])));
|
||||
expect(invalid).toEqual({
|
||||
ok: false,
|
||||
error: expect.stringContaining(
|
||||
`the structural line at byte ${Buffer.byteLength(prefix)} is not valid UTF-8`,
|
||||
),
|
||||
});
|
||||
const valid = await scan(bytesWithName(Buffer.from("�Δ")));
|
||||
const functions = valid.ok
|
||||
? "code" in valid.value
|
||||
? [...valid.value.code.values()]
|
||||
: valid.value
|
||||
: [];
|
||||
expect(functions).toEqual([
|
||||
expect.objectContaining({ name: "�Δ", address: "0x1000" }),
|
||||
]);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,55 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js";
|
||||
import { run, session } from "./kunaSessionFixture.js";
|
||||
|
||||
it.skipIf(process.platform === "win32").each([
|
||||
{ bytes: [0xff], valid: false },
|
||||
{ bytes: [...Buffer.from("�Δ")], valid: true },
|
||||
])(
|
||||
"keeps cached function text faithful to its source bytes ($valid)",
|
||||
async ({ bytes, valid }) => {
|
||||
const { open } = await session("rea-kuna-project-text-", {
|
||||
cBodyComment: bytes,
|
||||
});
|
||||
const client = open();
|
||||
|
||||
// Assembly requires the published project. The next query therefore reads
|
||||
// the verified on-disk .c span instead of a cold subprocess response.
|
||||
const assembly = await run(client, "procedure_assembly", {
|
||||
procedure: "main",
|
||||
});
|
||||
expect(assembly.rawResult).toMatchObject({
|
||||
source: { kind: "project-export" },
|
||||
});
|
||||
const result = await client.execute("procedure_pseudo_code", {
|
||||
procedure: "main",
|
||||
});
|
||||
if (valid) {
|
||||
expect(result).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
result: expect.stringContaining("/*�Δ*/"),
|
||||
rawResult: { source: { kind: "project-export" } },
|
||||
},
|
||||
});
|
||||
} else {
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
cause: { code: "invalid_utf8", kind: "malformed" },
|
||||
diagnostics: {
|
||||
file: expect.stringMatching(/program\.c$/u),
|
||||
byte_offset: Buffer.byteLength('#include "program.h"\n\n'),
|
||||
byte_length: expect.any(Number),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
// An unreadable C body does not invalidate independently verified assembly.
|
||||
const again = await run(client, "procedure_assembly", {
|
||||
procedure: "main",
|
||||
});
|
||||
expect(again.result).toEqual(assembly.result);
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user