1

One of my friends is having a big problem trying to debug a code that started showing "alignment trap" errors. The problem happens when a global structure is accessed by a specific function.

After some research on the web, it still not clear what does an "alignment trap" means or is. Could someone give an explanation having special attention to what usually causes alignment traps and how usually one would solve the problem (not just in terms on how to use a debugger, but also the problem itself)?

This all done in C code in an ARM processor (OMAP L138) with Embedded Linux.

NOTE: I'm not trying to get an error-specific solution suggestion with this answer, but, as the question title suggests, the understanding of what an "alignment trap" error means. This is why I don't plan to put a source code or the like.

Momergil
  • 2,213
  • 5
  • 29
  • 59
  • 1
    Please add code which defines the structure and the access function. – user694733 Oct 20 '14 at 11:19
  • @user694733 Well I'm not sure if that wouldn't make it deviate from the topic. I'm not seeking an error-specific solution with this question, but to understand the general idea behind what a "alignment trap" means and how generally it would be solved (the answer provided by Basile says exactly what I had in mind for an answer to this question). I only mentioned the structure thing to narrow the scope a little after reading Basile's answer and noticing this is a quite a broad error. – Momergil Oct 20 '14 at 11:26
  • 1
    Fair enough. Basiles answer pretty much covers it. Personally I would first investigate any pointer casts that might hide compiler warnings/errors. This smells like a naive deserialization code. – user694733 Oct 20 '14 at 11:35

2 Answers2

10

It is operating system and processor specific (and ABI specific too).

You have some memory corruption, or memory leak or buffer overflow, etc..., or you are dereferencing some bad pointer (either uninitialized, or computed wrongly) - e.g. a pointer to double which is not a multiple of 8 (or, on some architectures, a pointer to int which is not multiple of 4), or perhaps you are jumping to some invalid address (e.g. to a bad function pointer).

On Linux, I would suggest to compile with gcc -Wall -g and to use the debugger (gdb) and valgrind. You might be interested in using -fsanitize=address or -fsanitize=undefined compilation flags (with GCC 4.9). They both instrument (so modify) the generated code.

Read about undefined behavior. You surely got some.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

An alignment trap is triggered by ARM whenever an unaligned access is made. What is an unaligned access? It's when a multibyte value is accessed where its pointer is not a multiple of its alignment, e.g. when a uint32_t is accessed by dereferencing a pointer that isn't a multiple of 4.

You can get them if you have __attribute__((packed)) data structures like this:

struct foo {
    uint8_t a;
    uint32_t b;
} __attribute__((packed));

Accesses to b will be unaligned and therefore will cause an alignment trap. You have to memcpy the data into an aligned value, and then access it.

Jimmy Hartzell
  • 307
  • 2
  • 4