public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Markus F.X.J. Oberhumer" <markus@oberhumer.com>
To: Linus Torvalds <torvalds@osdl.org>
Cc: linux-kernel@vger.kernel.org, Andi Kleen <ak@suse.de>
Subject: Re: [PATCH] i386: fix stack alignment for signal handlers
Date: Wed, 14 Sep 2005 01:30:37 +0200	[thread overview]
Message-ID: <4327611D.7@oberhumer.com> (raw)
In-Reply-To: <Pine.LNX.4.58.0509131542510.26803@g5.osdl.org>

Linus Torvalds wrote:
> 
> On Tue, 13 Sep 2005, Markus F.X.J. Oberhumer wrote:
> 
>>It seems that the current signal code always sets up a stack frame so that
>>signal handlers are run with a somewhat mis-aligned stack, i.e. (esp % 8 == 4).
> 
> Actually, not really.
> 
> They get entered with the stack pointer aligned, at least in my tests.
> 
> 	#include <stdio.h>
> 	#include <signal.h>
> 	#include <unistd.h>
> 	
> 	extern void handler(int);
> 	void *saved_esp;
> 	
> 	asm("handler: movl %esp,saved_esp; ret");
> 	
> 	int main(int argc, char **argv)
> 	{
> 	        signal(SIGALRM, handler);
> 	        alarm(1);
> 	        pause();
> 	        printf("%p\n", saved_esp);
> 	        return 0;
> 	}
> 
> always prints out an aligned address for me.
> 
> You seem to be expecting that the address be aligned "before the return 
> address push", which is a totally different thing. Quite frankly, I don't 
> know which one gcc prefers or whether there's an ABI specifying any 
> preferences.

I'm pretty sure that on both amd64 and i386 the alignment has to be 
_before_ the address push from the call, though I cannot find any exact ABI 
specs at the moment. Experts please advise.

What do you get when running this slightly modified version of your test 
program? My patch would fix the alignment of Aligned16 here.

And for amd64, please also see arch/x86_64/kernel/signal.c where 8 is 
subtracted from the get_stack() result. Actually I wonder if other archs 
might be affected as well...

~Markus


#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>

typedef struct { double x,y; } Aligned16 __attribute__((__aligned__(16)));

void *saved_esp;
void handler(int unused) {
         Aligned16 a;
         assert(__alignof(a) >= 16),
         saved_esp = (void *) &a;
}

int main()
{
         Aligned16 a;
         assert(__alignof(a) >= 16),
         printf("%p\n", &a);
         signal(SIGALRM, handler);
         alarm(1);
         pause();
         printf("%p\n", saved_esp);
         return 0;
}


-- 
Markus Oberhumer, <markus@oberhumer.com>, http://www.oberhumer.com/

  reply	other threads:[~2005-09-13 23:24 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-13 20:55 [PATCH] i386: fix stack alignment for signal handlers Markus F.X.J. Oberhumer
2005-09-13 22:53 ` Linus Torvalds
2005-09-13 23:30   ` Markus F.X.J. Oberhumer [this message]
2005-09-13 23:52     ` Linus Torvalds
2005-09-14  1:39       ` Markus F.X.J. Oberhumer
2005-09-14  4:54       ` Andi Kleen
2005-09-14 14:22       ` Daniel Jacobowitz
2005-09-14 14:55         ` Linus Torvalds
2005-09-14 15:44           ` Andi Kleen
2005-10-09 16:54             ` Markus F.X.J. Oberhumer
2005-10-09 16:57               ` Andi Kleen
2005-10-09 17:06                 ` Markus F.X.J. Oberhumer
2005-10-11  0:23                 ` Markus F.X.J. Oberhumer
2005-09-14 20:11     ` J.A. Magallon

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=4327611D.7@oberhumer.com \
    --to=markus@oberhumer.com \
    --cc=ak@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /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