Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 941208 - media-gfx/imagemagick-9999 (and current stable) miscompiled with sys-devel/gcc-13.3.1_p20240614 -O3
Summary: media-gfx/imagemagick-9999 (and current stable) miscompiled with sys-devel/gc...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal
Assignee: Codec Project
URL:
Whiteboard:
Keywords:
Depends on: 942691 942692
Blocks: 915000
  Show dependency tree
 
Reported: 2024-10-09 16:47 UTC by Hadrien Lacour
Modified: 2024-11-02 01:50 UTC (History)
4 users (show)

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


Attachments
emerge --info media-gfx/imagemagick >emerge_info.txt (emerge_info.txt,17.58 KB, text/plain)
2024-10-09 16:47 UTC, Hadrien Lacour
Details
libMagickCore_7_Q16HDRI_la-composite.i.xz (libMagickCore_7_Q16HDRI_la-composite.i.xz,82.62 KB, application/x-xz)
2024-10-10 20:47 UTC, Sam James
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Hadrien Lacour 2024-10-09 16:47:46 UTC
Created attachment 905138 [details]
emerge --info media-gfx/imagemagick >emerge_info.txt

Hello, just hit this bug https://github.com/ImageMagick/ImageMagick/issues/7677 that goes away with -O2, thought people might want to know about it.

Here's my emerge --info.
Comment 1 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-09 21:03:14 UTC
I can take a look but please give full, verbose instructions to reproduce (given I've barely ever used IM)? I'll then probe it tomorrow. Thank you!
Comment 2 Hadrien Lacour 2024-10-09 21:29:40 UTC
The IM bug was updated to be more explicit. But to make it simple:

1. Take the current media-gfx/imagemagick (7.1.1.38-r1)
2. Build it with -O3 in your FLAGS using sys-devel/gcc-13.3.1_p20240614
3. Take any image with transparency, let us say "alpha.png"
4. Run `magick alpha.png -distort Resize 200% out.png`
5. Observe that the resulting out.png is completely black
6. Repeat all of that using -O2 instead to observe the right behaviour (resized image)

As said in the (now closed) bug, I don't know if this is a gcc or IM bug. And using clang instead doesn't reproduce, but that doesn't mean much.

Since I tested using upstream, this is not a bug in our packaging or IM.
Comment 3 Hadrien Lacour 2024-10-09 21:30:15 UTC
Typo; this is not a bug in our packaging of IM.
Comment 4 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-09 21:43:42 UTC
Thanks. Yeah, it's fine that it's not a packaging bug, I'm happy to spend some time looking at it nonetheless.
Comment 5 Hadrien Lacour 2024-10-10 06:10:53 UTC
Well, you're brave. Good luck, you'll need it!
Comment 6 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 09:20:33 UTC
The funny thing is that we had bug 940923 only the other day too - which was good fun.
Comment 7 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 12:19:43 UTC
14+15 (trunk) are fine, bisecting first.
Comment 8 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 14:08:00 UTC
r13-7841-g70639014a69cf5 breaks it, reverting that on releases/gcc-13 fixes it, but that doesn't mean it's actually to blame.

Looking more.
Comment 9 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 14:45:08 UTC
basepoints/gcc-14 works
releases/gcc-13.1.0 works

r13-7841-g70639014a69cf5 got backported for 13.3 to fix https://gcc.gnu.org/PR110315. The patch originally came from https://gcc.gnu.org/PR109695 but it got cherry-picked with a different title at least (not compared contents yet or figured out which commit on 14 it corresponds to).

This means we can't bisect for the fix on 13-vs-14 as 14 was never broken.
Comment 10 Hadrien Lacour 2024-10-10 16:49:30 UTC
Correction: the failing result isn't completely black, transparency is replaced by black.
Comment 11 Hadrien Lacour 2024-10-10 16:57:16 UTC
I manually reduced the '-O3' failure to '-O1 -funswitch-loops', if this can help.
Comment 12 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 17:21:42 UTC
That is indeed helpful, thanks, as it means I can rule out a lot.
Comment 13 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 20:47:00 UTC
Created attachment 905157 [details]
libMagickCore_7_Q16HDRI_la-composite.i.xz

libMagickCore_7_Q16HDRI_la-composite.o is the bad object. Attached preprocessed source.
Comment 14 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 20:55:38 UTC
--- a/libMagickCore_7_Q16HDRI_la-composite.i.141t.lim2
+++ b/libMagickCore_7_Q16HDRI_la-composite.i.141t.lim2
@@ -3402,6 +3402,7 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
     goto <bb 38>; [50.00%]

   <bb 38> [local count: 15364004]:
+  columns.88_174 = (long int) columns_26;

   <bb 4> [local count: 139672780]:
   # y_3 = PHI <_42(38), y_119(41)>
@@ -3433,7 +3434,6 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
     goto <bb 7>; [42.89%]

   <bb 7> [local count: 29952828]:
-  columns.88_174 = (long int) columns_26;
   if (columns.88_174 > 0)
     goto <bb 39>; [89.00%]
   else

[...]
@@ -12857,6 +12857,7 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
     goto <bb 91>; [3.66%]

   <bb 305> [local count: 11561895]:
+  _70 = ABS_EXPR <angle_range_249>;

   <bb 72> [local count: 228832040]:
   # y_1117 = PHI <y_546(311), 0(305)>
@@ -12930,7 +12931,6 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
   goto <bb 88>; [100.00%]

   <bb 79> [local count: 179090238]:
-  _70 = ABS_EXPR <angle_range_249>;
   if (_70 > 9.99999999999999979886647629255615367252843506129522666015e-13)
     goto <bb 80>; [50.00%]
   else

--

--- a/libMagickCore_7_Q16HDRI_la-composite.i.144t.sink1
+++ b/libMagickCore_7_Q16HDRI_la-composite.i.144t.sink1
@@ -3901,6 +3901,7 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
   residual_30 = *.omp_data_i_24(D).residual;
   beta_image_31 = *.omp_data_i_24(D).beta_image;
   alpha_image_32 = *.omp_data_i_24(D).alpha_image;
+  columns.88_174 = (long int) columns_26;

   <bb 4> [local count: 139672780]:
   # y_3 = PHI <_42(38), y_119(41)>
@@ -3937,7 +3938,6 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
     goto <bb 7>; [42.89%]

   <bb 7> [local count: 29952828]:
-  columns.88_174 = (long int) columns_26;
   if (columns.88_174 > 0)
     goto <bb 39>; [89.00%]
   else
@@ -4483,8 +4483,8 @@ MagickBooleanType CompositeOverImage (struct Image * image, const struct Image *
[...]
@@ -13963,6 +13963,7 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
     goto <bb 348>; [3.66%]

   <bb 305> [local count: 11561895]:
+  _70 = ABS_EXPR <angle_range_249>;

   <bb 72> [local count: 228832040]:
   # y_1117 = PHI <y_546(311), 0(305)>
@@ -14050,7 +14051,6 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
   goto <bb 88>; [100.00%]

   <bb 79> [local count: 179090238]:
-  _70 = ABS_EXPR <angle_range_249>;
   if (_70 > 9.99999999999999979886647629255615367252843506129522666015e-13)
     goto <bb 80>; [50.00%]
   else

---

--- a/libMagickCore_7_Q16HDRI_la-composite.i.148t.dse3
+++ b/libMagickCore_7_Q16HDRI_la-composite.i.148t.dse3
@@ -3232,6 +3232,7 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
   residual_30 = *.omp_data_i_24(D).residual;
   beta_image_31 = *.omp_data_i_24(D).beta_image;
   alpha_image_32 = *.omp_data_i_24(D).alpha_image;
+  columns.88_174 = (long int) columns_26;

   <bb 4> [local count: 139672780]:
   # y_3 = PHI <_42(38), y_119(41)>
@@ -3268,7 +3269,6 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
     goto <bb 7>; [42.89%]

   <bb 7> [local count: 29952828]:
-  columns.88_174 = (long int) columns_26;
   if (columns.88_174 > 0)
     goto <bb 39>; [89.00%]
   else
[...]
@@ -11754,6 +11754,7 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
     goto <bb 348>; [3.66%]

   <bb 305> [local count: 11561895]:
+  _70 = ABS_EXPR <angle_range_249>;

   <bb 72> [local count: 228832040]:
   # y_1117 = PHI <y_546(311), 0(305)>
@@ -11841,7 +11842,6 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
   goto <bb 88>; [100.00%]

   <bb 79> [local count: 179090238]:
-  _70 = ABS_EXPR <angle_range_249>;
   if (_70 > 9.99999999999999979886647629255615367252843506129522666015e-13)
     goto <bb 80>; [50.00%]
   else

----

--- a/libMagickCore_7_Q16HDRI_la-composite.i.149t.dce4
+++ b/libMagickCore_7_Q16HDRI_la-composite.i.149t.dce4
@@ -3232,6 +3232,7 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
   residual_30 = *.omp_data_i_24(D).residual;
   beta_image_31 = *.omp_data_i_24(D).beta_image;
   alpha_image_32 = *.omp_data_i_24(D).alpha_image;
+  columns.88_174 = (long int) columns_26;

   <bb 4> [local count: 139672780]:
   # y_3 = PHI <_42(38), y_119(41)>
@@ -3268,7 +3269,6 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
     goto <bb 7>; [42.89%]

   <bb 7> [local count: 29952828]:
-  columns.88_174 = (long int) columns_26;
   if (columns.88_174 > 0)
     goto <bb 39>; [89.00%]
   else
@@ -3757,8 +3757,8 @@ MagickBooleanType CompositeOverImage (struct Image * image, const struct Image *
[...]
@@ -11754,6 +11754,7 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
     goto <bb 348>; [3.66%]

   <bb 305> [local count: 11561895]:
+  _70 = ABS_EXPR <angle_range_249>;

   <bb 72> [local count: 228832040]:
   # y_1117 = PHI <y_546(311), 0(305)>
@@ -11841,7 +11842,6 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
   goto <bb 88>; [100.00%]

   <bb 79> [local count: 179090238]:
-  _70 = ABS_EXPR <angle_range_249>;
   if (_70 > 9.99999999999999979886647629255615367252843506129522666015e-13)
     goto <bb 80>; [50.00%]
   else

----

--- a/libMagickCore_7_Q16HDRI_la-composite.i.150t.fix_loops
+++ b/libMagickCore_7_Q16HDRI_la-composite.i.150t.fix_loops
@@ -3232,6 +3232,7 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
   residual_30 = *.omp_data_i_24(D).residual;
   beta_image_31 = *.omp_data_i_24(D).beta_image;
   alpha_image_32 = *.omp_data_i_24(D).alpha_image;
+  columns.88_174 = (long int) columns_26;

   <bb 4> [local count: 139672780]:
   # y_3 = PHI <_42(38), y_119(41)>
@@ -3268,7 +3269,6 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
     goto <bb 7>; [42.89%]

   <bb 7> [local count: 29952828]:
-  columns.88_174 = (long int) columns_26;
   if (columns.88_174 > 0)
     goto <bb 39>; [89.00%]
   else
[...]
@@ -11754,6 +11754,7 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
     goto <bb 348>; [3.66%]

   <bb 305> [local count: 11561895]:
+  _70 = ABS_EXPR <angle_range_249>;

   <bb 72> [local count: 228832040]:
   # y_1117 = PHI <y_546(311), 0(305)>
@@ -11841,7 +11842,6 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
   goto <bb 88>; [100.00%]

   <bb 79> [local count: 179090238]:
-  _70 = ABS_EXPR <angle_range_249>;
   if (_70 > 9.99999999999999979886647629255615367252843506129522666015e-13)
     goto <bb 80>; [50.00%]
   else
diff --git a/libMagickCore_7_Q16HDRI_la-composite.i.151t.loop b/libMagickCore_7_Q16HDRI_la-composite.i.151t.loop
index 9b848e2..2177f85 100644
--- a/libMagickCore_7_Q16HDRI_la-composite.i.151t.loop
+++ b/libMagickCore_7_Q16HDRI_la-composite.i.151t.loop
@@ -3188,6 +3188,7 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
   residual_30 = *.omp_data_i_24(D).residual;
   beta_image_31 = *.omp_data_i_24(D).beta_image;
   alpha_image_32 = *.omp_data_i_24(D).alpha_image;
+  columns.88_174 = (long int) columns_26;

   <bb 4> [local count: 139672780]:
   # y_3 = PHI <_42(38), y_119(41)>
@@ -3224,7 +3225,6 @@ void BlendRMSEResidual._omp_fn.0 (struct .omp_data_s.89 & restrict .omp_data_i)
     goto <bb 7>; [42.89%]

   <bb 7> [local count: 29952828]:
-  columns.88_174 = (long int) columns_26;
   if (columns.88_174 > 0)
     goto <bb 39>; [89.00%]
   else
[...]
@@ -14021,6 +14021,7 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
     goto <bb 348>; [3.66%]

   <bb 305> [local count: 11561895]:
+  _70 = ABS_EXPR <angle_range_249>;

   <bb 72> [local count: 228832040]:
   # y_1117 = PHI <y_546(311), 0(305)>
@@ -14108,7 +14109,6 @@ MagickBooleanType CompositeImage (struct Image * image, const struct Image * com
   goto <bb 88>; [100.00%]

   <bb 79> [local count: 179090238]:
-  _70 = ABS_EXPR <angle_range_249>;
   if (_70 > 9.99999999999999979886647629255615367252843506129522666015e-13)
     goto <bb 80>; [50.00%]
   else
Comment 15 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 20:57:12 UTC
Then sccp comes and does a lot.

What I don't get is, I don't see an unswitch-loops pass, but I see refs to -fdump-tree-unswitch-all?
Comment 16 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 20:58:02 UTC
OK, no, it makes sense (https://gcc.gnu.org/onlinedocs/gccint/Tree-SSA-passes.html):

> Loop invariant motion. This pass moves only invariants that would be hard to handle on RTL level (function calls, operations that expand to nontrivial sequences of insns). With -funswitch-loops it also moves operands of conditions that are invariant out of the loop, so that we can use just trivial invariantness analysis in loop unswitching. The pass also includes store motion. The pass is implemented in tree-ssa-loop-im.cc. 

so the goodies are in lim, and that's where we see our first difference
Comment 17 Hadrien Lacour 2024-10-10 21:10:06 UTC
Great detective work! I had myself narrowed it to the final composite operation via manual debugging, but didn't know how to proceed further with IM's spaghetti AbstractBeanFactory code.
Comment 18 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 21:15:18 UTC
I'll try extract it into something standalone next but kind of dreading it..
Comment 19 Hadrien Lacour 2024-10-10 21:52:06 UTC
Might help to know that I remember having reproduced --without-openmp, as it may make debugging much harder.
Comment 20 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-10 23:10:27 UTC
Thanks, I've swapped to that now (and it does help).
Comment 21 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-12 01:35:04 UTC
Thank you for reporting - reduced & sent upstream at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117100.
Comment 22 Larry the Git Cow gentoo-dev 2024-10-12 05:32:32 UTC
The bug has been closed via the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=56b835de43c60269973d4c7ea331338cd4eb9011

commit 56b835de43c60269973d4c7ea331338cd4eb9011
Author:     Sam James <sam@gentoo.org>
AuthorDate: 2024-10-12 05:29:09 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2024-10-12 05:31:22 +0000

    media-gfx/imagemagick: workaround GCC 13 bug
    
    -O3 implies -funswitch-loops which triggers an unfortunate rare edgecase
    with one of the switch statements in ImageMagick. Apply a workaround
    for GCC 13 for now (14+ is fixed already and it's latent otherwise).
    
    The fix will land on the 13 branch shortly upstream but let's get the fix
    out quickly on our end so we don't have to rush to stable it and so on
    (although we will of course do it soon).
    
    Bug: https://github.com/ImageMagick/ImageMagick/issues/7677
    Bug: https://gcc.gnu.org/PR117100
    Closes: https://bugs.gentoo.org/941208
    Signed-off-by: Sam James <sam@gentoo.org>

 .../imagemagick/imagemagick-6.9.13.16-r1.ebuild    | 245 ++++++++++++++++++++
 ....1.25.ebuild => imagemagick-7.1.1.25-r1.ebuild} |   3 +
 .../imagemagick/imagemagick-7.1.1.38-r2.ebuild     | 257 +++++++++++++++++++++
 media-gfx/imagemagick/imagemagick-9999.ebuild      |   3 +
 4 files changed, 508 insertions(+)
Comment 23 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-12 05:33:01 UTC
Workaround applied, I'll get it fixed on 13 branch shortly as well upstream, then can drop the workaround in due course. Thanks again for the report.
Comment 24 Hadrien Lacour 2024-10-12 08:05:15 UTC
Thanks a lot for your dedication! I'm amazed you're doing this without being paid, to be honest.
Comment 25 Larry the Git Cow gentoo-dev 2024-10-19 05:59:40 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=8496bc7616eaa8dda229ec15e2629051e10814a9

commit 8496bc7616eaa8dda229ec15e2629051e10814a9
Author:     Sam James <sam@gentoo.org>
AuthorDate: 2024-10-19 05:58:33 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2024-10-19 05:58:55 +0000

    sys-devel/gcc: add 13.3.1_p20241018
    
    Bug: https://bugs.gentoo.org/880677
    Bug: https://bugs.gentoo.org/941208
    Signed-off-by: Sam James <sam@gentoo.org>

 sys-devel/gcc/Manifest                    |  1 +
 sys-devel/gcc/gcc-13.3.1_p20241018.ebuild | 66 +++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
Comment 26 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-10-31 22:43:24 UTC
commit 1debd3c0d720f769042152e951c2d51b778a459f
Author: Sam James <sam@gentoo.org>
Date:   Tue Oct 29 04:58:47 2024 +0000

    sys-devel/gcc: keyword 13.3.1_p20241025

    Signed-off-by: Sam James <sam@gentoo.org>
Comment 27 Larry the Git Cow gentoo-dev 2024-10-31 22:44:10 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=0c5aab3a19d7273851a164c9140700cc3a54c5bf

commit 0c5aab3a19d7273851a164c9140700cc3a54c5bf
Author:     Sam James <sam@gentoo.org>
AuthorDate: 2024-10-31 22:42:37 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2024-10-31 22:43:45 +0000

    sys-devel/gcc: add 12.4.1_p20241031
    
    Bug: https://bugs.gentoo.org/941208
    Signed-off-by: Sam James <sam@gentoo.org>

 sys-devel/gcc/Manifest                    |  1 +
 sys-devel/gcc/gcc-12.4.1_p20241031.ebuild | 65 +++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)