about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-04-26 21:02:35 +0200
committerGitHub <noreply@github.com>2020-04-26 21:02:35 +0200
commit4199ef14d7fdea2f8c2d9ebb418b5304df3716ee (patch)
tree8d0eba646b80608ffd00dabb3c067d007d07328b
parent398d3eeca1f6cb84f91275826d66548c75e8fac0 (diff)
parent97983af76a3b7c180704a33246d91e9d98d2e42a (diff)
downloadrust-4199ef14d7fdea2f8c2d9ebb418b5304df3716ee.tar.gz
rust-4199ef14d7fdea2f8c2d9ebb418b5304df3716ee.zip
Rollup merge of #71537 - Mark-Simulacrum:no-self-open, r=davidtwco
Remove support for self-opening

This was only used for linkage test cases, which is already covered by
the [run-make-fulldeps/symbol-visibility test](https://github.com/rust-lang/rust/blob/master/src/test/run-make-fulldeps/symbol-visibility/Makefile) -- which fairly extensively makes
sure we're correctly exporting the right symbols at the right visibility (for
various Rust crate types).

This fixes #10379 and resolves #10356 by removing the test case (and underlying support in the compiler). AFAICT, the better way to test visibility is via nm, like the symbol visibility test. It seems like that's sufficient; I suspect that given that we don't use this we should just drop it (android is tier 2 anyway). But happy to hear otherwise.
-rw-r--r--src/librustc_interface/util.rs2
-rw-r--r--src/librustc_metadata/creader.rs2
-rw-r--r--src/librustc_metadata/dynamic_lib.rs47
-rw-r--r--src/librustc_metadata/dynamic_lib/tests.rs30
-rw-r--r--src/librustc_plugin_impl/load.rs2
-rw-r--r--src/test/run-make-fulldeps/extern-fn-reachable/main.rs2
-rw-r--r--src/test/ui-fulldeps/auxiliary/linkage-visibility.rs35
-rw-r--r--src/test/ui-fulldeps/linkage-visibility.rs13
8 files changed, 16 insertions, 117 deletions
diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs
index 367dc4dee7e..72c25270a5d 100644
--- a/src/librustc_interface/util.rs
+++ b/src/librustc_interface/util.rs
@@ -205,7 +205,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
 }
 
 fn load_backend_from_dylib(path: &Path) -> fn() -> Box<dyn CodegenBackend> {
-    let lib = DynamicLibrary::open(Some(path)).unwrap_or_else(|err| {
+    let lib = DynamicLibrary::open(path).unwrap_or_else(|err| {
         let err = format!("couldn't load codegen backend {:?}: {:?}", path, err);
         early_error(ErrorOutputType::default(), &err);
     });
diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs
index e7a863c63cc..6168f6e1e5c 100644
--- a/src/librustc_metadata/creader.rs
+++ b/src/librustc_metadata/creader.rs
@@ -591,7 +591,7 @@ impl<'a> CrateLoader<'a> {
 
         // Make sure the path contains a / or the linker will search for it.
         let path = env::current_dir().unwrap().join(path);
-        let lib = match DynamicLibrary::open(Some(&path)) {
+        let lib = match DynamicLibrary::open(&path) {
             Ok(lib) => lib,
             Err(err) => self.sess.span_fatal(span, &err),
         };
diff --git a/src/librustc_metadata/dynamic_lib.rs b/src/librustc_metadata/dynamic_lib.rs
index 3e78a585235..ce19240a009 100644
--- a/src/librustc_metadata/dynamic_lib.rs
+++ b/src/librustc_metadata/dynamic_lib.rs
@@ -16,10 +16,9 @@ impl Drop for DynamicLibrary {
 }
 
 impl DynamicLibrary {
-    /// Lazily open a dynamic library. When passed None it gives a
-    /// handle to the calling process
-    pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, String> {
-        let maybe_library = dl::open(filename.map(|path| path.as_os_str()));
+    /// Lazily open a dynamic library.
+    pub fn open(filename: &Path) -> Result<DynamicLibrary, String> {
+        let maybe_library = dl::open(filename.as_os_str());
 
         // The dynamic library must not be constructed if there is
         // an error opening the library so the destructor does not
@@ -57,24 +56,13 @@ mod dl {
     use std::ptr;
     use std::str;
 
-    pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
+    pub(super) fn open(filename: &OsStr) -> Result<*mut u8, String> {
         check_for_errors_in(|| unsafe {
-            match filename {
-                Some(filename) => open_external(filename),
-                None => open_internal(),
-            }
+            let s = CString::new(filename.as_bytes()).unwrap();
+            libc::dlopen(s.as_ptr(), libc::RTLD_LAZY) as *mut u8
         })
     }
 
-    unsafe fn open_external(filename: &OsStr) -> *mut u8 {
-        let s = CString::new(filename.as_bytes()).unwrap();
-        libc::dlopen(s.as_ptr(), libc::RTLD_LAZY) as *mut u8
-    }
-
-    unsafe fn open_internal() -> *mut u8 {
-        libc::dlopen(ptr::null(), libc::RTLD_LAZY) as *mut u8
-    }
-
     fn check_for_errors_in<T, F>(f: F) -> Result<T, String>
     where
         F: FnOnce() -> T,
@@ -124,10 +112,10 @@ mod dl {
 
     use winapi::shared::minwindef::HMODULE;
     use winapi::um::errhandlingapi::SetThreadErrorMode;
-    use winapi::um::libloaderapi::{FreeLibrary, GetModuleHandleExW, GetProcAddress, LoadLibraryW};
+    use winapi::um::libloaderapi::{FreeLibrary, GetProcAddress, LoadLibraryW};
     use winapi::um::winbase::SEM_FAILCRITICALERRORS;
 
-    pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
+    pub(super) fn open(filename: &OsStr) -> Result<*mut u8, String> {
         // disable "dll load failed" error dialog.
         let prev_error_mode = unsafe {
             let new_error_mode = SEM_FAILCRITICALERRORS;
@@ -139,22 +127,9 @@ mod dl {
             prev_error_mode
         };
 
-        let result = match filename {
-            Some(filename) => {
-                let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
-                let result = unsafe { LoadLibraryW(filename_str.as_ptr()) } as *mut u8;
-                ptr_result(result)
-            }
-            None => {
-                let mut handle = ptr::null_mut();
-                let succeeded = unsafe { GetModuleHandleExW(0, ptr::null(), &mut handle) };
-                if succeeded == 0 {
-                    Err(io::Error::last_os_error().to_string())
-                } else {
-                    Ok(handle as *mut u8)
-                }
-            }
-        };
+        let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
+        let result = unsafe { LoadLibraryW(filename_str.as_ptr()) } as *mut u8;
+        let result = ptr_result(result);
 
         unsafe {
             SetThreadErrorMode(prev_error_mode, ptr::null_mut());
diff --git a/src/librustc_metadata/dynamic_lib/tests.rs b/src/librustc_metadata/dynamic_lib/tests.rs
index cbf2b181e3c..7090bbf61c7 100644
--- a/src/librustc_metadata/dynamic_lib/tests.rs
+++ b/src/librustc_metadata/dynamic_lib/tests.rs
@@ -1,32 +1,4 @@
 use super::*;
-use std::mem;
-
-#[test]
-fn test_loading_atoi() {
-    if cfg!(windows) {
-        return;
-    }
-
-    // The C library does not need to be loaded since it is already linked in
-    let lib = match DynamicLibrary::open(None) {
-        Err(error) => panic!("Could not load self as module: {}", error),
-        Ok(lib) => lib,
-    };
-
-    let atoi: extern "C" fn(*const libc::c_char) -> libc::c_int = unsafe {
-        match lib.symbol("atoi") {
-            Err(error) => panic!("Could not load function atoi: {}", error),
-            Ok(atoi) => mem::transmute::<*mut u8, _>(atoi),
-        }
-    };
-
-    let argument = CString::new("1383428980").unwrap();
-    let expected_result = 0x52757374;
-    let result = atoi(argument.as_ptr());
-    if result != expected_result {
-        panic!("atoi({:?}) != {} but equaled {} instead", argument, expected_result, result)
-    }
-}
 
 #[test]
 fn test_errors_do_not_crash() {
@@ -39,7 +11,7 @@ fn test_errors_do_not_crash() {
     // Open /dev/null as a library to get an error, and make sure
     // that only causes an error, and not a crash.
     let path = Path::new("/dev/null");
-    match DynamicLibrary::open(Some(&path)) {
+    match DynamicLibrary::open(&path) {
         Err(_) => {}
         Ok(_) => panic!("Successfully opened the empty library."),
     }
diff --git a/src/librustc_plugin_impl/load.rs b/src/librustc_plugin_impl/load.rs
index f41bc44d177..f48d2b6c8b5 100644
--- a/src/librustc_plugin_impl/load.rs
+++ b/src/librustc_plugin_impl/load.rs
@@ -76,7 +76,7 @@ fn dylink_registrar(
     // Make sure the path contains a / or the linker will search for it.
     let path = env::current_dir().unwrap().join(&path);
 
-    let lib = match DynamicLibrary::open(Some(&path)) {
+    let lib = match DynamicLibrary::open(&path) {
         Ok(lib) => lib,
         // this is fatal: there are almost certainly macros we need
         // inside this crate, so continue would spew "macro undefined"
diff --git a/src/test/run-make-fulldeps/extern-fn-reachable/main.rs b/src/test/run-make-fulldeps/extern-fn-reachable/main.rs
index a9d28d1bebe..c1de6477585 100644
--- a/src/test/run-make-fulldeps/extern-fn-reachable/main.rs
+++ b/src/test/run-make-fulldeps/extern-fn-reachable/main.rs
@@ -8,7 +8,7 @@ use std::path::Path;
 pub fn main() {
     unsafe {
         let path = Path::new("libdylib.so");
-        let a = DynamicLibrary::open(Some(&path)).unwrap();
+        let a = DynamicLibrary::open(&path).unwrap();
         assert!(a.symbol::<isize>("fun1").is_ok());
         assert!(a.symbol::<isize>("fun2").is_ok());
         assert!(a.symbol::<isize>("fun3").is_ok());
diff --git a/src/test/ui-fulldeps/auxiliary/linkage-visibility.rs b/src/test/ui-fulldeps/auxiliary/linkage-visibility.rs
deleted file mode 100644
index 837ed1f002f..00000000000
--- a/src/test/ui-fulldeps/auxiliary/linkage-visibility.rs
+++ /dev/null
@@ -1,35 +0,0 @@
-// ignore-musl - dlsym doesn't see symbols without "-C link-arg=-Wl,--export-dynamic"
-
-#![feature(rustc_private)]
-
-extern crate rustc_metadata;
-
-use rustc_metadata::dynamic_lib::DynamicLibrary;
-
-#[no_mangle]
-pub fn foo() {
-    bar();
-}
-
-pub fn foo2<T>() {
-    fn bar2() {
-        bar();
-    }
-    bar2();
-}
-
-#[no_mangle]
-fn bar() {}
-
-#[allow(dead_code)]
-#[no_mangle]
-fn baz() {}
-
-pub fn test() {
-    let lib = DynamicLibrary::open(None).unwrap();
-    unsafe {
-        assert!(lib.symbol::<isize>("foo").is_ok());
-        assert!(lib.symbol::<isize>("baz").is_ok());
-        assert!(lib.symbol::<isize>("bar").is_ok());
-    }
-}
diff --git a/src/test/ui-fulldeps/linkage-visibility.rs b/src/test/ui-fulldeps/linkage-visibility.rs
deleted file mode 100644
index ae46fbc4e8a..00000000000
--- a/src/test/ui-fulldeps/linkage-visibility.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-// run-pass
-// aux-build:linkage-visibility.rs
-// ignore-android: FIXME(#10356)
-// ignore-windows: std::dynamic_lib does not work on Windows well
-// ignore-emscripten no dynamic linking
-
-extern crate linkage_visibility as foo;
-
-pub fn main() {
-    foo::test();
-    foo::foo2::<isize>();
-    foo::foo();
-}