-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fix stack overflow when generating debuginfo for 'recursive' type #59446
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
512069f
Fix stack overflow when generating debuginfo for 'recursive' type
Aaron1011 e1837a0
Fix inverted panic check
Aaron1011 aed7ec4
Add codegen test
Aaron1011 c4556a5
Fix typos
Aaron1011 d2584e3
Only track 'visited' state for function types
Aaron1011 c13daeb
Fix typo
Aaron1011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ use rustc::hir::def_id::DefId; | |
use rustc::ty::subst::SubstsRef; | ||
use rustc::ty::{self, Ty}; | ||
use rustc_codegen_ssa::traits::*; | ||
use rustc_data_structures::fx::FxHashSet; | ||
|
||
use rustc::hir; | ||
|
||
|
@@ -17,7 +18,8 @@ pub fn compute_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
qualified: bool) | ||
-> String { | ||
let mut result = String::with_capacity(64); | ||
push_debuginfo_type_name(cx, t, qualified, &mut result); | ||
let mut visited = FxHashSet::default(); | ||
push_debuginfo_type_name(cx, t, qualified, &mut result, &mut visited); | ||
result | ||
} | ||
|
||
|
@@ -26,7 +28,9 @@ pub fn compute_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | ||
t: Ty<'tcx>, | ||
qualified: bool, | ||
output: &mut String) { | ||
output: &mut String, | ||
visited: &mut FxHashSet<Ty<'tcx>>) { | ||
|
||
// When targeting MSVC, emit C++ style type names for compatibility with | ||
// .natvis visualizers (and perhaps other existing native debuggers?) | ||
let cpp_like_names = cx.sess().target.target.options.is_like_msvc; | ||
|
@@ -42,12 +46,12 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
ty::Foreign(def_id) => push_item_name(cx, def_id, qualified, output), | ||
ty::Adt(def, substs) => { | ||
push_item_name(cx, def.did, qualified, output); | ||
push_type_params(cx, substs, output); | ||
push_type_params(cx, substs, output, visited); | ||
}, | ||
ty::Tuple(component_types) => { | ||
output.push('('); | ||
for &component_type in component_types { | ||
push_debuginfo_type_name(cx, component_type, true, output); | ||
push_debuginfo_type_name(cx, component_type, true, output, visited); | ||
output.push_str(", "); | ||
} | ||
if !component_types.is_empty() { | ||
|
@@ -65,7 +69,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
hir::MutMutable => output.push_str("mut "), | ||
} | ||
|
||
push_debuginfo_type_name(cx, inner_type, true, output); | ||
push_debuginfo_type_name(cx, inner_type, true, output, visited); | ||
|
||
if cpp_like_names { | ||
output.push('*'); | ||
|
@@ -79,15 +83,15 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
output.push_str("mut "); | ||
} | ||
|
||
push_debuginfo_type_name(cx, inner_type, true, output); | ||
push_debuginfo_type_name(cx, inner_type, true, output, visited); | ||
|
||
if cpp_like_names { | ||
output.push('*'); | ||
} | ||
}, | ||
ty::Array(inner_type, len) => { | ||
output.push('['); | ||
push_debuginfo_type_name(cx, inner_type, true, output); | ||
push_debuginfo_type_name(cx, inner_type, true, output, visited); | ||
output.push_str(&format!("; {}", len.unwrap_usize(cx.tcx))); | ||
output.push(']'); | ||
}, | ||
|
@@ -98,7 +102,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
output.push('['); | ||
} | ||
|
||
push_debuginfo_type_name(cx, inner_type, true, output); | ||
push_debuginfo_type_name(cx, inner_type, true, output, visited); | ||
|
||
if cpp_like_names { | ||
output.push('>'); | ||
|
@@ -113,12 +117,31 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
&principal, | ||
); | ||
push_item_name(cx, principal.def_id, false, output); | ||
push_type_params(cx, principal.substs, output); | ||
push_type_params(cx, principal.substs, output, visited); | ||
} else { | ||
output.push_str("dyn '_"); | ||
} | ||
}, | ||
ty::FnDef(..) | ty::FnPtr(_) => { | ||
// We've encountered a weird 'recursive type' | ||
// Currently, the only way to generate such a type | ||
// is by using 'impl trait': | ||
// | ||
// fn foo() -> impl Copy { foo } | ||
// | ||
// There's not really a sensible name we can generate, | ||
// since we don't include 'impl trait' types (e.g. ty::Opaque) | ||
// in the output | ||
// | ||
// Since we need to generate *something*, we just | ||
// use a dummy string that should make it clear | ||
// that something unusual is going on | ||
if !visited.insert(t) { | ||
output.push_str("<recursive_type>"); | ||
return; | ||
} | ||
|
||
|
||
let sig = t.fn_sig(cx.tcx); | ||
if sig.unsafety() == hir::Unsafety::Unsafe { | ||
output.push_str("unsafe "); | ||
|
@@ -136,7 +159,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig); | ||
if !sig.inputs().is_empty() { | ||
for ¶meter_type in sig.inputs() { | ||
push_debuginfo_type_name(cx, parameter_type, true, output); | ||
push_debuginfo_type_name(cx, parameter_type, true, output, visited); | ||
output.push_str(", "); | ||
} | ||
output.pop(); | ||
|
@@ -155,8 +178,20 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
|
||
if !sig.output().is_unit() { | ||
output.push_str(" -> "); | ||
push_debuginfo_type_name(cx, sig.output(), true, output); | ||
push_debuginfo_type_name(cx, sig.output(), true, output, visited); | ||
} | ||
|
||
|
||
// We only keep the type in 'visited' | ||
// for the duration of the body of this method. | ||
// It's fine for a particular function type | ||
// to show up multiple times in one overall type | ||
// (e.g. MyType<fn() -> u8, fn() -> u8> | ||
// | ||
// We only care about avoiding recursing | ||
// directly back to the type we're currentlu | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: "currentlu" should be "currently" |
||
// processing | ||
visited.remove(t); | ||
}, | ||
ty::Closure(..) => { | ||
output.push_str("closure"); | ||
|
@@ -200,15 +235,16 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | |
// common denominator - otherwise we would run into conflicts. | ||
fn push_type_params<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, | ||
substs: SubstsRef<'tcx>, | ||
output: &mut String) { | ||
output: &mut String, | ||
visited: &mut FxHashSet<Ty<'tcx>>) { | ||
if substs.types().next().is_none() { | ||
return; | ||
} | ||
|
||
output.push('<'); | ||
|
||
for type_parameter in substs.types() { | ||
push_debuginfo_type_name(cx, type_parameter, true, output); | ||
push_debuginfo_type_name(cx, type_parameter, true, output, visited); | ||
output.push_str(", "); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// compile-flags: -g | ||
// | ||
// CHECK-LABEL: @main | ||
// CHECK: {{.*}}DIDerivedType(tag: DW_TAG_pointer_type, name: "fn() -> <recursive_type>",{{.*}} | ||
// | ||
// CHECK: {{.*}}DISubroutineType{{.*}} | ||
// CHECK: {{.*}}DIBasicType(name: "<recur_type>", encoding: DW_ATE_unsigned) | ||
|
||
pub fn foo() -> impl Copy { | ||
foo | ||
} | ||
|
||
fn main() { | ||
let my_res = foo(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// run-pass | ||
// compile-flags:-C debuginfo=2 | ||
fn foo() -> impl Copy { | ||
foo | ||
} | ||
fn main() { | ||
foo(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.