Skip to content

Instantly share code, notes, and snippets.

@pookjw
Last active February 1, 2024 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pookjw/584ef26756f674a3b6ef9cba87812bb0 to your computer and use it in GitHub Desktop.
Save pookjw/584ef26756f674a3b6ef9cba87812bb0 to your computer and use it in GitHub Desktop.
#include "get_sp.h"
void *get_sp(void) {
return __builtin_frame_address(1);
}
#ifndef get_sp_h
#define get_sp_h
extern void *get_sp(void);
#endif /* get_sp_h */
import Darwin
class FooObject {
func foo() {
print(self)
foo_2()
}
}
func foo_2() {
let fooObject: FooObject = get_sp().load(fromByteOffset: -0x10, as: FooObject.self)
// void *buffer[2] (Heap)
let buffer: UnsafeMutableBufferPointer<UnsafeMutableRawPointer?> = .allocate(capacity: 2)
let count: Int32 = backtrace(buffer.baseAddress, 2)
assert(count == 2)
var info: Dl_info = .init()
dladdr(buffer[1], &info)
buffer.deallocate()
typealias FooType = @convention(c) (UnsafeRawPointer) -> Void
let foo = unsafeBitCast(info.dli_saddr, to: FooType.self)
foo(Unmanaged.passUnretained(fooObject).toOpaque())
}
FooObject().foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment