In C, the compiler will lay out members of a struct in the order in which they're declared, with possible padding bytes inserted between members, or after the last member, to ensure that each member is aligned properly.
gcc provides a language extension, __attribute__((packed)), which tells the compiler not to insert padding, allowing struct members to be misaligned. For example, if the system normally requires all int objects to have 4-byte alignment, __attribute__((packed)) can cause int struct members to be allocated at odd offsets.
Quoting the gcc documentation:
The `packed' attribute specifies that a variable or structure field should have the smallest possible alignment--one byte for a variable, and one bit for a field, unless you specify a larger value with the `aligned' attribute.
Obviously the use of this extension can result in smaller data requirements but slower code, as the compiler must (on some platforms) generate code to access a misaligned member a byte at a time.
But are there any cases where this is unsafe? Does the compiler always generate correct (though slower) code to access misaligned members of packed structs? Is it even possible for it to do so in all cases?
Yes,
__attribute__((packed))is potentially unsafe on some systems. The symptom probably won't show up on an x86, which just makes the problem more insidious; testing on x86 systems won't reveal the problem. (On the x86, misaligned accesses are handled in hardware; if you dereference anint*pointer that points to an odd address, it will be a little slower than if it were properly aligned, but you'll get the correct result.)On some other systems, such as SPARC, attempting to access a misaligned
intobject causes a bus error, crashing the program.There have also been systems where a misaligned access quietly ignores the low-order bits of the address, causing it to access the wrong chunk of memory.
Consider the following program:
On x86 Ubuntu with gcc 4.5.2, it produces the following output:
On SPARC Solaris 9 with gcc 4.5.1, it produces the following:
In both cases, the program is compiled with no extra options, just
gcc packed.c -o packed.(A program that uses a single struct rather than array doesn't reliably exhibit the problem, since the compiler can allocate the struct on an odd address so the
xmember is properly aligned. With an array of twostruct fooobjects, at least one or the other will have a misalignedxmember.)(In this case,
p0points to a misaligned address, because it points to a packedintmember following acharmember.p1happens to be correctly aligned, since it points to the same member in the second element of the array, so there are twocharobjects preceding it -- and on SPARC Solaris the arrayarrappears to be allocated at an address that is even, but not a multiple of 4.)When referring to the member
xof astruct fooby name, the compiler knows thatxis potentially misaligned, and will generate additional code to access it correctly.Once the address of
arr[0].xorarr[1].xhas been stored in a pointer object, neither the compiler nor the running program knows that it points to a misalignedintobject. It just assumes that it's properly aligned, resulting (on some systems) in a bus error or similar other failure.Fixing this in gcc would, I believe, be impractical. A general solution would require, for each attempt to dereference a pointer to any type with non-trivial alignment requirements either (a) proving at compile time that the pointer doesn't point to a misaligned member of a packed struct, or (b) generating bulkier and slower code that can handle either aligned or misaligned objects.
I've submitted a gcc bug report. As I said, I don't believe it's practical to fix it, but the documentation should mention it (it currently doesn't).
UPDATE: As of 2018-12-20, this bug is marked as FIXED. The patch will appear in gcc 9 with the addition of a new
-Waddress-of-packed-memberoption, enabled by default.I've just built that version of gcc from source. For the above program, it produces these diagnostics: