Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(target_arch): pass arch via feature flags instead #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ repl_tools = { path = "./repl_tools" }
headcrab_inject = { path = "./headcrab_inject" }

[features]
default = ["x86_64"]
x86_64 = []
syntax-highlighting = ["syntect"]

[workspace]
4 changes: 4 additions & 0 deletions headcrab_inject/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ target-lexicon = "0.10.0"

[target.'cfg(unix)'.dev-dependencies]
nix = "0.17.0"

[features]
default = ["x86_64"]
x86_64 = []
2 changes: 1 addition & 1 deletion headcrab_inject/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// FIXME make this work on other systems too.
#![cfg(all(target_arch = "x86_64", target_os = "linux"))]
#![cfg(all(feature = "x86_64", target_os = "linux"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headcrab_inject directly uses the rip and rsp fields of libc::user_regs, so it won't compile for other archs.


use std::{collections::HashMap, error::Error};

Expand Down
4 changes: 4 additions & 0 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub use windows::*;

mod thread;

pub enum Arch {
X86_64,
}

#[derive(Debug)]
pub struct MemoryMap {
/// Start and end range of the mapped memory.
Expand Down
20 changes: 10 additions & 10 deletions src/target/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ pub use writemem::WriteMemory;

lazy_static::lazy_static! {
pub static ref PAGE_SIZE: usize = unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize };
#[cfg(target_arch="x86_64")]
#[cfg(feature="x86_64")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ptrace can only work with the local arch, so using target_arch makes more sense to me. Also enabling the x86_64 feature on other archs won't compile, as libc won't jave a u_debugreg field for the libc::user struct.

static ref DEBUG_REG_OFFSET: usize = unsafe {
let x = std::mem::zeroed::<libc::user>();
(&x.u_debugreg as *const _ as usize) - (&x as *const _ as usize)
};
}

#[cfg(target_arch = "x86_64")]
#[cfg(feature = "x86_64")]
const SUPPORTED_HARDWARE_BREAKPOINTS: usize = 4;

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "x86_64"))]
const SUPPORTED_HARDWARE_BREAKPOINTS: usize = 0;

struct LinuxThread {
Expand Down Expand Up @@ -260,7 +260,7 @@ impl LinuxTarget {
&mut self,
breakpoint: HardwareBreakpoint,
) -> Result<usize, Box<dyn std::error::Error>> {
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "x86_64")]
{
let index = if let Some(empty) = self.find_empty_watchpoint() {
empty
Expand Down Expand Up @@ -311,15 +311,15 @@ impl LinuxTarget {

Ok(index)
}
#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "x86_64"))]
Err(Box::new(HardwareBreakpointError::UnsupportedPlatform))
}

pub fn clear_hardware_breakpoint(
&mut self,
index: usize,
) -> Result<HardwareBreakpoint, Box<dyn std::error::Error>> {
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "x86_64")]
{
if self.hardware_breakpoints[index].is_none() {
return Err(Box::new(HardwareBreakpointError::DoesNotExist(index)));
Expand Down Expand Up @@ -357,7 +357,7 @@ impl LinuxTarget {
Ok(watchpoint.unwrap())
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "x86_64"))]
Err(Box::new(HardwareBreakpointError::UnsupportedPlatform))
}

Expand All @@ -376,7 +376,7 @@ impl LinuxTarget {
pub fn is_hardware_breakpoint_triggered(
&self,
) -> Result<Option<usize>, Box<dyn std::error::Error>> {
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "x86_64")]
{
let mut dr7 = self.ptrace_peekuser((*DEBUG_REG_OFFSET + 6 * 8) as *mut libc::c_void)?;

Expand All @@ -402,12 +402,12 @@ impl LinuxTarget {
Ok(None)
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "x86_64"))]
Err(Box::new(HardwareBreakpointError::UnsupportedPlatform))
}

// Temporary function until ptrace_peekuser is fixed in nix crate
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "x86_64")]
fn ptrace_peekuser(
&self,
addr: *mut libc::c_void,
Expand Down
2 changes: 1 addition & 1 deletion tests/readregs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod test_utils;

static BIN_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/testees/hello");

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
#[cfg(all(target_os = "linux", feature = "x86_64"))]
#[test]
fn read_regs() -> Result<(), Box<dyn std::error::Error>> {
test_utils::ensure_testees();
Expand Down