From abc3777c06abced90b415b920082a4814d9051d7 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 4 Apr 2016 14:18:44 +0000 Subject: Fix stack overflow detection on FreeBSD src/libstd/sys/unix/thread.rs Implement several stack-related functions on FreeBSD src/libstd/sys/unix/stack_overflow.rs Fix a comment --- src/libstd/sys/unix/stack_overflow.rs | 2 +- src/libstd/sys/unix/thread.rs | 45 ++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/unix/stack_overflow.rs b/src/libstd/sys/unix/stack_overflow.rs index 1553aba35a2..22d47ba0f62 100644 --- a/src/libstd/sys/unix/stack_overflow.rs +++ b/src/libstd/sys/unix/stack_overflow.rs @@ -64,7 +64,7 @@ mod imp { unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize { #[repr(C)] struct siginfo_t { - a: [libc::c_int; 3], // si_signo, si_code, si_errno, + a: [libc::c_int; 3], // si_signo, si_errno, si_code si_addr: *mut libc::c_void, } diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 6d966a0f694..0f238d5c11c 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -164,6 +164,7 @@ impl Drop for Thread { } #[cfg(all(not(all(target_os = "linux", not(target_env = "musl"))), + not(target_os = "freebsd"), not(target_os = "macos"), not(target_os = "bitrig"), not(all(target_os = "netbsd", not(target_vendor = "rumprun"))), @@ -177,6 +178,7 @@ pub mod guard { #[cfg(any(all(target_os = "linux", not(target_env = "musl")), + target_os = "freebsd", target_os = "macos", target_os = "bitrig", all(target_os = "netbsd", not(target_vendor = "rumprun")), @@ -199,6 +201,22 @@ pub mod guard { current().map(|s| s as *mut libc::c_void) } + #[cfg(target_os = "freebsd")] + unsafe fn get_stack_start() -> Option<*mut libc::c_void> { + let mut ret = None; + let mut attr: libc::pthread_attr_t = ::mem::zeroed(); + assert_eq!(libc::pthread_attr_init(&mut attr), 0); + if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 { + let mut stackaddr = ::ptr::null_mut(); + let mut stacksize = 0; + assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, + &mut stacksize), 0); + ret = Some(stackaddr); + } + assert_eq!(libc::pthread_attr_destroy(&mut attr), 0); + ret + } + #[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))] unsafe fn get_stack_start() -> Option<*mut libc::c_void> { let mut ret = None; @@ -248,7 +266,11 @@ pub mod guard { panic!("failed to allocate a guard page"); } - let offset = if cfg!(target_os = "linux") {2} else {1}; + let offset = if cfg!(any(target_os = "linux", target_os = "freebsd")) { + 2 + } else { + 1 + }; Some(stackaddr as usize + offset * psize) } @@ -282,6 +304,27 @@ pub mod guard { }) } + #[cfg(target_os = "freebsd")] + pub unsafe fn current() -> Option { + let mut ret = None; + let mut attr: libc::pthread_attr_t = ::mem::zeroed(); + assert_eq!(libc::pthread_attr_init(&mut attr), 0); + if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 { + let mut guardsize = 0; + assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0); + if guardsize == 0 { + panic!("there is no guard page"); + } + let mut stackaddr = ::ptr::null_mut(); + let mut size = 0; + assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, + &mut size), 0); + ret = Some(stackaddr as usize - guardsize as usize); + } + assert_eq!(libc::pthread_attr_destroy(&mut attr), 0); + ret + } + #[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))] pub unsafe fn current() -> Option { let mut ret = None; -- cgit 1.4.1-3-g733a5 From 112463a3b1b1fc30c8f407e50e9ef692034ccb37 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Tue, 5 Apr 2016 03:25:32 +0000 Subject: Reduce code duplication in thread.rs --- src/libstd/sys/unix/thread.rs | 60 +++++++++++++------------------------------ 1 file changed, 18 insertions(+), 42 deletions(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 0f238d5c11c..4b0201cfd01 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -201,28 +201,17 @@ pub mod guard { current().map(|s| s as *mut libc::c_void) } - #[cfg(target_os = "freebsd")] + #[cfg(any(target_os = "android", target_os = "freebsd", + target_os = "linux", target_os = "netbsd"))] unsafe fn get_stack_start() -> Option<*mut libc::c_void> { let mut ret = None; let mut attr: libc::pthread_attr_t = ::mem::zeroed(); assert_eq!(libc::pthread_attr_init(&mut attr), 0); - if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 { - let mut stackaddr = ::ptr::null_mut(); - let mut stacksize = 0; - assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, - &mut stacksize), 0); - ret = Some(stackaddr); - } - assert_eq!(libc::pthread_attr_destroy(&mut attr), 0); - ret - } - - #[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))] - unsafe fn get_stack_start() -> Option<*mut libc::c_void> { - let mut ret = None; - let mut attr: libc::pthread_attr_t = ::mem::zeroed(); - assert_eq!(libc::pthread_attr_init(&mut attr), 0); - if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 { + #[cfg(target_os = "freebsd")] + let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr); + #[cfg(not(target_os = "freebsd"))] + let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr); + if e == 0 { let mut stackaddr = ::ptr::null_mut(); let mut stacksize = 0; assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, @@ -304,33 +293,18 @@ pub mod guard { }) } - #[cfg(target_os = "freebsd")] - pub unsafe fn current() -> Option { - let mut ret = None; - let mut attr: libc::pthread_attr_t = ::mem::zeroed(); - assert_eq!(libc::pthread_attr_init(&mut attr), 0); - if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 { - let mut guardsize = 0; - assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0); - if guardsize == 0 { - panic!("there is no guard page"); - } - let mut stackaddr = ::ptr::null_mut(); - let mut size = 0; - assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, - &mut size), 0); - ret = Some(stackaddr as usize - guardsize as usize); - } - assert_eq!(libc::pthread_attr_destroy(&mut attr), 0); - ret - } - - #[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))] + #[cfg(any(target_os = "android", target_os = "freebsd", + target_os = "linux", target_os = "netbsd"))] pub unsafe fn current() -> Option { let mut ret = None; let mut attr: libc::pthread_attr_t = ::mem::zeroed(); assert_eq!(libc::pthread_attr_init(&mut attr), 0); - if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 { + #[cfg(target_os = "freebsd")] + let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr); + #[cfg(not(target_os = "freebsd"))] + let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr); + if e == 0 { + //if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 { let mut guardsize = 0; assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0); if guardsize == 0 { @@ -341,7 +315,9 @@ pub mod guard { assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut size), 0); - ret = if cfg!(target_os = "netbsd") { + ret = if cfg!(target_os = "freebsd") { + Some(stackaddr as usize - guardsize as usize) + } else if cfg!(target_os = "netbsd") { Some(stackaddr as usize) } else { Some(stackaddr as usize + guardsize as usize) -- cgit 1.4.1-3-g733a5 From 78ea972b9ff4a2c3eaa0328f6fa8e7038e8ca9d1 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 6 Apr 2016 05:40:59 +0000 Subject: Remove accidental comment --- src/libstd/sys/unix/thread.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 4b0201cfd01..cb34d1a5fbc 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -304,7 +304,6 @@ pub mod guard { #[cfg(not(target_os = "freebsd"))] let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr); if e == 0 { - //if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 { let mut guardsize = 0; assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0); if guardsize == 0 { -- cgit 1.4.1-3-g733a5