Skip to content

Commit f7cf915

Browse files
committed
Auto merge of #69371 - tmiasko:weak-lang-cycle, r=alexcrichton
Improve linking of crates with circular dependencies Previously, the code responsible for handling the cycles between crates introduces through weak lang items, would keep a set of missing language items: * extending it with items missing from the current crate, * removing items provided by the current crate, * grouping the crates when the set changed from non-empty back to empty. This could produce incorrect results, if a lang item was missing from a crate that comes after the crate that provides it (in the loop iteration order). In that case the grouping would not take place. The changes here address this specific failure scenario by keeping track of two separate sets of crates. Those that are required to link successfully, and those that are available for linking. Verified using test case from #69368.
2 parents 9381e81 + 9039320 commit f7cf915

File tree

5 files changed

+86
-7
lines changed

5 files changed

+86
-7
lines changed

src/librustc_codegen_ssa/back/link.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1519,17 +1519,25 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
15191519
// for the current implementation of the standard library.
15201520
let mut group_end = None;
15211521
let mut group_start = None;
1522-
let mut end_with = FxHashSet::default();
1522+
// Crates available for linking thus far.
1523+
let mut available = FxHashSet::default();
1524+
// Crates required to satisfy dependencies discovered so far.
1525+
let mut required = FxHashSet::default();
1526+
15231527
let info = &codegen_results.crate_info;
15241528
for &(cnum, _) in deps.iter().rev() {
15251529
if let Some(missing) = info.missing_lang_items.get(&cnum) {
1526-
end_with.extend(missing.iter().cloned());
1527-
if !end_with.is_empty() && group_end.is_none() {
1528-
group_end = Some(cnum);
1529-
}
1530+
let missing_crates = missing.iter().map(|i| info.lang_item_to_crate.get(i).copied());
1531+
required.extend(missing_crates);
1532+
}
1533+
1534+
required.insert(Some(cnum));
1535+
available.insert(Some(cnum));
1536+
1537+
if required.len() > available.len() && group_end.is_none() {
1538+
group_end = Some(cnum);
15301539
}
1531-
end_with.retain(|item| info.lang_item_to_crate.get(item) != Some(&cnum));
1532-
if end_with.is_empty() && group_end.is_some() {
1540+
if required.len() == available.len() && group_end.is_some() {
15331541
group_start = Some(cnum);
15341542
break;
15351543
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-include ../tools.mk
2+
3+
# Test that previously triggered a linker failure with root cause
4+
# similar to one found in the issue #69368.
5+
#
6+
# The crate that provides oom lang item is missing some other lang
7+
# items. Necessary to prevent the use of start-group / end-group.
8+
#
9+
# The weak lang items are defined in a separate compilation units,
10+
# so that linker could omit them if not used.
11+
#
12+
# The crates that need those weak lang items are dependencies of
13+
# crates that provide them.
14+
15+
all:
16+
$(RUSTC) a.rs
17+
$(RUSTC) b.rs
18+
$(RUSTC) c.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![crate_type = "rlib"]
2+
#![feature(lang_items)]
3+
#![feature(panic_unwind)]
4+
#![no_std]
5+
6+
extern crate panic_unwind;
7+
8+
#[panic_handler]
9+
pub fn panic_handler(_: &core::panic::PanicInfo) -> ! {
10+
loop {}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![crate_type = "rlib"]
2+
#![feature(alloc_error_handler)]
3+
#![no_std]
4+
5+
#[alloc_error_handler]
6+
pub fn error_handler(_: core::alloc::Layout) -> ! {
7+
panic!();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![crate_type = "bin"]
2+
#![feature(start)]
3+
#![no_std]
4+
5+
extern crate alloc;
6+
extern crate a;
7+
extern crate b;
8+
9+
use alloc::vec::Vec;
10+
use core::alloc::*;
11+
12+
struct Allocator;
13+
14+
unsafe impl GlobalAlloc for Allocator {
15+
unsafe fn alloc(&self, _: Layout) -> *mut u8 {
16+
loop {}
17+
}
18+
19+
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
20+
loop {}
21+
}
22+
}
23+
24+
#[global_allocator]
25+
static ALLOCATOR: Allocator = Allocator;
26+
27+
#[start]
28+
fn main(argc: isize, _argv: *const *const u8) -> isize {
29+
let mut v = Vec::new();
30+
for i in 0..argc {
31+
v.push(i);
32+
}
33+
v.iter().sum()
34+
}

0 commit comments

Comments
 (0)