fix(ghidra): classify modern Swift manglings in swift type analysis (#1208)

* fix(ghidra): classify modern Swift manglings in swift type analysis

Categorize modern Swift 4.2+ manglings ($s, _$s) instead of dropping them
in analyze_swift_types. The classifier only accepted the legacy _Tt marker,
so procedure names reported by current providers produced an empty inventory.
Legacy _Tt families and behavior are unchanged. Fixes #1187.

* fix(swift): retain provider aliases and category unknowns

---------

Co-authored-by: Saim Shafique <sx4imm@gmail.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Saim Shafique
2026-10-09 16:10:55 +08:00
committed by GitHub
co-authored by Saim Shafique morluto
parent 04a516b1e3
commit 6935dc7fe1
13 changed files with 968 additions and 31 deletions
@@ -0,0 +1,149 @@
import { afterEach, describe, expect, it } from "vitest";
import { EnhancedTools } from "../../../src/application/EnhancedTools.js";
import {
AnalysisArtifactChangedError,
AnalysisCapabilityUnavailableError,
AnalysisOutputError,
} from "../../../src/domain/analysisErrorCore.js";
import { err } from "../../../src/domain/result.js";
import { observed as ok } from "../../fixtures/analysisExecution.js";
import { GHIDRA_SWIFT_INVENTORY } from "../../fixtures/swift/ghidraInventory.js";
import {
closeEnhancedToolResources,
connect,
jsonResult,
} from "./enhancedToolsHarness.js";
afterEach(closeEnhancedToolResources);
describe("Swift classification through MCP", () => {
it.each([
{ arguments: {}, total: 6 },
{ arguments: { category: "structs" }, total: 5 },
{ arguments: { category: "structs", pattern: "Pair" }, total: 3 },
{ arguments: { pattern: "_$s4main4PairVMa" }, total: 1 },
])("classifies golden Ghidra observations with $arguments", async (test) => {
const client = await connect({
execute: async (operation) =>
ok(
operation === "list_procedures"
? GHIDRA_SWIFT_INVENTORY.procedures
: GHIDRA_SWIFT_INVENTORY.symbols,
),
});
const result = await client.callTool({
name: "analyze_swift_types",
arguments: test.arguments,
});
expect(result.isError).not.toBe(true);
expect(jsonResult(result)).toMatchObject({ total: test.total });
if (test.arguments.pattern === undefined)
expect(jsonResult(result)).toMatchObject({
unclassified: [
{
name: "main::$swiftIndirect",
reason: "category_not_decoded",
mangled_names: ["_$s4main13swiftIndirectySiAA7Scoring_p_SitF"],
},
],
});
else expect(jsonResult(result)).toMatchObject({ unclassified: [] });
});
it("keeps provider names and supporting aliases inline", async () => {
const client = await connect({
execute: async (operation) =>
ok(
operation === "list_procedures"
? GHIDRA_SWIFT_INVENTORY.procedures
: GHIDRA_SWIFT_INVENTORY.symbols,
),
});
expect(
jsonResult(
await client.callTool({
name: "analyze_swift_types",
arguments: { category: "structs", pattern: "typeMetadataAccessor" },
}),
),
).toMatchObject({
total: 2,
categories: {
structs: {
items: [
{
address: "0x100000ab4",
name: "main::Pair::typeMetadataAccessor",
mangled_names: ["_$s4main4PairVMa"],
},
{
address: "0x100000ac4",
name: "main::Scorer::typeMetadataAccessor",
mangled_names: ["_$s4main6ScorerVMa"],
},
],
},
},
unclassified: [],
});
});
it("retains known facts when a provider has no symbol inventory", async () => {
const client = await connect({
execute: async (operation) =>
operation === "list_procedures"
? ok([{ address: "0x1", value: "$s4main4PairVMa" }])
: err(
new AnalysisCapabilityUnavailableError(
"fixture",
operation,
"no symbols",
),
),
});
const result = await client.callTool({
name: "analyze_swift_types",
arguments: { category: "structs" },
});
expect(result.isError).not.toBe(true);
expect(jsonResult(result)).toMatchObject({
total: 1,
symbol_inventory_error: { code: "capability_unavailable" },
limitations: expect.arrayContaining([
expect.stringContaining("symbol inventory is unavailable"),
]),
});
});
it.each([
new AnalysisOutputError("list_names", "malformed symbols"),
new AnalysisArtifactChangedError(
"list_names",
"/fixture",
"fixture changed",
),
])("preserves symbol-inventory failures: $_tag", async (failure) => {
const tools = new EnhancedTools({
execute: async (operation) =>
operation === "list_procedures"
? ok([{ address: "0x1", value: "$s4main4PairVMa" }])
: err(failure),
});
expect(await tools.execute("analyze_swift_types", {})).toEqual(
err(failure),
);
});
it("returns cancellation during the symbol join", async () => {
const controller = new AbortController();
const tools = new EnhancedTools({
execute: async (operation) => {
if (operation === "list_names") controller.abort();
return ok([{ address: "0x1", value: "$s4main4PairVMa" }]);
},
});
expect(
await tools.execute("analyze_swift_types", {}, controller.signal),
).toMatchObject({ ok: false, error: { _tag: "AnalysisCancelledError" } });
});
});
@@ -0,0 +1,10 @@
protocol Scoring { func score(_ x: Int) -> Int }
struct Pair { var a: Int; var b: Int }
struct Scorer: Scoring {
@inline(never) func score(_ x: Int) -> Int { x + 29 }
}
@inline(never)
func swiftIndirect(_ s: any Scoring, _ x: Int) -> Int { s.score(x) }
@inline(never)
func swiftPair(_ p: Pair) -> Int { p.a + 3 * p.b }
print(swiftIndirect(Scorer(), CommandLine.arguments.count) + swiftPair(Pair(a: 1, b: 2)))
+281
View File
@@ -0,0 +1,281 @@
// Golden projection of Ghidra 12.1.4 list_procedures/list_names on macOS arm64.
// Source: tests/conformance/swift/type-inventory.swift, compiled with swiftc -O -g.
// Includes selected procedure entries, all their symbols, and an off-entry metadata symbol.
export const GHIDRA_SWIFT_INVENTORY = {
procedures: [
{
address: "0x100000858",
value: "entry",
procedure: {
external: false,
thunk: false,
thunk_target: null,
},
},
{
address: "0x100000954",
value: "main::Pair::init",
procedure: {
external: false,
thunk: false,
thunk_target: null,
},
},
{
address: "0x100000958",
value: "main::$swiftIndirect",
procedure: {
external: false,
thunk: false,
thunk_target: null,
},
},
{
address: "0x1000009bc",
value: "main::Pair::set_a",
procedure: {
external: false,
thunk: false,
thunk_target: null,
},
},
{
address: "0x100000a28",
value: "main::Scorer::$score",
procedure: {
external: false,
thunk: false,
thunk_target: null,
},
},
{
address: "0x100000ab4",
value: "main::Pair::typeMetadataAccessor",
procedure: {
external: false,
thunk: false,
thunk_target: null,
},
},
{
address: "0x100000ac4",
value: "main::Scorer::typeMetadataAccessor",
procedure: {
external: false,
thunk: false,
thunk_target: null,
},
},
],
symbols: [
{
address: "0x100000858",
value: "_main",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000858",
value: "entry",
symbol: {
primary: true,
dynamic: false,
external: false,
type: "function",
source: "imported",
},
},
{
address: "0x100000954",
value: "_$s4main4PairV1a1bACSi_SitcfC",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000954",
value: "_$s4main4PairV1aSivM.resume.0",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000954",
value: "_$s4main4PairV1aSivg",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000954",
value: "_$s4main4PairV1bSivM.resume.0",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000954",
value: "_$s4main6ScorerVACycfC",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000954",
value: "main::Pair::init",
symbol: {
primary: true,
dynamic: false,
external: false,
type: "function",
source: "analysis",
},
},
{
address: "0x100000958",
value: "_$s4main13swiftIndirectySiAA7Scoring_p_SitF",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000958",
value: "main::$swiftIndirect",
symbol: {
primary: true,
dynamic: false,
external: false,
type: "function",
source: "analysis",
},
},
{
address: "0x1000009bc",
value: "_$s4main4PairV1aSivs",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x1000009bc",
value: "main::Pair::set_a",
symbol: {
primary: true,
dynamic: false,
external: false,
type: "function",
source: "analysis",
},
},
{
address: "0x100000a28",
value: "_$s4main6ScorerV5scoreyS2iFTf4nd_n",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000a28",
value: "main::Scorer::$score",
symbol: {
primary: true,
dynamic: false,
external: false,
type: "function",
source: "analysis",
},
},
{
address: "0x100000ab4",
value: "_$s4main4PairVMa",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000ab4",
value: "main::Pair::typeMetadataAccessor",
symbol: {
primary: true,
dynamic: false,
external: false,
type: "function",
source: "analysis",
},
},
{
address: "0x100000ac4",
value: "_$s4main6ScorerVMa",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
{
address: "0x100000ac4",
value: "main::Scorer::typeMetadataAccessor",
symbol: {
primary: true,
dynamic: false,
external: false,
type: "function",
source: "analysis",
},
},
{
address: "0x100000c0c",
value: "_$s4main4PairVMn",
symbol: {
primary: false,
dynamic: false,
external: false,
type: "label",
source: "imported",
},
},
],
};