Skip to content

Commit

Permalink
fix(lib): use #[cfg(target_arch = ...)] to guard __read(fsd|gsq)word
Browse files Browse the repository at this point in the history
  • Loading branch information
oberrich committed Sep 3, 2024
1 parent e1d2d6f commit b886e94
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/// Bindings for `phnt` (nightly) generated by `bindgen`
pub mod ffi {
// use vendored bindings
// TODO: Restrict use of vendored bindings to x86_64 targets at compile-time. Alternatively: Ship vendored bindings for all architectures (or maybe have diffs and apply this in a build script if that is possible)
#[cfg_attr(docsrs, doc(cfg(not(feature = "regenerate"))))]
#[cfg(not(feature = "regenerate"))]
include!("ffi/generated.rs");
Expand All @@ -48,56 +49,37 @@ pub mod ext {
use crate::ffi::*;
use core::{arch::asm, mem, ptr};

#[macro_export]
macro_rules! InitializeObjectAttributes {
($p:expr, $n:expr, $a:expr, $r:expr, $s:expr) => {
let attrs = $p;
attrs.Length =
unsafe { u32::try_from(core::mem::size_of::<ffi::OBJECT_ATTRIBUTES>()).unwrap_unchecked() };
attrs.RootDirectory = $r;
attrs.ObjectName = $n;
attrs.Attributes = $a;
attrs.SecurityDescriptor = $s;
attrs.SecurityQualityOfService = core::ptr::null_mut();
};
}

#[inline]
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
pub unsafe fn __readfsdword(offset: u32) -> usize {
let out: usize;
asm!(
"mov {:e}, fs:[{:e}]",
lateout(reg) out,
in(reg) offset,
"mov {}, fs:[{}]",
lateout(reg) out, in(reg) offset,
options(nostack, pure, readonly),
);
out
}

#[inline]
#[cfg(target_pointer_width = "64")]
#[cfg(target_arch = "x86_64")]
pub unsafe fn __readgsqword(offset: u32) -> usize {
let out: usize;
asm!(
"mov {}, gs:[{:e}]",
lateout(reg) out,
in(reg) offset,
"mov {}, gs:[{}]",
lateout(reg) out, in(reg) offset,
options(nostack, pure, readonly),
);
out
}

#[inline]
pub unsafe fn NtCurrentTeb() -> *mut TEB {
let teb_offset = mem::offset_of!(NT_TIB, Self_) as u32;
const TEB_OFFSET = mem::offset_of!(NT_TIB, Self_) as u32;
#[cfg(target_arch = "x86_64")]
{
__readgsqword(teb_offset) as _
}
__readgsqword(TEB_OFFSET) as _
#[cfg(target_arch = "x86")]
{
__readfsdword(teb_offset) as _
}
__readfsdword(TEB_OFFSET) as _
}

#[cfg(test)]
Expand Down

0 comments on commit b886e94

Please sign in to comment.