Comment 2 for bug 715126

Revision history for this message
Miika Komu (miika-iki) wrote :

The alignment problems can be fixed with:

system("echo 3 > /proc/cpu/alignment");

However, this degrades the performance of the system radically. So, this was solved by a student (on some really old version of Android) using packed data structures. I have some patches from him, but I am not going to publish them online because they do not work with the current data base. Instead, I'll just quote his report:

When you cast unaligned pointer to an aligned type, the gcc takes your word and inlines memcpy. But this will generate unaligned trap, but will work in x86 and other processors where unaligned accesses will be fixed automatically, but not in ARM.

For example, the following code will raise SIGBUS when run in ARM
(in foo(), the types are same, even though we have typecasted it from a packed struct, so gcc inlines the memcpy)

#include <string.h>

typedef struct {
  unsigned int a;
  unsigned int b;
  unsigned char c;
} s;

typedef struct {
  unsigned int a;
  unsigned int b;
  unsigned char c;
} __attribute__ ((packed)) ust;

void foo(s *cp)
{
  s dst;

  memcpy(&dst, cp, sizeof(s));

  return 0;
}

int main(int k, char *kk[]) {

  ust tt;
  return foo(&tt);
}

One place where this was causing a problem was hadb.c::hip_hadb_find_byhits(). There is still one more place where this problem is present: nlink.c::xfrm_fill_selector(), because "struct in6_addr" is packed but not any of the xfrm* structs. But I am not sure if I should modify it, so I have disabled optimization for that lib (libhiptool) for now.