public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Wim Lewis <wiml@hhhh.org>
To: libffi-discuss@sources.redhat.com
Cc: Anthony Green <green@redhat.com>
Subject: Re: Structure-return bug on powerpc32 (patch)
Date: Sat, 30 May 2009 01:41:00 -0000	[thread overview]
Message-ID: <20090530014135.GA4607@underhill.hhhh.org> (raw)
In-Reply-To: <D9BFDE0F-A847-4E44-AC7D-954B9FF930D2@hhhh.org>

[-- Attachment #1: Type: text/plain, Size: 671 bytes --]

Here's a patch which fixes the bug. It also fixes an unrelated
problem I noticed in that section of code, which was that the structure-
return code was clobbering cr3 and cr4, which are designated as callee-saved
in the ppc abi documentation I have. (I've never seen a compiler actually
use those fields, but still.) This involved moving some of the bitfields
in the flags word around.

With this patch, libffi-3.0.8 passes four of the five tests which were
failing on netbsd5/ppc32 (the fifth test failure is an unrelated problem),
as well as the odd_struct.c tests I linked to in my last message.

-- 
   Wim Lewis <wiml@hhhh.org>, Seattle, WA, USA. PGP keyID 27F772C1

[-- Attachment #2: smst_buffer_overrun.patch --]
[-- Type: text/plain, Size: 4358 bytes --]

diff -c --recursive libffi-3.0.8-pristine/ChangeLog libffi-3.0.8/ChangeLog
*** libffi-3.0.8-pristine/ChangeLog	Fri Dec 19 08:06:04 2008
--- libffi-3.0.8/ChangeLog	Fri May 29 13:01:49 2009
***************
*** 1,3 ****
--- 1,10 ----
+ 2009-05-29  Wim Lewis  <wiml@hhhh.org>
+ 
+ 	* src/powerpc/sysv.S (small_struct_return_value): Fix overrun of
+ 	return buffer for odd-size structs.
+ 	* src/powerpc/ffi.c, src/powerpc/sysv.S: Avoid clobbering cr3 and cr4,
+ 	which are supposed to be callee-saved.
+ 
  2008-12-18  Rainer Orth  <ro@TechFak.Uni-Bielefeld.DE>
  
  	PR libffi/26048
diff -c --recursive libffi-3.0.8-pristine/src/powerpc/ffi.c libffi-3.0.8/src/powerpc/ffi.c
*** libffi-3.0.8-pristine/src/powerpc/ffi.c	Wed Nov 12 11:46:10 2008
--- libffi-3.0.8/src/powerpc/ffi.c	Fri May 29 12:30:34 2009
***************
*** 43,53 ****
    FLAG_RETURNS_64BITS   = 1 << (31-28),
  
    FLAG_RETURNS_128BITS  = 1 << (31-27), /* cr6  */
! 
!   FLAG_SYSV_SMST_R4     = 1 << (31-16), /* cr4, use r4 for FFI_SYSV 8 byte
  					   structs.  */
!   FLAG_SYSV_SMST_R3     = 1 << (31-15), /* cr3, use r3 for FFI_SYSV 4 byte
  					   structs.  */
    FLAG_ARG_NEEDS_COPY   = 1 << (31- 7),
    FLAG_FP_ARGUMENTS     = 1 << (31- 6), /* cr1.eq; specified by ABI */
    FLAG_4_GPR_ARGUMENTS  = 1 << (31- 5),
--- 43,54 ----
    FLAG_RETURNS_64BITS   = 1 << (31-28),
  
    FLAG_RETURNS_128BITS  = 1 << (31-27), /* cr6  */
!   FLAG_SYSV_SMST_R4     = 1 << (31-26), /* use r4 for FFI_SYSV 8 byte
  					   structs.  */
!   FLAG_SYSV_SMST_R3     = 1 << (31-25), /* use r3 for FFI_SYSV 4 byte
  					   structs.  */
+   /* Bits (31-24) through (31-19) store shift value for SMST */
+ 
    FLAG_ARG_NEEDS_COPY   = 1 << (31- 7),
    FLAG_FP_ARGUMENTS     = 1 << (31- 6), /* cr1.eq; specified by ABI */
    FLAG_4_GPR_ARGUMENTS  = 1 << (31- 5),
***************
*** 685,698 ****
  	      if (size <= 4)
  		{
  		  flags |= FLAG_SYSV_SMST_R3;
! 		  flags |= 8 * (4 - size) << 4;
  		  break;
  		}
  	      /* These structs are returned in r3 and r4. See above.   */
  	      if  (size <= 8)
  		{
! 		  flags |= FLAG_SYSV_SMST_R4;
! 		  flags |= 8 * (8 - size) << 4;
  		  break;
  		}
  	    }
--- 686,699 ----
  	      if (size <= 4)
  		{
  		  flags |= FLAG_SYSV_SMST_R3;
! 		  flags |= 8 * (4 - size) << 8;
  		  break;
  		}
  	      /* These structs are returned in r3 and r4. See above.   */
  	      if  (size <= 8)
  		{
! 		  flags |= FLAG_SYSV_SMST_R3 | FLAG_SYSV_SMST_R4;
! 		  flags |= 8 * (8 - size) << 8;
  		  break;
  		}
  	    }
diff -c --recursive libffi-3.0.8-pristine/src/powerpc/sysv.S libffi-3.0.8/src/powerpc/sysv.S
*** libffi-3.0.8-pristine/src/powerpc/sysv.S	Tue Feb 26 11:01:53 2008
--- libffi-3.0.8/src/powerpc/sysv.S	Fri May 29 16:58:19 2009
***************
*** 136,165 ****
  	b	L(done_return_value)
  
  L(small_struct_return_value):
! 	mtcrf	0x10,%r31	/* cr3  */
! 	bt-	15,L(smst_one_register)
! 	mtcrf	0x08,%r31	/* cr4  */
! 	bt-	16,L(smst_two_register)
! 	b       L(done_return_value)
! 
! L(smst_one_register):
! 	rlwinm  %r5,%r31,5+23,32-5,31 /* Extract the value to shift.  */
! 	slw	%r3,%r3,%r5
! 	stw	%r3,0(%r30)
! 	b	L(done_return_value)
! L(smst_two_register):
! 	rlwinm  %r5,%r31,5+23,32-5,31 /* Extract the value to shift.  */
! 	cmpwi	%r5,0
! 	subfic	%r9,%r5,32
! 	slw	%r29,%r3,%r5
! 	srw	%r9,%r4,%r9
! 	beq-	L(smst_8byte)
! 	or	%r3,%r9,%r29
! 	slw	%r4,%r4,%r5
! L(smst_8byte):
! 	stw	%r3,0(%r30)
! 	stw	%r4,4(%r30)
  	b	L(done_return_value)
  
  .LFE1:
  END(ffi_call_SYSV)
--- 136,154 ----
  	b	L(done_return_value)
  
  L(small_struct_return_value):
! 	extrwi	%r6,%r31,2,19         /* number of bytes padding = shift/8 */
! 	mtcrf	0x02,%r31	      /* copy flags to cr[24:27] (cr6) */
! 	extrwi	%r5,%r31,5,19         /* r5 <- number of bits of padding */
! 	subfic  %r6,%r6,4             /* r6 <- number of useful bytes in r3 */
! 	bf-	25,L(done_return_value) /* struct in r3 ? if not, done. */
! /* smst_one_register: */
! 	slw	%r3,%r3,%r5           /* Left-justify value in r3 */
! 	mtxer	%r6                   /* move byte count to XER ... */
! 	stswx	%r3,0,%r30            /* ... and store that many bytes */
! 	bf+	26,L(done_return_value)  /* struct in r3:r4 ? */
! 	add	%r6,%r6,%r30          /* adjust pointer */
! 	stswi	%r4,%r6,4             /* store last four bytes */
  	b	L(done_return_value)
  
  .LFE1:
  END(ffi_call_SYSV)


  reply	other threads:[~2009-05-30  1:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-22 19:32 Structure-return bug on powerpc32 Wim Lewis
2009-05-22 19:55 ` Andreas Tobler
2009-05-22 22:32   ` Wim Lewis
2009-05-30  1:41     ` Wim Lewis [this message]
2009-06-01 19:45       ` Structure-return bug on powerpc32 (patch) Andreas Tobler
2009-06-01 19:58         ` Anthony Green
2009-06-04  7:01         ` Wim Lewis

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=20090530014135.GA4607@underhill.hhhh.org \
    --to=wiml@hhhh.org \
    --cc=green@redhat.com \
    --cc=libffi-discuss@sources.redhat.com \
    /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).