rust-lang / rust

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

Compile-time panic when compiling (really) large files #39239

Closed stensonowen closed 5 years ago

stensonowen commented 7 years ago

When compiling a project with some large files (that were codegen'd), I run into a compiler panic that looks like the result of a 32-bit underflow.

This is the result of $ RUST_BACKTRACE=1 time cargo build --verbose

   Compiling siphasher v0.2.0
     Running `rustc --crate-name siphasher /home/qj/.cargo/registry/src/github.com-1ecc6299db9ec823/siphasher-0.2.0/src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=37216e8
c4cedbc17 -C extra-filename=-37216e8c4cedbc17 --out-dir /mnt/hdd0/wl/wikidata/target/debug/deps -L dependency=/mnt/hdd0/wl/wikidata/target/debug/deps --cap-lints allow`
   Compiling phf_shared v0.7.21
     Running `rustc --crate-name phf_shared /home/qj/.cargo/registry/src/github.com-1ecc6299db9ec823/phf_shared-0.7.21/src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=1f52
76fa90b55cc9 -C extra-filename=-1f5276fa90b55cc9 --out-dir /mnt/hdd0/wl/wikidata/target/debug/deps -L dependency=/mnt/hdd0/wl/wikidata/target/debug/deps --extern siphasher=/mnt/hdd0/wl/wikidata/targe
t/debug/deps/libsiphasher-37216e8c4cedbc17.rlib --cap-lints allow`
   Compiling phf v0.7.21
     Running `rustc --crate-name phf /home/qj/.cargo/registry/src/github.com-1ecc6299db9ec823/phf-0.7.21/src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=e3e74ffb62952a96 -
C extra-filename=-e3e74ffb62952a96 --out-dir /mnt/hdd0/wl/wikidata/target/debug/deps -L dependency=/mnt/hdd0/wl/wikidata/target/debug/deps --extern phf_shared=/mnt/hdd0/wl/wikidata/target/debug/deps/
libphf_shared-1f5276fa90b55cc9.rlib --cap-lints allow`
   Compiling wikidata v0.1.0 (file:///mnt/hdd0/wl/wikidata)
     Running `rustc --crate-name wikidata src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=c5ba9f8414673d2b -C extra-filename=-c5ba9f8414673d2b --out-dir /mnt/hdd0/wl/wikid
ata/target/debug/deps -L dependency=/mnt/hdd0/wl/wikidata/target/debug/deps --extern phf=/mnt/hdd0/wl/wikidata/target/debug/deps/libphf-e3e74ffb62952a96.rlib`
error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'begin <= end (4294967293 <= 0) when slicing `pub static PAGE_39801199_C: [u32;13] = [734, 20976473, 11039790, 39736, 5131, 20976473, 14580, 20021, 4525962, 32596236, 28073
83, 30463, 5387604];
pub static PAGE_39801199_P: [u32;1] = [32596236];
pub static PAGE_43209839_C: [u32;34] = [803, 4312, 181901, `[...]', /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcore/str/mod.rs:1758
stack backtrace:
   1:     0x7fca1af35e3c - std::sys::imp::backtrace::tracing::imp::write::h9c41d2f69e5caabf
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42
   2:     0x7fca1af4433e - std::panicking::default_hook::{{closure}}::hcc803c8663cda123
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/panicking.rs:351
   3:     0x7fca1af43ee3 - std::panicking::default_hook::hd5bda4e453dfb4be
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/panicking.rs:361
   4:     0x7fca1af447db - std::panicking::rust_panic_with_hook::hffbc74969c7b5d87
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/panicking.rs:555
   5:     0x7fca1af44674 - std::panicking::begin_panic::hc4c5d184a1e3fb7c
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/panicking.rs:517
   6:     0x7fca1af44599 - std::panicking::begin_panic_fmt::h34f5b320b0f94559
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/panicking.rs:501
   7:     0x7fca1af44527 - rust_begin_unwind
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/panicking.rs:477
   8:     0x7fca1af80d2d - core::panicking::panic_fmt::h1016b85b51d1931f
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcore/panicking.rs:69
   9:     0x7fca1af81f4e - core::str::slice_error_fail::h02b27cb27b0f1c1d
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcore/str/mod.rs:1758
  10:     0x7fca13b79653 - syntax::parse::lexer::StringReader::name_from::h524ff1b1c286a987
  11:     0x7fca13b7b5c6 - syntax::parse::lexer::StringReader::next_token_inner::h6394943adbde1b47
  12:     0x7fca13b78809 - syntax::parse::lexer::StringReader::advance_token::hf49ef159b7915623
  13:     0x7fca13b77149 - <syntax::parse::lexer::StringReader<'a> as syntax::parse::lexer::Reader>::try_next_token::h4912e5dad4ded461
  14:     0x7fca13b76bbd - syntax::parse::lexer::Reader::try_real_token::h5d2f9b0639fc02db
  15:     0x7fca13b76f66 - syntax::parse::lexer::Reader::real_token::h971657f4f40d943b
  16:     0x7fca13b14adb - syntax::parse::parser::Parser::next_tok::hb5bad3c6efc9668e
  17:     0x7fca13b21531 - syntax::parse::parser::Parser::bump::h940dfc34b1917473
  18:     0x7fca13b3c70b - syntax::parse::parser::Parser::parse_token_tree::hb02c6a16530b06b6
  19:     0x7fca13b3cc92 - syntax::parse::parser::Parser::parse_token_tree::hb02c6a16530b06b6
  20:     0x7fca13b3df1e - syntax::parse::parser::Parser::parse_all_token_trees::h629f59de3f7d2833
  21:     0x7fca13b84b9a - syntax::parse::filemap_to_tts::h7f2c82917d5ad6a5
  22:     0x7fca13b84493 - syntax::parse::filemap_to_parser::hfe3b80786bfd4210
  23:     0x7fca13b843d7 - syntax::parse::new_sub_parser_from_file::h42604b6405bcc010
  24:     0x7fca13c058f9 - syntax::ext::source_util::expand_include::hff47f21f7948aa32
  25:     0x7fca188bd4f6 - <F as syntax::ext::base::TTMacroExpander>::expand::hb12ee503a940d0c8
  26:     0x7fca13be3ce0 - syntax::ext::expand::MacroExpander::expand_invoc::hca805940e166fafd
  27:     0x7fca13be08bd - syntax::ext::expand::MacroExpander::expand::he64090ad698e4ac4
  28:     0x7fca13bdfdf5 - syntax::ext::expand::MacroExpander::expand_crate::h9effb4e2a488a85d
  29:     0x7fca1b2d402c - rustc_driver::driver::phase_2_configure_and_expand::{{closure}}::h3ddcf02b571a1c51
  30:     0x7fca1b2cb845 - rustc_driver::driver::phase_2_configure_and_expand::hd0c4535cf8df7dea
  31:     0x7fca1b2c1d5e - rustc_driver::driver::compile_input::h44853ffed84a12cb
  32:     0x7fca1b30b3f4 - rustc_driver::run_compiler::hdc4bb0fcf7d0917a
  33:     0x7fca1b217bfb - std::panicking::try::do_call::h84a15e0d2b943318
  34:     0x7fca1af4d61a - __rust_maybe_catch_panic
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libpanic_unwind/lib.rs:98
  35:     0x7fca1b23fe12 - <F as alloc::boxed::FnBox<A>>::call_box::h61c78220cad685ea
  36:     0x7fca1af43194 - std::sys::imp::thread::Thread::new::thread_start::h76badbf9b0ecaf58
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/liballoc/boxed.rs:615
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/sys_common/thread.rs:21
                        at /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/sys/unix/thread.rs:84
  37:     0x7fca12d140a3 - start_thread
  38:     0x7fca1abf962c - clone
  39:                0x0 - <unknown>

error: Could not compile `wikidata`.

Caused by:
  process didn't exit successfully: `rustc --crate-name wikidata src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=c5ba9f8414673d2b -C extra-filename=-c5ba9f8414673d2b --out-dir /mnt/hdd0/wl/wikidata/target/debug/deps -L dependency=/mnt/hdd0/wl/wikidata/target/debug/deps --extern phf=/mnt/hdd0/wl/wikidata/target/debug/deps/libphf-e3e74ffb62952a96.rlib` (exit code: 101)
Command exited with non-zero status 101

4294967293 is three less than 232, which leads me to believe a 32-bit integer underflowed. The file sizes total about 8GB and the memory usage when compiling reaches around 60GB.

Here are the Cargo.toml, Cargo.lock, src/lib.rs, and the first 100 lines of src/codegen_entries (generated by phf) and src/codegen_links.

Version info: I had this issue with a recent nightly, so I updated but had it again.

$ rustc --version --verbose
rustc 1.15.0-beta.5 (10893a9a3 2017-01-19)
binary: rustc
commit-hash: 10893a9a349cdd423f2490a6984acb5b3b7c8046
commit-date: 2017-01-19
host: x86_64-unknown-linux-gnu
release: 1.15.0-beta.5
LLVM version: 3.9
$ rustc --version
rustc 1.16.0-nightly (a52da95ce 2017-01-20)

Trying to build with beta gives the same error. Trying with stable gives

thread 'rustc' panicked at 'assertion failed: line_len == 0 || ((*lines)[line_len - 1] < pos)', ../src/libsyntax_pos/lib.rs:439
stack backtrace:
   1:     0x7fd14af9e7aa - std::sys::imp::backtrace::tracing::imp::write::h917062bce4ff48c3
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42
   2:     0x7fd14afb094f - std::panicking::default_hook::{{closure}}::h0bacac31b5ed1870
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panicking.rs:247
   3:     0x7fd14afad1a5 - std::panicking::default_hook::h5897799da33ece67
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panicking.rs:257
   4:     0x7fd14afad8b7 - std::panicking::rust_panic_with_hook::h109e116a3a861224
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panicking.rs:451
   5:     0x7fd1471f54e4 - std::panicking::begin_panic::h634e2b37a96f78d4
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panicking.rs:413
   6:     0x7fd1471f6667 - syntax_pos::FileMap::next_line::h543479a155af7ddd
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax_pos/lib.rs:439
   7:     0x7fd1477bf767 - syntax::parse::lexer::StringReader::bump::ha097d05c9bd5f4ee
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/lexer/mod.rs:435
   8:     0x7fd1477bdeb3 - syntax::parse::lexer::StringReader::advance_token::h3a038ac8677f2b92
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/lexer/mod.rs:613
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/lexer/mod.rs:316
   9:     0x7fd1477bcde8 - <syntax::parse::lexer::StringReader<'a> as syntax::parse::lexer::Reader>::try_next_token::h8c88ba16f8fa2199
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/lexer/mod.rs:127
  10:     0x7fd1477bca33 - syntax::parse::lexer::Reader::try_real_token::h77720140eecf77ef
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/lexer/mod.rs:54
  11:     0x7fd1477bccb9 - syntax::parse::lexer::Reader::real_token::hd1e7266048d9c54e
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/lexer/mod.rs:66
  12:     0x7fd14775c0ad - syntax::parse::parser::Parser::next_tok::he80952548ea93268
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/parser.rs:332
  13:     0x7fd147766444 - syntax::parse::parser::Parser::bump::hc7408584a5f78fe0
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/parser.rs:903
  14:     0x7fd147781658 - syntax::parse::parser::Parser::parse_token_tree::ha036432f7f608613
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/parser.rs:2760
  15:     0x7fd147781fbe - syntax::parse::parser::Parser::parse_all_token_trees::h548cc50571f4b4ba
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/parser.rs:2827
  16:     0x7fd1477c7c27 - syntax::parse::filemap_to_tts::h6e44ad5f4bc55f1c
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/mod.rs:212
  17:     0x7fd1477c75c7 - syntax::parse::filemap_to_parser::h064b268c5ef830fe
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/mod.rs:166
  18:     0x7fd1477c750a - syntax::parse::new_sub_parser_from_file::ha50b7b688dc48820
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/parse/mod.rs:157
  19:     0x7fd1478338b2 - syntax::ext::source_util::expand_include::hd3bca803309a64d7
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/ext/source_util.rs:96
  20:     0x7fd14891ae65 - <F as syntax::ext::base::TTMacroExpander>::expand::h27725b988f599bdf
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/ext/base.rs:211
  21:     0x7fd147816260 - syntax::ext::expand::MacroExpander::expand::hde16e53a7123afb4
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/ext/expand.rs:419
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/ext/expand.rs:322
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/ext/expand.rs:268
  22:     0x7fd147813dcd - syntax::ext::expand::MacroExpander::expand_crate::hd4343a1ada5fbe2e
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libsyntax/ext/expand.rs:208
  23:     0x7fd14b37981f - rustc_driver::driver::phase_2_configure_and_expand::{{closure}}::h06fe772c22a36b93
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc_driver/driver.rs:683
  24:     0x7fd14b32f096 - rustc_driver::driver::phase_2_configure_and_expand::h86e0a42d337e36fb
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc/util/common.rs:38
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc_driver/driver.rs:646
  25:     0x7fd14b328f63 - rustc_driver::driver::compile_input::h66a0adaf05fab85d
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc_driver/driver.rs:122
  26:     0x7fd14b354d30 - rustc_driver::run_compiler::hafe7bbfedf95a825
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc_driver/lib.rs:222
  27:     0x7fd14b289b48 - std::panicking::try::do_call::h5ff5e568cb9cce51
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc_driver/lib.rs:1138
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc_driver/lib.rs:138
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/librustc_driver/lib.rs:1072
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panic.rs:295
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panicking.rs:356
  28:     0x7fd14afb885a - __rust_maybe_catch_panic
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libpanic_unwind/lib.rs:97
  29:     0x7fd14b2a97a8 - <F as alloc::boxed::FnBox<A>>::call_box::h92e2f4ed0df1d5b6
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panicking.rs:332
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/panic.rs:351
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/thread/mod.rs:287
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/liballoc/boxed.rs:595
  30:     0x7fd14afac474 - std::sys::imp::thread::Thread::new::thread_start::ha102a6120fc52763
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/liballoc/boxed.rs:605
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/sys_common/thread.rs:21
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/obj/../src/libstd/sys/unix/thread.rs:84
  31:     0x7fd143f300a3 - start_thread
  32:     0x7fd14ac6762c - clone
  33:                0x0 - <unknown>

error: Could not compile `wikidata`.

Caused by:
  process didn't exit successfully: `rustc src/lib.rs --crate-name wikidata --crate-type lib -g -C metadata=c34c2e4f32110b4b --out-dir /mnt/hdd0/wl/wikidata/target/debug/deps --emit=dep-info,link -L dependency=/mnt/hdd0/wl/wikidata/target/debug/deps --extern phf=/mnt/hdd0/wl/wikidata/target/debug/deps/libphf-34568e436ad63da7.rlib` (exit code: 101)

and

rustc 1.14.0 (e8a012324 2016-12-16)
binary: rustc
commit-hash: e8a0123241f0d397d39cd18fcc4e5e7edde22730
commit-date: 2016-12-16
host: x86_64-unknown-linux-gnu
release: 1.14.0
LLVM version: 3.9

The files are large enough that I didn't host them anywhere, so you probably won't be able to duplicate this easily (also it requires a ~60GB swap file and ~40 minutes). If that's important I can try to find somewhere to host the files.

Is there something I can do to address this without scrapping the project? Do I have any alternatives?

petrochenkov commented 7 years ago

Can I ask, why do you need to keep all these 8GB of wiki data inside the executable instead of reading them dynamically from some other file instead?

stensonowen commented 7 years ago

I'm trying to do as much work as possible at compile time. Also I'm using phf to make these lookups fast, which results in a static hashmap, which I can't edit safely. I know it's a niche case.

keeperofdakeys commented 7 years ago

With 8GB of data, you may have better luck using some kind of database. Sqlite and postgres offer types of indexed full text search.

stensonowen commented 7 years ago

I'm using phf to reduce unnecessary cache misses in a hash table; I'd rather not keep data on disk. I was trying to use a hash table without collisions to reduce the maximum lookup time, and I don't think a database can offer comparable speeds. But if this isn't going to be fixed, I'll have to rethink things.

keeperofdakeys commented 7 years ago

I made an 8GB source file using a long string, and tried compiling it with a 64bit rustc. I got the same crash and backtrace. So this issue isn't related to phf.

aweinstock314 commented 7 years ago

I took a glance though the stack trace & grepped the source, found this: https://github.com/rust-lang/rust/blob/master/src/libsyntax_pos/lib.rs#L533-L536

From the comment, it looks like BytePos is a newtype of u32 by design, to save space.

An easy next thing to try would be changing this to u64 and

  1. seeing if both test-cases pass (wikipedia as per the original issue, 8GB simple string as per keeperofdakeys' minimal repro)
  2. getting memory profiler numbers for before-and-after

If it works for step 1, and step 2 shows it's an unacceptable perf hit to just u64 things, is there a (safe/sane) way to do fancy bitpacking? (e.g. use u32's until 2^31, and use the high bit to do something fancier)

stensonowen commented 7 years ago

That makes sense. I might play around with a rust fork for this project, but this seems to be expected and deliberate behavior so I won't expect a fix. Thanks.

retep998 commented 7 years ago

The ICE definitely is not intentional. At the very least we should emit a proper error here instead of failing hard.

steveklabnik commented 5 years ago

Triage: bytepos is still a u32: https://github.com/rust-lang/rust/blob/master/src/libsyntax_pos/lib.rs#L1210

I wonder what the easiest way to create a test case to reproduce this is.

Alexendoo commented 5 years ago

This got fixed in ccb2dfbfec812d1502626992a8856df27c4fa950

Mark-Simulacrum commented 5 years ago

This is such an edge case that a test probably isn't really worth it and would be somewhat hard due to constraints on space/time on CI.