slirp: fix -Waddress-of-packed-member warnings
cover slirp: fix -Waddress-of-packed-member warnings
These patches fix the clang -Waddress-of-packed-member warnings in the slirp code. For slirp, the structs marked as packed are already naturally packed/aligned, so adding the 'packed' attribute doesn't change their layout but means the compiler has to assume they might be misaligned. Instead, for the structs which map to on-the-wire packet formats, we add build time assertions that they have the sizes we think they should.
The 'struct ipq' and 'struct ipasfrag' aren't on-the-wire packet representations, so it's not clear why they were marked packed in the first place. (They contain host pointers and so have a size that depends on the host.) Here we just assert the only thing that's documented as a layout restriction:
that struct ipq's frag_link and struct ipasfrag's ipf_link are at the same offset.
Together, this fixes all the -Waddress-of-packed-member warnings in slirp, which I think was the only set remaining without patches at least being on-list. (Some of the block/ warning fixes are still working their way through the relevant trees.)
thanks
-- PMM
Peter Maydell (2):
slirp: Avoid marking naturally packed structs as QEMU_PACKED
slirp: Don't mark struct ipq or struct ipasfrag as packed
slirp/ip.h | 7 +++++-- slirp/ip6.h | 12 ++++++++++-- slirp/ip6_icmp.h | 20 +++++++++++++++----- 3 files changed, 30 insertions(+), 9 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/2 slirp: Avoid marking naturally packed structs as QEMU_PACKED
Various ipv6 structs in the slirp headers are marked QEMU_PACKED, but they are actually naturally aligned and will have no padding in them. Instead of marking them with the 'packed' attribute, assert at compile time that they are the size we expect. This allows us to take the address of fields within the structs without risking undefined behaviour, and suppresses clang -Waddress-of-packed-member warnings.
--- slirp/ip6.h | 12 ++++++++++-- slirp/ip6_icmp.h | 20 +++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-)
slirp/ip6.h +10 -2
@@ -133,7 +133,7 @@ struct ip6 {uint8_t ip_nh;/* next header */ uint8_t ip_hl;/* hop limit */ struct in6_addr ip_src, ip_dst;/* source and dest address */ -}QEMU_PACKED ; +};/* * IPv6 pseudo-header used by upper-layer protocols
@@ -145,7 +145,15 @@ struct ip6_pseudohdr {uint16_t ih_zero_hi;/* zero */ uint8_t ih_zero_lo;/* zero */ uint8_t ih_nh;/* next header */ -}QEMU_PACKED ; +}; +/* +* We don't want to mark these ip6 structs as packed as they are naturally +* correctly aligned; instead assert that there is no stray padding. +* If we marked the struct as packed then we would be unable to take +* the address of any of the fields in it. +*/ +QEMU_BUILD_BUG_ON (sizeof (struct ip6 ) !=40 ); +QEMU_BUILD_BUG_ON (sizeof (struct ip6_pseudohdr ) !=40 );#endif
slirp/ip6_icmp.h +15 -5
@@ -48,12 +48,16 @@ struct ndp_ra { /* Router Advertisement Message */uint16_t lifetime;/* Router Lifetime */ uint32_t reach_time;/* Reachable Time */ uint32_t retrans_time;/* Retrans Timer */ -}QEMU_PACKED ; +}; + +QEMU_BUILD_BUG_ON (sizeof (struct ndp_ra ) !=12 );struct ndp_ns {/* Neighbor Solicitation Message */ uint32_t reserved;struct in6_addr target;/* Target Address */ -}QEMU_PACKED ; +}; + +QEMU_BUILD_BUG_ON (sizeof (struct ndp_ns ) !=20 );struct ndp_na {/* Neighbor Advertisement Message */ #if G_BYTE_ORDER ==G_BIG_ENDIAN
@@ -72,13 +76,17 @@ struct ndp_na { /* Neighbor Advertisement Message */ reserved_lo:24 ;#endif struct in6_addr target;/* Target Address */ -}QEMU_PACKED ; +}; + +QEMU_BUILD_BUG_ON (sizeof (struct ndp_na ) !=20 );struct ndp_redirect {uint32_t reserved;struct in6_addr target;/* Target Address */ struct in6_addr dest;/* Destination Address */ -}QEMU_PACKED ; +}; + +QEMU_BUILD_BUG_ON (sizeof (struct ndp_redirect ) !=36 );/* * Structure of an icmpv6 header.
@@ -103,7 +111,9 @@ struct icmp6 {#define icmp6_nns icmp6_body.ndp_ns#define icmp6_nna icmp6_body.ndp_na#define icmp6_redirect icmp6_body.ndp_redirect -}QEMU_PACKED ; +}; + +QEMU_BUILD_BUG_ON (sizeof (struct icmp6 ) !=40 );#define ICMP6_MINLEN 4#define ICMP6_ERROR_MINLEN 8
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH 1/2] slirp: Avoid marking naturally packed structs as QEMU_PACKED
Quoted 15 lines · Peter Maydell
Various ipv6 structs in the slirp headers are marked
QEMU_PACKED, but they are actually naturally aligned and will have no padding in them. Instead of marking them with the 'packed' attribute, assert at compile time that they are the size we expect. This allows us to take the address of fields within the structs without risking undefined behaviour, and suppresses clang -Waddress-of-packed-member warnings.Signed-off-by Peter Maydell <[email protected]>--- slirp/ip6.h | 12 ++++++++++-- slirp/ip6_icmp.h | 20 +++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-)
slirp/ip6.h struct ip6_pseudohdr { · line 157
Quoted 8 lines
slirp/ip6.h +8
-0+/* +* We don't want to mark these ip6 structs as packed as they are naturally +* correctly aligned; instead assert that there is no stray padding. +* If we marked the struct as packed then we would be unable to take +* the address of any of the fields in it. +*/ +QEMU_BUILD_BUG_ON (sizeof (struct ip6 ) !=40 ); +QEMU_BUILD_BUG_ON (sizeof (struct ip6_pseudohdr ) !=40 );
Nice. (And IIRC, the IPv6 designers tried to pick natural alignments on purpose)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH 2/2 slirp: Don't mark struct ipq or struct ipasfrag as packed
In reply to Peter MaydellThere is no reason to mark the struct ipq and struct ipasfrag as
packed: they are naturally aligned anyway, and are not representing any on-the-wire packet format. Indeed they vary in size depending on the size of pointers on the host system, because the 'struct qlink' members include 'void *' fields.
Dropping the 'packed' annotation fixes clang -Waddress-of-packed-member warnings and probably lets the compiler generate better code too.
The only thing we do care about in the layout of the struct is that the frag_link matches up with the ipf_link of the struct ipasfrag, as documented in the comment on that struct; assert at build time that this is the case.
--- slirp/ip.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
slirp/ip.h +5 -2
@@ -217,7 +217,7 @@ struct ipq {uint8_t ipq_p;/* protocol of this fragment */ uint16_t ipq_id;/* sequence id for reassembly */ struct in_addr ipq_src,ipq_dst; -}QEMU_PACKED ; +};/* * Ip header, when holding a fragment.
@@ -227,7 +227,10 @@ struct ipq {struct ipasfrag {struct qlink ipf_link;struct ip ipf_ip; -}QEMU_PACKED ; +}; + +QEMU_BUILD_BUG_ON (offsetof(struct ipq , frag_link) != + offsetof(struct ipasfrag , ipf_link));#define ipf_off ipf_ip.ip_off#define ipf_tos ipf_ip.ip_tos
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH 2/2] slirp: Don't mark struct ipq or struct ipasfrag as packed
slirp/ip.h
Quoted 23 lines · Peter Maydell
There is no reason to mark the struct ipq and struct ipasfrag as
packed: they are naturally aligned anyway, and are not representing any on-the-wire packet format. Indeed they vary in size depending on the size of pointers on the host system, because the 'struct qlink' members include 'void *' fields.Dropping the 'packed' annotation fixes clang -Waddress-of-packed-member warnings and probably lets the compiler generate better code too.
The only thing we do care about in the layout of the struct is that the
frag_linkmatches up with theipf_linkof the struct ipasfrag, as documented in the comment on that struct; assert at build time that this is the case.Signed-off-by Peter Maydell <[email protected]>--- slirp/ip.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)slirp/ip.h +0
-0
Just here there's a misleading comment
* size 28 bytes
that should be purged
Quoted 11 lines
slirp/ip.h +1
-1@@ -217,7 +217,7 @@ struct ipq {uint8_t ipq_p;/* protocol of this fragment */ uint16_t ipq_id;/* sequence id for reassembly */ struct in_addr ipq_src,ipq_dst; -}QEMU_PACKED ; +};/* * Ip header, when holding a fragment. @@ -227,7 +227,10 @@ struct ipq {struct ipasfrag {
slirp/ip.h struct ipq { · line 236
Quoted 10 lines
slirp/ip.h +4
-1struct qlink ipf_link;struct ip ipf_ip; -}QEMU_PACKED ; +}; + +QEMU_BUILD_BUG_ON (offsetof(struct ipq , frag_link) != + offsetof(struct ipasfrag , ipf_link));#define ipf_off ipf_ip.ip_off#define ipf_tos ipf_ip.ip_tos
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [Qemu-devel] [PATCH 2/2] slirp: Don't mark struct ipq or struct ipasfrag as packed
Quoted 28 lines · Daniel P. Berrangé
slirp/ip.h
Quoted 23 lines · Peter Maydell
There is no reason to mark the struct ipq and struct ipasfrag as
packed: they are naturally aligned anyway, and are not representing any on-the-wire packet format. Indeed they vary in size depending on the size of pointers on the host system, because the 'struct qlink' members include 'void *' fields.Dropping the 'packed' annotation fixes clang -Waddress-of-packed-member warnings and probably lets the compiler generate better code too.
The only thing we do care about in the layout of the struct is that the
frag_linkmatches up with theipf_linkof the struct ipasfrag, as documented in the comment on that struct; assert at build time that this is the case.Signed-off-by Peter Maydell <[email protected]>--- slirp/ip.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)slirp/ip.h +0
-0Just here there's a misleading comment
Quoted 3 lines
* size 28 bytes
that should be purged
I noticed that one but left it alone on the basis that I wasn't sure what to replace it with, and as it is it provides the useful information of "somebody at some point thought the size of this struct mattered but they were badly confused, so tread with caution" :-)
thanks
-- PMM
Re: [Qemu-devel] [PATCH 2/2] slirp: Don't mark struct ipq or struct ipasfrag as packed
In reply to Peter MaydellQuoted 14 lines · Peter Maydell
There is no reason to mark the struct ipq and struct ipasfrag as
packed: they are naturally aligned anyway, and are not representing any on-the-wire packet format. Indeed they vary in size depending on the size of pointers on the host system, because the 'struct qlink' members include 'void *' fields.Dropping the 'packed' annotation fixes clang -Waddress-of-packed-member warnings and probably lets the compiler generate better code too.
The only thing we do care about in the layout of the struct is that the
frag_linkmatches up with theipf_linkof the struct ipasfrag, as documented in the comment on that struct; assert at build time that this is the case.
Quoted 4 lines
Signed-off-by Peter Maydell <[email protected]>--- slirp/ip.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Quoted 16 lines · Peter Maydell
These patches fix the clang -Waddress-of-packed-member warnings in the slirp code. For slirp, the structs marked as packed are already naturally packed/aligned, so adding the 'packed' attribute doesn't change their layout but means the compiler has to assume they might be misaligned. Instead, for the structs which map to on-the-wire packet formats, we add build time assertions that they have the sizes we think they should.
The 'struct ipq' and 'struct ipasfrag' aren't on-the-wire packet representations, so it's not clear why they were marked packed in the first place. (They contain host pointers and so have a size that depends on the host.) Here we just assert the only thing that's documented as a layout restriction:
that struct ipq'sfrag_linkand struct ipasfrag'sipf_linkare at the same offset.
Quoted 4 lines
Together, this fixes all the -Waddress-of-packed-member warnings in slirp, which I think was the only set remaining without patches at least being on-list. (Some of the
block/warning fixes are still working their way through the relevant trees.)
How does this series play with the ongoing effort to make libslirp an independent project? I'm assuming your use of QEMU_BUILD_BUG_ON() will have to be translated into whatever libslirp-specific mechanism we want to split off into that project.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Samuel Thibault
AppliedHello,
Peter Maydell, le mar. 22 janv. 2019 18:18:20 +0000, a ecrit:
Quoted 2 lines
These patches fix the clang -Waddress-of-packed-member warnings in the slirp code.
Applied to my tree, thanks!
Eric Blake, le mar. 22 janv. 2019 13:27:32 -0600, a ecrit:
Quoted 4 lines
How does this series play with the ongoing effort to make libslirp an independent project? I'm assuming your use of
QEMU_BUILD_BUG_ON()will have to be translated into whatever libslirp-specific mechanism we want to split off into that project.
Indeed, but I guess libslirp will want to have such kind of macro anyway, so I don't see a reason to hold this change.
Samuel