apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Trawick <traw...@attglobal.net>
Subject Re: [PATCH] fix alignment on shared memory
Date Fri, 01 Mar 2002 18:37:27 GMT
Aaron Bannert <aaron@clove.org> writes:

> On Fri, Mar 01, 2002 at 12:34:49PM -0500, Jeff Trawick wrote:
> > 
> >     printf("%d %d %d %d\n",
> >            offsetof(struct s1,b),
> >            offsetof(struct s2,b),
> >            offsetof(struct s3,b),
> >            offsetof(struct s4,b));
> > 
> >     s3 = (struct s3 *)((char *)malloc(100) + 4);
> 
> Of course it gives us a SIGBUS here, this is guaranteed on a sparc.
> I'm not entirely sure what you're trying to illustrate here though.

it doesn't give a SIGBUS here....   It gives a SIGBUS when we try to
store s3->b.

> 
> >     printf("%p\n",s3);
> >     s3->a = 'A';
> >     s3->b = 10241024;
> 
> Shouldn't this be:  s3->b = 10241024LL; ?
irrelevant
> 
> >     return 0;
> > }
> > 
> > On my sparc machine it prints
> > 1 4 8 8
> > for the offsets of the variables of different types.  Note that gcc
> > places long long and double on 64-bit boundaries.
> > 
> > It then dies with SIGBUS when I trick it into trying to store a
> > 64-bit quantity into storage which s 32-bit aligned but not 64-bit
> > aligned.
> 
> As expected.
> 
> > Back to the code above that you posted:
> > 
> > I think that it too needs to take into account alignment.  It would
> > need to round the size of each structure up to 64-bit alignmet before
> > multiplying to ensure that objects of any type can live in that
> > storage.  This currently isn't causing problems because the size of
> > each structure is consistent with 64-bit alignment (208 bytes for
> > worker_score, 24 bytes for global_score, 16 bytes for process_score),
> > at least in a 32-bit build for Sparc.
> 
> Yes, I completely agree that each structure needs to be 64-bit aligned.
> I don't see how the shared memory code itself is incorrect.

It is incorrect because it is returning addresses that aren't 64-bit
aligned.

This code is in error:

        /* metadata isn't usable */
        new_m->usable = (char *)new_m->base + sizeof(apr_size_t);

given that new_m->usable is returned to the application as storage
that it can use.

My test program was intended to show a SIGBUS where it does the same
thing that Apache is doing when it gets a SIGBUS.  In my test
program's case, I added 4 to the result of malloc() to get storage
which wasn't aligned.  In Apache's case, the code above to set
new_m->usable is doing essentially the same thing (adding 4 to a
properly-aligned value resulting in a storage address which is not
properly aligned).

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Mime
View raw message