Question

I stumbled upon an assembly programming challenge where I need to find why the following code gives a Bus Error when trying to run it. After much googling, I still can't figure out why.. My understanding of assembly x86 not great, any tips on finding the solution would be very appreciated.

Here is the code:

#include <stdlib.h>
int main(void) {
  asm("pushf\n"
      "orl $ 0x40000, (%esp)\n"
      "popf\n");

  *((int*) (((char*) malloc(5)) + 1)) = 23; // This line causes the Bus Error


  return 0;
}
Was it helpful?

Solution

Essentially you are setting a flag in the flags register. Flag 0x40000, aka bit 18 which according to http://en.wikipedia.org/wiki/FLAGS_register_%28computing%29 is

18 AC Alignment check (486SX+ only) X

If you search for "flag alignment check" you find amongst others:

http://forum.soft32.com/linux2/Turn-x86-Alignment-Check-ftopict12003.html

I hope this sets you on the right track. But do you really have a 486SX?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top