Skip to content

Commit 4199ef1

Browse files
authored
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.
2 parents 398d3ee + 97983af commit 4199ef1

File tree

8 files changed

+16
-117
lines changed

8 files changed

+16
-117
lines changed

src/librustc_interface/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
205205
}
206206

207207
fn load_backend_from_dylib(path: &Path) -> fn() -> Box<dyn CodegenBackend> {
208-
let lib = DynamicLibrary::open(Some(path)).unwrap_or_else(|err| {
208+
let lib = DynamicLibrary::open(path).unwrap_or_else(|err| {
209209
let err = format!("couldn't load codegen backend {:?}: {:?}", path, err);
210210
early_error(ErrorOutputType::default(), &err);
211211
});

src/librustc_metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ impl<'a> CrateLoader<'a> {
591591

592592
// Make sure the path contains a / or the linker will search for it.
593593
let path = env::current_dir().unwrap().join(path);
594-
let lib = match DynamicLibrary::open(Some(&path)) {
594+
let lib = match DynamicLibrary::open(&path) {
595595
Ok(lib) => lib,
596596
Err(err) => self.sess.span_fatal(span, &err),
597597
};

src/librustc_metadata/dynamic_lib.rs

+11-36
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ impl Drop for DynamicLibrary {
1616
}
1717

1818
impl DynamicLibrary {
19-
/// Lazily open a dynamic library. When passed None it gives a
20-
/// handle to the calling process
21-
pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, String> {
22-
let maybe_library = dl::open(filename.map(|path| path.as_os_str()));
19+
/// Lazily open a dynamic library.
20+
pub fn open(filename: &Path) -> Result<DynamicLibrary, String> {
21+
let maybe_library = dl::open(filename.as_os_str());
2322

2423
// The dynamic library must not be constructed if there is
2524
// an error opening the library so the destructor does not
@@ -57,24 +56,13 @@ mod dl {
5756
use std::ptr;
5857
use std::str;
5958

60-
pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
59+
pub(super) fn open(filename: &OsStr) -> Result<*mut u8, String> {
6160
check_for_errors_in(|| unsafe {
62-
match filename {
63-
Some(filename) => open_external(filename),
64-
None => open_internal(),
65-
}
61+
let s = CString::new(filename.as_bytes()).unwrap();
62+
libc::dlopen(s.as_ptr(), libc::RTLD_LAZY) as *mut u8
6663
})
6764
}
6865

69-
unsafe fn open_external(filename: &OsStr) -> *mut u8 {
70-
let s = CString::new(filename.as_bytes()).unwrap();
71-
libc::dlopen(s.as_ptr(), libc::RTLD_LAZY) as *mut u8
72-
}
73-
74-
unsafe fn open_internal() -> *mut u8 {
75-
libc::dlopen(ptr::null(), libc::RTLD_LAZY) as *mut u8
76-
}
77-
7866
fn check_for_errors_in<T, F>(f: F) -> Result<T, String>
7967
where
8068
F: FnOnce() -> T,
@@ -124,10 +112,10 @@ mod dl {
124112

125113
use winapi::shared::minwindef::HMODULE;
126114
use winapi::um::errhandlingapi::SetThreadErrorMode;
127-
use winapi::um::libloaderapi::{FreeLibrary, GetModuleHandleExW, GetProcAddress, LoadLibraryW};
115+
use winapi::um::libloaderapi::{FreeLibrary, GetProcAddress, LoadLibraryW};
128116
use winapi::um::winbase::SEM_FAILCRITICALERRORS;
129117

130-
pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
118+
pub(super) fn open(filename: &OsStr) -> Result<*mut u8, String> {
131119
// disable "dll load failed" error dialog.
132120
let prev_error_mode = unsafe {
133121
let new_error_mode = SEM_FAILCRITICALERRORS;
@@ -139,22 +127,9 @@ mod dl {
139127
prev_error_mode
140128
};
141129

142-
let result = match filename {
143-
Some(filename) => {
144-
let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
145-
let result = unsafe { LoadLibraryW(filename_str.as_ptr()) } as *mut u8;
146-
ptr_result(result)
147-
}
148-
None => {
149-
let mut handle = ptr::null_mut();
150-
let succeeded = unsafe { GetModuleHandleExW(0, ptr::null(), &mut handle) };
151-
if succeeded == 0 {
152-
Err(io::Error::last_os_error().to_string())
153-
} else {
154-
Ok(handle as *mut u8)
155-
}
156-
}
157-
};
130+
let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
131+
let result = unsafe { LoadLibraryW(filename_str.as_ptr()) } as *mut u8;
132+
let result = ptr_result(result);
158133

159134
unsafe {
160135
SetThreadErrorMode(prev_error_mode, ptr::null_mut());

src/librustc_metadata/dynamic_lib/tests.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,4 @@
11
use super::*;
2-
use std::mem;
3-
4-
#[test]
5-
fn test_loading_atoi() {
6-
if cfg!(windows) {
7-
return;
8-
}
9-
10-
// The C library does not need to be loaded since it is already linked in
11-
let lib = match DynamicLibrary::open(None) {
12-
Err(error) => panic!("Could not load self as module: {}", error),
13-
Ok(lib) => lib,
14-
};
15-
16-
let atoi: extern "C" fn(*const libc::c_char) -> libc::c_int = unsafe {
17-
match lib.symbol("atoi") {
18-
Err(error) => panic!("Could not load function atoi: {}", error),
19-
Ok(atoi) => mem::transmute::<*mut u8, _>(atoi),
20-
}
21-
};
22-
23-
let argument = CString::new("1383428980").unwrap();
24-
let expected_result = 0x52757374;
25-
let result = atoi(argument.as_ptr());
26-
if result != expected_result {
27-
panic!("atoi({:?}) != {} but equaled {} instead", argument, expected_result, result)
28-
}
29-
}
302

313
#[test]
324
fn test_errors_do_not_crash() {
@@ -39,7 +11,7 @@ fn test_errors_do_not_crash() {
3911
// Open /dev/null as a library to get an error, and make sure
4012
// that only causes an error, and not a crash.
4113
let path = Path::new("/dev/null");
42-
match DynamicLibrary::open(Some(&path)) {
14+
match DynamicLibrary::open(&path) {
4315
Err(_) => {}
4416
Ok(_) => panic!("Successfully opened the empty library."),
4517
}

src/librustc_plugin_impl/load.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn dylink_registrar(
7676
// Make sure the path contains a / or the linker will search for it.
7777
let path = env::current_dir().unwrap().join(&path);
7878

79-
let lib = match DynamicLibrary::open(Some(&path)) {
79+
let lib = match DynamicLibrary::open(&path) {
8080
Ok(lib) => lib,
8181
// this is fatal: there are almost certainly macros we need
8282
// inside this crate, so continue would spew "macro undefined"

src/test/run-make-fulldeps/extern-fn-reachable/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::Path;
88
pub fn main() {
99
unsafe {
1010
let path = Path::new("libdylib.so");
11-
let a = DynamicLibrary::open(Some(&path)).unwrap();
11+
let a = DynamicLibrary::open(&path).unwrap();
1212
assert!(a.symbol::<isize>("fun1").is_ok());
1313
assert!(a.symbol::<isize>("fun2").is_ok());
1414
assert!(a.symbol::<isize>("fun3").is_ok());

src/test/ui-fulldeps/auxiliary/linkage-visibility.rs

-35
This file was deleted.

src/test/ui-fulldeps/linkage-visibility.rs

-13
This file was deleted.

0 commit comments

Comments
 (0)