Skip to content

Instantly share code, notes, and snippets.

@ids1024
Created June 20, 2017 03:12
Show Gist options
  • Save ids1024/269a19d8e64c38b33c94bae3ef30094b to your computer and use it in GitHub Desktop.
Save ids1024/269a19d8e64c38b33c94bae3ef30094b to your computer and use it in GitHub Desktop.
diff --git a/src/syscall/mod.rs b/src/syscall/mod.rs
index 1d9cea6..497ccbb 100644
--- a/src/syscall/mod.rs
+++ b/src/syscall/mod.rs
@@ -40,6 +40,127 @@ pub mod time;
/// Validate input
pub mod validate;
+
+struct ByteStr<'a>(&'a[u8]);
+
+impl<'a> ::core::fmt::Debug for ByteStr<'a> {
+ fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
+ write!(f, "\"")?;
+ for i in self.0 {
+ if *i as char == '"' || *i as char == '\\' {
+ write!(f, "\\{}", *i as char)?
+ } else if *i >= 32 && *i <= 125 {
+ write!(f, "{}", *i as char)?
+ } else {
+ write!(f, "\\{}", *i)?
+ }
+ }
+ write!(f, "\"")?;
+ Ok(())
+ }
+}
+
+
+fn print_call(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize) -> Result<()> {
+ use collections::Vec;
+ match a {
+ //syscall::SYS_LINK =>
+ SYS_OPEN => print!("open({:?}, {})",
+ ByteStr(validate_slice(b as *const u8, c)?),
+ d),
+ SYS_CHMOD => print!("chmod({:?}, {})",
+ ByteStr(validate_slice(b as *const u8, c)?),
+ d),
+ SYS_RMDIR => print!("rmdir({:?})",
+ ByteStr(validate_slice(b as *const u8, c)?)),
+ SYS_UNLINK => print!("unlink({:?})",
+ ByteStr(validate_slice(b as *const u8, c)?)),
+ SYS_CLOSE => print!("close({})", b),
+ SYS_DUP => print!("dup({}, {:?})",
+ b,
+ ByteStr(validate_slice(c as *const u8, d)?)),
+ SYS_DUP2 => print!("dup2({}, {}, {:?})",
+ b,
+ c,
+ ByteStr(validate_slice(d as *const u8, e)?)),
+ // How to format second argument?
+ SYS_READ => print!("read({}, {:?})",
+ b,
+ ByteStr(validate_slice(c as *const u8, d)?)),
+ SYS_WRITE => print!("write({}, {:?})",
+ b,
+ ByteStr(validate_slice(c as *const u8, d)?)),
+ SYS_LSEEK => print!("lseek({}, {}, {})", b, c as isize, d),
+ SYS_FCNTL => print!("fcntl({}, {}, {})", b, c, d),
+ SYS_FEVENT => print!("fevent({}, {})", b, c),
+ SYS_FMAP => print!("fmap({}, {}, {})", b, c, d),
+ SYS_FUNMAP => print!("funmap({})", b),
+ // How to format second argument?
+ SYS_FPATH => print!("fpath({}, ({}, {}))", b, c, d),
+ // How to format second argument?
+ SYS_FSTAT => print!("fstat({}, ({}, {}))", b, c, d),
+ // How to format second argument?
+ SYS_FSTATVFS => print!("fstatvfs({}, ({}, {}))", b, c, d),
+ SYS_FSYNC => print!("fsync({})", b),
+ SYS_FTRUNCATE => print!("ftruncate({}, {})", b, c),
+
+ SYS_BRK => print!("brk({})", b),
+ SYS_CHDIR => print!("chdir({:?})",
+ ByteStr(validate_slice(b as *const u8, c)?)),
+ SYS_CLOCK_GETTIME => print!("clock_gettime({}, ({}, {}))",
+ b,
+ c,
+ d),
+ SYS_CLONE => print!("clone({})", b),
+ SYS_EXECVE => print!("execve({:?}, {:?})",
+ ByteStr(validate_slice(b as *const u8, c)?),
+ validate_slice(
+ d as *const [usize; 2],
+ e)?
+ .iter()
+ .map(|a|
+ validate_slice(a[0] as *const u8, a[1]).ok()
+ .and_then(|s| ::core::str::from_utf8(s).ok()))
+ .collect::<Vec<Option<&str>>>()),
+ SYS_EXIT => print!("exit({})", b),
+ SYS_FUTEX => print!("futex({}, {}, {}, {}, {})", b, c, d, e, f),
+ // How to format argument?
+ SYS_GETCWD => print!("getcwd(({}, {}))", b, c),
+ SYS_GETEGID => print!("getgid()"),
+ SYS_GETENS => print!("getens()"),
+ SYS_GETEUID => print!("geteuid()"),
+ SYS_GETGID => print!("getgid()"),
+ SYS_GETNS => print!("getns()"),
+ SYS_GETPID => print!("getpid()"),
+ SYS_GETUID => print!("getuid()"),
+ SYS_IOPL => print!("iopl({})", b),
+ SYS_KILL => print!("kill({}, {})", b, c),
+ SYS_MKNS => print!("mkns({:?})",
+ validate_slice(b as *const [usize; 2], c)?),
+ SYS_NANOSLEEP => print!("nanosleep({:?}, ({}, {}))",
+ validate_slice(b as *const TimeSpec, 1),
+ c,
+ d),
+ SYS_PHYSALLOC => print!("physalloc({})", b),
+ SYS_PHYSFREE => print!("physfree({}, {})", b, c),
+ SYS_PHYSMAP => print!("physmap({}, {}, {})", b, c, d),
+ SYS_PHYSUNMAP => print!("physunmap({})", b),
+ SYS_VIRTTOPHYS => print!("virttophys({})", b),
+ SYS_PIPE2 => print!("pipe2({:?}, {})",
+ validate_slice_mut(b as *mut usize, 2)?,
+ c),
+ SYS_SETREGID => print!("setregid({}, {})", b, c),
+ SYS_SETRENS => print!("setrens({}, {})", b, c),
+ SYS_SETREUID => print!("setreuid({}, {})", b, c),
+ SYS_WAITPID => print!("waitpid({}, {}, {})", b, c, d),
+ SYS_YIELD => print!("yield()"),
+ _ => print!("INVALID CALL")
+ }
+
+ Ok(())
+}
+
+
#[no_mangle]
pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, stack: usize) -> usize {
#[inline(always)]
@@ -106,6 +227,19 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
let result = inner(a, b, c, d, e, f, stack);
+ let contexts = ::context::contexts();
+ if let Some(context_lock) = contexts.current() {
+ let context = context_lock.read();
+ let context_name = context.name.lock();
+ let name = unsafe { ::core::str::from_utf8_unchecked(&context_name) };
+ if name == "file:/home/user/curl" || name == "file:/home/user/a.out" || name == "file:/bin/pkg" {
+ //if !name.starts_with("initfs:") {
+ print!("{}: ", name);
+ let _ = print_call(a, b, c, d, e, f);
+ println!(" = {:?}", result);
+ }
+ }
+
/*
if let Err(ref err) = result {
let contexts = ::context::contexts();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment