What does "unaligned access" mean, and how can I fix it?


Unaligned accesses typically come up when programs use malloc(3) or
other memory allocation routines in atypical ways, or when programs do
certain (hazardous) kinds of type casts.

malloc(3) returns data aligned to the most restrictive alignment (8
byte boundaries). If you are writing your own malloc wrapper (say to add a
reference count) and you write code like this:

char *mymalloc(int size)
{
short *newmem;

newmem = (short *) malloc(size + sizeof(short));
*newmem = 1; /* initialize reference count */
return (char *) (newmem + 1);
}

you are then returning a pointer that is no longer 8-byte aligned. Now, code
like

int *i;
i = (int *) mymalloc(sizeof(int));
*i = 10;

will generate unaligned access messages whenever *i is used.

An example of dangerous casting would be something like

char buffer[100];
int i;

i = (int)*((int *)&buffer[3]);

The program will usually still run correctly, because an exception
handler in the kernel performs an unaligned read. There are some rare
cases, however, where the fixed read yields incorrect results. The
messages are printed by default because one usually wants to know when
a program is generating the unaligned accesses.

Now, if you are only getting a few of these messages, it might not
matter, but if you are getting pages of them (or worse, have turned off
the logger because you were getting so many unaligned access
messages), you might consider correcting your program.

You can use the uac(1) (Unaligned Acces Message Control) command to
turn off the messages.

If you want to find the the problem in the source code, you can use dbx.
Suppose the message is:

Fixed up unaligned data access for pid 2337 (bozo) at pc 0x5ad364

This tells you that the problem occurs in the program "bozo". In dbx,
you would type, for example:

% dbx bozo
(dbx) 0x5ad364/i

*[main:206, 0x0x5ad364] lw r0,40(sp)

dbx prints the offending instruction, along with its location: line 206
in main().



UNIXguide.net
English to Visayan Cebuano Dictionary
Suggest a Site
Visayan Cebuano to English Dictionary