sys_util: replace sysconf(_SC_PAGESIZE) with a safe wrapper

There were a few places that used this to get the page size inside of an
unsafe block, For convenience, this adds a safe wrapper in sys_util and
replaces all extant usage of sysconf with the wrapper version.

BUG=chromium:800626
TEST=./build_test

Change-Id: Ic65bf72aea90eabd4158fbdcdbe25c3f13ca93ac
Reviewed-on: https://chromium-review.googlesource.com/857907
Commit-Ready: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Zach Reizner 2018-01-09 15:49:04 -08:00 committed by chrome-bot
parent 20bb597636
commit ee2f1fe770
2 changed files with 13 additions and 5 deletions

View file

@ -17,11 +17,11 @@ use std::collections::hash_map::Entry;
use std::os::raw::*;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use libc::{open, sysconf, O_RDWR, O_CLOEXEC, EINVAL, ENOSPC, ENOENT, _SC_PAGESIZE};
use libc::{open, O_RDWR, O_CLOEXEC, EINVAL, ENOSPC, ENOENT};
use kvm_sys::*;
use sys_util::{GuestAddress, GuestMemory, MemoryMapping, EventFd, Error, Result};
use sys_util::{GuestAddress, GuestMemory, MemoryMapping, EventFd, Error, Result, pagesize};
#[allow(unused_imports)]
use sys_util::{ioctl, ioctl_with_val, ioctl_with_ref, ioctl_with_mut_ref, ioctl_with_ptr,
ioctl_with_mut_ptr};
@ -266,7 +266,7 @@ impl Vm {
/// region `slot` represents. For example, if the size of `slot` is 16 pages, `dirty_log` must
/// be 2 bytes or greater.
pub fn get_dirty_log(&self, slot: u32, dirty_log: &mut [u8]) -> Result<()> {
let page_size = unsafe { sysconf(_SC_PAGESIZE) } as usize;
let page_size = pagesize();
match self.device_memory.get(&slot) {
Some(mmap) => {
// Ensures that there are as many bits in dirty_log as there are pages in the mmap.
@ -907,7 +907,7 @@ mod tests {
fn vcpu_mmap_size() {
let kvm = Kvm::new().unwrap();
let mmap_size = kvm.get_vcpu_mmap_size().unwrap();
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
let page_size = pagesize();
assert!(mmap_size >= page_size);
assert!(mmap_size % page_size == 0);
}

View file

@ -55,10 +55,18 @@ pub use signalfd::Error as SignalFdError;
use std::ffi::CStr;
use std::ptr;
use libc::{kill, syscall, waitpid, c_long, pid_t, uid_t, gid_t, SIGKILL, WNOHANG};
use libc::{kill, syscall, sysconf, waitpid, c_long, pid_t, uid_t, gid_t, _SC_PAGESIZE,
SIGKILL, WNOHANG};
use syscall_defines::linux::LinuxSyscall::SYS_getpid;
/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
#[inline(always)]
pub fn pagesize() -> usize {
// Trivially safe
unsafe { sysconf(_SC_PAGESIZE) as usize }
}
/// This bypasses `libc`'s caching `getpid(2)` wrapper which can be invalid if a raw clone was used
/// elsewhere.
#[inline(always)]