Skip to content

Commit 7b564c6

Browse files
committedJan 12, 2020
use winapi for non-stdlib Windows bindings
1 parent 1389494 commit 7b564c6

File tree

18 files changed

+94
-263
lines changed

18 files changed

+94
-263
lines changed
 

‎Cargo.lock

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ dependencies = [
201201
"serde_json",
202202
"time",
203203
"toml",
204+
"winapi 0.3.8",
204205
]
205206

206207
[[package]]
@@ -3491,6 +3492,7 @@ dependencies = [
34913492
"serialize",
34923493
"smallvec 1.0.0",
34933494
"stable_deref_trait",
3495+
"winapi 0.3.8",
34943496
]
34953497

34963498
[[package]]
@@ -3518,6 +3520,7 @@ dependencies = [
35183520
"rustc_target",
35193521
"serialize",
35203522
"syntax",
3523+
"winapi 0.3.8",
35213524
]
35223525

35233526
[[package]]
@@ -3537,6 +3540,7 @@ dependencies = [
35373540
"term_size",
35383541
"termcolor",
35393542
"unicode-width",
3543+
"winapi 0.3.8",
35403544
]
35413545

35423546
[[package]]
@@ -3647,6 +3651,7 @@ dependencies = [
36473651
"smallvec 1.0.0",
36483652
"syntax",
36493653
"tempfile",
3654+
"winapi 0.3.8",
36503655
]
36513656

36523657
[[package]]
@@ -3715,6 +3720,7 @@ dependencies = [
37153720
"smallvec 1.0.0",
37163721
"stable_deref_trait",
37173722
"syntax",
3723+
"winapi 0.3.8",
37183724
]
37193725

37203726
[[package]]

‎src/bootstrap/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ lazy_static = "1.3.0"
4949
time = "0.1"
5050
ignore = "0.4.10"
5151

52+
[target.'cfg(windows)'.dependencies.winapi]
53+
version = "0.3"
54+
features = ["fileapi", "ioapiset", "jobapi2", "handleapi", "winioctl"]
55+
5256
[dev-dependencies]
5357
pretty_assertions = "0.5"

‎src/bootstrap/job.rs

+10-78
Original file line numberDiff line numberDiff line change
@@ -35,84 +35,16 @@ use std::io;
3535
use std::mem;
3636
use std::ptr;
3737

38-
type HANDLE = *mut u8;
39-
type BOOL = i32;
40-
type DWORD = u32;
41-
type LPHANDLE = *mut HANDLE;
42-
type LPVOID = *mut u8;
43-
type JOBOBJECTINFOCLASS = i32;
44-
type SIZE_T = usize;
45-
type LARGE_INTEGER = i64;
46-
type UINT = u32;
47-
type ULONG_PTR = usize;
48-
type ULONGLONG = u64;
49-
50-
const FALSE: BOOL = 0;
51-
const DUPLICATE_SAME_ACCESS: DWORD = 0x2;
52-
const PROCESS_DUP_HANDLE: DWORD = 0x40;
53-
const JobObjectExtendedLimitInformation: JOBOBJECTINFOCLASS = 9;
54-
const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: DWORD = 0x2000;
55-
const JOB_OBJECT_LIMIT_PRIORITY_CLASS: DWORD = 0x00000020;
56-
const SEM_FAILCRITICALERRORS: UINT = 0x0001;
57-
const SEM_NOGPFAULTERRORBOX: UINT = 0x0002;
58-
const BELOW_NORMAL_PRIORITY_CLASS: DWORD = 0x00004000;
59-
60-
extern "system" {
61-
fn CreateJobObjectW(lpJobAttributes: *mut u8, lpName: *const u8) -> HANDLE;
62-
fn CloseHandle(hObject: HANDLE) -> BOOL;
63-
fn GetCurrentProcess() -> HANDLE;
64-
fn OpenProcess(dwDesiredAccess: DWORD, bInheritHandle: BOOL, dwProcessId: DWORD) -> HANDLE;
65-
fn DuplicateHandle(
66-
hSourceProcessHandle: HANDLE,
67-
hSourceHandle: HANDLE,
68-
hTargetProcessHandle: HANDLE,
69-
lpTargetHandle: LPHANDLE,
70-
dwDesiredAccess: DWORD,
71-
bInheritHandle: BOOL,
72-
dwOptions: DWORD,
73-
) -> BOOL;
74-
fn AssignProcessToJobObject(hJob: HANDLE, hProcess: HANDLE) -> BOOL;
75-
fn SetInformationJobObject(
76-
hJob: HANDLE,
77-
JobObjectInformationClass: JOBOBJECTINFOCLASS,
78-
lpJobObjectInformation: LPVOID,
79-
cbJobObjectInformationLength: DWORD,
80-
) -> BOOL;
81-
fn SetErrorMode(mode: UINT) -> UINT;
82-
}
83-
84-
#[repr(C)]
85-
struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION {
86-
BasicLimitInformation: JOBOBJECT_BASIC_LIMIT_INFORMATION,
87-
IoInfo: IO_COUNTERS,
88-
ProcessMemoryLimit: SIZE_T,
89-
JobMemoryLimit: SIZE_T,
90-
PeakProcessMemoryUsed: SIZE_T,
91-
PeakJobMemoryUsed: SIZE_T,
92-
}
93-
94-
#[repr(C)]
95-
struct IO_COUNTERS {
96-
ReadOperationCount: ULONGLONG,
97-
WriteOperationCount: ULONGLONG,
98-
OtherOperationCount: ULONGLONG,
99-
ReadTransferCount: ULONGLONG,
100-
WriteTransferCount: ULONGLONG,
101-
OtherTransferCount: ULONGLONG,
102-
}
103-
104-
#[repr(C)]
105-
struct JOBOBJECT_BASIC_LIMIT_INFORMATION {
106-
PerProcessUserTimeLimit: LARGE_INTEGER,
107-
PerJobUserTimeLimit: LARGE_INTEGER,
108-
LimitFlags: DWORD,
109-
MinimumWorkingsetSize: SIZE_T,
110-
MaximumWorkingsetSize: SIZE_T,
111-
ActiveProcessLimit: DWORD,
112-
Affinity: ULONG_PTR,
113-
PriorityClass: DWORD,
114-
SchedulingClass: DWORD,
115-
}
38+
use winapi::shared::minwindef::{DWORD, FALSE, LPVOID};
39+
use winapi::um::errhandlingapi::SetErrorMode;
40+
use winapi::um::handleapi::{CloseHandle, DuplicateHandle};
41+
use winapi::um::jobapi2::{AssignProcessToJobObject, CreateJobObjectW, SetInformationJobObject};
42+
use winapi::um::processthreadsapi::{GetCurrentProcess, OpenProcess};
43+
use winapi::um::winbase::{BELOW_NORMAL_PRIORITY_CLASS, SEM_NOGPFAULTERRORBOX};
44+
use winapi::um::winnt::{
45+
JobObjectExtendedLimitInformation, DUPLICATE_SAME_ACCESS, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
46+
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOB_OBJECT_LIMIT_PRIORITY_CLASS, PROCESS_DUP_HANDLE,
47+
};
11648

11749
pub unsafe fn setup(build: &mut Build) {
11850
// Enable the Windows Error Reporting dialog which msys disables,

‎src/bootstrap/util.rs

+13-49
Original file line numberDiff line numberDiff line change
@@ -123,37 +123,24 @@ pub fn symlink_dir(config: &Config, src: &Path, dest: &Path) -> io::Result<()> {
123123
// what can be found here:
124124
//
125125
// http://www.flexhex.com/docs/articles/hard-links.phtml
126-
//
127-
// Copied from std
128126
#[cfg(windows)]
129-
#[allow(nonstandard_style)]
130127
fn symlink_dir_inner(target: &Path, junction: &Path) -> io::Result<()> {
131128
use std::ffi::OsStr;
132129
use std::os::windows::ffi::OsStrExt;
133130
use std::ptr;
134131

135-
const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
136-
const GENERIC_WRITE: DWORD = 0x40000000;
137-
const OPEN_EXISTING: DWORD = 3;
138-
const FILE_FLAG_OPEN_REPARSE_POINT: DWORD = 0x00200000;
139-
const FILE_FLAG_BACKUP_SEMANTICS: DWORD = 0x02000000;
140-
const FSCTL_SET_REPARSE_POINT: DWORD = 0x900a4;
141-
const IO_REPARSE_TAG_MOUNT_POINT: DWORD = 0xa0000003;
142-
const FILE_SHARE_DELETE: DWORD = 0x4;
143-
const FILE_SHARE_READ: DWORD = 0x1;
144-
const FILE_SHARE_WRITE: DWORD = 0x2;
145-
146-
type BOOL = i32;
147-
type DWORD = u32;
148-
type HANDLE = *mut u8;
149-
type LPCWSTR = *const u16;
150-
type LPDWORD = *mut DWORD;
151-
type LPOVERLAPPED = *mut u8;
152-
type LPSECURITY_ATTRIBUTES = *mut u8;
153-
type LPVOID = *mut u8;
154-
type WCHAR = u16;
155-
type WORD = u16;
156-
132+
use winapi::shared::minwindef::{DWORD, WORD};
133+
use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
134+
use winapi::um::handleapi::CloseHandle;
135+
use winapi::um::ioapiset::DeviceIoControl;
136+
use winapi::um::winbase::{FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT};
137+
use winapi::um::winioctl::FSCTL_SET_REPARSE_POINT;
138+
use winapi::um::winnt::{
139+
FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_WRITE,
140+
IO_REPARSE_TAG_MOUNT_POINT, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, WCHAR,
141+
};
142+
143+
#[allow(non_snake_case)]
157144
#[repr(C)]
158145
struct REPARSE_MOUNTPOINT_DATA_BUFFER {
159146
ReparseTag: DWORD,
@@ -165,29 +152,6 @@ pub fn symlink_dir(config: &Config, src: &Path, dest: &Path) -> io::Result<()> {
165152
ReparseTarget: WCHAR,
166153
}
167154

168-
extern "system" {
169-
fn CreateFileW(
170-
lpFileName: LPCWSTR,
171-
dwDesiredAccess: DWORD,
172-
dwShareMode: DWORD,
173-
lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
174-
dwCreationDisposition: DWORD,
175-
dwFlagsAndAttributes: DWORD,
176-
hTemplateFile: HANDLE,
177-
) -> HANDLE;
178-
fn DeviceIoControl(
179-
hDevice: HANDLE,
180-
dwIoControlCode: DWORD,
181-
lpInBuffer: LPVOID,
182-
nInBufferSize: DWORD,
183-
lpOutBuffer: LPVOID,
184-
nOutBufferSize: DWORD,
185-
lpBytesReturned: LPDWORD,
186-
lpOverlapped: LPOVERLAPPED,
187-
) -> BOOL;
188-
fn CloseHandle(hObject: HANDLE) -> BOOL;
189-
}
190-
191155
fn to_u16s<S: AsRef<OsStr>>(s: S) -> io::Result<Vec<u16>> {
192156
Ok(s.as_ref().encode_wide().chain(Some(0)).collect())
193157
}
@@ -212,7 +176,7 @@ pub fn symlink_dir(config: &Config, src: &Path, dest: &Path) -> io::Result<()> {
212176
ptr::null_mut(),
213177
);
214178

215-
let mut data = [0u8; MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
179+
let mut data = [0u8; MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize];
216180
let db = data.as_mut_ptr() as *mut REPARSE_MOUNTPOINT_DATA_BUFFER;
217181
let buf = &mut (*db).ReparseTarget as *mut u16;
218182
let mut i = 0;

‎src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#![feature(const_transmute)]
3535
#![feature(core_intrinsics)]
3636
#![feature(drain_filter)]
37-
#![cfg_attr(windows, feature(libc))]
3837
#![feature(never_type)]
3938
#![feature(exhaustive_patterns)]
4039
#![feature(overlapping_marker_traits)]

‎src/librustc_data_structures/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ measureme = "0.7.1"
3131
[dependencies.parking_lot]
3232
version = "0.9"
3333
features = ["nightly"]
34+
35+
[target.'cfg(windows)'.dependencies]
36+
winapi = { version = "0.3", features = ["fileapi", "psapi"] }

‎src/librustc_data_structures/flock.rs

+3-31
Original file line numberDiff line numberDiff line change
@@ -87,39 +87,11 @@ cfg_if! {
8787
} else if #[cfg(windows)] {
8888
use std::mem;
8989
use std::os::windows::prelude::*;
90-
use std::os::windows::raw::HANDLE;
9190
use std::fs::{File, OpenOptions};
92-
use std::os::raw::{c_ulong, c_int};
93-
94-
type DWORD = c_ulong;
95-
type BOOL = c_int;
96-
type ULONG_PTR = usize;
97-
98-
type LPOVERLAPPED = *mut OVERLAPPED;
99-
const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
100-
const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
101-
102-
const FILE_SHARE_DELETE: DWORD = 0x4;
103-
const FILE_SHARE_READ: DWORD = 0x1;
104-
const FILE_SHARE_WRITE: DWORD = 0x2;
105-
106-
#[repr(C)]
107-
struct OVERLAPPED {
108-
Internal: ULONG_PTR,
109-
InternalHigh: ULONG_PTR,
110-
Offset: DWORD,
111-
OffsetHigh: DWORD,
112-
hEvent: HANDLE,
113-
}
11491

115-
extern "system" {
116-
fn LockFileEx(hFile: HANDLE,
117-
dwFlags: DWORD,
118-
dwReserved: DWORD,
119-
nNumberOfBytesToLockLow: DWORD,
120-
nNumberOfBytesToLockHigh: DWORD,
121-
lpOverlapped: LPOVERLAPPED) -> BOOL;
122-
}
92+
use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
93+
use winapi::um::fileapi::LockFileEx;
94+
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};
12395

12496
#[derive(Debug)]
12597
pub struct Lock {

‎src/librustc_data_structures/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ extern crate libc;
3333
#[macro_use]
3434
extern crate cfg_if;
3535

36-
#[cfg(windows)]
37-
extern crate libc;
38-
3936
pub use rustc_serialize::hex::ToHex;
4037

4138
#[inline(never)]

‎src/librustc_data_structures/profiling.rs

+13-33
Original file line numberDiff line numberDiff line change
@@ -569,39 +569,19 @@ fn get_resident() -> Option<usize> {
569569

570570
#[cfg(windows)]
571571
fn get_resident() -> Option<usize> {
572-
type BOOL = i32;
573-
type DWORD = u32;
574-
type HANDLE = *mut u8;
575-
use libc::size_t;
576-
#[repr(C)]
577-
#[allow(non_snake_case)]
578-
struct PROCESS_MEMORY_COUNTERS {
579-
cb: DWORD,
580-
PageFaultCount: DWORD,
581-
PeakWorkingSetSize: size_t,
582-
WorkingSetSize: size_t,
583-
QuotaPeakPagedPoolUsage: size_t,
584-
QuotaPagedPoolUsage: size_t,
585-
QuotaPeakNonPagedPoolUsage: size_t,
586-
QuotaNonPagedPoolUsage: size_t,
587-
PagefileUsage: size_t,
588-
PeakPagefileUsage: size_t,
589-
}
590-
#[allow(non_camel_case_types)]
591-
type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
592-
#[link(name = "psapi")]
593-
extern "system" {
594-
fn GetCurrentProcess() -> HANDLE;
595-
fn GetProcessMemoryInfo(
596-
Process: HANDLE,
597-
ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
598-
cb: DWORD,
599-
) -> BOOL;
600-
}
601-
let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { std::mem::zeroed() };
602-
pmc.cb = std::mem::size_of_val(&pmc) as DWORD;
603-
match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
572+
use std::mem::{self, MaybeUninit};
573+
use winapi::shared::minwindef::DWORD;
574+
use winapi::um::processthreadsapi::GetCurrentProcess;
575+
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
576+
577+
let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
578+
match unsafe {
579+
GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
580+
} {
604581
0 => None,
605-
_ => Some(pmc.WorkingSetSize as usize),
582+
_ => {
583+
let pmc = unsafe { pmc.assume_init() };
584+
Some(pmc.WorkingSetSize as usize)
585+
}
606586
}
607587
}

‎src/librustc_driver/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ rustc_serialize = { path = "../libserialize", package = "serialize" }
3232
syntax = { path = "../libsyntax" }
3333
rustc_span = { path = "../librustc_span" }
3434

35+
[target.'cfg(windows)'.dependencies]
36+
winapi = { version = "0.3", features = ["consoleapi", "debugapi", "processenv"] }
37+
3538
[features]
3639
llvm = ['rustc_interface/llvm']

‎src/librustc_driver/lib.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -515,15 +515,10 @@ fn stdout_isatty() -> bool {
515515

516516
#[cfg(windows)]
517517
fn stdout_isatty() -> bool {
518-
type DWORD = u32;
519-
type BOOL = i32;
520-
type HANDLE = *mut u8;
521-
type LPDWORD = *mut u32;
522-
const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
523-
extern "system" {
524-
fn GetStdHandle(which: DWORD) -> HANDLE;
525-
fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
526-
}
518+
use winapi::um::consoleapi::GetConsoleMode;
519+
use winapi::um::processenv::GetStdHandle;
520+
use winapi::um::winbase::STD_OUTPUT_HANDLE;
521+
527522
unsafe {
528523
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
529524
let mut out = 0;
@@ -1215,11 +1210,8 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12151210
#[cfg(windows)]
12161211
unsafe {
12171212
if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
1218-
extern "system" {
1219-
fn DebugBreak();
1220-
}
12211213
// Trigger a debugger if we crashed during bootstrap
1222-
DebugBreak();
1214+
winapi::um::debugapi::DebugBreak();
12231215
}
12241216
}
12251217
}

‎src/librustc_errors/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ atty = "0.2"
1919
termcolor = "1.0"
2020
annotate-snippets = "0.6.1"
2121
term_size = "0.3.1"
22+
23+
[target.'cfg(windows)'.dependencies]
24+
winapi = { version = "0.3", features = ["handleapi", "synchapi", "winbase"] }

‎src/librustc_errors/lock.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,14 @@
1212
use std::any::Any;
1313

1414
#[cfg(windows)]
15-
#[allow(nonstandard_style)]
1615
pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
1716
use std::ffi::CString;
1817
use std::io;
1918

20-
type LPSECURITY_ATTRIBUTES = *mut u8;
21-
type BOOL = i32;
22-
type LPCSTR = *const u8;
23-
type HANDLE = *mut u8;
24-
type DWORD = u32;
25-
26-
const INFINITE: DWORD = !0;
27-
const WAIT_OBJECT_0: DWORD = 0;
28-
const WAIT_ABANDONED: DWORD = 0x00000080;
29-
30-
extern "system" {
31-
fn CreateMutexA(
32-
lpMutexAttributes: LPSECURITY_ATTRIBUTES,
33-
bInitialOwner: BOOL,
34-
lpName: LPCSTR,
35-
) -> HANDLE;
36-
fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
37-
fn ReleaseMutex(hMutex: HANDLE) -> BOOL;
38-
fn CloseHandle(hObject: HANDLE) -> BOOL;
39-
}
19+
use winapi::shared::ntdef::HANDLE;
20+
use winapi::um::handleapi::CloseHandle;
21+
use winapi::um::synchapi::{CreateMutexA, ReleaseMutex, WaitForSingleObject};
22+
use winapi::um::winbase::{INFINITE, WAIT_ABANDONED, WAIT_OBJECT_0};
4023

4124
struct Handle(HANDLE);
4225

@@ -65,7 +48,7 @@ pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
6548
//
6649
// This will silently create one if it doesn't already exist, or it'll
6750
// open up a handle to one if it already exists.
68-
let mutex = CreateMutexA(std::ptr::null_mut(), 0, cname.as_ptr() as *const u8);
51+
let mutex = CreateMutexA(std::ptr::null_mut(), 0, cname.as_ptr());
6952
if mutex.is_null() {
7053
panic!(
7154
"failed to create global mutex named `{}`: {}",

‎src/librustc_interface/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ rustc_resolve = { path = "../librustc_resolve" }
4242
tempfile = "3.0.5"
4343
once_cell = "1"
4444

45+
[target.'cfg(windows)'.dependencies]
46+
winapi = { version = "0.3", features = ["libloaderapi"] }
47+
4548
[dev-dependencies]
4649
rustc_target = { path = "../librustc_target" }
4750

‎src/librustc_interface/util.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,17 @@ fn sysroot_candidates() -> Vec<PathBuf> {
340340
fn current_dll_path() -> Option<PathBuf> {
341341
use std::ffi::OsString;
342342
use std::os::windows::prelude::*;
343+
use std::ptr;
343344

344-
extern "system" {
345-
fn GetModuleHandleExW(dwFlags: u32, lpModuleName: usize, phModule: *mut usize) -> i32;
346-
fn GetModuleFileNameW(hModule: usize, lpFilename: *mut u16, nSize: u32) -> u32;
347-
}
348-
349-
const GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS: u32 = 0x00000004;
345+
use winapi::um::libloaderapi::{
346+
GetModuleFileNameW, GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
347+
};
350348

351349
unsafe {
352-
let mut module = 0;
350+
let mut module = ptr::null_mut();
353351
let r = GetModuleHandleExW(
354352
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
355-
current_dll_path as usize,
353+
current_dll_path as usize as *mut _,
356354
&mut module,
357355
);
358356
if r == 0 {

‎src/librustc_metadata/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ rustc_expand = { path = "../librustc_expand" }
2727
rustc_parse = { path = "../librustc_parse" }
2828
rustc_span = { path = "../librustc_span" }
2929
rustc_error_codes = { path = "../librustc_error_codes" }
30+
31+
[target.'cfg(windows)'.dependencies]
32+
winapi = { version = "0.3", features = ["errhandlingapi", "libloaderapi"] }

‎src/librustc_metadata/dynamic_lib.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ mod dl {
111111
) -> Result<*mut u8, String> {
112112
check_for_errors_in(|| libc::dlsym(handle as *mut libc::c_void, symbol) as *mut u8)
113113
}
114+
114115
pub(super) unsafe fn close(handle: *mut u8) {
115116
libc::dlclose(handle as *mut libc::c_void);
116-
()
117117
}
118118
}
119119

@@ -124,27 +124,15 @@ mod dl {
124124
use std::os::windows::prelude::*;
125125
use std::ptr;
126126

127-
use libc::{c_char, c_uint, c_void};
128-
129-
type DWORD = u32;
130-
type HMODULE = *mut u8;
131-
type BOOL = i32;
132-
type LPCWSTR = *const u16;
133-
type LPCSTR = *const i8;
134-
135-
extern "system" {
136-
fn SetThreadErrorMode(dwNewMode: DWORD, lpOldMode: *mut DWORD) -> c_uint;
137-
fn LoadLibraryW(name: LPCWSTR) -> HMODULE;
138-
fn GetModuleHandleExW(dwFlags: DWORD, name: LPCWSTR, handle: *mut HMODULE) -> BOOL;
139-
fn GetProcAddress(handle: HMODULE, name: LPCSTR) -> *mut c_void;
140-
fn FreeLibrary(handle: HMODULE) -> BOOL;
141-
}
127+
use winapi::shared::minwindef::HMODULE;
128+
use winapi::um::errhandlingapi::SetThreadErrorMode;
129+
use winapi::um::libloaderapi::{FreeLibrary, GetModuleHandleExW, GetProcAddress, LoadLibraryW};
130+
use winapi::um::winbase::SEM_FAILCRITICALERRORS;
142131

143132
pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
144133
// disable "dll load failed" error dialog.
145134
let prev_error_mode = unsafe {
146-
// SEM_FAILCRITICALERRORS 0x01
147-
let new_error_mode = 1;
135+
let new_error_mode = SEM_FAILCRITICALERRORS;
148136
let mut prev_error_mode = 0;
149137
let result = SetThreadErrorMode(new_error_mode, &mut prev_error_mode);
150138
if result == 0 {
@@ -156,12 +144,12 @@ mod dl {
156144
let result = match filename {
157145
Some(filename) => {
158146
let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
159-
let result = unsafe { LoadLibraryW(filename_str.as_ptr()) };
147+
let result = unsafe { LoadLibraryW(filename_str.as_ptr()) } as *mut u8;
160148
ptr_result(result)
161149
}
162150
None => {
163151
let mut handle = ptr::null_mut();
164-
let succeeded = unsafe { GetModuleHandleExW(0 as DWORD, ptr::null(), &mut handle) };
152+
let succeeded = unsafe { GetModuleHandleExW(0, ptr::null(), &mut handle) };
165153
if succeeded == 0 {
166154
Err(io::Error::last_os_error().to_string())
167155
} else {
@@ -177,7 +165,10 @@ mod dl {
177165
result
178166
}
179167

180-
pub(super) unsafe fn symbol(handle: *mut u8, symbol: *const c_char) -> Result<*mut u8, String> {
168+
pub(super) unsafe fn symbol(
169+
handle: *mut u8,
170+
symbol: *const libc::c_char,
171+
) -> Result<*mut u8, String> {
181172
let ptr = GetProcAddress(handle as HMODULE, symbol) as *mut u8;
182173
ptr_result(ptr)
183174
}

‎src/tools/compiletest/src/runtest.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ mod tests;
4242
#[cfg(windows)]
4343
fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
4444
use std::sync::Mutex;
45-
const SEM_NOGPFAULTERRORBOX: u32 = 0x0002;
46-
extern "system" {
47-
fn SetErrorMode(mode: u32) -> u32;
48-
}
45+
use winapi::um::errhandlingapi::SetErrorMode;
46+
use winapi::um::winbase::SEM_NOGPFAULTERRORBOX;
4947

5048
lazy_static! {
5149
static ref LOCK: Mutex<()> = { Mutex::new(()) };

0 commit comments

Comments
 (0)
Please sign in to comment.