public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "thomas at reactsoft dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/16660] attribute((aligned)) doesn't work for variables on the stack for greater than required alignement
Date: Wed, 06 Sep 2006 07:14:00 -0000	[thread overview]
Message-ID: <20060906071442.10667.qmail@sourceware.org> (raw)
In-Reply-To: <bug-16660-1824@http.gcc.gnu.org/bugzilla/>



------- Comment #5 from thomas at reactsoft dot com  2006-09-06 07:14 -------
(In reply to comment #3)
> Actually this is just a missed diagnostic.  The compiler cannot align the stack
> variables where the alignment is greater than stack alignment that the compiler
> can give for the stack.

The least GCC could and should do then is warn about it...

If the code is not very complex, the alignment appears to work, though. But as
soon as the code becomes complex, GCC screwes the alignment and even
accesses variables that don't even exist (I'll go into detail later).

Basically code like this is affected (this is *NOT* a test case, I'm going to
post a test case as soon as I get it to work):

typedef struct _somestruct {
    int a;
};

static void checkstruct (volatile struct _somestruct *palignedvar)
{
    if ((size_t)palignedvar & 0xF)
        printf("structure misaligned!\n");
}

void somefunc(int a, int b, int c) {
    __attribute__((aligned (16))) volatile struct _somestruct alignedvar;

    while (1)
    {
        /* [other code] */
        if (a) {
            if (c) {
                /* [other code] */
                alignedvar.a = c;
                checkstruct(&alignedvar);
            } else {
                /* [other code] */
                break;
            }
        } else {
            if (b) {
                /* [other code] */
                alignedvar.a = a;
                checkstruct(&alignedvar);
            } else {
                if (c) {
                    break;
                } else {
                    /* [other code] */
                    alignedvar.a = a;
                    checkstruct(&alignedvar);
                }
            }
        }
        /* [other code] */
    }
}

I analyzed the generated assembly code. GCC reserves an area big enough to hold
the structure plus padding, so it can align the structure dynamically at
runtime. It stores a pointer to the reserved area and a pointer to the
structure within the area. As long as the code is simple, GCC uses the pointer
to the structure to access the data. However, if the code is complex enough,
GCC mistakenly uses the pointer to the reserved area - which of course is
sometimes not properly aligned. As a result, also the data of the structure
members are read/write incorrectly.

the stack is organized like this (the order may not match as showed in this
abstracted example):

struct {
    void *reserved_area;     /* this is the pointer GCC sometimes accidently
grabs */
    void *aligned_structure; /* this is the pointer GCC should always grab */

    char reserved[sizeof(structure) + sizeof(padding)];
};

I encountered this bug with -O3, I don't know if GCC also generates broken code
without optimizations. I tried to create a simple test case that triggers the
problem, but I failed. I'm going to do that in the next few days.


-- 

thomas at reactsoft dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thomas at reactsoft dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16660


  parent reply	other threads:[~2006-09-06  7:14 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-16660-1824@http.gcc.gnu.org/bugzilla/>
2005-11-06  6:11 ` [Bug target/16660] attribute((aligned)) doesn't work for variables on the stack pinskia at gcc dot gnu dot org
2006-03-18 17:43 ` pinskia at gcc dot gnu dot org
2006-03-18 23:43 ` [Bug middle-end/16660] attribute((aligned)) doesn't work for variables on the stack for greater than required alignement sabre at nondot dot org
2006-09-06  7:14 ` thomas at reactsoft dot com [this message]
2007-01-13  1:58 ` pinskia at gcc dot gnu dot org
2007-10-03 22:04 ` hjl at lucon dot org
2007-10-03 22:08 ` pinskia at gcc dot gnu dot org
2007-10-03 22:17 ` hjl at lucon dot org
2007-10-03 22:27 ` hjl at lucon dot org
2007-10-03 22:41 ` pinskia at gcc dot gnu dot org
2007-10-04  0:02 ` hjl at lucon dot org
2008-01-03 18:01 ` pinskia at gcc dot gnu dot org
2008-07-31  1:05 ` hjl dot tools at gmail dot com
2008-07-31  1:07 ` pinskia at gcc dot gnu dot org
2009-03-05 14:03 ` hjl dot tools at gmail dot com
2009-04-16  9:22 ` nospamname at web dot de
2009-08-20  8:49 ` lessen42+gcc at gmail dot com
     [not found] <bug-16660-4@http.gcc.gnu.org/bugzilla/>
2010-10-01 23:04 ` pinskia at gcc dot gnu.org
2013-02-15  0:52 ` brooks at gcc dot gnu.org
2013-02-15  1:07 ` pinskia at gcc dot gnu.org
2013-02-15  1:12 ` brooks at gcc dot gnu.org
2014-02-05  8:40 ` kugel at rockbox dot org
2014-02-05  8:54 ` kugel at rockbox dot org
2014-02-16 10:03 ` jackie.rosen at hushmail dot com
2021-12-23  5:57 ` pinskia at gcc dot gnu.org

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=20060906071442.10667.qmail@sourceware.org \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).