diff --git a/src/lib.rs b/src/lib.rs index 7fb26b9..91dd5bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); @@ -48,40 +49,25 @@ 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::()).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 @@ -89,15 +75,11 @@ pub mod ext { #[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)]