Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demangle swift #62

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ function Runtime() {
numClasses = api.objc_getClassList(classHandles, numClasses);
for (let i = 0; i !== numClasses; i++) {
const handle = classHandles.add(i * pointerSize).readPointer();
const name = api.class_getName(handle).readUtf8String();
const rawName = api.class_getName(handle)
const name = tryDemangleSwift(rawName) || rawName.readUtf8String();
cachedClasses[name] = handle;
}
numCachedClasses = numClasses;
Expand Down Expand Up @@ -544,12 +545,14 @@ function Runtime() {
return cachedClass;
case "$className":
if (cachedClassName === null) {
let rawName = null;
if (superSpecifier)
cachedClassName = api.class_getName(superSpecifier.add(pointerSize).readPointer()).readUtf8String();
rawName = api.class_getName(superSpecifier.add(pointerSize).readPointer());
else if (isClass())
cachedClassName = api.class_getName(handle).readUtf8String();
rawName = api.class_getName(handle);
else
cachedClassName = api.object_getClassName(handle).readUtf8String();
rawName = api.object_getClassName(handle);
cachedClassName = tryDemangleSwift(rawName) || rawName.readUtf8String();;
}
return cachedClassName;
case "$moduleName":
Expand Down Expand Up @@ -1691,29 +1694,36 @@ function Runtime() {
const classHandle = classHandles.add(i * pointerSize).readPointer();

const rawName = classGetName(classHandle);
let name = null;
let name = tryDemangleSwift(rawName);

let modulePath = modules.findPath(rawName);
const possiblySwift = (modulePath === null) && (unfiltered || allModules.findPath(rawName) === null);
if (possiblySwift) {
name = rawName.readCString();
const probablySwift = name.indexOf('.') !== -1;
const swiftName = rawName.readCString();
const probablySwift = swiftName.indexOf('.') !== -1;
if (probablySwift) {
const nominalTypeDescriptor = classHandle.add(swiftNominalTypeDescriptorOffset).readPointer();
modulePath = modules.findPath(nominalTypeDescriptor);
name = name || swiftName;
}
}

if (modulePath !== null) {
if (name === null)
name = rawName.readUtf8String();
onMatch(name, modulePath);
onMatch(name || rawName.readUtf8String(), modulePath);
}
}

callbacks.onComplete();
}

function tryDemangleSwift (symbol) {
if (api.swift_demangle === undefined) return null;
const demangled = api.swift_demangle(symbol, api._platform_strlen(symbol), ptr(0), 0, 0);
const demangledString = demangled !== null ? demangled.readUtf8String() : null;
if (demangled !== null) api.free(demangled)
return demangledString;
}

function enumerateLoadedClassesSync(options = {}) {
const result = {};
enumerateLoadedClasses(options, {
Expand Down
16 changes: 15 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ function getApi() {
{
module: "libsystem_malloc.dylib",
functions: {
"free": ['void', ['pointer']]
"free": ['void', ['pointer']],
}
},
{
module: "libsystem_c.dylib",
functions: {
"_platform_strlen": ['size_t', ['pointer']]
}
}, {
module: "libobjc.A.dylib",
Expand Down Expand Up @@ -96,6 +102,14 @@ function getApi() {
this._dispatch_main_q = address;
}
}
}, {
module: "libswiftCore.dylib",
functions: {
"swift_demangle": ['pointer', ['pointer', 'size_t', 'pointer', 'size_t', 'uint32']]
},
optionals: {
"swift_demangle": 'ABI'
}
}
];
let remaining = 0;
Expand Down