[Bug 1770676] Re: gcc optimizer bug

Scott Emmons 1770676 at bugs.launchpad.net
Fri May 11 15:54:44 UTC 2018


** Also affects: gcc-5 (Ubuntu)
   Importance: Undecided
       Status: New

** Description changed:

- Note: Package gcc-7 selected, but this affects other gcc versions.
- 
  While investigating why cronolog was no longer changing a log symlink in
  bionic, I discovered a bug in gcc (reproducable in 7.3.0-16ubuntu3 in
  bionic and 5.4.0-6ubuntu1~16.04.9 in xenial) for code compiled with -O2.
  I have created a simple testcase to reproduce the problem, which is
  attached.
  
  You can reproduce the problem with the following:
  
  # ====
  set -x
  echo "First, we compile and run without optimization:"
  gcc -o testcase testcase.c && ./testcase
  
  echo "Then, we compile and run with optimization:"
  gcc -O2 -o testcase testcase.c && ./testcase
  # ====
  
  Which outputs:
  + echo 'First, we compile and run without optimization:'
  First, we compile and run without optimization:
  + gcc -o testcase testcase.c
  + ./testcase
  foo in func() is NULL (expected)
  foo in func() is NULL (expected)
  foo in main() is NULL (expected)
  foo in main() is NULL (expected)
  + echo 'Then, we compile and run with optimization:'
  Then, we compile and run with optimization:
  + gcc -O2 -o testcase testcase.c
  + ./testcase
  foo in func() is NULL (expected)
  foo in func() is not NULL (NOT EXPECTED!)
  foo in main() is NULL (expected)
  foo in main() is NULL (expected)
  
  The problem occurs after calling:
  
-   stat(foo, &stat_buf);
+   stat(foo, &stat_buf);
  
  Where foo is a NULL pointer. After the return from this function, foo
  will no longer be NULL when the code is compiled with optimization. (You
  can argue calling stat with a NULL pointer is bad behavior, however this
  code has been in cronolog, working fine when compiled with -O2, for
  years).
  
  I can reproduce this easily on multiple systems, bionic with "gcc
  (Ubuntu 7.3.0-16ubuntu3) 7.3.0" and xenial with "gcc (Ubuntu
  5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609".
  
  I could not initially reproduce the cronolog testcase in xenial, leading
  me to some suspicion that it may be related to retpoline or other recent
  compiler changes - and in fact after recompiling cronolog on xenial I
  can reproduce the issue there as well.

** Description changed:

  While investigating why cronolog was no longer changing a log symlink in
  bionic, I discovered a bug in gcc (reproducable in 7.3.0-16ubuntu3 in
  bionic and 5.4.0-6ubuntu1~16.04.9 in xenial) for code compiled with -O2.
  I have created a simple testcase to reproduce the problem, which is
  attached.
  
  You can reproduce the problem with the following:
  
  # ====
  set -x
  echo "First, we compile and run without optimization:"
  gcc -o testcase testcase.c && ./testcase
  
  echo "Then, we compile and run with optimization:"
  gcc -O2 -o testcase testcase.c && ./testcase
  # ====
  
  Which outputs:
  + echo 'First, we compile and run without optimization:'
  First, we compile and run without optimization:
  + gcc -o testcase testcase.c
  + ./testcase
  foo in func() is NULL (expected)
  foo in func() is NULL (expected)
  foo in main() is NULL (expected)
  foo in main() is NULL (expected)
  + echo 'Then, we compile and run with optimization:'
  Then, we compile and run with optimization:
  + gcc -O2 -o testcase testcase.c
  + ./testcase
  foo in func() is NULL (expected)
  foo in func() is not NULL (NOT EXPECTED!)
  foo in main() is NULL (expected)
  foo in main() is NULL (expected)
  
  The problem occurs after calling:
  
    stat(foo, &stat_buf);
  
  Where foo is a NULL pointer. After the return from this function, foo
- will no longer be NULL when the code is compiled with optimization. (You
- can argue calling stat with a NULL pointer is bad behavior, however this
- code has been in cronolog, working fine when compiled with -O2, for
- years).
+ will no longer be NULL when the code is compiled with optimization - but
+ only when the pointer is a function parameter of func(). The issue does
+ not occur when the same codepath is called in main(). (You can argue
+ calling stat with a NULL pointer is bad behavior, however this code has
+ been in cronolog, working fine when compiled with -O2, for years).
  
  I can reproduce this easily on multiple systems, bionic with "gcc
  (Ubuntu 7.3.0-16ubuntu3) 7.3.0" and xenial with "gcc (Ubuntu
  5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609".
  
  I could not initially reproduce the cronolog testcase in xenial, leading
  me to some suspicion that it may be related to retpoline or other recent
  compiler changes - and in fact after recompiling cronolog on xenial I
  can reproduce the issue there as well.

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to gcc-5 in Ubuntu.
https://bugs.launchpad.net/bugs/1770676

Title:
  gcc optimizer bug

Status in gcc-5 package in Ubuntu:
  New
Status in gcc-7 package in Ubuntu:
  New

Bug description:
  While investigating why cronolog was no longer changing a log symlink
  in bionic, I discovered a bug in gcc (reproducable in 7.3.0-16ubuntu3
  in bionic and 5.4.0-6ubuntu1~16.04.9 in xenial) for code compiled with
  -O2. I have created a simple testcase to reproduce the problem, which
  is attached.

  You can reproduce the problem with the following:

  # ====
  set -x
  echo "First, we compile and run without optimization:"
  gcc -o testcase testcase.c && ./testcase

  echo "Then, we compile and run with optimization:"
  gcc -O2 -o testcase testcase.c && ./testcase
  # ====

  Which outputs:
  + echo 'First, we compile and run without optimization:'
  First, we compile and run without optimization:
  + gcc -o testcase testcase.c
  + ./testcase
  foo in func() is NULL (expected)
  foo in func() is NULL (expected)
  foo in main() is NULL (expected)
  foo in main() is NULL (expected)
  + echo 'Then, we compile and run with optimization:'
  Then, we compile and run with optimization:
  + gcc -O2 -o testcase testcase.c
  + ./testcase
  foo in func() is NULL (expected)
  foo in func() is not NULL (NOT EXPECTED!)
  foo in main() is NULL (expected)
  foo in main() is NULL (expected)

  The problem occurs after calling:

    stat(foo, &stat_buf);

  Where foo is a NULL pointer. After the return from this function, foo
  will no longer be NULL when the code is compiled with optimization -
  but only when the pointer is a function parameter of func(). The issue
  does not occur when the same codepath is called in main(). (You can
  argue calling stat with a NULL pointer is bad behavior, however this
  code has been in cronolog, working fine when compiled with -O2, for
  years).

  I can reproduce this easily on multiple systems, bionic with "gcc
  (Ubuntu 7.3.0-16ubuntu3) 7.3.0" and xenial with "gcc (Ubuntu
  5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609".

  I could not initially reproduce the cronolog testcase in xenial,
  leading me to some suspicion that it may be related to retpoline or
  other recent compiler changes - and in fact after recompiling cronolog
  on xenial I can reproduce the issue there as well.

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/gcc-5/+bug/1770676/+subscriptions



More information about the foundations-bugs mailing list