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:
co-authored by
Saim Shafique
morluto
parent
04a516b1e3
commit
6935dc7fe1
@@ -0,0 +1,70 @@
|
||||
const LEGACY_CATEGORIES = [
|
||||
["_TtC", "classes"],
|
||||
["_TtV", "structs"],
|
||||
["_TtO", "enums"],
|
||||
["_TtP", "protocols"],
|
||||
["_TtE", "extensions"],
|
||||
] as const;
|
||||
|
||||
/** Supported procedure categories derived from Swift symbol encodings. */
|
||||
export type SwiftTypeCategory = (typeof LEGACY_CATEGORIES)[number][1] | "other";
|
||||
|
||||
const NOMINAL_CATEGORIES: ReadonlyMap<string, SwiftTypeCategory> = new Map([
|
||||
["C", "classes"],
|
||||
["V", "structs"],
|
||||
["O", "enums"],
|
||||
["P", "protocols"],
|
||||
]);
|
||||
|
||||
/**
|
||||
* Recognize Swift manglings and decode literal module/nominal contexts.
|
||||
* Substitutions, generic contexts, and other encodings remain unknown.
|
||||
*/
|
||||
export const classifySwiftSymbol = (
|
||||
name: string,
|
||||
): { readonly category: SwiftTypeCategory | null } | undefined => {
|
||||
const prefix = /^_?(?:\$[sS]|_T0)/u.exec(name);
|
||||
if (prefix === null) {
|
||||
if (!name.includes("_Tt")) return undefined;
|
||||
return {
|
||||
category:
|
||||
LEGACY_CATEGORIES.find(([prefix]) => name.startsWith(prefix))?.[1] ??
|
||||
null,
|
||||
};
|
||||
}
|
||||
const mangling = name.slice(prefix[0].length);
|
||||
let offset = identifierEnd(mangling, 0);
|
||||
if (offset === undefined) return { category: null };
|
||||
let category: SwiftTypeCategory | null = null;
|
||||
for (;;) {
|
||||
const end = identifierEnd(mangling, offset);
|
||||
if (end === undefined) break;
|
||||
const kind = mangling.charAt(end);
|
||||
const nominal = NOMINAL_CATEGORIES.get(kind);
|
||||
if (nominal === undefined) {
|
||||
if (kind === "E" && category !== null) category = "extensions";
|
||||
break;
|
||||
}
|
||||
category = nominal;
|
||||
offset = end + 1;
|
||||
}
|
||||
return { category };
|
||||
};
|
||||
|
||||
const identifierEnd = (
|
||||
mangling: string,
|
||||
offset: number,
|
||||
): number | undefined => {
|
||||
const lengthToken = /^[1-9][0-9]*/u.exec(mangling.slice(offset))?.[0];
|
||||
if (lengthToken === undefined) return undefined;
|
||||
const start = offset + lengthToken.length;
|
||||
const end = start + Number(lengthToken);
|
||||
// Swift identifiers with substitutions/punycode need a fuller decoder.
|
||||
if (
|
||||
!Number.isSafeInteger(end) ||
|
||||
end > mangling.length ||
|
||||
!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(mangling.slice(start, end))
|
||||
)
|
||||
return undefined;
|
||||
return end;
|
||||
};
|
||||
Reference in New Issue
Block a user