Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 859922 - media-libs/libvisual-0.4.0-r3 fails to compile (lto): lv_color.c:281:18: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
Summary: media-libs/libvisual-0.4.0-r3 fails to compile (lto): lv_color.c:281:18: erro...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal
Assignee: Sebastian Pipping
URL: https://github.com/Libvisual/libvisua...
Whiteboard:
Keywords:
Depends on:
Blocks: lto
  Show dependency tree
 
Reported: 2022-07-22 06:50 UTC by Agostino Sarubbo
Modified: 2024-09-02 01:14 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
build.log (build.log,100.46 KB, text/plain)
2022-07-22 06:50 UTC, Agostino Sarubbo
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Agostino Sarubbo gentoo-dev 2022-07-22 06:50:14 UTC
https://blogs.gentoo.org/ago/2020/07/04/gentoo-tinderbox/

Issue: media-libs/libvisual-0.4.0-r3 fails to compile (lto).
Discovered on: amd64 (internal ref: lto_tinderbox)

NOTE:
This machine uses lto with CFLAGS=-flto -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing
Comment 1 Agostino Sarubbo gentoo-dev 2022-07-22 06:50:16 UTC
Created attachment 793010 [details]
build.log

build log and emerge --info
Comment 2 Agostino Sarubbo gentoo-dev 2022-07-26 09:02:30 UTC
Here is a bit of explanation:

-Werror=lto-type-mismatch:
User to find possible runtime issues in packages. It likely means the package is unsafe to build & use with LTO.
For projects using the same identifier but with different types across different files, they must be fixed to be consistent across the codebase.

-Werror=odr:
Used to find possible runtime issues in packages. These bugs are a problem anyway but may be even worse when combined with LTO. C++ code must comply with the One Definition Rule (ODR) - see https://en.cppreference.com/w/cpp/language/definition#One_Definition_Rule.

-Werror=strict-aliasing:
Used to find possible runtime issues in packages. These bugs are a problem anyway but may be even worse when combined with LTO.

Workarounds:
- If upstream is friendly and still active, file a bug upstream. For emulators, codecs, games, or multimedia packages, it may be worth just applying a workaround instead, as upstreams sometimes aren't receptive to these bugs (VALID FOR ALL).
- Use the new 'filter-lto' from flag-o-matic.eclass as it's likely to be unsafe with LTO (VALID FOR lto-type-mismatch - odr).
- Fix it yourself if interested, of course (VALID FOR ALL).
- Append-flags -fno-strict-aliasing (VALID FOR strict-aliasing).
- Use memcpy() but a union is sometimes suitable too (VALID FOR strict-aliasing).
- -fstrict-aliasing is implied by -O2, so this must be addressed in some form (VALID FOR strict-aliasing).

See also: https://marc.info/?l=gentoo-dev&m=165639574126280&w=2
Comment 3 Sebastian Pipping gentoo-dev 2022-11-20 17:36:55 UTC
The issue with strict aliasing here seems to be this:

There is a struct "_color16" containing a bit field defined as:

>> typedef struct {
>> 	uint16_t b:5, g:6, r:5;
>> } _color16;

Now function "visual_color_to_uint16" uses a pointer cast to be able to access the combined uint16_t value in the return statement:

>> uint16_t visual_color_to_uint16 (VisColor *color)
>> {
>> 	_color16 colors;
>> 
>> 	visual_log_return_val_if_fail (color != NULL, 0);
>> 
>> 	colors.r = color->r >> 2;
>> 	colors.g = color->g >> 3;
>> 	colors.b = color->b >> 2;
>> 
>> 	return *((uint16_t *) &colors);
>> }

One way to workaround the strict aliasing violation would be to define a union that has two members — one of type _color16, one of type uint16_t — and access the underlaying bytes using that union (and I now see you did mention use of a union as an option).

While it's good to know about the violation of strict aliasing, I'm not sure this is a practical issue rather than just an anomaly.  What I'm trying to say is: Does this even need a fix in Gentoo and/or upstream?  What do you suggest for moving forward?
Comment 4 Pacho Ramos gentoo-dev 2022-11-20 19:46:20 UTC
In Fedora they are disabling strict-aliasing as a workaround  https://src.fedoraproject.org/rpms/libvisual/blob/rawhide/f/libvisual.spec
Comment 5 David Seifert gentoo-dev 2022-11-20 20:45:45 UTC
(In reply to Sebastian Pipping from comment #3)
> The issue with strict aliasing here seems to be this:
> 
> There is a struct "_color16" containing a bit field defined as:
> 
> >> typedef struct {
> >> 	uint16_t b:5, g:6, r:5;
> >> } _color16;
> 
> Now function "visual_color_to_uint16" uses a pointer cast to be able to
> access the combined uint16_t value in the return statement:
> 
> >> uint16_t visual_color_to_uint16 (VisColor *color)
> >> {
> >> 	_color16 colors;
> >> 
> >> 	visual_log_return_val_if_fail (color != NULL, 0);
> >> 
> >> 	colors.r = color->r >> 2;
> >> 	colors.g = color->g >> 3;
> >> 	colors.b = color->b >> 2;
> >> 
> >> 	return *((uint16_t *) &colors);
> >> }
> 
> One way to workaround the strict aliasing violation would be to define a
> union that has two members — one of type _color16, one of type uint16_t —
> and access the underlaying bytes using that union (and I now see you did
> mention use of a union as an option).
> 
> While it's good to know about the violation of strict aliasing, I'm not sure
> this is a practical issue rather than just an anomaly.  What I'm trying to
> say is: Does this even need a fix in Gentoo and/or upstream?  What do you
> suggest for moving forward?

It _will_ break at some point, so yes, please fix it, or we might have to mask it again, given how obvious the transgression is.
Comment 6 Sebastian Pipping gentoo-dev 2022-11-21 01:58:20 UTC
Made a pull request upstream for review now…
Comment 7 Larry the Git Cow gentoo-dev 2022-12-06 00:17:05 UTC
The bug has been closed via the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=577e8bde7cef90e98d5fcdca120abd09318fc9da

commit 577e8bde7cef90e98d5fcdca120abd09318fc9da
Author:     Sebastian Pipping <sping@gentoo.org>
AuthorDate: 2022-12-05 23:57:42 +0000
Commit:     Sebastian Pipping <sping@gentoo.org>
CommitDate: 2022-12-06 00:15:44 +0000

    media-libs/libvisual: 0.4.1 + EAPI 8
    
    Closes: https://bugs.gentoo.org/859922
    Closes: https://bugs.gentoo.org/871015
    Closes: https://bugs.gentoo.org/882811
    Signed-off-by: Sebastian Pipping <sping@gentoo.org>

 media-libs/libvisual/Manifest               |  1 +
 media-libs/libvisual/libvisual-0.4.1.ebuild | 50 +++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)