Skip to content

Conversation

@aescolar
Copy link
Member

@aescolar aescolar commented Jul 1, 2025

toolchain/gcc.h: Add macro to get address of unaligned member

In principle getting the address of an unaligned structure member
is undefined behaviour, and clang warns as much.
Let's provide a macro which can return such an address.

subsys/net/ip/tcp: Avoid directly accessing address of unaligned struct

Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

subsys/net/l2/ieee802154: Do not access addr of unaligned struct

Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.


#endif

#define UNALIGNED_MEMBER_ADDR(_var, _member) ((__typeof__(_var->_member) *) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really introduce this kind of macros if they provide access to unaligned member? @rlubos is currently fixing issues reported by UBSAN in part of #90882

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but these are new warnings generated by Clang, and not part of the UBSAN runtime checks. Essentially this is an extension to:
#39192

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional explanation: here clang is essentially saying "you are creating a pointer to a uint16_t that is not properly 2-byte aligned, careful with what you will do with it". This is a bit different from other cases where this macro (and UBSAN issues like the ones in #90954) where we were placing non-packed structs inside a packed one. In this case this is a native type (uint16_t) inside a packed struct.

carlescufi
carlescufi previously approved these changes Jul 1, 2025
In principle getting the address of an unaligned structure member
is undefined behaviour, and clang warns as much.
Let's provide a macro which can return such an address.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
aescolar added 2 commits July 1, 2025 15:23
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
rlubos
rlubos previously approved these changes Jul 1, 2025
jukkar
jukkar previously approved these changes Jul 1, 2025
@aescolar aescolar added this to the v4.2.0 milestone Jul 1, 2025
@carlescufi carlescufi added bug The issue is a bug, or the PR is fixing a bug Hotfix Fix for issues blocking development, i.e. upstream CI issues, tests failing in upstream CI , etc. labels Jul 1, 2025
carlescufi
carlescufi previously approved these changes Jul 1, 2025
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
@aescolar aescolar dismissed stale reviews from jukkar and rlubos via 10b7392 July 1, 2025 13:38
aescolar added 3 commits July 1, 2025 15:46
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly
unaligned structures members instead of attempting to directly
get the address as an offset.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
@aescolar aescolar changed the title subsys/net/ip/tcp: Avoid directly accessing address of unaligned struct net: Avoid directly accessing address of unaligned struct Jul 1, 2025
@github-actions github-actions bot added the area: HTTP HTTP client/server support label Jul 1, 2025
@github-actions github-actions bot requested a review from mrodgers-witekio July 1, 2025 13:49
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jul 1, 2025

@danieldegrasse danieldegrasse merged commit 35a1093 into zephyrproject-rtos:main Jul 1, 2025
30 checks passed
@aescolar aescolar deleted the net_unalign branch July 1, 2025 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: HTTP HTTP client/server support area: IEEE 802.15.4 area: Networking area: Toolchains Toolchains bug The issue is a bug, or the PR is fixing a bug Hotfix Fix for issues blocking development, i.e. upstream CI issues, tests failing in upstream CI , etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants