Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVentura committed Mar 29, 2024
1 parent 2bfd407 commit fdecfdf
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/auxv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ unsafe fn store_auxv() {
if key == libc::AT_SYSINFO_EHDR as usize {
ptr = val;
}
if key == libc::AT_PAGESZ as usize {
if key == libc::AT_PAGESZ as usize {
pagesize = val;
}
out = out.offset(2);
Expand Down
2 changes: 1 addition & 1 deletion src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{error::Error, time::SystemTime};

use tpom::{vdso, Kind, Time, TimeSpec, TimeVal, TVDSOFun};
use tpom::{vdso, Kind, TVDSOFun, Time, TimeSpec, TimeVal};

extern crate tpom;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
//! assert_ne!(time_c, time_d);
//! ```

pub mod auxv;
mod opcodes;
pub(crate) mod trampolines;
pub mod vdso;
pub mod auxv;

use crate::trampolines::*;
use crate::vdso::vDSO;
Expand Down
8 changes: 1 addition & 7 deletions src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ fn _generate_opcodes_riscv64(jmp_target: usize, symbol_len: usize) -> Vec<u8> {
let addr_bytes = jmp_target.to_le_bytes().to_vec();

let nop = vec![0x13, 0x0, 0x0, 0x0];
let mut opcodes = [
auipc_t0,
ld_t0_plus12,
jr,
addr_bytes
]
.concat();
let mut opcodes = [auipc_t0, ld_t0_plus12, jr, addr_bytes].concat();
while symbol_len > opcodes.len() {
opcodes.append(&mut nop.clone());
}
Expand Down
27 changes: 16 additions & 11 deletions src/vdso.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::*;
use cacheflush_sys;
use core::slice;
use goblin::elf::*;
use goblin::strtab::Strtab;
use core::slice;
use std::error::Error;
use std::fs;
use cacheflush_sys;
use std::sync::Mutex;

static vdso_mutex: Mutex<i32> = Mutex::new(0);
Expand All @@ -23,25 +23,31 @@ pub struct vDSO {
data: Vec<u8>,
}

#[cfg(target_pointer_width="32")]
#[cfg(target_pointer_width = "32")]
const ELF_HDR_SIZE: usize = 52;

#[cfg(target_pointer_width="64")]
#[cfg(target_pointer_width = "64")]
const ELF_HDR_SIZE: usize = 64;

impl vDSO {
pub fn read() -> Result<vDSO, Box<dyn Error>> {
let auxvec = auxv::read_aux_vec()?;

// As the size of the vDSO is unknown, read first only the header which has constant size
let header_bytes: &[u8] = unsafe { slice::from_raw_parts(&*(auxvec.vdso_base as *const u8), ELF_HDR_SIZE) };
let header_bytes: &[u8] =
unsafe { slice::from_raw_parts(&*(auxvec.vdso_base as *const u8), ELF_HDR_SIZE) };
let bare_header = Elf::parse_header(&header_bytes).unwrap();
// Having parsed the header, we can now calculate the len of the vDSO
let vdso_len = usize::from(bare_header.e_shnum * bare_header.e_shentsize) + (bare_header.e_shoff as usize);
let vdso_len = usize::from(bare_header.e_shnum * bare_header.e_shentsize)
+ (bare_header.e_shoff as usize);
// And with the len, we can read the right amount
let vdso_bytes = unsafe { slice::from_raw_parts(&*(auxvec.vdso_base as *const u8), vdso_len) };
let vdso_bytes =
unsafe { slice::from_raw_parts(&*(auxvec.vdso_base as *const u8), vdso_len) };

Ok(vDSO {data: vdso_bytes.into(), avv: auxvec })
Ok(vDSO {
data: vdso_bytes.into(),
avv: auxvec,
})
}

pub(crate) fn change_mode(&self, write: bool) {
Expand All @@ -52,9 +58,9 @@ impl vDSO {
};
// As we need to mprotect() the vDSO and that can only be done in full pages, we need
// to bump the vDSO length to the next page
let vdso_size_page_aligned = (self.data.len() + self.avv.page_size-1) & !(self.avv.page_size-1);
let vdso_size_page_aligned =
(self.data.len() + self.avv.page_size - 1) & !(self.avv.page_size - 1);
unsafe {

libc::mprotect(
self.avv.vdso_base as *mut libc::c_void,
vdso_size_page_aligned,
Expand Down Expand Up @@ -137,7 +143,6 @@ impl vDSO {
let kind = match ds.name.as_str() {
// Per the man page:
// > "All of these symbols are also available without the "__vdso_" prefix, but you should ignore those."

#[cfg(target_arch = "aarch64")]
"__kernel_clock_gettime" => Some(Kind::GetTime),
#[cfg(target_arch = "aarch64")]
Expand Down
6 changes: 3 additions & 3 deletions tests/pub.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod tests {
use std::time::{SystemTime, Duration};
use tpom::{vdso, Kind, TVDSOFun, TimeSpec};
use std::thread;
use std::hint::black_box;
use std::sync::Mutex;
use std::thread;
use std::time::{Duration, SystemTime};
use tpom::{vdso, Kind, TVDSOFun, TimeSpec};

static tm: Mutex<i32> = Mutex::new(0);

Expand Down

0 comments on commit fdecfdf

Please sign in to comment.