From: Jeremie Courreges-Anglas References: <8737brk7vm.fsf@...2274...> <20170526165606.GQ97029@...1296...> <877f0jvmwi.fsf@...2274...> <20170610174642.GK97029@...1296...> <8760g3u3m2.fsf@...2274...> <20170610181126.GN97029@...1296...> Date: Sat, 10 Jun 2017 21:10:13 +0200 Message-ID: <87ink3slpm.fsf@...2274...> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Subject: Re: [Openvpn-devel] Alignement crash on sparc64 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gert Doering Cc: openvpn-devel@lists.sourceforge.net --=-=-= Content-Type: text/plain Gert Doering writes: > Hi, > > On Sat, Jun 10, 2017 at 07:58:13PM +0200, Jeremie Courreges-Anglas wrote: >> OpenBSD: >> >> struct ip { >> #if _BYTE_ORDER == _LITTLE_ENDIAN >> u_int ip_hl:4, /* header length */ >> ip_v:4; /* version */ >> #endif >> #if _BYTE_ORDER == _BIG_ENDIAN >> u_int ip_v:4, /* version */ >> ip_hl:4; /* header length */ >> #endif >> ... >> >> u_char -> u_int. Dunno if it matters to the compiler, I can take a look >> later. > > I'm fairly sure it does matter. An int is likely to be at least 16 bit, > maybe 32 bit, so the compiler can assume "word aligned" - and *boom*. It doesn't seem to matter that much. Which aligns well with the "bit fields are nasty" experience people seem to have. From a quick glance at the c99 standard, I couldn't find language about this. "A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type." Here's a snippet: --8<-- #include #include #include #include struct ip { #if _BYTE_ORDER == _LITTLE_ENDIAN u_int ip_hl:4, /* header length */ ip_v:4; /* version */ #endif #if _BYTE_ORDER == _BIG_ENDIAN u_int ip_v:4, /* version */ ip_hl:4; /* header length */ #endif u_int8_t ip_tos; /* type of service */ u_int16_t ip_len; /* total length */ u_int16_t ip_id; /* identification */ u_int16_t ip_off; /* fragment offset field */ #define IP_RF 0x8000 /* reserved fragment flag */ #define IP_DF 0x4000 /* dont fragment flag */ #define IP_MF 0x2000 /* more fragments flag */ #define IP_MF 0x2000 /* more fragments flag */ #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ u_int8_t ip_ttl; /* time to live */ u_int8_t ip_p; /* protocol */ u_int16_t ip_sum; /* checksum */ struct in_addr ip_src, ip_dst; /* source and dest address */ }; struct ip_freebsd { #if _BYTE_ORDER == _LITTLE_ENDIAN u_char ip_hl:4, /* header length */ ip_v:4; /* version */ #endif #if _BYTE_ORDER == _BIG_ENDIAN u_char ip_v:4, /* version */ ip_hl:4; /* header length */ #endif u_int8_t ip_tos; /* type of service */ u_int16_t ip_len; /* total length */ u_int16_t ip_id; /* identification */ u_int16_t ip_off; /* fragment offset field */ #define IP_RF 0x8000 /* reserved fragment flag */ #define IP_DF 0x4000 /* dont fragment flag */ #define IP_MF 0x2000 /* more fragments flag */ #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ u_int8_t ip_ttl; /* time to live */ u_int8_t ip_p; /* protocol */ u_int16_t ip_sum; /* checksum */ struct in_addr ip_src, ip_dst; /* source and dest address */ }; void f(void *p) { struct ip *iph; iph = (struct ip *)p; if (iph->ip_v == 6) abort(); } void g(void *p) { struct ip_freebsd *iph; iph = (struct ip_freebsd *)p; if (iph->ip_v == 6) abort(); } --8<-- gcc-4.2.1 (system compiler on OpenBSD) (gdb) disas f Dump of assembler code for function f: 0x0000000000000040 : save %sp, -208, %sp 0x0000000000000044 : ld [ %i0 ], %g1 0x0000000000000048 : sethi %hi(0xf0000000), %g2 0x000000000000004c : sethi %hi(0x60000000), %g3 0x0000000000000050 : and %g1, %g2, %g1 0x0000000000000054 : cmp %g1, %g3 0x0000000000000058 : be,pn %icc, 0x68 0x000000000000005c : nop 0x0000000000000060 : rett %i7 + 8 0x0000000000000064 : nop 0x0000000000000068 : call 0x68 0x000000000000006c : nop 0x0000000000000070 : nop End of assembler dump. (gdb) disas g Dump of assembler code for function g: 0x0000000000000000 : save %sp, -208, %sp 0x0000000000000004 : ld [ %i0 ], %g1 0x0000000000000008 : sethi %hi(0xf0000000), %g2 0x000000000000000c : sethi %hi(0x60000000), %g3 0x0000000000000010 : and %g1, %g2, %g1 0x0000000000000014 : cmp %g1, %g3 0x0000000000000018 : be,pn %icc, 0x28 0x000000000000001c : nop 0x0000000000000020 : rett %i7 + 8 0x0000000000000024 : nop 0x0000000000000028 : call 0x28 0x000000000000002c : nop 0x0000000000000030 : nop End of assembler dump. gcc 4.9.4: (gdb) disas f Dump of assembler code for function f: 0x0000000000000000 : save %sp, -176, %sp 0x0000000000000004 : ld [ %i0 ], %g2 0x0000000000000008 : sethi %hi(0xf0000000), %g1 0x000000000000000c : and %g2, %g1, %g1 0x0000000000000010 : sethi %hi(0x60000000), %g2 0x0000000000000014 : cmp %g1, %g2 0x0000000000000018 : be,pn %icc, 0x28 0x000000000000001c : nop 0x0000000000000020 : rett %i7 + 8 0x0000000000000024 : nop 0x0000000000000028 : call 0x28 0x000000000000002c : nop 0x0000000000000030 : nop End of assembler dump. (gdb) disas g Dump of assembler code for function g: 0x0000000000000040 : save %sp, -176, %sp 0x0000000000000044 : ld [ %i0 ], %g2 0x0000000000000048 : sethi %hi(0xf0000000), %g1 0x000000000000004c : and %g2, %g1, %g1 0x0000000000000050 : sethi %hi(0x60000000), %g2 0x0000000000000054 : cmp %g1, %g2 0x0000000000000058 : be,pn %icc, 0x68 0x000000000000005c : nop 0x0000000000000060 : rett %i7 + 8 0x0000000000000064 : nop 0x0000000000000068 : call 0x68 0x000000000000006c : nop 0x0000000000000070 : nop End of assembler dump. So, no difference in the generated code between the OpenBSD and FreeBSD structures. -- jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF DDCC 0DFA 74AE 1524 E7EE --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEUTWSwa02UpMr393MDfp0rhUk5+4FAlk8RBUACgkQDfp0rhUk 5+7UaA/7BVBk1lrfsS6vL15wmgu6bfHM+P+LswLmrge7Q7IR+EQH5niw6DEtafpG Yx0bMKzXbSYwTxJC/6juxqlyAdOWcZ90dKaC3x9P8djXCWFKryZNk6YFdUHtSc5x hIJ0kFIZP4JiXBpgiaskONjnD7xCc9y8AT+nxwWOyFfmKhHh9ZHmlC9SxBHgy0sy RXNi5uFKvTtqy3BgPM++vgHuygHTkBsVHENRtrJpSWONToEkHZphVPp/Dw3khFQb JP/asjrm69kcWkm950TA/JdAv91fnNORleC/+12R5G5TLqIxMF97BBcnbXeJ5OW8 LWtsvxi5QhHcfwOYFQgP4Yosfq40oPEPj7ck6a3HDHrn78WqYJpliOtoDwyIKVWc cWNo4OuYsKrWMR7/fqrx0UQ1UiK3VXcxRaWA2im9edLx+1pJd2pwW383NvzmihVg L3WYF7jLxxwfxnlQ2DmmfMbET3a0dETk5H4oiVl8zyIaFGmuMImV7F7v1amfC3Ge ZkBrS8NlNi6r132wVhkp3FbDz/gWA7gbqU7ftDd0TTo4GOqIv3fQjC3Q/g3TC28Z Vn+xhc9yXGbhorshNLwZpedCHMhdx5ymwoOjtjPR+hOepyl98CVeXoW8Ujrfduga TvrikttFQAfZk+FhD3cG0k0RjcIeUMX2v40xMKhYqMPWzh5zcFE= =bKmp -----END PGP SIGNATURE----- --=-=-=--