Skip to content

[clang] Main Function Missing Due to Incorrect Infinite Loop Optimization #66307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gbdngb12 opened this issue Sep 14, 2023 · 9 comments
Closed
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc.

Comments

@gbdngb12
Copy link

We are submitting this report to bring to your attention an issue that I have encountered while using clang compiler, specifically related to compiler optimization. While working with clang compiler, I have observed an error that seems to be related to optimization levels O1, O2, and O3. Although I had expected an infinite loop to occur when applying these optimization options, I have instead encountered an unexpected termination of the main function.

I have conducted extensive research and testing to pinpoint the root cause of this problem, but unfortunately, my efforts have not yielded a conclusive solution. Given the complexity of LLVM and its extensive code base, I believe that your expertise and resources can shed light on this issue and help clarify its underlying causes.

Extended Description

Proof of Concept to trigger the bug

#include <stdint.h>

uint8_t uint8_sub(uint8_t ui1, uint8_t ui2) {
  return ui1 - ui2;
}

int main() {
    int32_t i;
    for(i = 0; i > (-22); i = uint8_sub(i, 1)) { }
    return 0;
}

While it was anticipated that an infinite loop would ensue when employing all optimization options, the principal function becomes non-existent, leading to anomalous termination across O1, O2, and O3 optimization levels.

Expected Result

# version
$ clang-18 --version
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 4706251a3186c34da0ee8fd894f7e6b095da8fdc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/dongFiles/compiler_trunk/llvm-project/build/bin

# compile with O0 optimization
$ clang-18 target.c -o clang_O0 -O0 

# correct result: infinite loop
$ ./clang_O0 # Infinite loop                                                                     
^C

Clang Result (incorrect)

  • clang x64 (incorrect)
$ clang-18 --version
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 4706251a3186c34da0ee8fd894f7e6b095da8fdc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/dongFiles/compiler_trunk/llvm-project/build/bin

$ clang-18 target.c -o clang_O1 -O1
$ clang-18 target.c -o clang_O2 -O2
$ clang-18 target.c -o clang_O3 -O3

$ ./clang_O1 
$ echo $?   # Abnormal termination
64
$ ./clang_O2 
$ echo $?   # Abnormal termination
64
$ ./clang_O3 
$ echo $?   # Abnormal termination
64
  • clang aarch64 (incorrect)
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O0_aarch64 -O0
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O1_aarch64 -O1
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O2_aarch64 -O2
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O3_aarch64 -O3

$ ./clang_O1_aarch64 
$ echo $?    # Abnormal termination
1
$ ./clang_O2_aarch64 
$ echo $?    # Abnormal termination
1
$ ./clang_O3_aarch64 
$ echo $?    # Abnormal termination
1

Comparison to GCC Result (correct)

$ gcc-trunk --version                                                    
gcc-trunk (GCC) 14.0.0 20230913 (experimental)
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc-trunk target.c -o gcc_O0 -O0 
$ gcc-trunk target.c -o gcc_O1 -O1
$ gcc-trunk target.c -o gcc_O2 -O2
$ gcc-trunk target.c -o gcc_O3 -O3 
$ ./gcc_O0   # Infinite loop   
^C
$ ./gcc_O1   # Infinite loop   
^C
$ ./gcc_O2   # Infinite loop   
^C
$ ./gcc_O3   # Infinite loop
^C
@github-actions github-actions bot added clang Clang issues not falling into any other category new issue labels Sep 14, 2023
@shafik
Copy link
Collaborator

shafik commented Sep 14, 2023

I believe this is another case of: #48943

@EugeneZelenko EugeneZelenko added clang:codegen IR generation bugs: mangling, exceptions, etc. and removed clang Clang issues not falling into any other category new issue labels Sep 14, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 14, 2023

@llvm/issue-subscribers-clang-codegen

We are submitting this report to bring to your attention an issue that I have encountered while using clang compiler, specifically related to compiler optimization. While working with clang compiler, I have observed an error that seems to be related to optimization levels `O1`, `O2`, and `O3`. Although I had expected an infinite loop to occur when applying these optimization options, I have instead encountered an unexpected termination of the main function.

I have conducted extensive research and testing to pinpoint the root cause of this problem, but unfortunately, my efforts have not yielded a conclusive solution. Given the complexity of LLVM and its extensive code base, I believe that your expertise and resources can shed light on this issue and help clarify its underlying causes.

Extended Description

Proof of Concept to trigger the bug

#include <stdint.h>

uint8_t uint8_sub(uint8_t ui1, uint8_t ui2) {
  return ui1 - ui2;
}

int main() {
    int32_t i;
    for(i = 0; i > (-22); i = uint8_sub(i, 1)) { }
    return 0;
}

While it was anticipated that an infinite loop would ensue when employing all optimization options, the principal function becomes non-existent, leading to anomalous termination across O1, O2, and O3 optimization levels.

Expected Result

# version
$ clang-18 --version
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 4706251a3186c34da0ee8fd894f7e6b095da8fdc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/dongFiles/compiler_trunk/llvm-project/build/bin

# compile with O0 optimization
$ clang-18 target.c -o clang_O0 -O0 

# correct result: infinite loop
$ ./clang_O0 # Infinite loop                                                                     
^C

Clang Result (incorrect)

  • clang x64 (incorrect)
$ clang-18 --version
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 4706251a3186c34da0ee8fd894f7e6b095da8fdc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/dongFiles/compiler_trunk/llvm-project/build/bin

$ clang-18 target.c -o clang_O1 -O1
$ clang-18 target.c -o clang_O2 -O2
$ clang-18 target.c -o clang_O3 -O3

$ ./clang_O1 
$ echo $?   # Abnormal termination
64
$ ./clang_O2 
$ echo $?   # Abnormal termination
64
$ ./clang_O3 
$ echo $?   # Abnormal termination
64
  • clang aarch64 (incorrect)
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O0_aarch64 -O0
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O1_aarch64 -O1
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O2_aarch64 -O2
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O3_aarch64 -O3

$ ./clang_O1_aarch64 
$ echo $?    # Abnormal termination
1
$ ./clang_O2_aarch64 
$ echo $?    # Abnormal termination
1
$ ./clang_O3_aarch64 
$ echo $?    # Abnormal termination
1

Comparison to GCC Result (correct)

$ gcc-trunk --version                                                    
gcc-trunk (GCC) 14.0.0 20230913 (experimental)
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc-trunk target.c -o gcc_O0 -O0 
$ gcc-trunk target.c -o gcc_O1 -O1
$ gcc-trunk target.c -o gcc_O2 -O2
$ gcc-trunk target.c -o gcc_O3 -O3 
$ ./gcc_O0   # Infinite loop   
^C
$ ./gcc_O1   # Infinite loop   
^C
$ ./gcc_O2   # Infinite loop   
^C
$ ./gcc_O3   # Infinite loop
^C

@gbdngb12
Copy link
Author

Duplicated #60622

@gbdngb12
Copy link
Author

gbdngb12 commented Oct 11, 2023

$ cat poc.c
#include <stdint.h>
#include <unistd.h>

uint8_t uint8_sub(uint8_t ui1, uint8_t ui2) {
  return ui1 - ui2;
}

int main() {
    int32_t i;
    for(i = 0; i > (-22); i = uint8_sub(i, 1)) { }
    return 0;
}

void vuln_func() {
   execve("/bin/sh", NULL, NULL);
}
$ ./clang-18 poc.c -o poc -O1
$ ./poc
$ whoami
dong

This bug, while somewhat different from the existing bug #48943, #60622, arises in clang and can lead developers to trigger a security vulnerability. Hence, we are reopening the issue.

@gbdngb12 gbdngb12 reopened this Oct 11, 2023
@shafik
Copy link
Collaborator

shafik commented Oct 11, 2023

I do understand that the problems are all somewhat different but as far I understand the underlying cause is the same in that we are turning functions with unconditional unreachable code into empty functions. I believe the solution would be to turn many although not all cases into traps but there are some subtitles around implementing this.

The problem with keeping all the different discussions open is that it can lead to repetitive discussions or key points being spread across several issues which then makes it difficult for folks tackling the problem to fully understand the scope.

CC @AaronBallman @dwblaikie @efriedma-quic for additional perspectives.

@AaronBallman
Copy link
Collaborator

CC @AaronBallman @dwblaikie @efriedma-quic for additional perspectives.

Yeah, this looks to be the same root cause as #48943 and #60622 -- we should close this issue so as not to spread discussion too thin (I'm not certain which out of the two linked is the "better" to continue discussion in, but we should probably pick one and close the other).

@dwblaikie
Copy link
Collaborator

Yep - if they're both similar - pick the earlier one.

Duplicate of #48943

@dwblaikie
Copy link
Collaborator

I think #60622 is trying to be about the distinct issue of "is an infinite loop with no side effects UB" - I'd happily close it as "yes, it is", but if folks want to keep debating/discussing it there, so be it, but I wouldn't dup it against #48943

@gbdngb12
Copy link
Author

same root cause #60622

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc.
Projects
None yet
Development

No branches or pull requests

6 participants