Skip to content
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

Infinite recursion can trigger undefined behavior (I think) #54049

Closed
davidlattimore opened this issue Sep 8, 2018 · 1 comment
Closed

Infinite recursion can trigger undefined behavior (I think) #54049

davidlattimore opened this issue Sep 8, 2018 · 1 comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness

Comments

@davidlattimore
Copy link
Contributor

davidlattimore commented Sep 8, 2018

pub fn bar(i: i32) -> i32 {
    if i < 1000 {
        bar(i % 2)
    } else {
        1234567
    }
}

fn main() {
    println!("RESULT: {}", bar(10));
}

If I cargo run this, I get a stack overflow as expected. If I cargo run --release however, I get:
RESULT: 1234567

The else should be unreachable (with input 10). I suspect LLVM is correctly identifying that it will recurse indefinitely, but that it perhaps considers that undefined behavior, so decides that it's free to remove the if statement and just unconditionally return the constant value. Indeed, if I look at the assembly produced for bar, it's just:

	movl	$1234567, %eax
	retq

rustc version 1.28.0

@Centril Centril added I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. labels Sep 8, 2018
@nagisa
Copy link
Member

nagisa commented Sep 8, 2018

A duplicate of #18785. I’m surprised we ended up closing #18785 without really checking whether the underlying reason is the same. I’ll reopen that issue and close this in favour of that.

Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
Projects
None yet
Development

No branches or pull requests

3 participants