Project

General

Profile

Actions

Bug #6164

closed

warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

Added by fixeria 8 months ago. Updated 7 months ago.

Status:
Resolved
Priority:
Normal
Assignee:
arehbein
Target version:
-
Start date:
08/31/2023
Due date:
% Done:

100%

Spec Reference:

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 issues

Related to libosmo-netif - Feature #5753: io_uring support in libosmo-netifResolvedHoernchen11/09/2022

Actions
Actions #1

Updated by fixeria 8 months ago

  • Assignee set to arehbein

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.

Actions #2

Updated by fixeria 8 months ago

  • Related to Feature #5753: io_uring support in libosmo-netif added
Actions #3

Updated by arehbein 8 months ago

  • 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...

  1. Add the `-fno-strict-aliasing` flag to libosmo-netif (and make sure we don't have more than the appropriate optimization for that setting)
  2. `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).
  3. Use offset defines to get the values
  4. 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)).
Actions #5

Updated by arehbein 7 months ago

  • % Done changed from 0 to 80
Actions #6

Updated by arehbein 7 months ago

  • Status changed from Feedback to Resolved
  • % Done changed from 80 to 100

Should be fixed now after submitting the mentioned patch

Actions

Also available in: Atom PDF

Add picture from clipboard (Maximum size: 48.8 MB)