Don’t touch that stack pointer!

A while ago I read a post by Raymond Chen about abusing the stack pointer. He equated it to “playing with fire”.

Every since AMX Mod X began using GWM Vissers’s Linux JIT for Small/AMX, we experienced random crashes on Linux. No one really knew why except that it seemed to be far more problematic depending on the pingboost level. Finally, about a week ago, Jussi Kivilinna noted that the JIT swapped the stack pointer. On Linux pthreads, he said, the stack pointer is used to determine the current thread id. Ouch.

Why does the JIT do this? Efficiency. The job of the JIT is to, as closely as possible, turn the virtual AMX machine into native execution. So logically it swaps the internal “AMX stack” for the real stack, then exchanges them again to return to the caller. However, there can be a lot of execution in between these swaps, so it’s quite conceivable these random crashes occurred because the OS was returning garbage or non-existant data for the stack pointer.

It took about 10-12 hours of editing and testing to fix this, at the loss of a bit of optimization (I’m sure more bugs will crop up before the next release). The lesson here is: Do not play with the stack pointer unless you have a very good reason! Whether or not the JIT was right to do this is another story (as it worked fine on Windows, and the JIT didn’t even support NASM until a few months ago), but the quirky behaviour of x86 anyway should deter people from doing it at all.

Whether this will actually stop the random crashing in Linux, only time will tell. (If you’re wondering how this is relevant, remember that SourceMod will have Small/Pawn support).

Leave a Reply

You must be logged in to post a comment.