Syscalls for Rustaceans!

by Gargi Sharma
github.com/gs0510   

Follow the slides: tiny.cc/rustrace

What even is a systems programming language?

1970s:Improving on Assembly

"A system program is
an integrated set of
subprograms
...
exceeding some
threshold of size
and/or complexity.
"

Systems Programming Languages
(Bergeron et al. 1972)

1970s:

  • The system program is likely to be used to support other software programs.

  • It is designed for continued “production” use rather than a one-shot solution to a single applications problem.

1970s:

A language which can be used without undue concern for bit twiddling.

Credit: Systems Programming (Donovan 1972)

1990s:Rise of Scripting languages

1990s:

Rise of Scripting languages

  • Bash, languages like Perl, Python, Ruby, Javascript etc. worked their way into the mainstream.
  • Systems programming languages designed to build more primitive computer elements.
  • Scripting languages are designed for gluing.

2010s:Boundaries Blur

2010s:

Boundaries Blur

  • Dropbox was able to build surprisingly large and scalable systems on just Python.
  • Javascript is used to render real-time, complex UIs in billions of web pages.
  • Gradual typing has gained steam in Javascript, Python, etc. enabling a transition from “prototype” code to “production” code.

What is a systems programming language today?

Where does Rust shine?

Systems programming is
programming where
you spend more time reading man pages than reading the internet.

- Kamal Mahrubi

Writing our own strace!


System Calls!

Syscalls!

Around 330 system calls in Linux:

  • File Access - creat, read, write, open, close, etc.
  • Process Control - wait, waitpid , kill, fork, etc.
  • Network Access - socket, getsockopt , listen, etc.

What happens when a syscall occurs?

What happens when a syscall occurs?

Making a syscall.

  • Setup register with syscall number & parameters.
  • Send a trap to kernel.
  • Result stored in register.

What is strace?

  • A diagnostic, debugging and instructional userspace utility for Linux.
  • Invaluable for solving problems with programs for which the source is not readily available.

What is strace?


        fn main() {
          println!("Hello RustConf!");
        }
        

What is strace?

Writing our own strace.

  • How to observe another process?
  • How to trap a system call?
  • How to fetch register values?

How to observe a process?

ptrace!!

How to observe a process?


        let output = cmd.before_exec(ptrace::traceme());
          
        let mut child = cmd.spawn();
        

How to trap a system call?

How to trap a system call?


        ptrace(
          Request::PTRACE_SYSCALL,
          pid,
          ptr::null_mut(),
          ptr::null_mut(),
        );

        waitpid(pid, None);
        

How to fetch register values?


        let res = ptrace::ptrace(
          Request::PTRACE_GETREGS,
          pid,
          PT_NULL as *mut c_void,
          &mut regs as *mut _ as *mut c_void,
        );
        

DEMO!

Conclusion 😊

A Shoutout!

  1. Arshia Mufti
  2. Shalom Abete
  3. Hailey James-Sorenson
  4. Taylor James-Sorenson
  5. Carrie Wu
  6. Andrew Zuckerman
  7. Theo Constantinedes
  8. Charilaos Pipis
  9. Wassim Marrakchi
  10. Stephen Koo

Thank you!

Resources

  1. A Go Programmer's Guide to Syscalls
  2. Rust implementation of glibc
  3. System call table for Linux x86
  4. What is systems programming really?
  5. Scripting: Higher Level Programming for the 21st Century
  6. Rust + nix = easier unix systems programming
  7. Slide theme from @jlengstorf