Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trap unaligned memory access?

I am working on a pet open-source project that implements some stream cipher algorithms and I am having trouble with a bug triggered only when I run it on an ARM processor. I have even tried running the ARM binary in x86 under qemu, but the bug isn't triggered there.

The specifics mechanisms of the bug remains elusive, but my best shot is to believe that it is caused by unaligned memory access attempt made in my program, that is fulfilled by qemu, but silently ignored by the real ARM processor in my development board.

So, since the problem is showing to be very hard to diagnose, I would like to know if there is any tool that I could use to trap unaligned memory access made by my running program, so that I can see exactly where the problem happens.

I could also use some way of enabling, on my ARM development board, some signal (SIGBUS, maybe?) to be issued if the process violates memory alignment restrictions, like we get SIGSEGV when accessing unmapped memory address. It is running Linux 2.6.32.

like image 308
lvella Avatar asked May 14 '13 16:05

lvella


People also ask

What is unaligned memory access?

Unaligned memory accesses occur when you try to read N bytes of data starting from an address that is not evenly divisible by N (i.e. addr % N != 0). For example, reading 4 bytes of data from address 0x10000004 is fine, but reading 4 bytes of data from address 0x10000005 would be an unaligned memory access.

Does ARM support unaligned access?

ANSWER. By default, ARM7 and ARM9 based microcontrollers do not allow un-aligned accesses to 16-bit and 32-bit data types. Cortex-M3 supports even un-aligned accesses, so the program above would behave correctly.


2 Answers

Linux can do the fixup for you or warn about the access.

You can enable the behavior in /proc/cpu/alignment, see http://www.mjmwired.net/kernel/Documentation/arm/mem_alignment for an explanation of the different values.

0 - Do nothing (default behavior)
1 - Warning in kernel-log with PC and Memory-Address printed.
2 - Fixup error
3 - Warn and Fixup
4 - Send a SIGBUS to the process
5 - Send SIGBUS and output Warning
like image 119
Nico Erfurth Avatar answered Oct 04 '22 06:10

Nico Erfurth


ARM Linux maintains a list of alignment handler exceptions,

$ cat /proc/cpu/alignment 
User:           0
System:         0
Skipped:        0
Half:           0
Word:           0
DWord:          0
Multi:          0
User faults:    0 (ignored)

It is only active with procfs, but it is hard to imagine a system without procfs. The specific code handling this is in alignment.c. You can use echo 3 > /proc/cpu/alignment to have Linux fixup the instruction and provide some dmesg output. Generally, handling un-aligned accesses through emulation is very in-efficient. It is better to correct the code. The signal option with a debugger attached should give some clue as to the source of the exception.

Read the manual. ;-)

like image 28
artless noise Avatar answered Oct 04 '22 04:10

artless noise