Created
January 23, 2019 11:53
-
-
Save lukaskollmer/8782527495b275844214781097bf59bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
do { | |
let libobjc = dlopen("/usr/lib/libobjc.A.dylib", RTLD_LAZY)! | |
let _objc_getClass = FFIFunctionInvocation(symbol: "objc_getClass", handle: libobjc, returnType: .pointer, parameterTypes: [.pointer]) | |
var text = "NSProcessInfo".utf8CString | |
var arg: UnsafePointer<CChar>! | |
text.withUnsafeBufferPointer { ptr in | |
arg = ptr.baseAddress! | |
} | |
_objc_getClass.setArgument(&arg, atIndex: 0) | |
var ptr: Int = _objc_getClass.invoke() | |
print("ptr", ptr) | |
let _class_getName = FFIFunctionInvocation(symbol: "class_getName", handle: libobjc, returnType: .pointer, parameterTypes: [.pointer]) | |
_class_getName.setArgument(&ptr, atIndex: 0) | |
let _classname: UnsafePointer<CChar> = _class_getName.invoke() | |
print("name", _classname) | |
puts(_classname) | |
} | |
*/ | |
let libobjc = dlopen("/usr/lib/libobjc.A.dylib", RTLD_LAZY)! | |
let _objc_getClass = FFIFunction( | |
symbol: "objc_getClass", | |
handle: libobjc, | |
returnType: .pointer, | |
parameterTypes: [.pointer] | |
) | |
var _NSProcessInfo: Int! | |
do { | |
var _NSProcessInfo_classname = "NSProcessInfo".utf8CString | |
var arg0: UnsafePointer<CChar>! | |
_NSProcessInfo_classname.withUnsafeBufferPointer { ptr in | |
arg0 = ptr.baseAddress! | |
} | |
_objc_getClass.setArgument(&arg0, atIndex: 0) | |
_NSProcessInfo = _objc_getClass.invoke() as Int | |
} | |
let _sel_getUid = FFIFunction( | |
symbol: "sel_getUid", | |
handle: libobjc, | |
returnType: .pointer, | |
parameterTypes: [.pointer], | |
isVariadic: false | |
) | |
func get_sel(_ name: String) -> Int { | |
var bytes = name.utf8CString | |
var arg0: UnsafePointer<CChar>! | |
bytes.withUnsafeBufferPointer { ptr in | |
arg0 = ptr.baseAddress! | |
} | |
_sel_getUid.setArgument(&arg0, atIndex: 0) | |
return _sel_getUid.invoke() as Int | |
} | |
let msgSend_id = FFIFunction( | |
symbol: "objc_msgSend", | |
handle: libobjc, | |
returnType: .pointer, | |
parameterTypes: [.pointer, .pointer], | |
isVariadic: true | |
) | |
func msgSend<T>(target: inout Int, sel sel_name: String, args: inout [Int]) -> T { | |
var sel = get_sel(sel_name) | |
msgSend_id.setArgument(&target, atIndex: 0) | |
msgSend_id.setArgument(&sel, atIndex: 1) | |
for (index, var arg) in args.enumerated() { | |
msgSend_id.setArgument(&arg, atIndex: index + 2) | |
} | |
return msgSend_id.invoke() | |
} | |
var args: [Int] = [] | |
var processInfo: Int = msgSend(target: &_NSProcessInfo, sel: "processInfo", args: &args) | |
var processName_ptr: Int = msgSend(target: &processInfo, sel: "processName", args: &args) | |
let processName = unsafeBitCast(processName_ptr, to: NSString.self) | |
print(processName) | |
fatalError() | |
let fn = FFIFunction( | |
symbol: "strlen", | |
handle: nil, | |
returnType: FFIType.int64, | |
parameterTypes: [.pointer], | |
isVariadic: false | |
) | |
let foo = "Hello World".utf8CString | |
var arg0: UnsafePointer<CChar>! | |
foo.withUnsafeBufferPointer { ptr in | |
arg0 = ptr.baseAddress | |
} | |
fn.setArgument(&arg0, atIndex: 0) | |
let length: size_t = fn.invoke() | |
print(length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment