Skip to content

Commit abc3777

Browse files
committed
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
1 parent bbe8e35 commit abc3777

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/libstd/sys/unix/stack_overflow.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 44 additions & 1 deletion
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,6 +201,22 @@ pub mod guard {
199201
current().map(|s| s as *mut libc::c_void)
200202
}
201203

204+
#[cfg(target_os = "freebsd")]
205+
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
206+
let mut ret = None;
207+
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
208+
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
209+
if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 {
210+
let mut stackaddr = ::ptr::null_mut();
211+
let mut stacksize = 0;
212+
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
213+
&mut stacksize), 0);
214+
ret = Some(stackaddr);
215+
}
216+
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
217+
ret
218+
}
219+
202220
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
203221
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
204222
let mut ret = None;
@@ -248,7 +266,11 @@ pub mod guard {
248266
panic!("failed to allocate a guard page");
249267
}
250268

251-
let offset = if cfg!(target_os = "linux") {2} else {1};
269+
let offset = if cfg!(any(target_os = "linux", target_os = "freebsd")) {
270+
2
271+
} else {
272+
1
273+
};
252274

253275
Some(stackaddr as usize + offset * psize)
254276
}
@@ -282,6 +304,27 @@ pub mod guard {
282304
})
283305
}
284306

307+
#[cfg(target_os = "freebsd")]
308+
pub unsafe fn current() -> Option<usize> {
309+
let mut ret = None;
310+
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
311+
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
312+
if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 {
313+
let mut guardsize = 0;
314+
assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0);
315+
if guardsize == 0 {
316+
panic!("there is no guard page");
317+
}
318+
let mut stackaddr = ::ptr::null_mut();
319+
let mut size = 0;
320+
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
321+
&mut size), 0);
322+
ret = Some(stackaddr as usize - guardsize as usize);
323+
}
324+
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
325+
ret
326+
}
327+
285328
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
286329
pub unsafe fn current() -> Option<usize> {
287330
let mut ret = None;

0 commit comments

Comments
 (0)