Skip to content

Commit 7c2304d

Browse files
Map the stack guard page with max protection on NetBSD
On NetBSD the initial mmap() protection of a mapping can not be made less restrictive with mprotect(). So when mapping a stack guard page, use the maximum protection we ever want to use, then mprotect() it to the permission we want it to have initially.
1 parent f900bcf commit 7c2304d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/libstd/sys/unix/thread.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,23 @@ pub mod guard {
326326
// Reallocate the last page of the stack.
327327
// This ensures SIGBUS will be raised on
328328
// stack overflow.
329-
let result = mmap(stackaddr, PAGE_SIZE, PROT_NONE,
330-
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
329+
if cfg!(target_os = "netbsd") {
330+
let result = mmap(stackaddr, PAGE_SIZE, PROT_READ | PROT_WRITE,
331+
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
332+
if result != stackaddr || result == MAP_FAILED {
333+
panic!("failed to allocate a guard page");
334+
}
335+
let result = mprotect(stackaddr, PAGE_SIZE, 0);
331336

332-
if result != stackaddr || result == MAP_FAILED {
333-
panic!("failed to allocate a guard page");
337+
if result != 0 {
338+
panic!("unable to protect the guard page");
339+
}
340+
} else {
341+
let result = mmap(stackaddr, PAGE_SIZE, PROT_NONE,
342+
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
343+
if result != stackaddr || result == MAP_FAILED {
344+
panic!("failed to allocate a guard page");
345+
}
334346
}
335347

336348
let guardaddr = stackaddr as usize;

0 commit comments

Comments
 (0)