public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* libffi does not follow proper ABI on ia32 (?)
@ 2014-12-22 19:36 u-xsnf
  2014-12-24 13:58 ` showstopper bug on x86 (Re: libffi does not follow proper ABI on ia32) u-xsnf
  0 siblings, 1 reply; 14+ messages in thread
From: u-xsnf @ 2014-12-22 19:36 UTC (permalink / raw)
  To: libffi-discuss

Hello,

I am following the directions given on https://sourceware.org/libffi/
about submitting bug reports to the list.

Below you see excerpts from a letter posted to the pcc mailing list
based on a try to build libffi with pcc (the "Portable C Compiler")

This looks like libffi is not following the necessary ABI. Is there any
chance for this to be fixed, short of having to learn the internals of
libffi and submitting a patch?

Regards,
Rune

----- Forwarded message -----

Date: Sun, 21 Dec 2014 18:47:01 +0100
Subject: Re: [Pcc] libffi tests segfault with pcc (cvs 20141115 on i486)
To: pcc@lists.ludd.ltu.se

[skipped ...]

On Sat, Dec 20, 2014 at 05:40:27PM +0100, u-lxna@aetey.se wrote:
> res_dbl = ((cls_struct_12byte(*)(cls_struct_12byte, cls_struct_12byte))(code))(h_dbl, j_dbl);

> The relevant code generated by pcc looks like
>  ...
> 0x8048826 <main+430>:   call   *%eax                 <=== the call
> 0x8048828 <main+432>:   add    $0x18,%esp
> 0x804882b <main+435>:   mov    %eax,%esi
> 0x804882d <main+437>:   lea    -0x80(%ebp),%edi
> 0x8048830 <main+440>:   movsl  %ds:(%esi),%es:(%edi) <=== segfault

> (gdb) i r
> eax            0xd      13

> while with gcc (4.2.3) it is afaict
>  ...
> 0x080487ad <main+509>:  call   *0xec(%esp)           <=== the call
> 0x080487b4 <main+516>:  cld    
> 0x080487b5 <main+517>:  mov    0x34(%esp),%edi
> 0x080487b9 <main+521>:  mov    0x28(%esp),%esi
> 0x080487bd <main+525>:  mov    %ebx,%ecx
> 0x080487bf <main+527>:  rep movsl %ds:(%esi),%es:(%edi)
> 0x080487c1 <main+529>:  pop    %ebp
> 0x080487c2 <main+530>:  pop    %eax

In other words pcc is relying on the contents of %eax at the return
from the function call while gcc clearly ignores/overwrites that value.

https://en.wikipedia.org/wiki/X86_calling_conventions
says among others about "cdecl" ABI:
"
the caller allocates memory and passes a pointer to it as a hidden first
parameter; the callee populates the memory and returns the pointer
"

Which makes me wonder whether libffi conforms to the above in this case,
%eax on return is 13, which value is being used as the pointer
by pcc and crashes the program.

Gcc seems to remember where the memory area for the data to return is
and does not use the pointer "expected" to be returned by the callee.

Does this analysis look correct?

[skipped ...]

A half-hearted check seems to show that gcc actually makes the callee
return the pointer even though the caller never uses it.

[skipped ...]

So libffi might have to be fixed to provide the pointer too but the opinion
of its developers is yet to be determined.

[skipped ...]

----- End forwarded message -----

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2015-01-06 16:52 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-22 19:36 libffi does not follow proper ABI on ia32 (?) u-xsnf
2014-12-24 13:58 ` showstopper bug on x86 (Re: libffi does not follow proper ABI on ia32) u-xsnf
2014-12-24 16:31   ` Richard Henderson
2014-12-24 21:35     ` u-xsnf
2014-12-31 16:12     ` Richard Henderson
2015-01-02 18:57       ` u-xsnf
2015-01-02 22:20         ` Richard Henderson
2015-01-03 10:15           ` u-xsnf
2015-01-03 10:36             ` u-xsnf
2015-01-05 21:34             ` Richard Henderson
2015-01-06 10:21               ` u-xsnf
2015-01-06 12:46                 ` libffi vs pcc (was: showstopper bug on x86 / " u-xsnf
2015-01-06 16:20                 ` showstopper bug on x86 (Re: " Richard Henderson
2015-01-06 16:52                   ` u-xsnf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).