Structure assignment in Linux fails in ARM but succeeds in x86
Asked Answered
F

4

4

I've noticed something really strange. say I've got the following structure defined

typedef struct
{
  uint32_t a;
  uint16_t b;
  uint32_t c;
} foo;

This structure is contained in a big buffer I receive from network.

The following code works in x86, but I receive SIGBUS on ARM.

extern void * buffer;
foo my_foo;
my_foo = (( foo * ) buffer)[0];

replacing the pointer dereferencing with memcpy solved the issue.

Searching about SIGBUS in ARM pointed me to the fact that this is related to memory alignment somwhow.

Can someone explain what's going on ?

Featherbrain answered 1/10, 2013 at 11:1 Comment(3)
Google: structure serialization and see Wikipedias serialization. Alignment and endianess being a constant theme. Even if the ARM was to support this, it is going to break on a big endian CPU. You need to annotate the struct as packed or the memcpy() may work (not SIGBUS), but it won't function properly.Amino
How to trap unaligned memory access has some additional information on this issue with ARM Linux. However, I think your underlying mechanism is flawed. There are lots of serialization routines, protocols, and frameworks (ntohl(), ntohs(), protobufs, JSON, XSD, XDR, etc).Amino
@artlessnoise thanks for the comments, but why the -1 ?Featherbrain
D
5

You said it yourself: there are memory alignment restrictions on your particular processor, and buffer is not aligned right to permit reading larger than a byte from it. The assignment is probably compiled into three moves of larger entities.

With memcpy(), there are no alignment restrictions, it has to be able to copy between any two addresses, so it does whatever is needed to implement that. Probably copying byte-by-byte until the addresses are aligned, that's a common pattern.

As an aside, I find it clearer to write your code without array indexing:

extern const void *buffer;
const foo my_foo = *(const foo *) buffer;
Digestible answered 1/10, 2013 at 11:9 Comment(0)
S
3

C Standard [ISO/IEC 9899:2011] - 6.3.2.3, paragraph 7:

A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined.

Source: CERT Secure Coding Standards

Scaife answered 1/10, 2013 at 11:22 Comment(0)
T
1

ARM-based systems expect structures to be aligned on a word boundary. If it is not the case you can have different behaviours (in the linux kernel for instance, these behaviours are described in /proc/cpu/alignement and one of them is to send a SIGBUS).

What you did with memcpy() is that you have forced the data structure alignment.

Tripura answered 1/10, 2013 at 11:25 Comment(0)
J
0

i was developing some download application on freescale imx a while back....had a memory alignment problem there(requirement was that executable be in multiple of 512 bytes)...Fundamental difference between arm and x86...But the thing to remember with memcpy is that it does a byte by byte copy ...So, it might work but please be sure to check for run-time problems...Donot be fooled by memcpy...Always a good idea to have a memory aligned structure for your specific platform.

Jude answered 1/10, 2013 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.