Skip to content

Commit 81b7942

Browse files
Gankraworkingjubilee
authored andcommitted
WIP PROOF-OF-CONCEPT fixup linux libs
1 parent 312187b commit 81b7942

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

library/std/src/backtrace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ impl RawFrame {
493493
match self {
494494
RawFrame::Actual(frame) => frame.ip(),
495495
#[cfg(test)]
496-
RawFrame::Fake => ptr::invalid_mut(1),
496+
RawFrame::Fake => crate::ptr::invalid_mut(1),
497497
}
498498
}
499499
}

library/std/src/os/unix/net/addr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ mod libc {
1717

1818
fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
1919
// Work with an actual instance of the type since using a null pointer is UB
20-
let base = addr as *const _ as usize;
21-
let path = &addr.sun_path as *const _ as usize;
20+
let base = (addr as *const libc::sockaddr_un).addr();
21+
let path = (&addr.sun_path as *const i8).addr();
2222
path - base
2323
}
2424

library/std/src/sys/unix/memchr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
99
haystack.len(),
1010
)
1111
};
12-
if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) }
12+
if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) }
1313
}
1414

1515
pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
@@ -26,7 +26,7 @@ pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
2626
haystack.len(),
2727
)
2828
};
29-
if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) }
29+
if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) }
3030
}
3131

3232
#[cfg(not(target_os = "linux"))]

library/std/src/sys/unix/thread.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -505,24 +505,24 @@ pub mod guard {
505505
#[cfg(target_os = "macos")]
506506
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
507507
let th = libc::pthread_self();
508-
let stackaddr =
509-
libc::pthread_get_stackaddr_np(th) as usize - libc::pthread_get_stacksize_np(th);
510-
Some(stackaddr as *mut libc::c_void)
508+
let stackptr = libc::pthread_get_stackaddr_np(th);
509+
Some(stackptr.with_addr(stackptr.with_addr() - libc::pthread_get_stacksize_np(th)))
511510
}
512511

513512
#[cfg(target_os = "openbsd")]
514513
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
515514
let mut current_stack: libc::stack_t = crate::mem::zeroed();
516515
assert_eq!(libc::pthread_stackseg_np(libc::pthread_self(), &mut current_stack), 0);
517516

517+
let stack_ptr = current_stack.ss_sp;
518518
let stackaddr = if libc::pthread_main_np() == 1 {
519519
// main thread
520-
current_stack.ss_sp as usize - current_stack.ss_size + PAGE_SIZE.load(Ordering::Relaxed)
520+
stack_ptr.addr() - current_stack.ss_size + PAGE_SIZE.load(Ordering::Relaxed)
521521
} else {
522522
// new thread
523-
current_stack.ss_sp as usize - current_stack.ss_size
523+
stack_ptr.addr() - current_stack.ss_size
524524
};
525-
Some(stackaddr as *mut libc::c_void)
525+
Some(stack_ptr.with_addr(stack_addr))
526526
}
527527

528528
#[cfg(any(
@@ -557,19 +557,20 @@ pub mod guard {
557557
unsafe fn get_stack_start_aligned() -> Option<*mut libc::c_void> {
558558
let page_size = PAGE_SIZE.load(Ordering::Relaxed);
559559
assert!(page_size != 0);
560-
let stackaddr = get_stack_start()?;
560+
let stackptr = get_stack_start()?;
561+
let stackaddr = stackptr.addr();
561562

562563
// Ensure stackaddr is page aligned! A parent process might
563564
// have reset RLIMIT_STACK to be non-page aligned. The
564565
// pthread_attr_getstack() reports the usable stack area
565566
// stackaddr < stackaddr + stacksize, so if stackaddr is not
566567
// page-aligned, calculate the fix such that stackaddr <
567568
// new_page_aligned_stackaddr < stackaddr + stacksize
568-
let remainder = (stackaddr as usize) % page_size;
569+
let remainder = (stackaddr) % page_size;
569570
Some(if remainder == 0 {
570-
stackaddr
571+
stackptr
571572
} else {
572-
((stackaddr as usize) + page_size - remainder) as *mut libc::c_void
573+
stackptr.with_addr(stackaddr + page_size - remainder)
573574
})
574575
}
575576

@@ -588,8 +589,8 @@ pub mod guard {
588589
// Instead, we'll just note where we expect rlimit to start
589590
// faulting, so our handler can report "stack overflow", and
590591
// trust that the kernel's own stack guard will work.
591-
let stackaddr = get_stack_start_aligned()?;
592-
let stackaddr = stackaddr as usize;
592+
let stackptr = get_stack_start_aligned()?;
593+
let stackaddr = stackptr.addr();
593594
Some(stackaddr - page_size..stackaddr)
594595
} else if cfg!(all(target_os = "linux", target_env = "musl")) {
595596
// For the main thread, the musl's pthread_attr_getstack
@@ -602,8 +603,8 @@ pub mod guard {
602603
// at the bottom. If we try to remap the bottom of the stack
603604
// ourselves, FreeBSD's guard page moves upwards. So we'll just use
604605
// the builtin guard page.
605-
let stackaddr = get_stack_start_aligned()?;
606-
let guardaddr = stackaddr as usize;
606+
let stackptr = get_stack_start_aligned()?;
607+
let guardaddr = stackptr.addr();
607608
// Technically the number of guard pages is tunable and controlled
608609
// by the security.bsd.stack_guard_page sysctl, but there are
609610
// few reasons to change it from the default. The default value has
@@ -620,33 +621,34 @@ pub mod guard {
620621
// than the initial mmap() used, so we mmap() here with
621622
// read/write permissions and only then mprotect() it to
622623
// no permissions at all. See issue #50313.
623-
let stackaddr = get_stack_start_aligned()?;
624+
let stackptr = get_stack_start_aligned()?;
624625
let result = mmap(
625-
stackaddr,
626+
stackptr,
626627
page_size,
627628
PROT_READ | PROT_WRITE,
628629
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
629630
-1,
630631
0,
631632
);
632-
if result != stackaddr || result == MAP_FAILED {
633+
if result != stackptr || result == MAP_FAILED {
633634
panic!("failed to allocate a guard page: {}", io::Error::last_os_error());
634635
}
635636

636-
let result = mprotect(stackaddr, page_size, PROT_NONE);
637+
let result = mprotect(stackptr, page_size, PROT_NONE);
637638
if result != 0 {
638639
panic!("failed to protect the guard page: {}", io::Error::last_os_error());
639640
}
640641

641-
let guardaddr = stackaddr as usize;
642+
let guardaddr = stackptr.addr();
642643

643644
Some(guardaddr..guardaddr + page_size)
644645
}
645646
}
646647

647648
#[cfg(any(target_os = "macos", target_os = "openbsd", target_os = "solaris"))]
648649
pub unsafe fn current() -> Option<Guard> {
649-
let stackaddr = get_stack_start()? as usize;
650+
let stackptr = get_stack_start()?;
651+
let stackaddr = stackptr.addr();
650652
Some(stackaddr - PAGE_SIZE.load(Ordering::Relaxed)..stackaddr)
651653
}
652654

@@ -679,11 +681,11 @@ pub mod guard {
679681
panic!("there is no guard page");
680682
}
681683
}
682-
let mut stackaddr = crate::ptr::null_mut();
684+
let mut stackptr = crate::ptr::null_mut::<libc::c_void>();
683685
let mut size = 0;
684-
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut size), 0);
686+
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackptr, &mut size), 0);
685687

686-
let stackaddr = stackaddr as usize;
688+
let stackaddr = stackptr.addr();
687689
ret = if cfg!(any(target_os = "freebsd", target_os = "netbsd")) {
688690
Some(stackaddr - guardsize..stackaddr)
689691
} else if cfg!(all(target_os = "linux", target_env = "musl")) {

library/std/src/sys/unix/weak.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
// that, we'll just allow that some unix targets don't use this module at all.
2323
#![allow(dead_code, unused_macros)]
2424

25-
use crate::ffi::CStr;
25+
use crate::ffi::{c_void, CStr};
2626
use crate::marker::PhantomData;
2727
use crate::mem;
28+
use crate::ptr;
2829
use crate::sync::atomic::{self, AtomicUsize, Ordering};
2930

3031
// We can use true weak linkage on ELF targets.
@@ -129,25 +130,25 @@ impl<F> DlsymWeak<F> {
129130
// Cold because it should only happen during first-time initialization.
130131
#[cold]
131132
unsafe fn initialize(&self) -> Option<F> {
132-
assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>());
133+
assert_eq!(mem::size_of::<F>(), mem::size_of::<*mut ()>());
133134

134135
let val = fetch(self.name);
135136
// This synchronizes with the acquire fence in `get`.
136-
self.addr.store(val, Ordering::Release);
137+
self.addr.store(val.addr(), Ordering::Release);
137138

138-
match val {
139+
match val.addr() {
139140
0 => None,
140-
addr => Some(mem::transmute_copy::<usize, F>(&addr)),
141+
_ => Some(mem::transmute_copy::<*mut c_void, F>(&val)),
141142
}
142143
}
143144
}
144145

145-
unsafe fn fetch(name: &str) -> usize {
146+
unsafe fn fetch(name: &str) -> *mut c_void {
146147
let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
147148
Ok(cstr) => cstr,
148-
Err(..) => return 0,
149+
Err(..) => return ptr::null_mut(),
149150
};
150-
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize
151+
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr())
151152
}
152153

153154
#[cfg(not(any(target_os = "linux", target_os = "android")))]

0 commit comments

Comments
 (0)