Skip to content

segfault on 'index out of bounds' OS X Yosemite #51794

Closed

Description

@felipenoris

I get Segmentation Fault on this code when running the debug binary.

Only happens on OS X Yosemite 10.10.5 .

fn main() {
    let x = [1];
    println!("{}", x[1]);
}
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/segfault`
Segmentation fault: 11

$ cargo run --release
    Finished release [optimized] target(s) in 0.02s
     Running `target/release/segfault`
thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1', src/main.rs:3:20
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Meta

rustc 1.27.0 (3eda71b 2018-06-19)
binary: rustc
commit-hash: 3eda71b
commit-date: 2018-06-19
host: x86_64-apple-darwin
release: 1.27.0
LLVM version: 6.0

Activity

  1. added
    P-highHigh priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    on Jun 25, 2018
  2. Mark-Simulacrum commented on Jun 25, 2018

    @Mark-Simulacrum
    Member

    I cannot reproduce on macOS 10.13.5 (High Sierra).

    @rust-lang/release if anyone can reproduce and bisect, that would be amazing.

    cc @rust-lang/compiler -- stable regression

  3. kennytm commented on Jun 26, 2018

    @kennytm
    Member

    I bet it is the same cause as #51758 (misaligned TLS).

  4. Mark-Simulacrum commented on Jun 26, 2018

    @Mark-Simulacrum
    Member

    Seems likely -- this is in stable, though, so probably not caused by that. Getting confirmation would be good as well.

  5. kennytm commented on Jun 26, 2018

    @kennytm
    Member

    The use of --release matches #50586

  6. Mark-Simulacrum commented on Jun 26, 2018

    @Mark-Simulacrum
    Member

    The use of --release matches #50586

    This is the reverse of that PR; this bug seems to only happen without --release whereas that one happens only with it.

  7. nikomatsakis commented on Jun 26, 2018

    @nikomatsakis
    Contributor

    This does indeed seem quite bad.

  8. nikomatsakis commented on Jun 26, 2018

    @nikomatsakis
    Contributor

    I cannot reproduce on my laptop, but it is 10.11.6. I'll have to see if I have a machine running 10.10 anywhere.

  9. kennytm commented on Jun 26, 2018

    @kennytm
    Member

    @felipenoris Could you load target/debug/segfault into a debugger (gdb or lldb) and get the backtrace when it segfault?

  10. felipenoris commented on Jun 27, 2018

    @felipenoris
    Author

    Sure. I'll work on that.

  11. felipenoris commented on Jun 27, 2018

    @felipenoris
    Author
    $ lldb segfault
    (lldb) target create "segfault"
    Current executable set to 'segfault' (x86_64).
    (lldb) run
    Process 57139 launched: '/Users/felipenoris/Documents/src/learnrust/segfault/target/debug/segfault' (x86_64)
    segfault was compiled with optimization - stepping may behave oddly; variables may not be available.
    Process 57139 stopped
    * thread #1: tid = 0x793cbc, 0x0000000100011abf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] core::ptr::swap_nonoverlapping_bytes::h90af7d9428701cbf + 16 at ptr.rs:237, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
        frame #0: 0x0000000100011abf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] core::ptr::swap_nonoverlapping_bytes::h90af7d9428701cbf + 16 at ptr.rs:237 [opt]
  12. 2 remaining items

  13. felipenoris commented on Jun 27, 2018

    @felipenoris
    Author
    (lldb) bt
    * thread #1: tid = 0x796f84, 0x0000000100011abf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] core::ptr::swap_nonoverlapping_bytes::h90af7d9428701cbf + 16 at ptr.rs:237, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
      * frame #0: 0x0000000100011abf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] core::ptr::swap_nonoverlapping_bytes::h90af7d9428701cbf + 16 at ptr.rs:237 [opt]
        frame #1: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] core::ptr::swap_nonoverlapping::h644cc6a65b724c83 at ptr.rs:187 [opt]
        frame #2: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] core::mem::swap::h5a9cd575e67a6181 at mem.rs:634 [opt]
        frame #3: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] core::mem::replace::hb91d071e3288f217 at mem.rs:691 [opt]
        frame #4: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::init::hdc127b6594dc2f82 at local.rs:270 [opt]
        frame #5: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::try_with::h35683f0829174f6d at local.rs:296 [opt]
        frame #6: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::with::hd95e5656daaba459 at local.rs:248 [opt]
        frame #7: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 [inlined] std::panicking::update_panic_count::h9566b85bd2bcf46e at panicking.rs:240 [opt]
        frame #8: 0x0000000100011aaf segfault`std::panicking::rust_panic_with_hook::hc2253a5bcda7eda7 + 143 at panicking.rs:437 [opt]
        frame #9: 0x00000001000116b1 segfault`std::panicking::begin_panic_fmt::h2a4ecdf0c76cf1c3 + 49 at panicking.rs:350 [opt]
        frame #10: 0x0000000100011673 segfault`rust_begin_unwind + 35 at panicking.rs:328 [opt]
        frame #11: 0x00000001000463d6 segfault`core::panicking::panic_fmt::hb253bce78d883c0b + 118 at panicking.rs:71 [opt]
        frame #12: 0x0000000100046354 segfault`core::panicking::panic_bounds_check::h80816a3e0195fd50 + 116 at panicking.rs:58 [opt]
        frame #13: 0x0000000100000af7 segfault`segfault::main::hdaaaca17ad5afac1 + 199 at main.rs:3
        frame #14: 0x0000000100000ba2 segfault`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::hd2353479c4883d87 + 18 at rt.rs:74
        frame #15: 0x0000000100011638 segfault`std::panicking::try::do_call::h780530fa434ed2ed [inlined] std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::hcfde3e1c19e6fa13 + 24 at rt.rs:59 [opt]
        frame #16: 0x000000010001162c segfault`std::panicking::try::do_call::h780530fa434ed2ed + 12 at panicking.rs:310 [opt]
        frame #17: 0x000000010001d2bf segfault`__rust_maybe_catch_panic + 31 at lib.rs:105 [opt]
        frame #18: 0x0000000100009c82 segfault`std::rt::lang_start_internal::h46f9f692ece7505b [inlined] std::panicking::try::h0c5a2273ff0725da + 51 at panicking.rs:289 [opt]
        frame #19: 0x0000000100009c4f segfault`std::rt::lang_start_internal::h46f9f692ece7505b [inlined] std::panic::catch_unwind::h3474c6e3bb49278a at panic.rs:374 [opt]
        frame #20: 0x0000000100009c4f segfault`std::rt::lang_start_internal::h46f9f692ece7505b + 191 at rt.rs:58 [opt]
        frame #21: 0x0000000100000b82 segfault`std::rt::lang_start::hd3629e9310e6f506(main=(segfault`segfault::main::hdaaaca17ad5afac1 at main.rs:1), argc=1, argv=0x00007fff5fbffa38) + 66 at rt.rs:74
        frame #22: 0x0000000100000b25 segfault`main + 37
        frame #23: 0x00007fff894935c9 libdyld.dylib`start + 1
        frame #24: 0x00007fff894935c9 libdyld.dylib`start + 1
    
  14. felipenoris commented on Jun 27, 2018

    @felipenoris
    Author

    About the toolchain:

    $ cargo install rustup-toolchain-install-master
    ...
    $ rustup-toolchain-install-master 77a8ed98fc66821a410b0c4696ba99b07b713467
    Segmentation fault: 11
  15. kennytm commented on Jun 27, 2018

    @kennytm
    Member

    @felipenoris Does the prebuilt binary work then?

  16. felipenoris commented on Jun 27, 2018

    @felipenoris
    Author

    @kennytm , with the prebuilt binaries results looks good. It replaces the runtime error with compile-time error.

    $ cargo +77a8ed98fc66821a410b0c4696ba99b07b713467 run
       Compiling segfault v0.1.0 (file:///Users/felipenoris/Documents/src/learnrust/segfault)
    error: index out of bounds: the len is 1 but the index is 1
     --> src/main.rs:3:20
      |
    3 |     println!("{}", x[1]);
      |                    ^^^^
      |
      = note: #[deny(const_err)] on by default
    
    error: aborting due to previous error
    
    error: Could not compile `segfault`.
    
    To learn more, run the command again with --verbose.
    
  17. felipenoris commented on Jun 27, 2018

    @felipenoris
    Author

    Even if I rewrite the program to get a runtime error, the segfault doesn't happen.

    fn main() {
        let x = [1];
        let i = 1; 
        println!("{}", x[i]);
    }
    $ cargo +77a8ed98fc66821a410b0c4696ba99b07b713467 run
       Compiling segfault v0.1.0 (file:///Users/felipenoris/Documents/src/learnrust/segfault)
        Finished dev [unoptimized + debuginfo] target(s) in 0.27s
         Running `target/debug/segfault`
    thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1', src/main.rs:4:20
    note: Run with `RUST_BACKTRACE=1` for a backtrace.
  18. nikomatsakis commented on Jun 28, 2018

    @nikomatsakis
    Contributor

    Assigning to @kennytm, who seems to be making good progress here (cc #51828)

  19. nikomatsakis commented on Jun 28, 2018

    @nikomatsakis
    Contributor
  20. added a commit that references this issue on Jun 28, 2018
    29f834a
  21. added a commit that references this issue on Jun 30, 2018
    8c30769
  22. felipenoris commented on Jun 30, 2018

    @felipenoris
    Author

    I got another code example that reproduces the segfault. Again, the solution by @kennytm also covers this.

    fn main() {
        let i: u8 = 254;
        println!("{}", i+2);
    }
    $ cargo run
       Compiling strings v0.1.0 (file:///Users/felipenoris/Documents/src/learnrust/strings)
        Finished dev [unoptimized + debuginfo] target(s) in 0.27s
         Running `target/debug/strings`
    Segmentation fault: 11
    $ cargo +77a8ed98fc66821a410b0c4696ba99b07b713467 run
       Compiling strings v0.1.0 (file:///Users/felipenoris/Documents/src/learnrust/strings)
        Finished dev [unoptimized + debuginfo] target(s) in 1.57s
         Running `target/debug/strings`
    thread 'main' panicked at 'attempt to add with overflow', src/main.rs:5:20
    note: Run with `RUST_BACKTRACE=1` for a backtrace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

O-macosOperating system: macOSP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions