Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1520: mman module netbsd additions. r=asomers a=devnexen



1523: mman: add MAP_STACK to openbsd which is needed … r=asomers a=devnexen

…to created stack mappings

Co-authored-by: David Carlier <devnexen@gmail.com>
  • Loading branch information
bors[bot] and devnexen committed Sep 12, 2021
3 parents b3f58e8 + a477541 + ae66724 commit 1b7e485
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
23 changes: 20 additions & 3 deletions src/sys/mman.rs
Expand Up @@ -129,7 +129,7 @@ libc_bitflags!{
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
MAP_HASSEMAPHORE;
/// Region grows down, like a stack.
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
MAP_STACK;
/// Pages in this mapping are not retained in the kernel's memory cache.
#[cfg(any(target_os = "ios", target_os = "macos"))]
Expand All @@ -139,14 +139,22 @@ libc_bitflags!{
}
}

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
libc_bitflags!{
/// Options for `mremap()`.
pub struct MRemapFlags: c_int {
/// Permit the kernel to relocate the mapping to a new virtual address, if necessary.
#[cfg(target_os = "linux")]
MREMAP_MAYMOVE;
/// Place the mapping at exactly the address specified in `new_address`.
#[cfg(target_os = "linux")]
MREMAP_FIXED;
/// Permits to use the old and new address as hints to relocate the mapping.
#[cfg(target_os = "netbsd")]
MAP_FIXED;
/// Allows to duplicate the mapping to be able to apply different flags on the copy.
#[cfg(target_os = "netbsd")]
MAP_REMAPDUP;
}
}

Expand Down Expand Up @@ -334,15 +342,24 @@ pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: Ma
///
/// See the `mremap(2)` [man page](https://man7.org/linux/man-pages/man2/mremap.2.html) for
/// detailed requirements.
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub unsafe fn mremap(
addr: *mut c_void,
old_size: size_t,
new_size: size_t,
flags: MRemapFlags,
new_address: Option<* mut c_void>,
) -> Result<*mut c_void> {
#[cfg(target_os = "linux")]
let ret = libc::mremap(addr, old_size, new_size, flags.bits(), new_address.unwrap_or(std::ptr::null_mut()));
#[cfg(target_os = "netbsd")]
let ret = libc::mremap(
addr,
old_size,
new_address.unwrap_or(std::ptr::null_mut()),
new_size,
flags.bits(),
);

if ret == libc::MAP_FAILED {
Err(Error::from(Errno::last()))
Expand Down
14 changes: 12 additions & 2 deletions test/sys/test_mman.rs
Expand Up @@ -20,7 +20,7 @@ fn test_mmap_anonymous() {
}

#[test]
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn test_mremap_grow() {
const ONE_K : size_t = 1024;
let slice : &mut[u8] = unsafe {
Expand All @@ -35,9 +35,14 @@ fn test_mremap_grow() {
assert_eq !(slice[ONE_K - 1], 0xFF);

let slice : &mut[u8] = unsafe {
#[cfg(target_os = "linux")]
let mem = mremap(slice.as_mut_ptr() as * mut c_void, ONE_K, 10 * ONE_K,
MRemapFlags::MREMAP_MAYMOVE, None)
.unwrap();
#[cfg(target_os = "netbsd")]
let mem = mremap(slice.as_mut_ptr() as * mut c_void, ONE_K, 10 * ONE_K,
MRemapFlags::MAP_REMAPDUP, None)
.unwrap();
std::slice::from_raw_parts_mut(mem as * mut u8, 10 * ONE_K)
};

Expand All @@ -51,7 +56,7 @@ fn test_mremap_grow() {
}

#[test]
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn test_mremap_shrink() {
const ONE_K : size_t = 1024;
let slice : &mut[u8] = unsafe {
Expand All @@ -66,11 +71,16 @@ fn test_mremap_shrink() {
assert_eq !(slice[ONE_K - 1], 0xFF);

let slice : &mut[u8] = unsafe {
#[cfg(target_os = "linux")]
let mem = mremap(slice.as_mut_ptr() as * mut c_void, 10 * ONE_K, ONE_K,
MRemapFlags::empty(), None)
.unwrap();
// Since we didn't supply MREMAP_MAYMOVE, the address should be the
// same.
#[cfg(target_os = "linux")]
let mem = mremap(slice.as_mut_ptr() as * mut c_void, 10 * ONE_K, ONE_K,
MRemapFlags::MAP_FIXED), None)
.unwrap();
assert_eq !(mem, slice.as_mut_ptr() as * mut c_void);
std::slice::from_raw_parts_mut(mem as * mut u8, ONE_K)
};
Expand Down

0 comments on commit 1b7e485

Please sign in to comment.