Re: Alignement on ARM
OoO En cette soirée bien amorcée du mardi 11 mai 2010, vers 22:41, je disais: lldpd, a program of my own, available on https://trac.luffy.cx/lldpd/ , [...] I have another alignment problem on ARM (still not related to Debian). Since I did get valuable help here last year, I try again. :) [...] The user reporting the error is using some old uclibc which does not define getifaddrs() nor struct ifaddrs. Therefore, I have reimplemented getifaddrs() and I declare struct ifaddrs as it appears in /usr/include/ifaddrs.h (using struct sockaddr for ifa_addr). I see that there is some internal datatype called struct sockaddr_storage that tries to deal with alignment issues. Is it the key to my problem? Using a porter box, I have discovered that this also happens in sid for both ARM and mips with a standard libc. I have fixed my problems with this patch: http://cgit.luffy.cx/lldpd/commit/?id=06db3608149133dd5efc38e68d47c06840f9c86b While the memcpy stuff is not so ugly, I have used offsetof() to avoid an additional memcpy just to replace a cast. Luca, thanksfor your answer.Unfortunately, using __attribute__((aligned(2))) would align the pointer to struct sockaddr. I have no way to force alignment of the struct itself. For example, consider the following code : unsigned short int a; /* Just here to misalign the following struct */ struct sockaddr mysa; /* Suppose that myaddr is aligned on a 1-byte boundary but misaligned on a 2-byte boundary */ struct ifaddrs myifaddr; struct sockaddr_ll *mysall; myifaddr.ifa_addr = mysa; mysall = (struct sockaddr_ll *)(myifaddr.ifa_addr); /* Equivalent to: */ mysall = (struct sockaddr_ll *)mysa; I have tested anyway and the warning remain. While I have fixed my warnings, I am still a bit astonished that such a common code as : sa = (struct sockaddr_in *)ifa-ifa_addr; if (... sa-sin_addr ...) ... can lead to a misaligned access on ARM. Maybe there is something somewhere that ensure that struct sockaddr and struct sockaddr_ll are always aligned correctly on a 4-byte boundary? -- /* Fuck me gently with a chainsaw... */ 2.0.38 /usr/src/linux/arch/sparc/kernel/ptrace.c pgpkRqgOUNPrZ.pgp Description: PGP signature
Re: Alignement on ARM
On 05/12/2010 01:21 PM, Vincent Bernat wrote: While I have fixed my warnings, I am still a bit astonished that such a common code as : sa = (struct sockaddr_in *)ifa-ifa_addr; if (... sa-sin_addr ...) ... can lead to a misaligned access on ARM. Maybe there is something somewhere that ensure that struct sockaddr and struct sockaddr_ll are always aligned correctly on a 4-byte boundary? Because with enough casting and whatnot, any magic number can get plugged into ifa_addr. Look carefully at where the value that gets assigned to ifa_addr comes from. If it truly is the address of an independent structure that's allocated by the compiler (stack, data or code), then it's highly unlikely to be misaligned because IIRC all structures begin on 32-bit address boundaries. I think there's even some language in the C standard about that. I don't think your example code will exhibit any problems. If ifa_addr is instead the address of something inside of another structure, and that structure is packed, then ifa_addr could be a misaligned pointer because of the structure packing. And if ifa_addr is a casted pointer from a location in a byte stream, or a calculated value, then only $deity$ knows what you can say about it other than it's bad, bad code. :) b.g. -- Bill Gatliff b...@billgatliff.com -- To UNSUBSCRIBE, email to debian-arm-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/4beaf473.9060...@billgatliff.com
Re: Alignement on ARM
On 12 May 2010 20:21, Vincent Bernat ber...@debian.org wrote: Luca, thanks for your answer. Unfortunately, using __attribute__((aligned(2))) would align the pointer to struct sockaddr. I have no way to force alignment of the struct itself. Gee, I completely misread the code (and even copy-pasted it!!) Anyway, paying more attention to it, the first member of struct sockaddr is short int sa_family, that must be 2 byte aligned. Since C doesn't allow the reordering of struct members, the whole struct must be 2byte aligned. But then I don't understand why gcc is whining... Cheers, Luca -- To UNSUBSCRIBE, email to debian-arm-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/aanlktinvxpkhkmc0dhp8kday5hm146-cnylsnnlbz...@mail.gmail.com
Re: Alignement on ARM
OoO Peu avant le début de l'après-midi du samedi 07 mars 2009, vers 13:03, je disais: lldpd, a program of my own, available on https://trac.luffy.cx/lldpd/ , [...] Hi again! I have another alignment problem on ARM (still not related to Debian). Since I did get valuable help here last year, I try again. :) This time, a user of mine is getting this message when compiling: interfaces.c: In function `iface_minimal_checks': interfaces.c:421: warning: cast increases required alignment of target type The corresponding code is : static int iface_minimal_checks(struct lldpd *cfg, struct ifaddrs *ifa) { struct sockaddr_ll *sdl; [...] sdl = (struct sockaddr_ll *)ifa-ifa_addr; if (sdl-sll_hatype != ARPHRD_ETHER || !sdl-sll_halen) return 0; [...] } ifa-ifa_addr is struct sockaddr and I can understand that the alignment may be less than struct sockaddr_ll. For example, if my struct sockaddr is 8-bit aligned but not 16-bit aligned, then when I try to use it as struct sockaddr_ll which requires to be 16-bit aligned, I get an unaligned access. I could use a memcpy but I think this is not the right way. The user reporting the error is using some old uclibc which does not define getifaddrs() nor struct ifaddrs. Therefore, I have reimplemented getifaddrs() and I declare struct ifaddrs as it appears in /usr/include/ifaddrs.h (using struct sockaddr for ifa_addr). I see that there is some internal datatype called struct sockaddr_storage that tries to deal with alignment issues. Is it the key to my problem? I have also warnings in my (stolen) implementation of getifaddrs(). My getifaddrs() implementation: http://cgit.luffy.cx/lldpd/tree/src/getifaddrs.c My struct ifaddrs declaration: http://cgit.luffy.cx/lldpd/tree/src/compat.h#n146 The location of the warning: http://cgit.luffy.cx/lldpd/tree/src/interfaces.c#n421 Crosspost to my address would be appreciated. Thanks for any help on this! -- BOFH excuse #209: Only people with names beginning with 'A' are getting mail this week (a la Microsoft) pgpT6nvVPKiPC.pgp Description: PGP signature
Re: Alignement on ARM
Have you tried changing struct sockaddr *ifa_addr; with struct sockaddr *ifa_addr __attribute__((aligned(2))); in your declaration of struct ifaddrs? That attribute lets you force an arbitrary alignment for a variable. Cheers, Luca -- To UNSUBSCRIBE, email to debian-arm-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/aanlktikgud4dfshuatjawyqwn2wxf8fsznj4azagz...@mail.gmail.com
Alignement on ARM
Hi! lldpd, a program of my own, available on https://trac.luffy.cx/lldpd/ , is doing some unaligned memory access. There is no problem on x86 but I have a user that runs into trouble with ARM. I am novice on ARM architecture. I have understood the alignment problem and I have modified pointer arithmetic to use memcpy when alignment issues may occur. However, I would like to test myself the result. I have followed the howto available here: http://www.aurel32.net/info/debian_arm_qemu.php to setup an ARM system. However, I have no alignment issues with this platform. What kind of ARM platform may I setup that would produce no error but incorrect data when reading unaligned int? Crosspost to my address would be appreciated. Thanks for any help on this! -- BOFH excuse #143: had to use hammer to free stuck disk drive heads. pgpMfNA5Rs2iD.pgp Description: PGP signature
Re: Alignement on ARM
On 3/7/09, Vincent Bernat ber...@debian.org wrote: lldpd, a program of my own, available on https://trac.luffy.cx/lldpd/ , is doing some unaligned memory access. What kind of ARM platform may I setup that would produce no error but incorrect data when reading unaligned int? It depends on the setting of /proc/cpu/alignment. It is normally set to 0, which silently ignores non-aligned accesses and returns garbled data. If you echo 5 /proc/cpu/alignment it should generate a Bus Error instead. I keep a system set up in this mode with public acess for developers. Please write privately suggesting username if access to this would be useful. M -- To UNSUBSCRIBE, email to debian-arm-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Re: Alignement on ARM
On 3/7/09, Vincent Bernat ber...@debian.org wrote: I have followed the howto available here: http://www.aurel32.net/info/debian_arm_qemu.php to setup an ARM system. However, I have no alignment issues with this platform. What kind of ARM platform may I setup that would produce no error but incorrect data when reading unaligned int? It turns out that qemu does not emulate the ARM misaligned word access behaviour, but has the same behaviour as the host machine's instruction set. You will need to use real ARM hardware. M -- To UNSUBSCRIBE, email to debian-arm-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Re: Alignement on ARM
OoO Peu avant le début de l'après-midi du samedi 07 mars 2009, vers 13:40, Martin Guy martinw...@yahoo.it disait : I have followed the howto available here: http://www.aurel32.net/info/debian_arm_qemu.php to setup an ARM system. However, I have no alignment issues with this platform. What kind of ARM platform may I setup that would produce no error but incorrect data when reading unaligned int? It turns out that qemu does not emulate the ARM misaligned word access behaviour, but has the same behaviour as the host machine's instruction set. You will need to use real ARM hardware. I was becoming crazy to try to get an unaligned access. Thanks for the tip! :) I keep a system set up in this mode with public acess for developers. Please write privately suggesting username if access to this would be useful. Unfortunately, I need root access to be able to inject packets. I will try to get access to some NSLU2 platform. Thanks for helping! -- BOFH excuse #313: your process is not ISO 9000 compliant pgpXnx7Fnc32N.pgp Description: PGP signature
Re: Alignement on ARM
[off list] On 3/7/09, Vincent Bernat ber...@debian.org wrote: Unfortunately, I need root access to be able to inject packets. I will try to get access to some NSLU2 platform. Mmm, not that I don't trust you but I have to guarantee the service for other users. If you get stuck, let me know, as I can copy the nfsroot for a different test board, let you rampage in that and then reboot it back into its original rootfs when you have finished. Good luck M -- To UNSUBSCRIBE, email to debian-arm-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org