rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.98k stars 12.68k forks source link

Fatal runtime error and illegal instruction when compiling with LTO #11683

Closed dotdash closed 10 years ago

dotdash commented 10 years ago

After reading http://thornydev.blogspot.co.uk/2014/01/chinese-whispers-in-rust.html I tried to compile the example code with -Z lto. The resulting binary crashes with a fatal runtime error.

Code:

static N:int = 100000;

//
// Chinese Whispers in Rust
// Based on example by Rob Pike
// in http://www.youtube.com/watch?v=f6kdp27TYZs
//
fn main() {
    let (leftmost_port, leftmost_chan) = Chan::new();
    let mut leftc: Chan<int> = leftmost_chan;
    let mut rightp: Port<int>;

    for _ in range(0, N) {
        let (right_port, right_chan) = Chan::new();
        rightp = right_port;
        let c = leftc;
        let p = rightp;
        do spawn {
            whisper(c, p);
        }
        leftc = right_chan;
    }
    let rightmost_chan = leftc;
    do spawn {
        rightmost_chan.send(1);
    }
    println!("{}", leftmost_port.recv());
}

fn whisper(left: Chan<int>, right: Port<int>) {
    left.send( right.recv() + 1 );
}

Output without LTO:

$ ./chinese 
100001

Output with LTO:

$ ./chinese 

There are not many persons who know what wonders are opened to them in the
stories and visions of their youth; for when as children we listen and dream,
we think but half-formed thoughts, and when as men we try to remember, we are
dulled and prosaic with the poison of life. But some of us awake in the night
with strange phantasms of enchanted hills and gardens, of fountains that sing
in the sun, of golden cliffs overhanging murmuring seas, of plains that stretch
down to sleeping cities of bronze and stone, and of shadowy companies of heroes
that ride caparisoned white horses along the edges of thick forests; and then
we know that we have looked back through the ivory gates into that world of
wonder which was ours before we were wise and unhappy.

fatal runtime error:  assertion failed: !ptr.is_null()
Illegal instruction

Sometimes the error message is printed more than once.

dotdash commented 10 years ago

A smaller example that also triggers the error:

fn main() {
    let (p, c) = Chan::<int>::new();

    do spawn {
        c.send(1);
    }
    println!("{}", p.recv());
}
alexcrichton commented 10 years ago

What system are you running on and can you provide the exact invocations of rustc? I'm unable to reproduce this on OSX and linux so far.

dotdash commented 10 years ago

Reproduced on two (quite similar) i7-3770(K) systems. Both running an up-to-date Debian Sid. uname -a says Linux bs 3.12-1-amd64 #1 SMP Debian 3.12.6-2 (2013-12-29) x86_64 GNU/Linux

I just noticed that it doesn't fail if you don't pass -O.

Invocation that triggers the failure:

bs@bs:rust-play $ rustc -O -Z lto issue11683.rs
bs@bs:rust-play $ ./issue11683 
1
bs@bs:rust-play $ ./issue11683 
1
bs@bs:rust-play $ ./issue11683 
1

There are not many persons who know what wonders are opened to them in the
stories and visions of their youth; for when as children we listen and dream,
we think but half-formed thoughts, and when as men we try to remember, we are
dulled and prosaic with the poison of life. But some of us awake in the night
with strange phantasms of enchanted hills and gardens, of fountains that sing
in the sun, of golden cliffs overhanging murmuring seas, of plains that stretch
down to sleeping cities of bronze and stone, and of shadowy companies of heroes
that ride caparisoned white horses along the edges of thick forests; and then
we know that we have looked back through the ivory gates into that world of
wonder which was ours before we were wise and unhappy.

fatal runtime error:  assertion failed: !ptr.is_null()
Illegal instruction
alexcrichton commented 10 years ago

Well then. That's 6 hours of my life I'll never get back :)

Not exactly the bug I was expecting to find...