1 unstable release

0.0.1-alpha Jul 2, 2020

#5 in #dynasm

Download history 180/week @ 2022-03-11 158/week @ 2022-03-18 161/week @ 2022-03-25 160/week @ 2022-04-01 246/week @ 2022-04-08 76/week @ 2022-04-15 213/week @ 2022-04-22 203/week @ 2022-04-29 199/week @ 2022-05-06 187/week @ 2022-05-13 156/week @ 2022-05-20 102/week @ 2022-05-27 202/week @ 2022-06-03 210/week @ 2022-06-10 198/week @ 2022-06-17 140/week @ 2022-06-24

780 downloads per month
Used in syscall-linux-raw

Unlicense

28KB
618 lines

direct-asm

A Rust proc-macro to include pre-assembled instructions as a function call.

What

use libc::{c_int, c_void, size_t, ssize_t, c_long};

// Call write as a syscall on SysV x86-64 abi.
// WIP: The constant SYS_write needs to be provided by the caller for now.
#[direct_asm::assemble]
unsafe extern "C" 
fn sys_write(fd: c_int, ptr: *const c_void, len: size_t, wcall: c_long)
    -> ssize_t 
{
    "mov rax, rcx"; // Move sys call number to rax as required
    // Other arguments are already in correct register
    "syscall"; // Invoke actual system call placed in rax
    "ret" //Return actual result
}

fn sys_print(what: &str) -> libc::ssize_t {
    unsafe {
        sys_write(1, what.as_ptr() as *const libc::c_void, what.len(), SYS_write)
    }
}

Why

To show an alternative to inline-asm from gcc, possibly with more control while having well defined semantics. This will not be sufficient for all purposes but it is enough to read stack registers, to make system calls (I think) and much more. The included code must be position independent, has no access to globals (pass as arguments instead), and can not introduce any new symbols.

How

By aliasing two definitions with #[no_mangle] abuse. We precompile the asm using nasm into a raw binary form, then define a static byte array containing this code in the .text section and finally define an extern "C" function with the same symbol name. The linker then resolve that function to the array definition and hence calls the code as intended.

Wtf

Indeed. Don't use in prod.

Demo?

Within the minimal-rust folder we build a 186-byte binary on stable Rust.

License

The base software: Unlicense

The modified dynasm backend: Mozilla Public License Version 2.0

Dependencies

~0.5–1.2MB
~26K SLoC