pow() fails with a segmentation fault when executed from dynamically allocated memory

Carlos O'Donell carlos@systemhalted.org
Sat Jan 28 15:46:00 GMT 2012


On Sat, Jan 28, 2012 at 7:35 AM, Lars Magnusson <lavima@gmail.com> wrote:
> The code is just for testing. The real situation is that we have a
> system that generates millions of different programs in ML, which then
> is compiled into assembly by a custom lightweight compiler. I can
> provide you with more details if necessary.
>
> The assembly is:
> mov r11, address of my_pow
> call r11
> ret

In the future please provide a *complete* test case or someone (not
me, because I'm always nice) will be upset with you :-)

Given your use of r11 I *assume* you are using x86-64 ABI.

Your assembly violates the x86_64 procedure calling ABI by not saving
%rbp and that leaves %rsp misaligned.

You must push %rbp to keep the stack 16-byte aligned, otherwise pow's
use of movapd uses an unaligned stack and that causes a general
protection fault. You might have gotten lucky in the past, but your
assembly is incorrect.

I suggest the following:
~~~
push %rbp
mov %rsp, %rbp
...
leaveq
retq
~~~

Cheers,
Carlos.



More information about the Libc-help mailing list