feat: added (dumb) sigsegv catcher, small fmt fix
This commit is contained in:
parent
b66749cc43
commit
2be585fc7b
3 changed files with 39 additions and 3 deletions
|
@ -21,3 +21,4 @@ serde = "1.0.159"
|
||||||
serde_json = "1.0.95"
|
serde_json = "1.0.95"
|
||||||
nix = "0.26.2"
|
nix = "0.26.2"
|
||||||
pretty-hex = "0.3"
|
pretty-hex = "0.3"
|
||||||
|
signal-hook = "0.3.15"
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
use std::{ffi::c_void, num::NonZeroUsize};
|
use std::{ffi::c_void, num::NonZeroUsize};
|
||||||
|
|
||||||
use mlua::{Lua, Error, Variadic, Value, ToLua};
|
use mlua::{Lua, Error, Variadic, Value, ToLua};
|
||||||
use nix::sys::mman::{mprotect, ProtFlags, mmap, MapFlags, munmap};
|
|
||||||
use pox::{proc_maps::get_process_maps, tricks::fmt_path};
|
use pox::{proc_maps::get_process_maps, tricks::fmt_path};
|
||||||
|
use nix::sys::{mman::{mprotect, ProtFlags, mmap, MapFlags, munmap}, signal::{Signal::SIGSEGV, SigHandler}};
|
||||||
|
|
||||||
use crate::helpers::pretty_lua;
|
use crate::helpers::pretty_lua;
|
||||||
|
|
||||||
use super::{console::Console, HELPTEXT};
|
use super::{console::Console, HELPTEXT};
|
||||||
|
|
||||||
|
const SIGSEGV_HOOK : AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
pub fn lua_help(lua: &Lua, _args: ()) -> Result<(), Error> {
|
pub fn lua_help(lua: &Lua, _args: ()) -> Result<(), Error> {
|
||||||
let console : Console = lua.globals().get("console")?;
|
let console : Console = lua.globals().get("console")?;
|
||||||
console.send(HELPTEXT.into())
|
console.send(HELPTEXT.into())
|
||||||
|
@ -185,6 +187,37 @@ pub fn lua_munmap(_: &Lua, (addr, len): (usize, usize)) -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern fn handle_sigsegv(_signal: c_int) {
|
||||||
|
eprintln!("Segmentation fault (ignored)");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lua_catch_sigsev(_: &Lua, mode: Option<bool>) -> Result<bool, Error> {
|
||||||
|
match mode {
|
||||||
|
Some(m) => match m {
|
||||||
|
true => {
|
||||||
|
let handler = SigHandler::Handler(handle_sigsegv);
|
||||||
|
match unsafe { nix::sys::signal::signal(SIGSEGV, handler) } {
|
||||||
|
Ok(_h) => {
|
||||||
|
SIGSEGV_HOOK.store(true, Ordering::Relaxed);
|
||||||
|
Ok(true)
|
||||||
|
},
|
||||||
|
Err(e) => Err(Error::RuntimeError(format!("could not set sig handler ({}): {}", e, e.desc()))),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
false => {
|
||||||
|
match unsafe { nix::sys::signal::signal(SIGSEGV, SigHandler::SigDfl) } {
|
||||||
|
Ok(_h) => {
|
||||||
|
SIGSEGV_HOOK.store(false, Ordering::Relaxed);
|
||||||
|
Ok(false)
|
||||||
|
},
|
||||||
|
Err(e) => Err(Error::RuntimeError(format!("could not reset sig handler ({}): {}", e, e.desc()))),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
None => Ok(SIGSEGV_HOOK.load(Ordering::Relaxed)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn lua_exit(_: &Lua, code: Option<i32>) -> Result<(), Error> {
|
pub fn lua_exit(_: &Lua, code: Option<i32>) -> Result<(), Error> {
|
||||||
#[allow(unreachable_code)]
|
#[allow(unreachable_code)]
|
||||||
Ok(std::process::exit(code.unwrap_or(0)))
|
Ok(std::process::exit(code.unwrap_or(0)))
|
||||||
|
|
|
@ -32,6 +32,7 @@ pub fn register_builtin_fn(lua: &Lua, console: broadcast::Sender<String>) -> Res
|
||||||
lua.globals().set("mmap", lua.create_function(lua_mmap)?)?;
|
lua.globals().set("mmap", lua.create_function(lua_mmap)?)?;
|
||||||
lua.globals().set("munmap", lua.create_function(lua_munmap)?)?;
|
lua.globals().set("munmap", lua.create_function(lua_munmap)?)?;
|
||||||
lua.globals().set("mprotect", lua.create_function(lua_mprotect)?)?;
|
lua.globals().set("mprotect", lua.create_function(lua_mprotect)?)?;
|
||||||
|
lua.globals().set("sigsegv", lua.create_function(lua_catch_sigsev)?)?;
|
||||||
lua.globals().set("help", lua.create_function(lua_help)?)?;
|
lua.globals().set("help", lua.create_function(lua_help)?)?;
|
||||||
lua.globals().set("x", lua.create_function(lua_hex)?)?;
|
lua.globals().set("x", lua.create_function(lua_hex)?)?;
|
||||||
lua.globals().set("b", lua.create_function(lua_bytes)?)?;
|
lua.globals().set("b", lua.create_function(lua_bytes)?)?;
|
||||||
|
@ -57,8 +58,9 @@ pub const HELPTEXT : &str = "?> This is a complete lua repl
|
||||||
> procmaps([ret]) get process memory maps as string
|
> procmaps([ret]) get process memory maps as string
|
||||||
> read(addr, size) read {size} raw bytes at {addr}
|
> read(addr, size) read {size} raw bytes at {addr}
|
||||||
> write(addr, bytes) write given {bytes} at {addr}
|
> write(addr, bytes) write given {bytes} at {addr}
|
||||||
> find(ptr, len, match, [first]) search from {ptr} to {ptr+len} any or first {match}
|
> find(ptr, len, match, [first]) search from {ptr} to {ptr+len} for {match} and return addrs
|
||||||
> x(number) show hex representation of given {number}
|
> x(number, [prefix]) show hex representation of given {number}
|
||||||
> b(string) return array of bytes from given {string}
|
> b(string) return array of bytes from given {string}
|
||||||
|
> sigsegv([set]) get or set SIGSEGV handler state
|
||||||
> help() print these messages
|
> help() print these messages
|
||||||
";
|
";
|
||||||
|
|
Loading…
Reference in a new issue