3 ms·
Sticking a Hand Through Time: Adventures on the call stack
- skibz 12y agoquite an insightful article. and, no, i do now understand frame pointers. ;)
- patrickmay 12y agoNeat trick, but still an example of Greenspan's 10th ("Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp."). The Common Lisp condition system (http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html http://www.gigamonkeys.com/book/beyond-exception-handling-co...) is worth understanding, even if you're developing in other languages. It's an elegant example of how to manipulate the call stack. Unfortunately, it's hard to implement in most other languages.
- webkike 12y agoI've always wondered: if I implement Common Lisp in C or Fortran, what does that say about Common Lisp?
- pkhuong 12y agoThere is no special call stack manipulation in the CL condition system. It's built on top of dynamic scoping and normal catch/throw unwinds; see http://www.pipeline.com/~hbaker1/MetaCircular.html http://www.pipeline.com/~hbaker1/MetaCircular.html for block/return-from on top of catch/throw.
- patrickmay 12y agoIndeed, but the Common Lisp condition system exposes it and wraps it elegantly.
- hyc_symas 12y agoHe calls it a C trick but the actual trick is implemented in asm. It would be a cool trick if he did it purely in C. Especially today, "all the world's not x86". (And I used to be a gcc maintainer for m68k and i860. I'm quite familiar with frame pointers and their non-use.) Btw: you can do this trick without resorting to assembly - start by taking the address of a local variable.
- webkike 12y agoThat depends entirely on the calling convention - potentially you can take two words down from the first argument, but that is no guarantee, and would not suffice for the didactic goals this article attempts to accomplish.
- thisjepisje 12y agoWhat does the mentioned switch statement do?
- webkike 12y agoIt is equivalent to: if (x == 1 || x == 2 || x == 3 || x == 4) printf("What the fuck?");
- Someone 12y agoMy guess would be that it triggers a compiler error because of the missing braces {}. However, reading http://en.cppreference.com/w/c/language/switch http://en.cppreference.com/w/c/language/switch and https://msdn.microsoft.com/en-us/library/66k51h7a.aspx https://msdn.microsoft.com/en-us/library/66k51h7a.aspx, I am starting to wonder about that. Both pages claim that switch(expression) can be followed by any statement, not just compound ones. That seems to make it legal C. It also would make switch(i) x += 1; and possibly even switch(i) foo: x += 1; valid (but nonsensical) C. Chances are that the standard contains special wording that prohibits the latter two, though. If not, this is useful to know for the obfuscated C contest (more so if you can jump to that label)
- webkike 12y agoIt does not, GCC compiles the example perfectly fine.
- PaulHoule 12y agoReminds me of the good old days programming in TCL.
- tptacek 12y agoWorth knowing that this also describes some of the mechanics of a relatively common C vulnerability, and a tactic used by some content protection schemes.