How to trap unaligned memory access?

13,653

Solution 1

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

Solution 2

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. ;-)

Share:
13,653

Related videos on Youtube

lvella
Author by

lvella

Programmer and free-software idealist.

Updated on September 20, 2022

Comments

  • lvella
    lvella over 1 year

    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.

  • artless noise
    artless noise almost 11 years
    Sorry, we are a race condition. If you want to grab info from my answer, I will delete it.
  • Nico Erfurth
    Nico Erfurth almost 11 years
    No problem with me, let OP decide. ;) I'm just here to help.
  • linuxfreak
    linuxfreak over 9 years
    I don't see the option /proc/cpu/alignment in x86 or powerpc. How do we find out unaligned accesses made by the application in the case of x86 or powerpc?
  • Nico Erfurth
    Nico Erfurth over 9 years
    IIRC there are performance counters on x86 which can count the unaligned accesses. You'll need a profiler to read them. Not sure about powerpc. On ARMv5 an unaligned access generates an exception which the kernel has to handle. Other architectures can handle unaligned accesses naturally without the kernel interfering.
  • linuxfreak
    linuxfreak over 9 years
    I don't see any msg in dmesg though I set /proc/cpu/alignment to 5. Ideally, it should send a signal and also generate a warning. Am I missing something?
  • artless noise
    artless noise over 9 years
    @linuxfreak You should do cat /proc/cpu/alignment and see what the User faults line says (should be 5 (signal+warn). Also, your kernel maybe modified. Verify that the User line is incrementing. Line 915 has the printk, it is unadorned so most normal log levels should show it. If the User line doesn't increment, then you may have a CPU that is doing unaligned accesses.
  • lvella
    lvella almost 8 years
    I believe this answer to be better because it lists all the possible behaviors I can set in Linux kernel to handle the issue. In my case, since I was debugging the program, I used 4 - Send a SIGBUS to the process, and GDB delivered me the precise line causing the problem.