Skip to content

Commit 7d4d3cb

Browse files
committed
Auto merge of #32726 - asomers:master, r=alexcrichton
Fix stack overflow detection on FreeBSD
2 parents 4b71f8d + 78ea972 commit 7d4d3cb

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/libstd/sys/unix/stack_overflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mod imp {
6464
unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
6565
#[repr(C)]
6666
struct siginfo_t {
67-
a: [libc::c_int; 3], // si_signo, si_code, si_errno,
67+
a: [libc::c_int; 3], // si_signo, si_errno, si_code
6868
si_addr: *mut libc::c_void,
6969
}
7070

src/libstd/sys/unix/thread.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl Drop for Thread {
164164
}
165165

166166
#[cfg(all(not(all(target_os = "linux", not(target_env = "musl"))),
167+
not(target_os = "freebsd"),
167168
not(target_os = "macos"),
168169
not(target_os = "bitrig"),
169170
not(all(target_os = "netbsd", not(target_vendor = "rumprun"))),
@@ -177,6 +178,7 @@ pub mod guard {
177178

178179

179180
#[cfg(any(all(target_os = "linux", not(target_env = "musl")),
181+
target_os = "freebsd",
180182
target_os = "macos",
181183
target_os = "bitrig",
182184
all(target_os = "netbsd", not(target_vendor = "rumprun")),
@@ -199,12 +201,17 @@ pub mod guard {
199201
current().map(|s| s as *mut libc::c_void)
200202
}
201203

202-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
204+
#[cfg(any(target_os = "android", target_os = "freebsd",
205+
target_os = "linux", target_os = "netbsd"))]
203206
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
204207
let mut ret = None;
205208
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
206209
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
207-
if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 {
210+
#[cfg(target_os = "freebsd")]
211+
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
212+
#[cfg(not(target_os = "freebsd"))]
213+
let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr);
214+
if e == 0 {
208215
let mut stackaddr = ::ptr::null_mut();
209216
let mut stacksize = 0;
210217
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
@@ -248,7 +255,11 @@ pub mod guard {
248255
panic!("failed to allocate a guard page");
249256
}
250257

251-
let offset = if cfg!(target_os = "linux") {2} else {1};
258+
let offset = if cfg!(any(target_os = "linux", target_os = "freebsd")) {
259+
2
260+
} else {
261+
1
262+
};
252263

253264
Some(stackaddr as usize + offset * psize)
254265
}
@@ -282,12 +293,17 @@ pub mod guard {
282293
})
283294
}
284295

285-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
296+
#[cfg(any(target_os = "android", target_os = "freebsd",
297+
target_os = "linux", target_os = "netbsd"))]
286298
pub unsafe fn current() -> Option<usize> {
287299
let mut ret = None;
288300
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
289301
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
290-
if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 {
302+
#[cfg(target_os = "freebsd")]
303+
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
304+
#[cfg(not(target_os = "freebsd"))]
305+
let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr);
306+
if e == 0 {
291307
let mut guardsize = 0;
292308
assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0);
293309
if guardsize == 0 {
@@ -298,7 +314,9 @@ pub mod guard {
298314
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
299315
&mut size), 0);
300316

301-
ret = if cfg!(target_os = "netbsd") {
317+
ret = if cfg!(target_os = "freebsd") {
318+
Some(stackaddr as usize - guardsize as usize)
319+
} else if cfg!(target_os = "netbsd") {
302320
Some(stackaddr as usize)
303321
} else {
304322
Some(stackaddr as usize + guardsize as usize)

0 commit comments

Comments
 (0)