Skip to content

Commit 9e182c3

Browse files
committed
improve error handling
see rust-lang#115 See sqlpage/SQLPage#814
1 parent a4027ca commit 9e182c3

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

src/lib.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ psm_stack_manipulation! {
163163
-1, // Some implementations assert fd = -1 if MAP_ANON is specified
164164
0
165165
);
166-
if new_stack == libc::MAP_FAILED {
167-
let error = std::io::Error::last_os_error();
168-
panic!("allocating stack failed with: {}", error)
169-
}
166+
assert_ne!(new_stack, libc::MAP_FAILED, "mmap failed to allocate stack: {}", std::io::Error::last_os_error());
170167
let guard = StackRestoreGuard {
171168
new_stack,
172169
stack_bytes,
@@ -191,11 +188,7 @@ psm_stack_manipulation! {
191188
} else {
192189
-1
193190
};
194-
if result == -1 {
195-
let error = std::io::Error::last_os_error();
196-
drop(guard);
197-
panic!("setting stack permissions failed with: {}", error)
198-
}
191+
assert_ne!(result, -1, "mprotect/mmap failed: {}", std::io::Error::last_os_error());
199192
guard
200193
}
201194
}
@@ -222,7 +215,7 @@ psm_stack_manipulation! {
222215
fn _grow(stack_size: usize, callback: &mut dyn FnMut()) {
223216
// Calculate a number of pages we want to allocate for the new stack.
224217
// For maximum portability we want to produce a stack that is aligned to a page and has
225-
// a size thats a multiple of page size. Furthermore we want to allocate two extras pages
218+
// a size that's a multiple of page size. Furthermore we want to allocate two extras pages
226219
// for the stack guard. To achieve that we do our calculations in number of pages and
227220
// convert to bytes last.
228221
let page_size = page_size();
@@ -412,30 +405,51 @@ cfg_if! {
412405
Some(mi.assume_init().AllocationBase as usize + get_thread_stack_guarantee() + 0x1000)
413406
}
414407
} else if #[cfg(any(target_os = "linux", target_os="solaris", target_os = "netbsd"))] {
408+
unsafe fn destroy_pthread_attr(attr: *mut libc::pthread_attr_t) {
409+
let ret = libc::pthread_attr_destroy(attr);
410+
assert!(ret == 0, "pthread_attr_destroy failed with error code: {}", ret);
411+
}
412+
415413
unsafe fn guess_os_stack_limit() -> Option<usize> {
416414
let mut attr = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit();
417-
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
418-
assert_eq!(libc::pthread_getattr_np(libc::pthread_self(),
419-
attr.as_mut_ptr()), 0);
415+
if libc::pthread_attr_init(attr.as_mut_ptr()) != 0 {
416+
return None;
417+
}
418+
if libc::pthread_getattr_np(libc::pthread_self(), attr.as_mut_ptr()) != 0 {
419+
destroy_pthread_attr(attr.as_mut_ptr());
420+
return None;
421+
}
420422
let mut stackaddr = std::ptr::null_mut();
421423
let mut stacksize = 0;
422-
assert_eq!(libc::pthread_attr_getstack(
423-
attr.as_ptr(), &mut stackaddr, &mut stacksize
424-
), 0);
425-
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
424+
if libc::pthread_attr_getstack(attr.as_ptr(), &mut stackaddr, &mut stacksize) != 0 {
425+
destroy_pthread_attr(attr.as_mut_ptr());
426+
return None;
427+
}
428+
destroy_pthread_attr(attr.as_mut_ptr());
426429
Some(stackaddr as usize)
427430
}
428431
} else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "illumos"))] {
432+
unsafe fn destroy_pthread_attr(attr: *mut libc::pthread_attr_t) {
433+
let ret = libc::pthread_attr_destroy(attr);
434+
assert!(ret == 0, "pthread_attr_destroy failed with error code: {}", ret);
435+
}
436+
429437
unsafe fn guess_os_stack_limit() -> Option<usize> {
430438
let mut attr = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit();
431-
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
432-
assert_eq!(libc::pthread_attr_get_np(libc::pthread_self(), attr.as_mut_ptr()), 0);
439+
if libc::pthread_attr_init(attr.as_mut_ptr()) != 0 {
440+
return None;
441+
}
442+
if libc::pthread_attr_get_np(libc::pthread_self(), attr.as_mut_ptr()) != 0 {
443+
destroy_pthread_attr(attr.as_mut_ptr());
444+
return None;
445+
}
433446
let mut stackaddr = std::ptr::null_mut();
434447
let mut stacksize = 0;
435-
assert_eq!(libc::pthread_attr_getstack(
436-
attr.as_ptr(), &mut stackaddr, &mut stacksize
437-
), 0);
438-
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
448+
if libc::pthread_attr_getstack(attr.as_ptr(), &mut stackaddr, &mut stacksize) != 0 {
449+
destroy_pthread_attr(attr.as_mut_ptr());
450+
return None;
451+
}
452+
destroy_pthread_attr(attr.as_mut_ptr());
439453
Some(stackaddr as usize)
440454
}
441455
} else if #[cfg(target_os = "openbsd")] {

0 commit comments

Comments
 (0)