Skip to content

Commit 9946734

Browse files
author
hyd-dev
committed
Do not return DefId that doesn't have exported symbol in exported_symbols
1 parent d39f0c6 commit 9946734

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

src/bin/miri.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use log::debug;
1919

2020
use rustc_driver::Compilation;
2121
use rustc_errors::emitter::{ColorConfig, HumanReadableErrorType};
22-
use rustc_hir::def_id::LOCAL_CRATE;
22+
use rustc_hir::{self as hir, def_id::LOCAL_CRATE, Node};
2323
use rustc_interface::interface::Config;
2424
use rustc_middle::{
2525
middle::exported_symbols::{ExportedSymbol, SymbolExportLevel},
@@ -109,12 +109,27 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
109109
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L62-L63
110110
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L174
111111
tcx.reachable_set(()).iter().filter_map(|&local_def_id| {
112-
tcx.codegen_fn_attrs(local_def_id)
113-
.contains_extern_indicator()
114-
.then_some((
115-
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
116-
SymbolExportLevel::C,
117-
))
112+
// Do the same filtering that rustc does:
113+
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L84-L102
114+
// Otherwise it may cause unexpected behaviours and ICEs
115+
// (https://github.com/rust-lang/rust/issues/86261).
116+
let is_reachable_non_generic = matches!(
117+
tcx.hir().get(tcx.hir().local_def_id_to_hir_id(local_def_id)),
118+
Node::Item(&hir::Item {
119+
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn(..),
120+
..
121+
}) | Node::ImplItem(&hir::ImplItem {
122+
kind: hir::ImplItemKind::Fn(..),
123+
..
124+
})
125+
if !tcx.generics_of(local_def_id).requires_monomorphization(tcx)
126+
);
127+
(is_reachable_non_generic
128+
&& tcx.codegen_fn_attrs(local_def_id).contains_extern_indicator())
129+
.then_some((
130+
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
131+
SymbolExportLevel::C,
132+
))
118133
}),
119134
)
120135
}

test-cargo-miri/Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-cargo-miri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ issue_1567 = { path = "issue-1567" }
1515
issue_1691 = { path = "issue-1691" }
1616
issue_1705 = { path = "issue-1705" }
1717
issue_1760 = { path = "issue-1760" }
18+
issue_rust_86261 = { path = "issue-rust-86261" }
1819

1920
[dev-dependencies]
2021
rand = { version = "0.8", features = ["small_rng"] }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "issue_rust_86261"
3+
version = "0.1.0"
4+
authors = ["Miri Team"]
5+
edition = "2018"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(unused_imports, unused_attributes, no_mangle_generic_items)]
2+
3+
// Regression test for https://github.com/rust-lang/rust/issues/86261:
4+
// `#[no_mangle]` on a `use` item.
5+
#[no_mangle]
6+
use std::{thread,panic, io, boxed, any, string};
7+
8+
// `#[no_mangle]` on a struct has a similar problem.
9+
#[no_mangle]
10+
pub struct NoMangleStruct;
11+
12+
// If `#[no_mangle]` has effect on the `struct` above, calling `NoMangleStruct` will fail with
13+
// "multiple definitions of symbol `NoMangleStruct`" error.
14+
#[export_name = "NoMangleStruct"]
15+
fn no_mangle_struct() {}
16+
17+
// `#[no_mangle]` on a generic function can also cause ICEs.
18+
#[no_mangle]
19+
fn no_mangle_generic<T>() {}
20+
21+
// Same as `no_mangle_struct()` but for the `no_mangle_generic()` generic function.
22+
#[export_name = "no_mangle_generic"]
23+
fn no_mangle_generic2() {}

0 commit comments

Comments
 (0)