https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112329

            Bug ID: 112329
           Summary: Faulty arithmetic comparison in O2, O3 of s390x-gcc
           Product: gcc
           Version: 11.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 22s302h0659 at sonline20 dot sen.go.kr
  Target Milestone: ---

### Environment

- Compiler: s390x-linux-gnu-gcc (64bit)
- Version: gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
- Platform: Windows 11_5.15.90.1-microsoft-standard-WSL2
- Build Optimization Options: O0, O1, O2, O3

I installed the s390x-linux-gnu toolchain using the 'apt' package manager in
Ubuntu and utilized s390x-linux-gnu-gcc (version 11.4.0) from it.

### build script & excution script

```bash
# build scripts
s390x-linux-gnu-gcc ./bug_Poc.c -o test_O0 -I"/home/z3rodae0/csmith/include" -g
-O0 -fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
s390x-linux-gnu-gcc ./bug_Poc.c -o test_O1 -I"/home/z3rodae0/csmith/include" -g
-O1 -fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
s390x-linux-gnu-gcc ./bug_Poc.c -o test_O2 -I"/home/z3rodae0/csmith/include" -g
-O2 -fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
s390x-linux-gnu-gcc ./bug_Poc.c -o test_O3 -I"/home/z3rodae0/csmith/include" -g
-O3 -fsanitize=undefined -Wall -Wextra

gcc ./bug_Poc.c -o gcc_O0 -I"/home/z3rodae0/csmith/include" -g -O0
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
gcc ./bug_Poc.c -o gcc_O1 -I"/home/z3rodae0/csmith/include" -g -O1
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
gcc ./bug_Poc.c -o gcc_O2 -I"/home/z3rodae0/csmith/include" -g -O2
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
gcc ./bug_Poc.c -o gcc_O3 -I"/home/z3rodae0/csmith/include" -g -O3
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv

clang ./bug_Poc.c -o clang_O0 -I"/home/z3rodae0/csmith/include" -g -O0
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
clang ./bug_Poc.c -o clang_O1 -I"/home/z3rodae0/csmith/include" -g -O1
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
clang ./bug_Poc.c -o clang_O2 -I"/home/z3rodae0/csmith/include" -g -O2
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
clang ./bug_Poc.c -o clang_O3 -I"/home/z3rodae0/csmith/include" -g -O3
-fsanitize=undefined -Wall -Wextra -fno-strict-aliasing -fwrapv
```

```bash
# run scripts
qemu-s390x-static -L /usr/s390x-linux-gnu/ ./test_O0
qemu-s390x-static -L /usr/s390x-linux-gnu/ ./test_O1
qemu-s390x-static -L /usr/s390x-linux-gnu/ ./test_O2
qemu-s390x-static -L /usr/s390x-linux-gnu/ ./test_O3

./gcc_O0
./gcc_O1
./gcc_O2
./gcc_O3

./clang_O0
./clang_O1
./clang_O2
./clang_O3
```

When casting the negative value -1 to an unsigned char at the 6th line, it
becomes the value 255. Then, comparing it with g_89, the expression 255 <= 1 is
false, so it is correct to return 0. However, with optimization options O2, O3,
it returns the value 1.

### Source Code

```c
1 #include <stdio.h>
2 short g_14 = -1;
3 short g_89 = 1;
4 int main ()
5 { 
6     printf("bug== %d\n", ((unsigned char)(g_14)) <= g_89); 
7     return 0;
8 }
```

### Result

```bash
bug== 0
bug== 0
bug== 1
bug== 1
bug== 0
bug== 0
bug== 0
bug== 0
bug== 0
bug== 0
bug== 0
bug== 0
```

### Coclusion

I'm reporting this issue due to incorrect results in integer comparison
operations. I've reported bugs twice before that occurred in the s390x
architecture. Although the cross-compiler I'm using is not the latest version,
I consider this to be quite important. This is because most people install the
cross-compiler via 'apt install', so incorrect code generation in this version
poses a risk.
  • [Bug c/112329] New: Fa... 22s302h0659 at sonline20 dot sen.go.kr via Gcc-bugs

Reply via email to