Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(semantics): retain trailing rest argument flows (#756)

Rudy Mizrahi Celekli4bfd8038be0a
diff --git a/src/domain/javascriptSemanticAnalysis.calls.test.ts b/src/domain/javascriptSemanticAnalysis.calls.test.ts
index 9db99d8af..899a1267d 100644
--- a/src/domain/javascriptSemanticAnalysis.calls.test.ts
+++ b/src/domain/javascriptSemanticAnalysis.calls.test.ts
@@ -234,3 +234,52 @@ describe("JavaScript semantic analysis: calls 2", () => {
);
});
});
+
+describe("JavaScript semantic rest parameter flow", () => {
+ it.each([
+ {
+ parameters: "first, ...rest",
+ arguments: "1, 2, 3",
+ expected: [
+ [0, "first"],
+ [1, "rest"],
+ [2, "rest"],
+ ],
+ },
+ {
+ parameters: "...rest",
+ arguments: "1, 2, 3",
+ expected: [
+ [0, "rest"],
+ [1, "rest"],
+ [2, "rest"],
+ ],
+ },
+ {
+ parameters: "first, second",
+ arguments: "1, 2, 3",
+ expected: [
+ [0, "first"],
+ [1, "second"],
+ ],
+ },
+ {
+ parameters: "first, ...rest",
+ arguments: "1, ...values, 3",
+ expected: [[0, "first"]],
+ },
+ ])(
+ "links exact positions for ($parameters) called with ($arguments)",
+ ({ parameters, arguments: arguments_, expected }) => {
+ const ir = analyzeJavaScriptSemantics(
+ `function collect(${parameters}) { return 1; } collect(${arguments_});`,
+ );
+ expect(
+ ir.argumentFlows.map(({ argumentIndex, parameterBindingId }) => [
+ argumentIndex,
+ semanticBinding(ir, parameterBindingId)?.name,
+ ]),
+ ).toEqual(expected);
+ },
+ );
+});
diff --git a/src/domain/javascriptSemanticCallResolution.ts b/src/domain/javascriptSemanticCallResolution.ts
index af8d1d0db..4ffcc48e0 100644
--- a/src/domain/javascriptSemanticCallResolution.ts
+++ b/src/domain/javascriptSemanticCallResolution.ts
@@ -179,22 +179,29 @@ export const parameterBindings = (
binding.definitions.some(
({ kind, location }) =>
kind === "parameter" &&
- parameterIndex(callable, location, state) === index,
+ parameterAcceptsArgument(callable, location, index, state),
),
)
.sort((left, right) => compareCodePoints(left.bindingId, right.bindingId));
};
-const parameterIndex = (
+const parameterAcceptsArgument = (
callable: JavaScriptSemanticCallable,
location: JavaScriptSemanticArgumentFlow["parameterLocation"],
+ argumentIndex: number,
state: JavaScriptSemanticAnalysisState,
-): number => {
+): boolean => {
const node = state.callableNodesById.get(callable.callableId);
- if (!t.isFunction(node)) return -1;
- return node.params.findIndex((parameter) =>
+ if (!t.isFunction(node)) return false;
+ const parameterIndex = node.params.findIndex((parameter) =>
rangeContains(range(parameter), location),
);
+ if (parameterIndex === argumentIndex) return true;
+ return (
+ parameterIndex >= 0 &&
+ parameterIndex < argumentIndex &&
+ t.isRestElement(node.params[parameterIndex])
+ );
};
const rangeContains = (
diff --git a/tests/acceptance/applications/applicationWorkflowCli.test.ts b/tests/acceptance/applications/applicationWorkflowCli.test.ts
index 887835d0b..917dbc1b8 100644
--- a/tests/acceptance/applications/applicationWorkflowCli.test.ts
+++ b/tests/acceptance/applications/applicationWorkflowCli.test.ts
@@ -191,6 +191,54 @@ describe("application workflow CLI parity", () => {
}, 20_000);
});
+describe("rest parameter semantic trace CLI", () => {
+ it("traces each ordinary rest argument to its parameter through public application Evidence", async () => {
+ const root = await createTestTempDirectory("rea-rest-arguments-cli-");
+ await writeFile(
+ join(root, "app.js"),
+ "function collect(first, ...rest) { return rest; } collect('first', 'second', 'third');",
+ );
+ const application = await runCli([
+ "analyze-javascript-application",
+ root,
+ "--json",
+ ]);
+ const analyzed = await analyzeJavaScriptApplication({ input_path: root });
+ if (!analyzed.ok) throw analyzed.error;
+ const graph = javascriptApplicationAnalysisResultSchema.parse(
+ analyzed.value.normalized_result,
+ ).semantic_graph;
+ for (const index of [1, 2]) {
+ const argument = graph.nodes.find(
+ ({ kind, label }) =>
+ kind === "expression" && label === `argument ${String(index)}`,
+ );
+ if (argument === undefined) throw new Error("Missing argument node");
+ const traced = await runCli([
+ "trace-javascript-semantics",
+ JSON.stringify({
+ application,
+ query: {
+ seed: { kind: "semantic-node", node_id: argument.node_id },
+ direction: "forward-influence",
+ allowed_relations: ["argument-to-parameter"],
+ expected: { role: "sink", classes: ["parameter"] },
+ },
+ }),
+ "--json",
+ ]);
+ expect(traced).toMatchObject({
+ normalized_result: {
+ status: "found",
+ nodes: expect.arrayContaining([
+ expect.objectContaining({ kind: "parameter", label: "rest" }),
+ ]),
+ },
+ });
+ }
+ }, 20_000);
+});
+
describe("application workflow CLI input", () => {
it("rejects Evidence ID-only workflow inputs", async () => {
const result = await runCli([