Skip to content

Commit

Permalink
code cleanup, signature checking func, clippy lints, etc (#95)
Browse files Browse the repository at this point in the history
* Use `[workspace.lints]` instead of manually specifying them in each crate, and fix a bunch of clippy lints.

* Add `auxtools_check_signatures`, and allow auxtools to be loaded as an independent dll it can be used.

* Update dependencies (except `clap` because I'm lazy)

* Fix a lot more lints and iffy code
  • Loading branch information
Absolucy authored Jan 10, 2025
1 parent eca853c commit 73690f1
Show file tree
Hide file tree
Showing 36 changed files with 1,059 additions and 815 deletions.
1,394 changes: 813 additions & 581 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ repository = "https://github.com/wilox/auxtools"
rust-version = "1.76"
version = "0.1.0"

[workspace.dependencies]
lazy_static = "1"
detour = { version = "0.8", default-features = false }

[workspace.lints.rust]
static_mut_refs = "allow"

[workspace.lints.clippy]
complexity = { level = "warn", priority = -1 }
correctness = { level = "warn", priority = -1 }
perf = { level = "warn", priority = -1 }
style = { level = "warn", priority = -1 }
suspicious = { level = "warn", priority = -1 }
missing_safety_doc = "allow"
missing_transmute_annotations = "allow"
type_complexity = "allow"

[profile.release]
opt-level = 3
lto = 'thin'
Expand Down
5 changes: 4 additions & 1 deletion auxcov/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ crate-type = ["cdylib", "lib"]
auxtools = { path = "../auxtools" }
instruction_hooking = { path = "../instruction_hooking" }
dmasm = { git = "https://github.com/willox/dmasm" }
grcov = "0.8.18"
grcov = "0.8"

[lints]
workspace = true
34 changes: 13 additions & 21 deletions auxcov/src/codecov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub struct Tracker {
impl Tracker {
pub fn new() -> Tracker {
let mut hittable_lines = HashMap::<String, HashSet<u32>>::new();
let mut i: u32 = 0;
let mut i = 0_u32;
while let Some(proc) = Proc::from_id(raw_types::procs::ProcId(i)) {
i = i + 1;
i += 1;

let mut current_file_option;
let bytecode;
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Tracker {
continue;
}

hittable_lines.entry(file_name).or_insert(HashSet::new()).insert(line);
hittable_lines.entry(file_name).or_default().insert(line);
}
}
_ => {}
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Tracker {
return;
}

let filename_id = StringId::from(ctx.filename);
let filename_id = ctx.filename;
let proc_map_index = proc_instance.proc.0 as usize;
let line = ctx.line as usize;

Expand Down Expand Up @@ -158,16 +158,13 @@ impl Tracker {
}
}

if let Some(remove_index) = remove_index_option {
self.contexts.remove(remove_index);
if let Err(error) = result {
return Err(error);
match remove_index_option {
Some(remove_index) => {
self.contexts.remove(remove_index);
result.map(|_| true)
}

return Ok(true);
None => Ok(false)
}

Ok(false)
}

fn finalize(&mut self) -> Result<(), Vec<Error>> {
Expand All @@ -177,9 +174,7 @@ impl Tracker {
if let Err(error) = result {
match &mut errors_option {
None => {
let mut new_error_vec = Vec::new();
new_error_vec.push(error);
errors_option = Some(new_error_vec);
errors_option = Some(vec![error]);
}
Some(existing_vec) => {
existing_vec.push(error);
Expand All @@ -201,7 +196,7 @@ impl Tracker {
impl Drop for Tracker {
fn drop(&mut self) {
let _result = self.finalize(); // dropping the result here because what can ya
// do?
// do?
}
}

Expand Down Expand Up @@ -346,15 +341,12 @@ impl TrackerContext {
let output_path = Path::new(&self.output_file_name);
let mut path_buf = output_path.to_path_buf();
if path_buf.pop() {
let create_dir_result = create_dir_all(path_buf);
if let Err(create_dir_error) = create_dir_result {
return Err(create_dir_error);
}
create_dir_all(path_buf)?;
}

// reee wtf mozilla, what is this shitty rust?
let result = catch_unwind(|| {
output_cobertura(None, &result_tuples, Some(output_path), false);
output_cobertura(None, &result_tuples, Some(output_path), false, true);
});

if result.is_err() {
Expand Down
14 changes: 2 additions & 12 deletions auxcov/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ where
// that.
#[hook("/proc/start_code_coverage")]
fn start_code_coverage(coverage_file: Value) {
let coverage_file_string_result = coverage_file.as_string();
if let Err(runtime) = coverage_file_string_result {
return Err(runtime);
}

let coverage_file_string = coverage_file_string_result.unwrap();
let coverage_file_string = coverage_file.as_string()?;

let mut init_result = false;
with_tracker_option(
Expand All @@ -64,12 +59,7 @@ fn start_code_coverage(coverage_file: Value) {

#[hook("/proc/stop_code_coverage")]
fn stop_code_coverage(coverage_file: Value) {
let coverage_file_string_result = coverage_file.as_string();
if let Err(runtime) = coverage_file_string_result {
return Err(runtime);
}

let coverage_file_string = coverage_file_string_result.unwrap();
let coverage_file_string = coverage_file.as_string()?;

let mut result = Ok(Value::null());
with_tracker_option(
Expand Down
6 changes: 3 additions & 3 deletions auxtools-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ doctest = false
proc-macro2 = "1.0"
quote = "1.0"
hex = "0.4"
syn = { version = "2", features = ["full"] }

[dependencies.syn]
version = "1.0"
features = ["full"]
[lints]
workspace = true
12 changes: 3 additions & 9 deletions auxtools-impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![warn(clippy::complexity, clippy::correctness, clippy::perf, clippy::style)]

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, spanned::Spanned, Lit};
Expand Down Expand Up @@ -201,7 +199,6 @@ pub fn pin_dll(attr: TokenStream) -> TokenStream {
/// Value::null()
/// }
/// ```
#[proc_macro_attribute]
pub fn hook(attr: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
Expand Down Expand Up @@ -251,12 +248,9 @@ pub fn hook(attr: TokenStream, item: TokenStream) -> TokenStream {
if let syn::Pat::Ident(p) = &*arg.pat {
arg_names.push(p.ident.clone());
let index = arg_names.len() - 1;
proc_arg_unpacker.push(
(quote! {
&args[#index]
})
.into()
);
proc_arg_unpacker.push(quote! {
&args[#index]
});
}
}
let _default_null = quote! {
Expand Down
25 changes: 14 additions & 11 deletions auxtools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ rust-version.workspace = true
repository.workspace = true
license.workspace = true

[lib]
crate-type = ["lib", "cdylib"]

[build-dependencies]
cc = "1.0"

[dependencies]
auxtools-impl = { path = "../auxtools-impl", version = "0.1.0", package = "auxtools-impl" }
once_cell = "1.10.0"
inventory = "0.2.3"
lazy_static = "1.4.0"
dashmap = "3.11.10"
ahash = "0.7.6"
fxhash = "0.2.1"
ctor = "0.1.22"

[dependencies.detour]
version = "0.8.1"
default-features = false
once_cell = "1"
inventory = "0.3"
lazy_static = { workspace = true }
dashmap = "6"
ahash = "0.8"
fxhash = "0.2"
ctor = "0.2"
detour = { workspace = true }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["winuser", "libloaderapi", "psapi", "processthreadsapi"] }

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[lints]
workspace = true
6 changes: 3 additions & 3 deletions auxtools/src/bytecode_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ pub fn set_bytecode(proc: &Proc, mut bytecode: Vec<u32>) {
(*ptr).as_mut().unwrap()
};

if !state.original.contains_key(&proc.id) {
state.original.entry(proc.id).or_insert_with(|| {
let (ptr, len) = unsafe { proc.bytecode_mut_ptr() };

state.original.insert(proc.id, (ptr, len));
}
(ptr, len)
});

let (ptr, len) = {
let len = bytecode.len();
Expand Down
11 changes: 7 additions & 4 deletions auxtools/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ impl StackFrame {
// values than names)
let args = (0..(*instance).args_count)
.map(|i| {
let name = match param_names.get(i as usize) {
Some(name) => Some(name.clone()),
None => None
};
let name = param_names.get(i as usize).cloned();
(name, Value::from_raw(*((*instance).args).add(i as usize)))
})
.collect();
Expand Down Expand Up @@ -89,6 +86,12 @@ enum CallStackKind {
Suspended
}

impl Default for CallStacks {
fn default() -> Self {
Self::new()
}
}

impl CallStacks {
pub fn new() -> CallStacks {
let mut suspended = vec![];
Expand Down
14 changes: 7 additions & 7 deletions auxtools/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn init() -> Result<(), String> {
let runtime_hook = RawDetour::new(raw_types::funcs::runtime_byond as *const (), runtime_hook as *const ()).unwrap();

runtime_hook.enable().unwrap();
runtime_original = std::mem::transmute(runtime_hook.trampoline());
runtime_original = runtime_hook.trampoline() as *const () as *const c_void;

let call_hook = RawDetour::new(
raw_types::funcs::call_proc_by_id_byond as *const (),
Expand All @@ -89,7 +89,7 @@ pub fn init() -> Result<(), String> {
.unwrap();

call_hook.enable().unwrap();
call_proc_by_id_original = std::mem::transmute(call_hook.trampoline());
call_proc_by_id_original = call_hook.trampoline() as *const () as *const c_void;

DETOURS.with(|detours_cell| {
let mut detours = detours_cell.borrow_mut();
Expand Down Expand Up @@ -121,11 +121,11 @@ thread_local! {
fn hook_by_id(id: raw_types::procs::ProcId, hook: ProcHook, hook_path: String) -> Result<(), HookFailure> {
PROC_HOOKS.with(|h| {
let mut map = h.borrow_mut();
if map.contains_key(&id) {
return Err(HookFailure::AlreadyHooked);
} else {
map.insert(id, (hook, hook_path));
if let std::collections::hash_map::Entry::Vacant(e) = map.entry(id) {
e.insert((hook, hook_path));
Ok(())
} else {
Err(HookFailure::AlreadyHooked)
}
})
}
Expand Down Expand Up @@ -187,7 +187,7 @@ extern "C" fn call_proc_by_id_hook(

match result {
Ok(r) => {
let result_raw = (&r).raw;
let result_raw = r.raw;
// Stealing our reference out of the Value
std::mem::forget(r);
Some(result_raw)
Expand Down
25 changes: 22 additions & 3 deletions auxtools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![warn(clippy::complexity, clippy::correctness, clippy::perf, clippy::style)]

//! For when BYOND is not enough. Probably often.
//#[cfg(not(target_pointer_width = "32"))]
Expand Down Expand Up @@ -294,7 +292,7 @@ byond_ffi_fn! { auxtools_init(_input) {
return Some("FAILED (Could not pin the library in memory.)".to_owned());
}

if let Err(_) = hooks::init() {
if hooks::init().is_err() {
return Some("Failed (Couldn't initialize proc hooking)".to_owned());
}

Expand Down Expand Up @@ -406,3 +404,24 @@ byond_ffi_fn! { auxtools_full_shutdown(_input) {
};
Some("SUCCESS".to_owned())
} }

byond_ffi_fn! { auxtools_check_signatures(_input) {
let byondcore = match sigscan::Scanner::for_module(BYONDCORE) {
Some(v) => v,
None => return Some("FAILED (Couldn't create scanner for byondcore.dll)".to_owned())
};
if let Err(e) = version::init() {
return Some(format!("FAILED ({})", e));
}
let mut missing = Vec::<&'static str>::new();
for (name, found) in SIGNATURES0.check_all(&byondcore) {
if !found {
missing.push(name);
}
}
if missing.is_empty() {
Some("SUCCESS".to_owned())
} else {
Some(format!("MISSING: {}", missing.join(", ")))
}
} }
Loading

0 comments on commit 73690f1

Please sign in to comment.