Bug #6164
closed
warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
Added by fixeria 9 months ago.
Updated 8 months ago.
Description
I am seeing these warnings when building libosmo-netif with gcc v13.2.1:
In file included from ../../../src/libosmo-netif/src/ipa.c:34:
../../../src/libosmo-netif/src/ipa.c: In function 'ipa_check_pull_headers':
../../../src/libosmo-netif/include/osmocom/netif/ipa.h:26:42: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
26 | #define OSMO_IPA_MSGB_CB(__msg) ((struct osmo_ipa_msgb_cb *)&((__msg)->cb[0]))
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../src/libosmo-netif/include/osmocom/netif/ipa.h:27:41: note: in expansion of macro 'OSMO_IPA_MSGB_CB'
27 | #define osmo_ipa_msgb_cb_proto(__x) OSMO_IPA_MSGB_CB(__x)->proto
| ^~~~~~~~~~~~~~~~
../../../src/libosmo-netif/src/ipa.c:385:9: note: in expansion of macro 'osmo_ipa_msgb_cb_proto'
385 | osmo_ipa_msgb_cb_proto(msg) = ih->proto;
| ^~~~~~~~~~~~~~~~~~~~~~
../../../src/libosmo-netif/include/osmocom/netif/ipa.h:26:42: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
26 | #define OSMO_IPA_MSGB_CB(__msg) ((struct osmo_ipa_msgb_cb *)&((__msg)->cb[0]))
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../src/libosmo-netif/include/osmocom/netif/ipa.h:28:41: note: in expansion of macro 'OSMO_IPA_MSGB_CB'
28 | #define osmo_ipa_msgb_cb_proto_ext(__x) OSMO_IPA_MSGB_CB(__x)->proto_ext
| ^~~~~~~~~~~~~~~~
../../../src/libosmo-netif/src/ipa.c:397:9: note: in expansion of macro 'osmo_ipa_msgb_cb_proto_ext'
397 | osmo_ipa_msgb_cb_proto_ext(msg) = msg->data[0];
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
libosmo-netif.git aede010687c87b8d1b47ca0643528cf39bbda406
Related commit:
commit 8355b65d9bcfd50c3ae666824464ab4789863345
Author: arehbein <arehbein@sysmocom.de>
Date: Sat Jul 8 17:24:45 2023 +0200
ipa: Add segmentation callback
Assigning to @arehbein.
- Related to Feature #5753: io_uring support in libosmo-netif added
- Status changed from New to Feedback
I wrote a comment about that here (originally, I was using offset defines... but I was told a packed struct would be preferred):
https://gerrit.osmocom.org/c/libosmo-netif/+/33652/comments/c2295e0a_11bb3927
Back then I must have done a quick check on the libosmocore build configuration of a local repo:
libosmocore$ ag aliasing
libosmogb.pc.in
11:Cflags: -I${includedir}/ -fno-strict-aliasing
src/gb/Makefile.am
7:AM_CFLAGS = -Wall -fno-strict-aliasing \
However I can't find that flag in my `libosmo-netif` repositories. Shouldn't this be unified across Osmocom projects?
Also, does it matter (what optimizations do we allow)?
What's odd to me is that the error only pops up with the new version of gcc. It seems like an obvious violation of strict aliasing principles that should have been detected before.
Anyways, what's the preferred way of handling this...
- Add the `-fno-strict-aliasing` flag to libosmo-netif (and make sure we don't have more than the appropriate optimization for that setting)
- `memcpy` the `cb` buffer into a packed struct of the type previously used for dereferencing (probably not a good idea here, because this would introduce an additional memcpy to quite performance-critical code).
- Use offset defines to get the values
- Turn `cb` into a union of packed structs defined by some macro that we can extend over time if needed (iirc, unions can be used to type-pun when gcc is used; vanilla C is more strict (I think it's only allowed to read with the same union type that was previously used to write to the same memory object)).
- % Done changed from 0 to 80
- Status changed from Feedback to Resolved
- % Done changed from 80 to 100
Should be fixed now after submitting the mentioned patch
Also available in: Atom
PDF