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
+86 -24
View File
@@ -1,5 +1,9 @@
import type { JsonValue } from "./jsonValue.js";
import type { AddressedName } from "./hopperValues.js";
import {
classifySwiftSymbol,
type SwiftTypeCategory,
} from "./swiftSymbolClassification.js";
/** Select and deduplicate Objective-C class labels. */
export const discoverObjcClasses = (
@@ -34,50 +38,108 @@ export const discoverObjcProtocols = (
};
};
const SWIFT_CATEGORIES = [
["classes", "_TtC"],
["structs", "_TtV"],
["enums", "_TtO"],
["protocols", "_TtP"],
["extensions", "_TtE"],
] as const;
type SwiftTypeCategory = (typeof SWIFT_CATEGORIES)[number][0] | "other";
/** Categorize deduplicated Swift mangled symbols. */
/** Categorize procedure names using Swift manglings at their exact addresses. */
export const categorizeSwiftTypes = (
procedures: readonly AddressedName[],
filter: {
readonly category?: SwiftTypeCategory | undefined;
readonly pattern?: string | undefined;
} = {},
): JsonValue => {
const groups: Record<string, AddressedName[]> = Object.fromEntries(
[...SWIFT_CATEGORIES.map(([category]) => category), "other"].map(
symbols: readonly AddressedName[] = [],
) => {
const groups: Record<
string,
Array<{
address: string;
name: string;
mangled_names: string[];
}>
> = Object.fromEntries(
["classes", "structs", "enums", "protocols", "extensions", "other"].map(
(category) => [category, []],
),
);
const aliases = swiftAliasesByAddress(symbols);
const pattern = filter.pattern;
const seen = new Set<string>();
const unclassifiedNames = new Set<string>();
const unclassified: Array<{
address: string;
name: string;
mangled_names: string[];
reason: "category_not_decoded" | "conflicting_categories";
}> = [];
for (const entry of procedures) {
if (!entry.name.includes("_Tt") || seen.has(entry.name)) continue;
const category =
SWIFT_CATEGORIES.find(([, prefix]) =>
entry.name.startsWith(prefix),
)?.[0] ?? "other";
if (filter.category !== undefined && filter.category !== category) continue;
if (filter.pattern !== undefined && !entry.name.includes(filter.pattern))
if (seen.has(entry.name)) continue;
const mangledNames = [
...new Set([
...(classifySwiftSymbol(entry.name) === undefined ? [] : [entry.name]),
...(aliases.get(entry.address) ?? []),
]),
];
if (mangledNames.length === 0) continue;
if (
pattern !== undefined &&
![entry.name, ...mangledNames].some((name) => name.includes(pattern))
)
continue;
seen.add(entry.name);
groups[category]?.push(entry);
const kinds = new Set(
mangledNames.map((name) => classifySwiftSymbol(name)?.category ?? null),
);
const category = kinds.size === 1 ? ([...kinds][0] ?? null) : null;
const item = {
address: entry.address,
name: entry.name,
mangled_names: mangledNames,
};
if (category === null && !unclassifiedNames.has(entry.name)) {
unclassifiedNames.add(entry.name);
unclassified.push({
...item,
reason:
kinds.size > 1 && !kinds.has(null)
? "conflicting_categories"
: "category_not_decoded",
});
}
const bucket = category ?? "other";
if (filter.category === undefined || filter.category === bucket) {
seen.add(entry.name);
groups[bucket]?.push(item);
}
}
const categories = Object.fromEntries(
Object.entries(groups).map(([category, items]) => [
category,
{ count: items.length, items: items.map(toJsonEntry) },
{ count: items.length, items },
]),
);
return { total: seen.size, categories };
return {
total: Object.values(groups).reduce((sum, items) => sum + items.length, 0),
categories,
unclassified,
limitations: [
"Identification uses Swift mangled procedure names or same-address symbol aliases. Demangled names without retained Swift manglings cannot be identified. Counts deduplicate procedure names, not source declarations.",
...(unclassified.length === 0
? []
: [
"Some Swift categories could not be established from the retained manglings. Unclassified observations remain inline regardless of the category filter.",
]),
],
};
};
const swiftAliasesByAddress = (symbols: readonly AddressedName[]) => {
const aliases = new Map<string, string[]>();
for (const { address, name } of symbols) {
if (classifySwiftSymbol(name) === undefined) continue;
const existing = aliases.get(address);
if (existing === undefined) aliases.set(address, [name]);
else existing.push(name);
}
return aliases;
};
const uniqueByName = (entries: readonly AddressedName[]): AddressedName[] => {