title: How to debug stack-overwriting errors with Valgrind?
body: <p>I just spent some time chasing down a bug that boiled down to the following.  Code was erroneously overwriting the stack, and I think it wrote over the return address of the function call. Following the return, the program would crash and stack would be corrupted. Running the program in valgrind would return an error such as:</p>
  
  <pre><code>vex x86-&gt;IR: unhandled instruction bytes: 0xEA 0x3 0x0 0x0
  ==9222== valgrind: Unrecognised instruction at address 0x4e925a8.
  </code></pre>
  
  <p>I figure this is because the return jumped to a random location, containing stuff that were not valid x86 opcodes.  (Though I am somehow suspicious that this address 0x4e925a8 happened to be in an executable page.  I imagine valgrind would throw a different error if this wasn't the case.) </p>
  
  <p>I am certain that the problem was of the stack-overwriting type, and I've since fixed it. Now I am trying to think how I could catch errors like this more effectively.  Obviously, valgrind can't warn me if I rewrite <em>data</em> on the stack, but maybe it can catch when someone writes over a return address on the stack.  In principle, it can detect when something like 'push EIP' happens (so it can flag where the return addresses are on the stack).</p>
  
  <p>I was wondering if anyone knows if Valgrind, or anything else can do that? If not, can you comment on other suggestions regarding debugging errors of this type efficiently.</p>