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

Filter out items other than non-generic functions and statics in our version of exported_symbols #1833

Merged
merged 2 commits into from Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use log::debug;

use rustc_driver::Compilation;
use rustc_errors::emitter::{ColorConfig, HumanReadableErrorType};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::{self as hir, def_id::LOCAL_CRATE, Node};
use rustc_interface::interface::Config;
use rustc_middle::{
middle::exported_symbols::{ExportedSymbol, SymbolExportLevel},
Expand Down Expand Up @@ -109,12 +109,27 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L62-L63
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L174
tcx.reachable_set(()).iter().filter_map(|&local_def_id| {
tcx.codegen_fn_attrs(local_def_id)
.contains_extern_indicator()
.then_some((
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
SymbolExportLevel::C,
))
// Do the same filtering that rustc does:
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L84-L102
// Otherwise it may cause unexpected behaviours and ICEs
// (https://github.com/rust-lang/rust/issues/86261).
let is_reachable_non_generic = matches!(
tcx.hir().get(tcx.hir().local_def_id_to_hir_id(local_def_id)),
Node::Item(&hir::Item {
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn(..),
..
}) | Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Fn(..),
..
})
if !tcx.generics_of(local_def_id).requires_monomorphization(tcx)
);
(is_reachable_non_generic
&& tcx.codegen_fn_attrs(local_def_id).contains_extern_indicator())
.then_some((
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
SymbolExportLevel::C,
))
}),
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
second_crate: tcx.crate_name(cnum),
});
}
if tcx.def_kind(def_id) != DefKind::Fn {
if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
throw_ub_format!(
"attempt to call an exported symbol that is not defined as a function"
);
Expand Down
5 changes: 5 additions & 0 deletions test-cargo-miri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test-cargo-miri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ issue_1567 = { path = "issue-1567" }
issue_1691 = { path = "issue-1691" }
issue_1705 = { path = "issue-1705" }
issue_1760 = { path = "issue-1760" }
issue_rust_86261 = { path = "issue-rust-86261" }

[dev-dependencies]
rand = { version = "0.8", features = ["small_rng"] }
Expand Down
9 changes: 9 additions & 0 deletions test-cargo-miri/exported-symbol-dep/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
fn exported_symbol() -> i32 {
123456
}

pub struct AssocFn;

impl AssocFn {
#[no_mangle]
pub fn assoc_fn_as_exported_symbol() -> i32 {
Comment on lines +9 to +10
Copy link
Author

@ghost ghost Jun 14, 2021

Choose a reason for hiding this comment

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

(This is testing #[no_mangle] on Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }), which is included in exported_symbols (but previously broken in Miri: da2ed6f -- I fix that together in this PR in order to test this).)

I can't test this in tests/run-pass, because #[no_mangle] only seems to work on associated functions that are public in a library crate...
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f1244afcdd26e2a28445f6e82ca46b50

If the associated function is used directly, it can works with rustc when compiling as a binrary, but it still doesn't work in Miri:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=46fc298b46ff44093c45cab9fddb6650

Copy link
Member

Choose a reason for hiding this comment

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

So you are saying even with this patch, https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=46fc298b46ff44093c45cab9fddb6650 fails? Could you open an issue for that?

-123456
}
}
5 changes: 5 additions & 0 deletions test-cargo-miri/issue-rust-86261/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "issue_rust_86261"
version = "0.1.0"
authors = ["Miri Team"]
edition = "2018"
23 changes: 23 additions & 0 deletions test-cargo-miri/issue-rust-86261/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![allow(unused_imports, unused_attributes, no_mangle_generic_items)]

// Regression test for https://github.com/rust-lang/rust/issues/86261:
// `#[no_mangle]` on a `use` item.
#[no_mangle]
use std::{thread,panic, io, boxed, any, string};

// `#[no_mangle]` on a struct has a similar problem.
#[no_mangle]
pub struct NoMangleStruct;

// If `#[no_mangle]` has effect on the `struct` above, calling `NoMangleStruct` will fail with
// "multiple definitions of symbol `NoMangleStruct`" error.
#[export_name = "NoMangleStruct"]
fn no_mangle_struct() {}

// `#[no_mangle]` on a generic function can also cause ICEs.
#[no_mangle]
fn no_mangle_generic<T>() {}

// Same as `no_mangle_struct()` but for the `no_mangle_generic()` generic function.
#[export_name = "no_mangle_generic"]
fn no_mangle_generic2() {}
7 changes: 7 additions & 0 deletions test-cargo-miri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ mod test {
fn exported_symbol() {
extern crate cargo_miri_test;
extern crate exported_symbol;
extern crate issue_rust_86261;
// Test calling exported symbols in (transitive) dependencies.
// Repeat calls to make sure the `Instance` cache is not broken.
for _ in 0..3 {
extern "Rust" {
fn exported_symbol() -> i32;
fn assoc_fn_as_exported_symbol() -> i32;
fn make_true() -> bool;
fn NoMangleStruct();
fn no_mangle_generic();
}
assert_eq!(unsafe { exported_symbol() }, 123456);
assert_eq!(unsafe { assoc_fn_as_exported_symbol() }, -123456);
assert!(unsafe { make_true() });
unsafe { NoMangleStruct() }
unsafe { no_mangle_generic() }
}
}
}