Last active
February 1, 2024 12:35
-
-
Save pookjw/584ef26756f674a3b6ef9cba87812bb0 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
#include "get_sp.h" | |
void *get_sp(void) { | |
return __builtin_frame_address(1); | |
} |
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
#ifndef get_sp_h | |
#define get_sp_h | |
extern void *get_sp(void); | |
#endif /* get_sp_h */ |
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
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