Skip to content

Commit

Permalink
Merge pull request rust-lang#1253 from afonso360/msvc-nosysroot
Browse files Browse the repository at this point in the history
Fix `no_sysroot` testsuite for MSVC environments
  • Loading branch information
bjorn3 authored Aug 1, 2022
2 parents 231f7c6 + c9bb519 commit 0c5b61f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build_system/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ impl TestRunner {
out_dir.push("out");

let is_native = host_triple == target_triple;
let jit_supported = target_triple.contains("x86_64") && is_native;
let jit_supported = target_triple.contains("x86_64") && is_native && !host_triple.contains("windows");

let mut rust_flags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string());
let mut run_wrapper = Vec::new();
Expand Down
12 changes: 10 additions & 2 deletions example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
}

#[lang = "box_free"]
unsafe fn box_free<T: ?Sized>(ptr: Unique<T>, alloc: ()) {
unsafe fn box_free<T: ?Sized>(ptr: Unique<T>, _alloc: ()) {
libc::free(ptr.pointer.0 as *mut u8);
}

Expand Down Expand Up @@ -575,11 +575,19 @@ pub mod intrinsics {
}

pub mod libc {
// With the new Universal CRT, msvc has switched to all the printf functions being inline wrapper
// functions. legacy_stdio_definitions.lib which provides the printf wrapper functions as normal
// symbols to link against.
#[cfg_attr(unix, link(name = "c"))]
#[cfg_attr(target_env="msvc", link(name="legacy_stdio_definitions"))]
extern "C" {
pub fn printf(format: *const i8, ...) -> i32;
}

#[cfg_attr(unix, link(name = "c"))]
#[cfg_attr(target_env = "msvc", link(name = "msvcrt"))]
extern "C" {
pub fn puts(s: *const i8) -> i32;
pub fn printf(format: *const i8, ...) -> i32;
pub fn malloc(size: usize) -> *mut u8;
pub fn free(ptr: *mut u8);
pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
Expand Down
103 changes: 89 additions & 14 deletions example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub struct bool_11 {
field10: bool,
}

extern "C" fn bool_struct_in_11(arg0: bool_11) {}
extern "C" fn bool_struct_in_11(_arg0: bool_11) {}

#[allow(unreachable_code)] // FIXME false positive
fn main() {
Expand Down Expand Up @@ -375,6 +375,7 @@ struct pthread_attr_t {
}

#[link(name = "pthread")]
#[cfg(unix)]
extern "C" {
fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;

Expand All @@ -391,6 +392,91 @@ extern "C" {
) -> c_int;
}

type DWORD = u32;
type LPDWORD = *mut u32;

type LPVOID = *mut c_void;
type HANDLE = *mut c_void;

#[link(name = "msvcrt")]
#[cfg(windows)]
extern "C" {
fn WaitForSingleObject(
hHandle: LPVOID,
dwMilliseconds: DWORD
) -> DWORD;

fn CreateThread(
lpThreadAttributes: LPVOID, // Technically LPSECURITY_ATTRIBUTES, but we don't use it anyway
dwStackSize: usize,
lpStartAddress: extern "C" fn(_: *mut c_void) -> *mut c_void,
lpParameter: LPVOID,
dwCreationFlags: DWORD,
lpThreadId: LPDWORD
) -> HANDLE;
}

struct Thread {
#[cfg(windows)]
handle: HANDLE,
#[cfg(unix)]
handle: pthread_t,
}

impl Thread {
unsafe fn create(f: extern "C" fn(_: *mut c_void) -> *mut c_void) -> Self {
#[cfg(unix)]
{
let mut attr: pthread_attr_t = zeroed();
let mut thread: pthread_t = 0;

if pthread_attr_init(&mut attr) != 0 {
assert!(false);
}

if pthread_create(&mut thread, &attr, f, 0 as *mut c_void) != 0 {
assert!(false);
}

Thread {
handle: thread,
}
}

#[cfg(windows)]
{
let handle = CreateThread(0 as *mut c_void, 0, f, 0 as *mut c_void, 0, 0 as *mut u32);

if (handle as u64) == 0 {
assert!(false);
}

Thread {
handle,
}
}
}


unsafe fn join(self) {
#[cfg(unix)]
{
let mut res = 0 as *mut c_void;
pthread_join(self.handle, &mut res);
}

#[cfg(windows)]
{
// The INFINITE macro is used to signal operations that do not timeout.
let infinite = 0xffffffff;
assert!(WaitForSingleObject(self.handle, infinite) == 0);
}
}
}




#[thread_local]
#[cfg(not(jit))]
static mut TLS: u8 = 42;
Expand All @@ -404,21 +490,10 @@ extern "C" fn mutate_tls(_: *mut c_void) -> *mut c_void {
#[cfg(not(jit))]
fn test_tls() {
unsafe {
let mut attr: pthread_attr_t = zeroed();
let mut thread: pthread_t = 0;

assert_eq!(TLS, 42);

if pthread_attr_init(&mut attr) != 0 {
assert!(false);
}

if pthread_create(&mut thread, &attr, mutate_tls, 0 as *mut c_void) != 0 {
assert!(false);
}

let mut res = 0 as *mut c_void;
pthread_join(thread, &mut res);
let thread = Thread::create(mutate_tls);
thread.join();

// TLS of main thread must not have been changed by the other thread.
assert_eq!(TLS, 42);
Expand Down

0 comments on commit 0c5b61f

Please sign in to comment.