5 ms·
There's a far simpler and also very generic method that I'm surprised no one has really mentioned much, although it's well-known in some corners of the RE/Asm c
by userbinator 4y ago
There's a far simpler and also very generic method that I'm surprised no one has really mentioned much, although it's well-known in some corners of the RE/Asm community and I believe some debuggers (but not gdb?) use it: scan the stack for values that look like valid code addresses, then disassemble backwards from there to see if they were actually return addresses written there by a call instruction. You will find a chain that leads back to the entry point.
Of course it won't work in edge cases like handwritten Asm that uses the stack in more clever ways, but when you're dealing with compiler output, it'll be fine. No need for all this complexity, and works in almost all cases.
- loeg 4y agoDo you analyze the dynamic loader segments (to determine code-like addresses) or just do some hard-coded approximations per platform? Pretty cute idea and I agree it will often work.
- userbinator 4y agoIt just needs to be on an executable page. The processor doesn't care about anything else, so this will work with things like JIT-generated code too. The key idea is that in essentially all cases of compiler-generated code, every return address will be immediately after the call that wrote it.
- loeg 4y agoYeah, but how do you look up executable page mappings? (Dynamic loader segments will have at least one executable Seg mapped somewhere.)
- userbinator 4y agoLinux: /proc/$pid/maps Windows: VirtualQuery() Mac: vm_region() Your own OS (like this article): you should know.
- loeg 4y agoOk, some platform specific mechanisms. Cool.
- the_mitsuhiko 4y agoThat method is pretty much guarnateed to give you completely broken stacks. We (Sentry) use it as a fallback if nothing else works, and the success rate is awful.
- loeg 4y agoPlease elaborate on what breaks.
- xuhu 4y agoProbably most valid code addresses on the stack are from older calls that are no longer part of the current call chain.
- userbinator 4y agoThere's a chance of that, but in practice it's easy to filter them out, since only one of the chains will be complete from the very top of the stack to the current stack pointer. The others will almost certainly be partially overwritten by other stack activity and incomplete, or not match the current stack pointer.
- the_mitsuhiko 4y agoThe act of stackwalking breaks. You often end up in completely nonsensical stacks halfway through and then all is lost.
- js2 4y agoThis has been a fallback method for Google Breakpad's stackwalker for more than a decade. It generates basically useless stack traces, at least for stacks captured from Android where you're trying to trace through NDK code invoked from the ART. Compounding the problem is a complete lack of available symbols for Android. https://github.com/google/breakpad/blob/main/docs/stack_walking.md https://github.com/google/breakpad/blob/main/docs/stack_walk... Also see recent discussion here: https://news.ycombinator.com/item?id=35438171 https://news.ycombinator.com/item?id=35438171
- saagarjha 4y agoTo be fair, doing backtrackes through JNI calls is generally pretty complicated.
- userbinator 4y agoNot exactly. I dug around in that code a little, and although it contains a disassembler, it's not being used for inspecting the call chain. All it does is check that the address appears to be inside a code section. https://github.com/google/breakpad/blob/main/src/processor/stackwalker.cc#L330 https://github.com/google/breakpad/blob/main/src/processor/s...
- js2 4y agoTrue, but that's because all it has is a snapshot of the stack to work with. The code isn't available to it.
- ShroudedNight 4y agoHow does this work in scenarios with heavy GOT (Global Offset Table) usage, trampolines, or where v-tables are prevalent?