Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* unaligned traps question
@ 2002-10-15  0:50 Ram Viswanadha
  2002-10-15  1:38 ` Michal Jaegermann
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Ram Viswanadha @ 2002-10-15  0:50 UTC (permalink / raw)
  To: axp-list

Hi everyone,
ICU (http://oss.software.ibm.com/icu/) libraries when compiled with stock RH
6.2 kernel and GCC used to produce unaligned traps in some of the tools. I
upgraded to RH 7.2 (stock kernel and GCC) and now I am not seeing any
unaligned trap messages.
I read Rich Payne's mails in the archives and they suggest that the problems
are in users code not in the kernel.
So there are two possibilities, since my code has not changed:
a) The kernel has become more lenient and is not producing any unaligned
trap messages.
b) GCC had a bug which produced unaligned data which has hence been fixed.

Which of the above is true, or is there a better explanation to the above
symptom.

Thank you very much for your answer.

Regards,

Ram
---------------------------------------------------
Ram Viswanadha
International Components For Unicode
GCoC San Jose
IBM




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15  0:50 Ram Viswanadha
@ 2002-10-15  1:38 ` Michal Jaegermann
  2002-10-15  2:03 ` Uncle George
  2002-10-15 15:33 ` George France
  2 siblings, 0 replies; 26+ messages in thread
From: Michal Jaegermann @ 2002-10-15  1:38 UTC (permalink / raw)
  To: axp-list

On Mon, Oct 14, 2002 at 05:50:18PM -0700, Ram Viswanadha wrote:
> I upgraded to RH 7.2 (stock kernel and GCC) and now I am not seeing any
> unaligned trap messages.
> I read Rich Payne's mails in the archives and they suggest that the problems
> are in users code not in the kernel.

Correct.

> So there are two possibilities, since my code has not changed:
> a) The kernel has become more lenient and is not producing any unaligned
> trap messages.
> b) GCC had a bug which produced unaligned data which has hence been fixed.

Most likely seems to be the third option - printk in a kernel code which
was producing these messages was turned off. :-)

Unless you can show otherwise assume that "unaligned access" means that
your code has bugs.  Even if your kernel is silent about these, which
does not mean at all that it is more "lenient", they are still bugs.

On many ocasions "unaligned access" messages are side-effects, not
causes, of killer bugs.  Other times your kernel will trap, do a fixup,
and move along but you will get a performance hit.  If this happens
once per some infrequently used program this is not a big deal.
If this is in some widely used library then this may be significant.

  Michal




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15  0:50 Ram Viswanadha
  2002-10-15  1:38 ` Michal Jaegermann
@ 2002-10-15  2:03 ` Uncle George
  2002-10-15 15:39   ` George France
  2002-10-15 15:33 ` George France
  2 siblings, 1 reply; 26+ messages in thread
From: Uncle George @ 2002-10-15  2:03 UTC (permalink / raw)
  To: axp-list

I suppose one might say that all misalignments are trapped. The kernel then
figures out what to do.
If the misalignment is within the kernel, that also traps, but the kernel gets
a little peaved. This generally happens when a new device driver is included (
ie never tested on a 64bit machine before.)
If misalignment is within user space, then there is a per-process flag ( i
think a dec-unix ( OSF?) flag ) to indicate that a user fault should happen, or
an automatic 'fixup' should happen ( and the user would be none to the
wiser.)   I think with the 'user fault' category, then kernel can either stop
the task, or trap to the user task for the user to determine what to do.

And Yes, gcc et al ( particularuly ) the runtime start-up code does set this
flag. I cannot tell you who deceides this ( ie kernel is set to trap, but glibc
tells the kernel to silently fix these things up. or the other way around.)

I would like to see the unalignment faults, maybe then these things 'might' get
fixed. The theory, i suppose, is that maybe you can find the 'coder' who in all
likelyhood only thinks in intel, and probably has no time to delve into the
problem - ergo a lot of trap messages with no one to repair same!

/gat

Ram Viswanadha wrote:

> Hi everyone,
>
> So there are two possibilities, since my code has not changed:
> a) The kernel has become more lenient and is not producing any unaligned
> trap messages.
> b) GCC had a bug which produced unaligned data which has hence been fixed.




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15  0:50 Ram Viswanadha
  2002-10-15  1:38 ` Michal Jaegermann
  2002-10-15  2:03 ` Uncle George
@ 2002-10-15 15:33 ` George France
  2002-10-17 22:17   ` Ram Viswanadha
  2 siblings, 1 reply; 26+ messages in thread
From: George France @ 2002-10-15 15:33 UTC (permalink / raw)
  To: axp-list, Ram Viswanadha

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

Hello Ram,

Attached is a program that will help you find the alignment
problems in ICU.

Best Regards,


--George


On Monday 14 October 2002 20:50, Ram Viswanadha wrote:
> Hi everyone,
> ICU (http://oss.software.ibm.com/icu/) libraries when compiled with stock
> RH 6.2 kernel and GCC used to produce unaligned traps in some of the tools.
> I upgraded to RH 7.2 (stock kernel and GCC) and now I am not seeing any
> unaligned trap messages.
> I read Rich Payne's mails in the archives and they suggest that the
> problems are in users code not in the kernel.
> So there are two possibilities, since my code has not changed:
> a) The kernel has become more lenient and is not producing any unaligned
> trap messages.
> b) GCC had a bug which produced unaligned data which has hence been fixed.
>
> Which of the above is true, or is there a better explanation to the above
> symptom.
>
> Thank you very much for your answer.
>
> Regards,
>
> Ram
> ---------------------------------------------------
> Ram Viswanadha
> International Components For Unicode
> GCoC San Jose
> IBM
>
>
>
> _______________________________________________
> Axp-list mailing list
> Axp-list@redhat.com
> https://listman.redhat.com/mailman/listinfo/axp-list

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: alignment.c --]
[-- Type: text/x-c; name="alignment.c", Size: 2729 bytes --]

#include <errno.h> 
#include <stdio.h> 

#ifndef __linux__ 
#include <sys/sysinfo.h> 
#else 
#include <asm/sysinfo.h> 
#include <asm/unistd.h> 

static int setsysinfo(unsigned long op, void *buffer, unsigned long size, 
                      int *start, void *arg, unsigned long flag) 
{ 
  syscall(__NR_osf_setsysinfo, op, buffer, size, start, arg, flag); 
} 
#endif 

static void usage(void) 
{ 
        fprintf(stderr, 
                "usage: unaligned <command-path> [command-args...]\n\n" 
                " This program is designed to assist debugging of\n" 
                " unaligned traps by running the program in gdb\n" 
                " and causing it to get SIGBUS when it encounters\n" 
                " an unaligned trap.\n\n" 
                " It is free software written by Sean Hunter <sean@uncarved.co.uk>\n" 
                " based on code by Richard Henderson and Andrew Morgan. It is provided\n" 
                " under the gnu public license without warrantees of any kind.\n\n"); 

        exit(1); 
} 

void trap_unaligned(void) 
{ 
  unsigned int buf[2]; 
  buf[0] = SSIN_UACPROC; 
  buf[1] = UAC_SIGBUS | UAC_NOPRINT; 
  setsysinfo(SSI_NVPAIRS, buf, 1, 0, 0, 0); 

} 

int main(int argc, char **argv) 
{ 
    char* tmp_filename; 
    char* my_debugger = "/usr/bin/gdb"; 
    FILE* tmp_file; 
    int curr_arg; 

    /* check that we have at least 1 argument */ 
    if (argc < 2) { 
        usage(); 
    } 

    trap_unaligned(); 

    if (argc > 2) { 
        /* add the extra args to a file to pass to gdb */ 
        tmp_filename = tmpnam(NULL); 
        tmp_file = fopen(tmp_filename, "w+"); 
        if (!tmp_file) { 
                fprintf(stderr, "Unable to create temp file %s reason: %s\n", 
                    tmp_filename, 
                    strerror(errno)); 
        } 

        fprintf(tmp_file, "file %s\n", argv[1]); 
        fprintf(tmp_file, "set args"); 
        for(curr_arg = 2; curr_arg < argc; curr_arg++) { 
            fprintf(tmp_file, " %s", argv[curr_arg]); 
        } 
        fprintf(tmp_file, "\n"); 
#ifndef NOAUTORUN 
        fprintf(tmp_file, "run\n"); 
#endif 
        fclose(tmp_file); 

        printf("Extra arguments passed to gdb in file %s.\n" 
               "Be sure to delete it when you're done.\n\n", 
               tmp_filename); 
         
        execlp(my_debugger, argv[1], "-x", tmp_filename, NULL); 

    } 
    else { 
        execlp(my_debugger, argv[1], NULL); 
    } 

    /* if we fall through to here, our exec failed -- announce the fact */ 
    fprintf(stderr, "Unable to execute command: %s\n", strerror(errno)); 

    usage(); 

} 


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15  2:03 ` Uncle George
@ 2002-10-15 15:39   ` George France
  2002-10-15 16:46     ` Michal Jaegermann
  2002-10-15 18:08     ` Uncle George
  0 siblings, 2 replies; 26+ messages in thread
From: George France @ 2002-10-15 15:39 UTC (permalink / raw)
  To: axp-list, Uncle George

On Monday 14 October 2002 22:03, Uncle George wrote:
> I suppose one might say that all misalignments are trapped. The kernel then
> figures out what to do.

close enough.

> If the misalignment is within the kernel, that also traps, but the kernel
> gets a little peaved. This generally happens when a new device driver is
> included ( ie never tested on a 64bit machine before.)
> If misalignment is within user space, then there is a per-process flag ( i
> think a dec-unix ( OSF?) flag ) to indicate that a user fault should
> happen, or an automatic 'fixup' should happen ( and the user would be none
> to the wiser.)   I think with the 'user fault' category, then kernel can
> either stop the task, or trap to the user task for the user to determine
> what to do.

Alignment traps are not a serious bug but will make the code run significantly
slower.  They are mostly really annoying.

>
> And Yes, gcc et al ( particularuly ) the runtime start-up code does set
> this flag. I cannot tell you who deceides this ( ie kernel is set to trap,
> but glibc tells the kernel to silently fix these things up. or the other
> way around.)

Please see:

   linux/arch/alpha/mm/fault.c

and

   alignment.c (included as attachment in previous e-mail)

for the exact details.

>
> I would like to see the unalignment faults, maybe then these things 'might'
> get fixed. The theory, i suppose, is that maybe you can find the 'coder'
> who in all likelyhood only thinks in intel, and probably has no time to
> delve into the problem - ergo a lot of trap messages with no one to repair
> same!

Location and repair is usually trivial with 'alignment.c'. Used properly it will
take you to the line of code that is causing the issue.  There are occasions
when the problem is severe and the program should just re-architect-ed.

Best Regards,


--George




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 15:39   ` George France
@ 2002-10-15 16:46     ` Michal Jaegermann
  2002-10-15 18:24       ` alvin
  2002-10-15 18:08     ` Uncle George
  1 sibling, 1 reply; 26+ messages in thread
From: Michal Jaegermann @ 2002-10-15 16:46 UTC (permalink / raw)
  To: axp-list

On Tue, Oct 15, 2002 at 11:39:36AM -0400, George France wrote:
> 
> Alignment traps are not a serious bug but will make the code run significantly
> slower.  They are mostly really annoying.

Let me add an example when such trap indictates something more serious
instead of beeing just an instance of a somewhat careless programming.
Cases like that were observed in "real life" instead of beeing
artificially constructed.

A library provides a variable of an 'int' type and another program is
accessing it as 'long'.  That bug does not show up on x86 as there
sizeof(int) and sizeof(long) happens to be the same.  When this
is not true, like on Alpha but not only, then either you are
scribbling where you should not or you are reading into your program
some unexpected/random values.  Direct effects will depend how
important really were these extra bits in a particular situation.

If you are lucky then that library variable is NOT aligned on a long
boundary so attempts to access it as long generate unaligned traps.
These are, of course, symptoms as the real bug is something else.
If you recompile a loader may align library variables differently
and traps may vanish; the bug is still there.

There are other possible bugs which follow roughly the same pattern.

  Michal




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 15:39   ` George France
  2002-10-15 16:46     ` Michal Jaegermann
@ 2002-10-15 18:08     ` Uncle George
  2002-10-15 19:29       ` George France
  1 sibling, 1 reply; 26+ messages in thread
From: Uncle George @ 2002-10-15 18:08 UTC (permalink / raw)
  Cc: axp-list

George France wrote:

> On Monday 14 October 2002 22:03, Uncle George wrote:
> > I suppose one might say that all misalignments are trapped. The kernel then
> > figures out what to do.
>
> close enough.

Close enough to what?

>
>
> gory, then kernel can
> > either stop the task, or trap to the user task for the user to determine
> > what to do.
>
> Alignment traps are not a serious bug but will make the code run significantly
> slower.  They are mostly really annoying.

Your opinion is amusing.  If the kernel didnt bother fixing these faults, then
you'd have a dead program - but some incentive to fix the problem. If the kernel
could fix them, then you pay the price in delayed execution of that faulty
instruction - with fairly no incentive to go and get it fixed. If the processor is
so fast, then maybe its not even worth bothering.
So what would it take for this fault to escalate into a serious matter.  On a
266Mhz 21064 machine, it may warrent an evaluation. on a 800mhz 21264, under the
same condidions it may warrent a yawn.  And a different type of person may change
that concept alltogether.

>
>
> >
> > And Yes, gcc et al ( particularuly ) the runtime start-up code does set
> > this flag. I cannot tell you who deceides this ( ie kernel is set to trap,
> > but glibc tells the kernel to silently fix these things up. or the other
> > way around.)
>
> Please see:
>
>    linux/arch/alpha/mm/fault.c

I'd have to look for some alpha code, but maybe my references were for FP faults. I
think that there was just as much code for alignment faults, but i'd have to check
again.




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 16:46     ` Michal Jaegermann
@ 2002-10-15 18:24       ` alvin
  2002-10-15 19:33         ` George France
                           ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: alvin @ 2002-10-15 18:24 UTC (permalink / raw)
  To: axp-list

Michal Jaegermann wrote:
> On Tue, Oct 15, 2002 at 11:39:36AM -0400, George France wrote:
> 
>>Alignment traps are not a serious bug but will make the code run significantly
>>slower.  They are mostly really annoying.
> 
> 
> Let me add an example when such trap indictates something more serious
> instead of beeing just an instance of a somewhat careless programming.
> Cases like that were observed in "real life" instead of beeing
> artificially constructed.

[snip]

This brings to mind a question.

Are unaligned accesses guarenteed to generate an error or the correct 
results. Although the correct results may require some huge amount of 
processing on the part of the kernel to repair the problem.







-- 
Alvin Starr                   ||   voice: (416)785-4051
Interlink Connectivity        ||   fax:   (416)785-3668
alvin@iplink.net              ||




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 18:08     ` Uncle George
@ 2002-10-15 19:29       ` George France
  2002-10-15 20:10         ` Uncle George
  0 siblings, 1 reply; 26+ messages in thread
From: George France @ 2002-10-15 19:29 UTC (permalink / raw)
  To: axp-list, Uncle George

On Tuesday 15 October 2002 14:08, Uncle George wrote:
> George France wrote:
> >
> > Alignment traps are not a serious bug but will make the code run
> > significantly slower.  They are mostly really annoying.
>
> Your opinion is amusing.  If the kernel didnt bother fixing these faults,
> then you'd have a dead program - but some incentive to fix the problem. If
> the kernel could fix them, then you pay the price in delayed execution of
> that faulty instruction - with fairly no incentive to go and get it fixed.
> If the processor is so fast, then maybe its not even worth bothering.
> So what would it take for this fault to escalate into a serious matter.  On
> a 266Mhz 21064 machine, it may warrent an evaluation. on a 800mhz 21264,
> under the same condidions it may warrent a yawn.  And a different type of
> person may change that concept alltogether.
>

It is precisely because the kernel fixes the faults that it is not a serious
problem. It is just question of whether or not you can live with the delay.

Best Regards,


--George




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 18:24       ` alvin
@ 2002-10-15 19:33         ` George France
  2002-10-15 19:40         ` Falk Hueffner
       [not found]         ` <3DAC6EF2.BC962839@voicenet.com>
  2 siblings, 0 replies; 26+ messages in thread
From: George France @ 2002-10-15 19:33 UTC (permalink / raw)
  To: axp-list, alvin

On Tuesday 15 October 2002 14:24, alvin wrote:
> Michal Jaegermann wrote:
> > On Tue, Oct 15, 2002 at 11:39:36AM -0400, George France wrote:
> >>Alignment traps are not a serious bug but will make the code run
> >> significantly slower.  They are mostly really annoying.
> >
> > Let me add an example when such trap indictates something more serious
> > instead of beeing just an instance of a somewhat careless programming.
> > Cases like that were observed in "real life" instead of beeing
> > artificially constructed.
>
> [snip]
>
> This brings to mind a question.
>
> Are unaligned accesses guarenteed to generate an error or the correct
> results. Although the correct results may require some huge amount of
> processing on the part of the kernel to repair the problem.

On the Alpha Architecture, unaligned accesses are guarenteed to generate
an error.  The error is trapped by the kernel and fixed by the kernel.  The
kernel will always make the correct fix for alignment at cost of cpu cycles.

Beest Regards,


--George




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 18:24       ` alvin
  2002-10-15 19:33         ` George France
@ 2002-10-15 19:40         ` Falk Hueffner
       [not found]         ` <3DAC6EF2.BC962839@voicenet.com>
  2 siblings, 0 replies; 26+ messages in thread
From: Falk Hueffner @ 2002-10-15 19:40 UTC (permalink / raw)
  To: axp-list

alvin <alvin@iplink.net> writes:

> Are unaligned accesses guarenteed to generate an error or the
> correct results. Although the correct results may require some huge
> amount of processing on the part of the kernel to repair the
> problem.

Yes. It isn't really particularly difficult to fix. Most of the time
in the handler is spent in saving and restoring registers.

-- 
	Falk




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 19:29       ` George France
@ 2002-10-15 20:10         ` Uncle George
  2002-10-15 20:46           ` George France
  0 siblings, 1 reply; 26+ messages in thread
From: Uncle George @ 2002-10-15 20:10 UTC (permalink / raw)
  To: axp-list

Because things are fixed, does not mean that they are not serious faults.

Just because AAA tows your car home every week for free ( AAAPlus program)
because of a faulty engine, that does not mean that you do not have a serious
problem with your car.  Some folks would suggest that you should fix your kar,
some might even suggest that you should get a better towing service package.

But i think it would be more appropriate to suggest that it is a serious problem
because the kernel is taking some form of corrective action.

I suppose it a matter of temperment, and opinion.

George France wrote:

>
>
> It is precisely because the kernel fixes the faults that it is not a serious
> problem. It is just question of whether or not you can live with the delay.
>




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 20:10         ` Uncle George
@ 2002-10-15 20:46           ` George France
  0 siblings, 0 replies; 26+ messages in thread
From: George France @ 2002-10-15 20:46 UTC (permalink / raw)
  To: axp-list, Uncle George

I will agree that they are faults.  I will also agree that they should be fixed, so that
they no longer occur.

Most of the time they do not bother me enough for me to seriously think about
them.  To me a serious problem would be a problem that prevents me from using
my system, like; I can't use my Hard Drives because of problems with the SCSI
driver, etc......

I just can not agree that they are serious unless they are either causing such a delay,
that they prevent me from using the system or filling up my HD with warning messages.

Seriously,


--George



On Tuesday 15 October 2002 16:10, Uncle George wrote:
> Because things are fixed, does not mean that they are not serious faults.
>
> Just because AAA tows your car home every week for free ( AAAPlus program)
> because of a faulty engine, that does not mean that you do not have a
> serious problem with your car.  Some folks would suggest that you should
> fix your kar, some might even suggest that you should get a better towing
> service package.
>
> But i think it would be more appropriate to suggest that it is a serious
> problem because the kernel is taking some form of corrective action.
>
> I suppose it a matter of temperment, and opinion.
>
> George France wrote:
> > It is precisely because the kernel fixes the faults that it is not a
> > serious problem. It is just question of whether or not you can live with
> > the delay.
>
> _______________________________________________
> Axp-list mailing list
> Axp-list@redhat.com
> https://listman.redhat.com/mailman/listinfo/axp-list




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
       [not found]         ` <3DAC6EF2.BC962839@voicenet.com>
@ 2002-10-16 18:15           ` alvin
  2002-10-16 19:18             ` George France
                               ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: alvin @ 2002-10-16 18:15 UTC (permalink / raw)
  To: Uncle George, axp-list

Uncle George wrote:
> alignment faults are guarenteed to generate a trap. Does that necessarily guarentee
> correct results? wello no. You have to teach the kernel, or the user ( if the
> kernel passes the fault to the user ) to process the misalignment.  It may seem
> real simple to fetch 4 bytes from address 6, but you really also have to deceide
> what register the result should be placed in ( u have to decode the faulting
> op-code ) .  Then you have to restore the user process environment, albeit with the
> correct results, and in the proper register.There aren't too many op-codes in a
> risc processor that require memory addressing, so checking is limited .
> 
> My amazment is with 'pipelining' ( multiple instructions in various stages of
> execution ), and insuring that these (fixed) results wont interfere with other
> instructiions in the pipeline. I Think that this is a special case, but I'd have to
> look it up again to be sure.
> /gat

I have spent a good chunk of the last day or so trying to generate a 
case where this problem does actually exist. Sadly I cannot recreate a 
simple bug.  I have a program (l2tpd) that runs a network tunneling 
protocol. Anyway. This program works on streams of bytes that are the 
control packets. Some of the packets have 16bit words that are unaligned 
  when they are going to be written.

So I have a chunk of code like this:

int add_message_type_avp(struct buffer *buf, _u16 type) {
         _u16 *raw = (_u16 *)(buf->start + buf->len);
         raw[0] = htons(0x8 | MBIT);
         raw[1] = htons(VENDOR_ID);
         raw[2] = 0;
         raw[3] = htons(type);
         buf->len += 8;
         return 0;
}

I end up converting it to this:

int add_message_type_avp(struct buffer *buf, _u16 type) {
         _u16 raw[32];
         raw[0] = htons(0x8 | MBIT);
         raw[1] = htons(VENDOR_ID);
         raw[2] = 0;
         raw[3] = htons(type);
         memcpy((char *)(buf->start + buf->len), (char *)&raw,8); 
buf->len += 8;
         return 0;
}


This is not pretty but it does work.

I spent a lot of time tracking this one down and could only come to the 
conclusion that the unaligned trap handler was not working correctly.
I was hoping to come up with a nice small proveable case but I just 
could not seem to make things break on demand. and the only other way to 
get this to break would involve kicking 40 customers off line(Not to 
likely).



-- 
Alvin Starr                   ||   voice: (416)785-4051
Interlink Connectivity        ||   fax:   (416)785-3668
alvin@iplink.net              ||




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-16 18:15           ` alvin
@ 2002-10-16 19:18             ` George France
  2002-10-16 19:45               ` Falk Hueffner
  2002-10-16 20:23             ` Michal Jaegermann
  2002-10-17  1:47             ` Uncle George
  2 siblings, 1 reply; 26+ messages in thread
From: George France @ 2002-10-16 19:18 UTC (permalink / raw)
  To: axp-list, alvin

Hello Alvin,

On Wednesday 16 October 2002 14:15, alvin wrote:
> I have spent a good chunk of the last day or so trying to generate a
> case where this problem does actually exist. Sadly I cannot recreate a
> simple bug.  I have a program (l2tpd) that runs a network tunneling
> protocol. Anyway. This program works on streams of bytes that are the
> control packets. Some of the packets have 16bit words that are unaligned
>   when they are going to be written.
>
> So I have a chunk of code like this:
>
> int add_message_type_avp(struct buffer *buf, _u16 type) {
>          _u16 *raw = (_u16 *)(buf->start + buf->len);
>          raw[0] = htons(0x8 | MBIT);
>          raw[1] = htons(VENDOR_ID);
>          raw[2] = 0;
>          raw[3] = htons(type);
>          buf->len += 8;
>          return 0;
> }
>
> I end up converting it to this:
>
> int add_message_type_avp(struct buffer *buf, _u16 type) {
>          _u16 raw[32];
>          raw[0] = htons(0x8 | MBIT);
>          raw[1] = htons(VENDOR_ID);
>          raw[2] = 0;
>          raw[3] = htons(type);
>          memcpy((char *)(buf->start + buf->len), (char *)&raw,8);
> buf->len += 8;
>          return 0;
> }
>
>
> This is not pretty but it does work.


Your converted code makes sense.  A prettier method is used by the 
kernel in asm/unaligned.h via:

              get_unaligned(ptr);
              put_unaligned(val, ptr);

More information can be found at:  

   http://www.xml.com/ldd/chapter/book/ch10.html

under the section titled "Data Alignment".

>
> I spent a lot of time tracking this one down and could only come to the
> conclusion that the unaligned trap handler was not working correctly.

If I had to take a wild guess, then I think that the unaligned trap handler
is working correctly and that it is more likely that you have found a compiler
optimization bug, which would explain your difficulty in reproducing the
problem.  Have you tried rebuilding your application with -O1, '-O2 
-fno-sched-interblock' or '-O2 -fno-strength-reduce'???

> I was hoping to come up with a nice small proveable case but I just
> could not seem to make things break on demand. and the only other way to
> get this to break would involve kicking 40 customers off line(Not to
> likely).

I hope this helps.

Best Regards,


--George




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-16 19:18             ` George France
@ 2002-10-16 19:45               ` Falk Hueffner
  2002-10-16 20:08                 ` George France
  0 siblings, 1 reply; 26+ messages in thread
From: Falk Hueffner @ 2002-10-16 19:45 UTC (permalink / raw)
  To: axp-list

George France <france@handhelds.org> writes:

> Your converted code makes sense.  A prettier method is used by the
> kernel in asm/unaligned.h via:
> 
>               get_unaligned(ptr);
>               put_unaligned(val, ptr);

Note that the code gcc generates for these is pretty inefficient,
though, so the memcpy method is probably faster. (Incidentally, I have
a patch lying around that makes better code on Alphas with the BWX
extension, but I never got around to finishing it... does anybody
think it is worthwhile?)

-- 
	Falk




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-16 19:45               ` Falk Hueffner
@ 2002-10-16 20:08                 ` George France
  2002-10-17 18:37                   ` Falk Hueffner
  0 siblings, 1 reply; 26+ messages in thread
From: George France @ 2002-10-16 20:08 UTC (permalink / raw)
  To: axp-list, Falk Hueffner

On Wednesday 16 October 2002 15:45, Falk Hueffner wrote:
> George France <france@handhelds.org> writes:
> > Your converted code makes sense.  A prettier method is used by the
> > kernel in asm/unaligned.h via:
> >
> >               get_unaligned(ptr);
> >               put_unaligned(val, ptr);
>
> Note that the code gcc generates for these is pretty inefficient,
> though, so the memcpy method is probably faster. (Incidentally, I have
> a patch lying around that makes better code on Alphas with the BWX
> extension, but I never got around to finishing it... does anybody
> think it is worthwhile?)

Yup, pretty may not equal fast. :-)  Maybe Alvin would like to test this
and let us know?

Lots of people could take advantage of the BWX extension.  For people
with those systems it is probably worthwhile.

Best Regards,


--George.




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-16 18:15           ` alvin
  2002-10-16 19:18             ` George France
@ 2002-10-16 20:23             ` Michal Jaegermann
  2002-10-16 21:30               ` alvin
  2002-10-17  1:47             ` Uncle George
  2 siblings, 1 reply; 26+ messages in thread
From: Michal Jaegermann @ 2002-10-16 20:23 UTC (permalink / raw)
  To: axp-list

On Wed, Oct 16, 2002 at 02:15:55PM -0400, alvin wrote:
> 
> So I have a chunk of code like this:
> 
> int add_message_type_avp(struct buffer *buf, _u16 type) {
>          _u16 *raw = (_u16 *)(buf->start + buf->len);
>          raw[0] = htons(0x8 | MBIT);
>          raw[1] = htons(VENDOR_ID);
>          raw[2] = 0;
>          raw[3] = htons(type);
>          buf->len += 8;
>          return 0;
> }

Regardless of all other issues this is a really costly method for
doing this.  You may cause four kernel traps.

> 
> I end up converting it to this:
> 
> int add_message_type_avp(struct buffer *buf, _u16 type) {
>          _u16 raw[32];
>          raw[0] = htons(0x8 | MBIT);
>          raw[1] = htons(VENDOR_ID);
>          raw[2] = 0;
>          raw[3] = htons(type);
>          memcpy((char *)(buf->start + buf->len), (char *)&raw,8); 
> buf->len += 8;
>          return 0;
> }
> 
> 
> This is not pretty but it does work.

Save some code questions this is actually a correct way of coding
something of that sort.  Should compile to something reasonably fast
too.

Here are questions.
  - Why you are pushing on stack raw[32] while only four positions
    are used?
  - Are MBIT and VENDOR_ID really constants as suggested by a case?
    If yes then how about making a static initializer for first three
    positions in raw[4] and filling up only one cell with htons(type)?
  - With a correct size a "magic constant" of 8 can and should be
    replaced by sizeof(raw).

> I spent a lot of time tracking this one down and could only come to the 
> conclusion that the unaligned trap handler was not working correctly.

Seems like there is a potential for few other mess-ups.  You really
eliminated them all?

   Michal




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-16 20:23             ` Michal Jaegermann
@ 2002-10-16 21:30               ` alvin
  0 siblings, 0 replies; 26+ messages in thread
From: alvin @ 2002-10-16 21:30 UTC (permalink / raw)
  To: axp-list

Michal Jaegermann wrote:
> On Wed, Oct 16, 2002 at 02:15:55PM -0400, alvin wrote:
> 
>>So I have a chunk of code like this:
>>
>>int add_message_type_avp(struct buffer *buf, _u16 type) {
>>         _u16 *raw = (_u16 *)(buf->start + buf->len);
>>         raw[0] = htons(0x8 | MBIT);
>>         raw[1] = htons(VENDOR_ID);
>>         raw[2] = 0;
>>         raw[3] = htons(type);
>>         buf->len += 8;
>>         return 0;
>>}
> 
> 
> Regardless of all other issues this is a really costly method for
> doing this.  You may cause four kernel traps.
> 
> 
>>I end up converting it to this:
>>
>>int add_message_type_avp(struct buffer *buf, _u16 type) {
>>         _u16 raw[32];
>>         raw[0] = htons(0x8 | MBIT);
>>         raw[1] = htons(VENDOR_ID);
>>         raw[2] = 0;
>>         raw[3] = htons(type);
>>         memcpy((char *)(buf->start + buf->len), (char *)&raw,8); 
>>buf->len += 8;
>>         return 0;
>>}
>>
>>
>>This is not pretty but it does work.
> 
> 
> Save some code questions this is actually a correct way of coding
> something of that sort.  Should compile to something reasonably fast
> too.
> 
> Here are questions.
>   - Why you are pushing on stack raw[32] while only four positions
>     are used?

I just did a global search and replace. It seemed easier to make the 
array larger than the largest item and not worry about it

>   - Are MBIT and VENDOR_ID really constants as suggested by a case?
>     If yes then how about making a static initializer for first three
 >     positions in raw[4] and filling up only one cell with htons(type)?

This is true. I was looking to make the fewest changes to the way the 
code operated. As the number of changes increse so does the likelyhood 
of new bugs creeping in.

>   - With a correct size a "magic constant" of 8 can and should be
>     replaced by sizeof(raw).

Hmmm. This is true. Possibly time to look at changing this code to clean 
it up a bit.

I did not write this code originaly. Personaly I think I would have 
created a structure and tried to work with that.

This is only one of about a hundred routines or so that do just about 
the same thing.



> 
>>I spent a lot of time tracking this one down and could only come to the 
>>conclusion that the unaligned trap handler was not working correctly.
> 
> 
> Seems like there is a potential for few other mess-ups.  You really
> eliminated them all?

Well the software seems to work quite reliably. But on the other hand 
the system wedged on the weekend.




-- 
Alvin Starr                   ||   voice: (416)785-4051
Interlink Connectivity        ||   fax:   (416)785-3668
alvin@iplink.net              ||




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-16 18:15           ` alvin
  2002-10-16 19:18             ` George France
  2002-10-16 20:23             ` Michal Jaegermann
@ 2002-10-17  1:47             ` Uncle George
  2 siblings, 0 replies; 26+ messages in thread
From: Uncle George @ 2002-10-17  1:47 UTC (permalink / raw)
  To: axp-list

This has the smell of being possible. I you have a (older) machine that was unable to
fetch bytes/shorts because there was no machine lang instructions to do so. The gcc
compiler would have to generate the appropriate  extract/insert code for each usage of
byte/short.  Not sure how the code would handle the odd address though. But
none-the-less those kernels would only see faults for ints,or longs ( as there were no
short fetches on a machine level )

Now there are machines that can fetch shorts, and bytes on a single instruction level. I
suspect that there wont be an alignment problem with byte, but there is the possibility
that  the alignment fault correction code in the kernel as it relates to the new 'short'
load/store's may not have been extensively tested. A Hypothesis to be sure.

In either case, can u post some of the particulars. ie machine type, gcc version, gcc
compile options?

/gat

BTW do u get an alignment messages in syslogd's /var/log/messages file?

alvin wrote:

>
>
> I spent a lot of time tracking this one down and could only come to the
> conclusion that the unaligned trap handler was not working correctly.




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
       [not found] <673E0A4C-E1DF-11D6-AC80-000393768D2C@mcgillsociety.org>
@ 2002-10-17 15:50 ` George France
  0 siblings, 0 replies; 26+ messages in thread
From: George France @ 2002-10-17 15:50 UTC (permalink / raw)
  To: William H. Magill, axp-list; +Cc: alvin

On Thursday 17 October 2002 10:47, William H. Magill wrote:
<snip>
>
> ============
> [Note that the compiler referred to above is the Compaq C compiler, NOT
> gcc. Gcc is historically much, much worse with this sort of thing on
> the Alpha. While I would trust CCC on this score, unless GCC has been
> recently (ie in the past year) re-written to address the problems, it
> doesn't deal with Alpha code generation issues well at all.]

Although I also have more faith in CCC.  This is not a compiler issue. I
could hand code alignment failures in assembly.  The question is what
does the kernel do about them.  IIRC, Alvin's premise is that the kernel is 
not fixing up the alignment faults correctly.  I beleive that the kernel
is doing the "right" thing.

>
> The issue of fixing the unaligned access errors is directly related to
> their occurrence. 

I agree.

>One shot programs with single errors are easily
> ignored. However, in a production program even "just a couple" of
> errors can quickly eat up cpu cycles. And in a "common routine" they
> can be disastrous -- much more wall-clock-time is spent not only fixing
> the error, but in writing out the error message than is spent
> generating the results. This yields EXTREMELY poor application
> performance... and it is measured in Wall-clock-time, not in CPU cycles!

yup, IMHO,  it is just a matter of the length of the delay verses the amount of
time that I have as a programmer to find and them.

Best Regards,


--George




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-16 20:08                 ` George France
@ 2002-10-17 18:37                   ` Falk Hueffner
  0 siblings, 0 replies; 26+ messages in thread
From: Falk Hueffner @ 2002-10-17 18:37 UTC (permalink / raw)
  To: George France; +Cc: axp-list

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

George France <france@handhelds.org> writes:

> On Wednesday 16 October 2002 15:45, Falk Hueffner wrote:
> > Note that the code gcc generates for these is pretty inefficient,
> > though, so the memcpy method is probably faster. (Incidentally, I have
> > a patch lying around that makes better code on Alphas with the BWX
> > extension, but I never got around to finishing it... does anybody
> > think it is worthwhile?)
> 
> Yup, pretty may not equal fast. :-)  Maybe Alvin would like to test this
> and let us know?
> 
> Lots of people could take advantage of the BWX extension.  For people
> with those systems it is probably worthwhile.

OK, here it is... if anybody likes it, I could try to submit it to the
gcc maintainers.

-- 
	Falk


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: unaligned.patch --]
[-- Type: text/x-patch, Size: 2844 bytes --]

Index: alpha.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/alpha/alpha.c,v
retrieving revision 1.277
diff -u -r1.277 alpha.c
--- alpha.c	27 Sep 2002 13:30:07 -0000	1.277
+++ alpha.c	17 Oct 2002 18:10:53 -0000
@@ -4075,9 +4075,15 @@
            lda    r3,X(r11)                lda    r3,X+2(r11)
            extwl  r1,r3,r1                 extql  r1,r3,r1
            extwh  r2,r3,r2                 extqh  r2,r3,r2
-           or     r1.r2.r1                 or     r1,r2,r1
+           or     r1,r2,r1                 or     r1,r2,r1
                                            sra    r1,48,r1
 
+   (BWX)   ldbu   r1,X(a0)                 ldbu   r1,X(a0)
+           ldbu   r2,X+1(a0)               ldbu   r2,X+1(a0)
+           sll    r2,8,r2                  sll    r2,8,r2
+           or     r1,r2,r1                 or     r1,r2,r1
+                                           sextwl r1,r1
+
    long:   ldq_u  r1,X(r11)                ldq_u  r1,X(r11)
            ldq_u  r2,X+3(r11)              ldq_u  r2,X+3(r11)
            lda    r3,X(r11)                lda    r3,X(r11)
@@ -4102,6 +4108,26 @@
   rtx meml, memh, addr, extl, exth, tmp, mema;
   enum machine_mode mode;
 
+  if (TARGET_BWX && (int) size == 2)
+    {
+      rtx lo, hi;
+
+      lo = gen_reg_rtx (QImode);      
+      hi = gen_reg_rtx (QImode); 
+      emit_move_insn (lo, adjust_address (mem, QImode, 
+					  ofs + WORDS_BIG_ENDIAN ? 1 : 0));
+      emit_move_insn (hi, adjust_address (mem, QImode, 
+					  ofs + WORDS_BIG_ENDIAN ? 0 : 1));
+      tmp = expand_binop (HImode, ashl_optab, hi, GEN_INT (8), 
+			  tgt, 0, OPTAB_WIDEN);
+      tmp = expand_binop (HImode, ior_optab, lo, tmp, 
+			  tgt, 0, OPTAB_WIDEN);
+
+      if (tmp != tgt)
+	emit_move_insn (tgt, gen_lowpart(GET_MODE (tgt), tmp));
+      return;
+    }
+
   meml = gen_reg_rtx (DImode);
   memh = gen_reg_rtx (DImode);
   addr = gen_reg_rtx (DImode);
@@ -4225,7 +4251,26 @@
      HOST_WIDE_INT size, ofs;
 {
   rtx dstl, dsth, addr, insl, insh, meml, memh, dsta;
-  
+
+  /* For small data types (short and int), it's faster to write out byte-wise,
+     since that doesn't require any loads.  */
+
+  if (TARGET_BWX && (int) size <= 4)
+    {
+      int i;
+      for (i = 0; i < size; i++)
+        {
+          rtx ext = gen_reg_rtx (DImode);
+          expand_binop (DImode, lshr_optab, src, GEN_INT (i * 8),
+                        ext, 1, OPTAB_WIDEN);
+          emit_move_insn (adjust_address (dst, QImode,
+                                          ofs + (WORDS_BIG_ENDIAN
+                                                 ? size - i : i)),
+                          gen_lowpart(QImode, ext));
+        }
+      return;
+    }
+
   dstl = gen_reg_rtx (DImode);
   dsth = gen_reg_rtx (DImode);
   insl = gen_reg_rtx (DImode);

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-15 15:33 ` George France
@ 2002-10-17 22:17   ` Ram Viswanadha
  2002-10-18 15:36     ` Michal Jaegermann
  0 siblings, 1 reply; 26+ messages in thread
From: Ram Viswanadha @ 2002-10-17 22:17 UTC (permalink / raw)
  To: axp-list

Hi Everyone,
Thank you all for you answers and help.

George, thank you for your code, I actually used the code by Sean Hunter
available at http://linux.iol.unh.edu/linux/alpha/faq/iol_faq.html#9

There was only one instance in our tests that caused unaligned trap. The
runtime code, binary data generated did not produce any unaligned traps.
What we had was:

static uint8_t storage[100000]

testUTrie(){
    utrie_serialize(...,storage,sizeof(storage),..)
}
............ source was passed to a library function that cast the uint8_t
to internal structure ...
struct UTrieHeader {
    uint32_t signature;
    uint32_t options;
    int32_t indexLength;
    int32_t dataLength;
};
utrie_serialize(...., uint8_t* data,int32_t  capacity,....)
UTrieHeader* header = (UTrieHeader*) data;
header->signature =0x54726965; /**  this line caused unaligned trap **/
}

so the fix was to change storage to
struct{
    double bogus; /* needed for aligining the storage */
    uint8_t storage[10000000];
} storageHolder;

The expectation of the API is that input param "data" is aligned.

Some of our tools still cause unaligned traps but that is build time issue
and we decided we can live with the performance degradation.

Regards,

Ram
---------------------------------------------------
Ram Viswanadha
International Components For Unicode
GCoC San Jose
IBM
----- Original Message -----
From: "George France" <france@handhelds.org>
To: <axp-list@redhat.com>; "Ram Viswanadha" <ram@jtcsv.com>
Sent: Tuesday, October 15, 2002 8:33 AM
Subject: Re: unaligned traps question


> Hello Ram,
>
> Attached is a program that will help you find the alignment
> problems in ICU.
>
> Best Regards,
>
>
> --George
>
>
> On Monday 14 October 2002 20:50, Ram Viswanadha wrote:
> > Hi everyone,
> > ICU (http://oss.software.ibm.com/icu/) libraries when compiled with
stock
> > RH 6.2 kernel and GCC used to produce unaligned traps in some of the
tools.
> > I upgraded to RH 7.2 (stock kernel and GCC) and now I am not seeing any
> > unaligned trap messages.
> > I read Rich Payne's mails in the archives and they suggest that the
> > problems are in users code not in the kernel.
> > So there are two possibilities, since my code has not changed:
> > a) The kernel has become more lenient and is not producing any unaligned
> > trap messages.
> > b) GCC had a bug which produced unaligned data which has hence been
fixed.
> >
> > Which of the above is true, or is there a better explanation to the
above
> > symptom.
> >
> > Thank you very much for your answer.
> >
> > Regards,
> >
> > Ram
> > ---------------------------------------------------
> > Ram Viswanadha
> > International Components For Unicode
> > GCoC San Jose
> > IBM
> >
> >
> >
> > _______________________________________________
> > Axp-list mailing list
> > Axp-list@redhat.com
> > https://listman.redhat.com/mailman/listinfo/axp-list
>




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-17 22:17   ` Ram Viswanadha
@ 2002-10-18 15:36     ` Michal Jaegermann
  2002-10-18 17:17       ` Ram Viswanadha
  0 siblings, 1 reply; 26+ messages in thread
From: Michal Jaegermann @ 2002-10-18 15:36 UTC (permalink / raw)
  To: axp-list

On Thu, Oct 17, 2002 at 03:17:04PM -0700, Ram Viswanadha wrote:
....
> 
> so the fix was to change storage to
> struct{
>     double bogus; /* needed for aligining the storage */
>     uint8_t storage[10000000];
> } storageHolder;
> 
> The expectation of the API is that input param "data" is aligned.

Err...  if you are using such tricks to extricate yourself from
bad "type punning", then I would think that using union instead of
struct would be vastly preferable.  Not only you avoid reserving
some memory which we know will be not used for anything but you
also get much better chances that alignment restriction for the
widest type will be applied to all union members.

With struct a compiler can insert any amount of padding betwen
fields.  Sure, in this particular case you would not expect that
but relying on a compiler "reasonable behaviour" is by its nature
non C Standard conforming hence not portable.  "Worksforme on x86"
is behind a huge portion of bugs we are seeing on Alpha (and which
you will see on Itanium, or Hammer, or ... ).

gcc has a way to affect a stucture layout with '__attribute__ ((packed))'
but this is "gcc private" construct; widely used in Linux kernel, BTW.
It does affect what compiler generated to access fields in structures.

  Michal




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-18 15:36     ` Michal Jaegermann
@ 2002-10-18 17:17       ` Ram Viswanadha
  2002-10-18 18:47         ` Michal Jaegermann
  0 siblings, 1 reply; 26+ messages in thread
From: Ram Viswanadha @ 2002-10-18 17:17 UTC (permalink / raw)
  To: axp-list

Hi Michael,
Thank you for your expert analysis.
How about the using the below code, we have tested it on all platforms that
we have ported to and it works, even on platforms like the mainframes.

/**
 * This should align the memory properly on any machine.
 * This is very useful for the safeClone functions.
 */
typedef union {
    long    t1;
    double  t2;
    void   *t3;
} UAlignedMemory;

/**
 * Get the amount of bytes that a pointer is off by from
 * the previous aligned pointer
 */
#define U_ALIGNMENT_OFFSET(ptr) (((size_t)ptr) & (sizeof(UAlignedMemory) -
1))

/**
 * Get the amount of bytes to add to a pointer
 * in order to get the next aligned address
 */
#define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) -
U_ALIGNMENT_OFFSET(ptr))

Regards,

Ram
---------------------------------------------------
Ram Viswanadha
International Components For Unicode
GCoC San Jose
IBM
----- Original Message -----
From: "Michal Jaegermann" <michal@ellpspace.math.ualberta.ca>
To: <axp-list@redhat.com>
Sent: Friday, October 18, 2002 8:36 AM
Subject: Re: unaligned traps question


> On Thu, Oct 17, 2002 at 03:17:04PM -0700, Ram Viswanadha wrote:
> ....
> >
> > so the fix was to change storage to
> > struct{
> >     double bogus; /* needed for aligining the storage */
> >     uint8_t storage[10000000];
> > } storageHolder;
> >
> > The expectation of the API is that input param "data" is aligned.
>
> Err...  if you are using such tricks to extricate yourself from
> bad "type punning", then I would think that using union instead of
> struct would be vastly preferable.  Not only you avoid reserving
> some memory which we know will be not used for anything but you
> also get much better chances that alignment restriction for the
> widest type will be applied to all union members.
>
> With struct a compiler can insert any amount of padding betwen
> fields.  Sure, in this particular case you would not expect that
> but relying on a compiler "reasonable behaviour" is by its nature
> non C Standard conforming hence not portable.  "Worksforme on x86"
> is behind a huge portion of bugs we are seeing on Alpha (and which
> you will see on Itanium, or Hammer, or ... ).
>
> gcc has a way to affect a stucture layout with '__attribute__ ((packed))'
> but this is "gcc private" construct; widely used in Linux kernel, BTW.
> It does affect what compiler generated to access fields in structures.
>
>   Michal
>
>
>
> _______________________________________________
> Axp-list mailing list
> Axp-list@redhat.com
> https://listman.redhat.com/mailman/listinfo/axp-list
>




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: unaligned traps question
  2002-10-18 17:17       ` Ram Viswanadha
@ 2002-10-18 18:47         ` Michal Jaegermann
  0 siblings, 0 replies; 26+ messages in thread
From: Michal Jaegermann @ 2002-10-18 18:47 UTC (permalink / raw)
  To: axp-list

On Fri, Oct 18, 2002 at 10:17:55AM -0700, Ram Viswanadha wrote:
> How about the using the below code, we have tested it on all platforms that
> we have ported to and it works, even on platforms like the mainframes.
> 
> /**
>  * This should align the memory properly on any machine.
>  * This is very useful for the safeClone functions.
>  */
> typedef union {
>     long    t1;
>     double  t2;
>     void   *t3;
> } UAlignedMemory;

I would expect this to work in any reasonable situation. I am not
100% sure without study of relevant documents if Standard _guarantees_
what you expect  (anyone can clarify?) but I would be really surprised
if that would fail somewhere.

> 
> /**
>  * Get the amount of bytes that a pointer is off by from
>  * the previous aligned pointer
>  */
> #define U_ALIGNMENT_OFFSET(ptr) (((size_t)ptr) & (sizeof(UAlignedMemory) - 1))

???   Are you relying here that sizeof(UAlignedMemory) is a power of
two?  How you can be sure of that?  This is an implementation detail.

> /**
>  * Get the amount of bytes to add to a pointer
>  * in order to get the next aligned address
>  */
> #define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr))

I am not sure what all of this is about. If you have
  ptr *UAlignedMemory;
then '&ptr[5];', assuming that you "own" the referenced memory, will
make a compiler to compute properly that address by itself.  The same
if you will use pointers to some of union members.  Second-guessing
pointer arithmetic is often a source of nasty, and non-obvious on the
first look, bugs.  More debugging fun if you will hide behind macros. :-)

   Michal




^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2002-10-18 18:48 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <673E0A4C-E1DF-11D6-AC80-000393768D2C@mcgillsociety.org>
2002-10-17 15:50 ` unaligned traps question George France
2002-10-15  0:50 Ram Viswanadha
2002-10-15  1:38 ` Michal Jaegermann
2002-10-15  2:03 ` Uncle George
2002-10-15 15:39   ` George France
2002-10-15 16:46     ` Michal Jaegermann
2002-10-15 18:24       ` alvin
2002-10-15 19:33         ` George France
2002-10-15 19:40         ` Falk Hueffner
     [not found]         ` <3DAC6EF2.BC962839@voicenet.com>
2002-10-16 18:15           ` alvin
2002-10-16 19:18             ` George France
2002-10-16 19:45               ` Falk Hueffner
2002-10-16 20:08                 ` George France
2002-10-17 18:37                   ` Falk Hueffner
2002-10-16 20:23             ` Michal Jaegermann
2002-10-16 21:30               ` alvin
2002-10-17  1:47             ` Uncle George
2002-10-15 18:08     ` Uncle George
2002-10-15 19:29       ` George France
2002-10-15 20:10         ` Uncle George
2002-10-15 20:46           ` George France
2002-10-15 15:33 ` George France
2002-10-17 22:17   ` Ram Viswanadha
2002-10-18 15:36     ` Michal Jaegermann
2002-10-18 17:17       ` Ram Viswanadha
2002-10-18 18:47         ` Michal Jaegermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox