Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack alignment on x86

Tags:

linux

x86

gcc

sse

I had a mysterious bus error that occurred, on a x86 (32-bit) platform, when running code compiled with gcc-4.8.1 with -march=pentium4. I traced the problem to an SSE instruction:

movdqa %xmm5,0x50(%esp)

with esp = 0xbfffedac. movdqa requires the address to be 16-byte aligned, which is not the case here, thus the bus error.

The problem does not occur if compiling with -march=native (this is a Core-i3 processor).

As far as I know, the only stack alignment guaranteed on Linux/x86 is 4-byte. Thus, it seems weird that the code generator should choose to use movdqa, without some kind of alignment check, even though there is an instruction movdqu for possibly unaligned accesses.

So, this looks like there is a bug in gcc.

I'm not an expert on SSE and x86 ABI, and I'd appreciate feedback before I send a bug report.

like image 653
David Monniaux Avatar asked Feb 13 '14 07:02

David Monniaux


People also ask

What is stack alignment x86?

"Stack alignment" just means the address of the stack (SP or ESP) is a multiple of the machine word size (so always divisible by 8 for 64-bit mode, 4 for 32-bit, 2 for 16-bit).

Does x86 have a stack?

All x86 architectures use a stack as a temporary storage area in RAM that allows the processor to quickly store and retrieve data in memory. The current top of the stack is pointed to by the esp register.

What does stack alignment mean?

IIRC, stack alignment is when variables are placed on the stack "aligned" to a particular number of bytes. So if you are using a 16 bit stack alignment, each variable on the stack is going to start from a byte that is a multiple of 2 bytes from the current stack pointer within a function.

Where is the stack in x86?

On x86, the stack pointer is stored in the register called "rsp" (Register: Stack Pointer).


1 Answers

Now the default in gcc is -mpreferred-stack-boundary=4 (16-byte alignment), which sets -mincoming-stack-boundary=4.

Problems can thus occur if gcc code using SSE is called from code generated by other compilers which have different stack alignment assumptions, such as OCaml (see discussion on the OCaml bug tracker).

like image 56
David Monniaux Avatar answered Oct 09 '22 03:10

David Monniaux