Skip to content

Commit

Permalink
do not exit on missing offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Oct 4, 2023
1 parent 0750e78 commit c877219
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 39 deletions.
59 changes: 38 additions & 21 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use std::path::Path;

use crate::utils;
Expand All @@ -13,13 +13,13 @@ const CONFIG_FILE: &'static str = "./dfint_data/dfint_config.toml";
const OFFSETS_DIR: &'static str = "./dfint_data/offsets/";

#[static_init::dynamic]
pub static CONFIG: Config = Config::new().unwrap();
pub static CONFIG: Config = Config::new();

pub struct Config {
pub metadata: ConfigMetadata,
pub settings: Settings,
pub offset: OffsetsValues,
pub offset_metadata: OffsetsMetadata,
pub offset: Option<OffsetsValues>,
pub symbol: Option<SymbolsValues>,
pub hook_version: String,
}
Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct Settings {
#[derive(Deserialize)]
pub struct Offsets {
pub metadata: OffsetsMetadata,
pub offsets: OffsetsValues,
pub offsets: Option<OffsetsValues>,
pub symbols: Option<SymbolsValues>,
}

Expand Down Expand Up @@ -95,22 +95,39 @@ pub struct SymbolsValues {
}

impl Config {
pub fn new() -> Result<Self> {
let checksum = Self::checksum(Path::new(EXE_FILE))?;
let main_config = Self::parse_toml::<MainConfig>(Path::new(CONFIG_FILE))?;
match Self::walk_offsets(Path::new(OFFSETS_DIR), checksum) {
Ok(offsets) => Ok(Self {
pub fn new() -> Self {
let checksum = Self::checksum(Path::new(EXE_FILE)).unwrap();
let main_config = Self::parse_toml::<MainConfig>(Path::new(CONFIG_FILE)).unwrap();

let offsets = Self::walk_offsets(Path::new(OFFSETS_DIR), checksum);

match offsets {
Some(o) => Self {
metadata: main_config.metadata,
settings: main_config.settings,
offset: offsets.offsets,
offset_metadata: offsets.metadata,
symbol: offsets.symbols,
offset_metadata: o.metadata,
offset: o.offsets,
symbol: o.symbols,
hook_version: match option_env!("HOOK_VERSION") {
Some(version) => String::from(version),
None => String::from("not-defined"),
},
},
None => Self {
metadata: main_config.metadata,
settings: main_config.settings,
offset_metadata: OffsetsMetadata {
name: String::from("not found"),
version: String::from("not found"),
checksum,
},
offset: None,
symbol: None,
hook_version: match option_env!("HOOK_VERSION") {
Some(version) => String::from(version),
None => String::from("not-defined"),
},
}),
Err(e) => Err(anyhow!("Config error {:?}", e)),
},
}
}

Expand All @@ -126,20 +143,20 @@ impl Config {
let mut crc = checksum::crc::Crc::new(path.to_str().unwrap());
match crc.checksum() {
Ok(checksum) => Ok(checksum.crc32),
Err(e) => Err(anyhow!("Checksum error {:?}", e).into()),
Err(e) => Err(anyhow::anyhow!("Checksum error {:?}", e).into()),
}
}

fn walk_offsets(path: &Path, target_checksum: u32) -> Result<Offsets> {
for entry in std::fs::read_dir(path)? {
let entry = entry?;
fn walk_offsets(path: &Path, target_checksum: u32) -> Option<Offsets> {
for entry in std::fs::read_dir(path).unwrap() {
let entry = entry.unwrap();
let pentry = entry.path();
if !pentry.is_file() {
continue;
}
let offsets = Self::parse_toml::<Offsets>(&pentry)?;
let offsets = Self::parse_toml::<Offsets>(&pentry).unwrap();
if offsets.metadata.checksum == target_checksum {
return Ok(offsets);
return Some(offsets);
}
}

Expand All @@ -153,7 +170,7 @@ impl Config {
utils::MessageIconType::Error,
);

std::process::exit(2);
None
}

fn parse_toml<T: for<'de> serde::Deserialize<'de>>(path: &Path) -> Result<T> {
Expand Down
12 changes: 10 additions & 2 deletions src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ use r#macro::hook;
#[cfg(target_os = "linux")]
#[static_init::dynamic]
static ENABLER: usize = unsafe {
utils::symbol_handle_self::<*const i64>(&CONFIG.symbol.as_ref().unwrap().enabler.as_ref().unwrap()[1]) as usize
match CONFIG.symbol.is_some() {
true => {
utils::symbol_handle_self::<*const i64>(&CONFIG.symbol.as_ref().unwrap().enabler.as_ref().unwrap()[1]) as usize
}
false => 0 as usize,
}
};

pub unsafe fn attach_all() -> Result<()> {
Expand Down Expand Up @@ -238,7 +243,10 @@ fn standardstringentry(src: *const u8, maxlen: usize, flag: u8, events_ptr: *con
let utf_a = std::slice::from_raw_parts_mut(utf as *mut u32, 8);
#[cfg(target_os = "linux")]
{
let utf_a = std::slice::from_raw_parts_mut((*ENABLER + &CONFIG.offset.utf_input.unwrap()) as *mut u32, 8);
let utf_a = std::slice::from_raw_parts_mut(
(*ENABLER + &CONFIG.offset.as_ref().unwrap().utf_input.unwrap()) as *mut u32,
8,
);
}

for i in 0..8 {
Expand Down
30 changes: 16 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ extern "C" fn attach() {
if CONFIG.metadata.name != "dfint localization hook" {
error!("unable to find config file");
utils::message_box(
"Unable to find config file, translation unavaible",
"dfint hook error",
"Unable to find config file, translation unavaible",
utils::MessageIconType::Error,
);
return;
Expand All @@ -43,20 +43,22 @@ extern "C" fn attach() {
CONFIG.settings.dictionary,
DICTIONARY.size()
);
match unsafe { hooks::attach_all() } {
Ok(_) => debug!("hooks attached"),
Err(err) => {
error!("unable to attach hooks, {:?}", err);
utils::message_box(
"Unable to attach hooks, translation unavaible",
"dfint hook error",
utils::MessageIconType::Error,
);
return;
if CONFIG.offset_metadata.name != "not found" {
match unsafe { hooks::attach_all() } {
Ok(_) => debug!("hooks attached"),
Err(err) => {
error!("unable to attach hooks, {:?}", err);
utils::message_box(
"dfint hook error",
"Unable to attach hooks, translation unavaible",
utils::MessageIconType::Error,
);
return;
}
};
if CONFIG.settings.watchdog {
watchdog::install();
}
};
if CONFIG.settings.watchdog {
watchdog::install();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ pub fn hook(args: TokenStream, input: TokenStream) -> TokenStream {
(_, _, _, bo, _) if bo => attach.replace(
"target()",
format!(
"std::mem::transmute(utils::address(CONFIG.offset.{}.unwrap()))",
"std::mem::transmute(utils::address(CONFIG.offset.as_ref().unwrap().{}.unwrap()))",
ident.to_string()
)
.as_str(),
),
(_, _, _, _, _) => attach.replace(
"target()",
format!(
"std::mem::transmute(utils::address(CONFIG.offset.{}.unwrap()))",
"std::mem::transmute(utils::address(CONFIG.offset.as_ref().unwrap().{}.unwrap()))",
ident.to_string()
)
.as_str(),
Expand Down

0 comments on commit c877219

Please sign in to comment.