public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Haley <aph@redhat.com>
To: Igor Bogomazov <ygrex@ygrex.ru>
Cc: libffi-discuss@sourceware.org
Subject: Re: return value buffer malloc()'ed vs alloca()'ed
Date: Tue, 08 Oct 2013 17:33:00 -0000	[thread overview]
Message-ID: <525441E9.7080707@redhat.com> (raw)
In-Reply-To: <20131008101354.43baeeeb@ygrex-mac>

On 10/08/2013 06:13 PM, Igor Bogomazov wrote:
> Hello,
> 
> I've been trying to investigate valgrind warnings for a while and found
> an undocumented feature, please let me know if it is well-known.
> 
> What I did.
> 
> I modified a code given in «2.2 Simple Example» so that return value
> (rc), originally declared as (int), became an (int *)malloc(sizeof(int))
> so that it is resident in heap since that. Of cource, (&rc) replaced
> with (rc) later in the code.
> 
> What I get.
> 
> valgrind complaints about «Invalid write of size 8» while «Address
> 0x55ec040 is 0 bytes inside a block of size 4 alloc'd», it is exactly
> that allocated (rc) buffer.
> 
> Notes.
> 
> Allocating buffer for the return value using alloca() does the trick and
> makes valgrind silent.
> 
> Further.
> 
> I looked at x86/unix64.S, it is exactly the line:
> movq	%rax, (%rdi)
> that causes the valgrind's warning (at .Lst_uint32)
> 
> That is my question: is it necessary to allocate a buffer for the return
> value with alloca() and never with malloc()?

As far as I can see, libffi always writes a whole word into the rvalue:

.Lst_uint8:
	movzbq	%al, %rax
	movq	%rax, (%rdi)
	ret
	.align 2
.Lst_sint8:
	movsbq	%al, %rax
	movq	%rax, (%rdi)
	ret
	.align 2
.Lst_uint16:
	movzwq	%ax, %rax
	movq	%rax, (%rdi)
	.align 2
        ...

This looks quite deliberate, but it is rather different from what the
documentation specifies:

     RVALUE is a pointer to a chunk of memory that will hold the result
     of the function call.  This must be large enough to hold the
     result and must be suitably aligned; it is the caller's
     responsibility to ensure this

So it's definitely a bug, but I don't know whether it's a bug in libffi or
in its documentation.

Andrew.

      parent reply	other threads:[~2013-10-08 17:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-08 17:14 Igor Bogomazov
2013-10-08 17:30 ` Anthony Green
2013-10-08 17:36   ` Andrew Haley
2013-10-08 17:40     ` Anthony Green
2013-10-08 17:45       ` Andrew Haley
2013-10-09  5:09         ` Igor Bogomazov
2013-11-15 16:18       ` Broken tests in libffi testsuite Andrew Haley
2013-11-16 11:24         ` Alan Modra
2013-10-08 17:33 ` Andrew Haley [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=525441E9.7080707@redhat.com \
    --to=aph@redhat.com \
    --cc=libffi-discuss@sourceware.org \
    --cc=ygrex@ygrex.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).