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

Fix stack overflow when generating debuginfo for 'recursive' type #59446

Merged
merged 6 commits into from
Apr 2, 2019

Conversation

Aaron1011
Copy link
Member

By using 'impl trait', it's possible to create a self-referential
type as follows:

fn foo() -> impl Copy { foo }

This is a function which returns itself.
Normally, the signature of this function would be impossible
to write - it would look like 'fn foo() -> fn() -> fn() ...'
e.g. a function which returns a function, which returns a function...

Using 'impl trait' allows us to avoid writing this infinitely long
type. While it's useless for practical purposes, it does compile and run

However, issues arise when we try to generate llvm debuginfo for such a
type. All 'impl trait' types (e.g. ty::Opaque) are resolved when we
generate debuginfo, which can lead to us recursing back to the original
'fn' type when we try to process its return type.

To resolve this, I've modified debuginfo generation to account for these
kinds of weird types. Unfortunately, there's no 'correct' debuginfo that
we can generate - 'impl trait' does not exist in debuginfo, and this
kind of recursive type is impossible to directly represent.

To ensure that we emit something, this commit emits dummy
debuginfo/type names whenever it encounters a self-reference. In
practice, this should never happen - it's just to ensure that we can
emit some kind of debuginfo, even if it's not particularly meaningful

Fixes #58463

@rust-highfive
Copy link
Collaborator

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 26, 2019
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:02b0dc64:start=1553629294043766396,finish=1553629377744580850,duration=83700814454
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
$ export GCP_CACHE_BUCKET=rust-lang-ci-cache
Setting environment variables from .travis.yml
---
travis_time:start:test_codegen
Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
[01:20:13] 
[01:20:13] running 134 tests
[01:20:16] i..iii.....iii..iiii.....i...........F......i..i................i.....i.........ii...i..i.ii........ 100/134
[01:20:18] failures:
[01:20:18] thread 'main' panicked at 'Some tests failed', src/tools/compiletest/src/main.rs:496:22
[01:20:18] 
[01:20:18] ---- [codegen] codegen/generic-debug.rs stdout ----
[01:20:18] ---- [codegen] codegen/generic-debug.rs stdout ----
[01:20:18] 
[01:20:18] error: verification with 'FileCheck' failed
[01:20:18] status: exit code: 1
[01:20:18] command: "/usr/lib/llvm-6.0/bin/FileCheck" "--input-file" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen/generic-debug/generic-debug.ll" "/checkout/src/test/codegen/generic-debug.rs"
[01:20:18] ------------------------------------------
[01:20:18] 
[01:20:18] ------------------------------------------
[01:20:18] stderr:
[01:20:18] stderr:
[01:20:18] ------------------------------------------
[01:20:18] /checkout/src/test/codegen/generic-debug.rs:7:11: error: expected string not found in input
[01:20:18] // CHECK: {{.*}}DICompositeType{{.*}}tag: DW_TAG_structure_type,{{.*}}name: "Generic<i32>",{{.*}}
[01:20:18]           ^
[01:20:18] /checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen/generic-debug/generic-debug.ll:210:17: note: scanning from here
[01:20:18] define i32 @main(i32, i8**) unnamed_addr #4 {
[01:20:18]                 ^
[01:20:18] /checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen/generic-debug/generic-debug.ll:241:1: note: possible intended match here
[01:20:18] !5 = !DICompositeType(tag: DW_TAG_structure_type, name: "<recursive_type>", file: !2, size: 64, align: 64, elements: !6, templateParams: !4, identifier: "af3d6d4c35ff96191b3f257f03991af7")
[01:20:18] 
[01:20:18] ------------------------------------------
[01:20:18] 
[01:20:18] thread '[codegen] codegen/generic-debug.rs' panicked at 'explicit panic', src/tools/compiletest/src/runtest.rs:3369:9
---
[01:20:18] test result: FAILED. 104 passed; 1 failed; 29 ignored; 0 measured; 0 filtered out
[01:20:18] 
[01:20:18] 
[01:20:18] 
[01:20:18] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/codegen" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--mode" "codegen" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/usr/lib/llvm-6.0/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--target-rustcflags" "-Crpath -O -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python2.7" "--lldb-python" "/usr/bin/python2.7" "--gdb" "/usr/bin/gdb" "--quiet" "--llvm-version" "6.0.0\n" "--system-llvm" "--cc" "" "--cxx" "" "--cflags" "" "--llvm-components" "" "--llvm-cxxflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"
[01:20:18] 
[01:20:18] 
[01:20:18] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[01:20:18] Build completed unsuccessfully in 0:11:24
[01:20:18] Build completed unsuccessfully in 0:11:24
[01:20:18] make: *** [check] Error 1
[01:20:18] Makefile:48: recipe for target 'check' failed
The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:0318cea6
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
Tue Mar 26 21:03:24 UTC 2019
---
travis_time:end:0cc803b9:start=1553634206016130180,finish=1553634206021191530,duration=5061350
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:27c8e3c4
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:0a422390
travis_time:start:0a422390
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:04f6c2b0
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

// This kind of type cannot be properly represented
// via LLVM debuginfo. As a workaround,
// we register a temporary Ty to metadata mapping
// for the function before we compute its actual metadat.a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// for the function before we compute its actual metadat.a
// for the function before we compute its actual metadata

size.bits(),
align.bits() as u32,
DW_ATE_unsigned)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a bunch of empty lines here

// recursing forever.
//
// This function is used to remove the temporary metadata
// mapping after we've computed the actual metadat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// mapping after we've computed the actual metadat
// mapping after we've computed the actual metadata

t.fn_sig(cx.tcx),
usage_site_span).metadata;

type_map.borrow_mut().remove_type(t);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we registering this only temporarily and don't just leave it in?

Copy link
Member Author

@Aaron1011 Aaron1011 Mar 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The temporary metadata is only used in the case where we recurse back to processing the same type.

The actual type will be inserted into the map at the bottom of the function.

@Aaron1011
Copy link
Member Author

@oli-obk I've made the changes you requested

@oli-obk
Copy link
Contributor

oli-obk commented Mar 29, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Mar 29, 2019

📌 Commit 50ab958 has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 29, 2019
Centril added a commit to Centril/rust that referenced this pull request Mar 29, 2019
…=oli-obk

Fix stack overflow when generating debuginfo for 'recursive' type

By using 'impl trait', it's possible to create a self-referential
type as follows:

fn foo() -> impl Copy { foo }

This is a function which returns itself.
Normally, the signature of this function would be impossible
to write - it would look like 'fn foo() -> fn() -> fn() ...'
e.g. a function which returns a function, which returns a function...

Using 'impl trait' allows us to avoid writing this infinitely long
type. While it's useless for practical purposes, it does compile and run

However, issues arise when we try to generate llvm debuginfo for such a
type. All 'impl trait' types (e.g. ty::Opaque) are resolved when we
generate debuginfo, which can lead to us recursing back to the original
'fn' type when we try to process its return type.

To resolve this, I've modified debuginfo generation to account for these
kinds of weird types. Unfortunately, there's no 'correct' debuginfo that
we can generate - 'impl trait' does not exist in debuginfo, and this
kind of recursive type is impossible to directly represent.

To ensure that we emit *something*, this commit emits dummy
debuginfo/type names whenever it encounters a self-reference. In
practice, this should never happen - it's just to ensure that we can
emit some kind of debuginfo, even if it's not particularly meaningful

Fixes rust-lang#58463
@Centril
Copy link
Contributor

Centril commented Mar 30, 2019

Speculatively assigning blame of failure in #59538 (comment), @bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 30, 2019
By using 'impl trait', it's possible to create a self-referential
type as follows:

fn foo() -> impl Copy { foo }

This is a function which returns itself.
Normally, the signature of this function would be impossible
to write - it would look like 'fn foo() -> fn() -> fn() ...'
e.g. a function which returns a function, which returns a function...

Using 'impl trait' allows us to avoid writing this infinitely long
type. While it's useless for practical purposes, it does compile and run

However, issues arise when we try to generate llvm debuginfo for such a
type. All 'impl trait' types (e.g. ty::Opaque) are resolved when we
generate debuginfo, which can lead to us recursing back to the original
'fn' type when we try to process its return type.

To resolve this, I've modified debuginfo generation to account for these
kinds of weird types. Unfortunately, there's no 'correct' debuginfo that
we can generate - 'impl trait' does not exist in debuginfo, and this
kind of recursive type is impossible to directly represent.

To ensure that we emit *something*, this commit emits dummy
debuginfo/type names whenever it encounters a self-reference. In
practice, this should never happen - it's just to ensure that we can
emit some kind of debuginfo, even if it's not particularly meaningful

Fixes rust-lang#58463
@Aaron1011
Copy link
Member Author

@Centril: Fixed

@Centril Centril added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 1, 2019
// (e.g. MyType<fn() -> u8, fn() -> u8>
//
// We only care about avoiding recursing
// directly back to the type we're currentlu
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "currentlu" should be "currently"

@Aaron1011
Copy link
Member Author

@pnkfelix Fixed

@cramertj
Copy link
Member

cramertj commented Apr 1, 2019

r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned cramertj Apr 1, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Apr 2, 2019

Static items can also be recursive via impl Trait, but currently fail to compile due to other reasons #59631

This PR's changes can be extended to cover statics once statics actually hit the problematic situation and don't error out early.

@bors r+

@bors
Copy link
Contributor

bors commented Apr 2, 2019

📌 Commit c13daeb has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2019
Centril added a commit to Centril/rust that referenced this pull request Apr 2, 2019
…=oli-obk

Fix stack overflow when generating debuginfo for 'recursive' type

By using 'impl trait', it's possible to create a self-referential
type as follows:

fn foo() -> impl Copy { foo }

This is a function which returns itself.
Normally, the signature of this function would be impossible
to write - it would look like 'fn foo() -> fn() -> fn() ...'
e.g. a function which returns a function, which returns a function...

Using 'impl trait' allows us to avoid writing this infinitely long
type. While it's useless for practical purposes, it does compile and run

However, issues arise when we try to generate llvm debuginfo for such a
type. All 'impl trait' types (e.g. ty::Opaque) are resolved when we
generate debuginfo, which can lead to us recursing back to the original
'fn' type when we try to process its return type.

To resolve this, I've modified debuginfo generation to account for these
kinds of weird types. Unfortunately, there's no 'correct' debuginfo that
we can generate - 'impl trait' does not exist in debuginfo, and this
kind of recursive type is impossible to directly represent.

To ensure that we emit *something*, this commit emits dummy
debuginfo/type names whenever it encounters a self-reference. In
practice, this should never happen - it's just to ensure that we can
emit some kind of debuginfo, even if it's not particularly meaningful

Fixes rust-lang#58463
Centril added a commit to Centril/rust that referenced this pull request Apr 2, 2019
…=oli-obk

Fix stack overflow when generating debuginfo for 'recursive' type

By using 'impl trait', it's possible to create a self-referential
type as follows:

fn foo() -> impl Copy { foo }

This is a function which returns itself.
Normally, the signature of this function would be impossible
to write - it would look like 'fn foo() -> fn() -> fn() ...'
e.g. a function which returns a function, which returns a function...

Using 'impl trait' allows us to avoid writing this infinitely long
type. While it's useless for practical purposes, it does compile and run

However, issues arise when we try to generate llvm debuginfo for such a
type. All 'impl trait' types (e.g. ty::Opaque) are resolved when we
generate debuginfo, which can lead to us recursing back to the original
'fn' type when we try to process its return type.

To resolve this, I've modified debuginfo generation to account for these
kinds of weird types. Unfortunately, there's no 'correct' debuginfo that
we can generate - 'impl trait' does not exist in debuginfo, and this
kind of recursive type is impossible to directly represent.

To ensure that we emit *something*, this commit emits dummy
debuginfo/type names whenever it encounters a self-reference. In
practice, this should never happen - it's just to ensure that we can
emit some kind of debuginfo, even if it's not particularly meaningful

Fixes rust-lang#58463
bors added a commit that referenced this pull request Apr 2, 2019
Rollup of 4 pull requests

Successful merges:

 - #59166 (resolve: collect trait aliases along with traits)
 - #59341 (Fix custom relative libdir)
 - #59446 (Fix stack overflow when generating debuginfo for 'recursive' type)
 - #59529 (Added documentation on the remainder (Rem) operator for floating points.)

Failed merges:

r? @ghost
@bors bors merged commit c13daeb into rust-lang:master Apr 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
7 participants