Skip to content

Commit

Permalink
Don't report UB for #[no_mangle] on associated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hyd-dev committed Jun 14, 2021
1 parent 9946734 commit da2ed6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
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
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 {
-123456
}
}
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() }
}
}
}

0 comments on commit da2ed6f

Please sign in to comment.