1 unstable release

0.0.1-alpha Jul 2, 2020

#19 in #assemble

Download history 70/week @ 2023-12-06 47/week @ 2023-12-13 5/week @ 2023-12-20 3/week @ 2023-12-27 30/week @ 2024-01-03 71/week @ 2024-01-10 47/week @ 2024-01-17 104/week @ 2024-01-24 48/week @ 2024-01-31 55/week @ 2024-02-07 62/week @ 2024-02-14 68/week @ 2024-02-21 83/week @ 2024-02-28 100/week @ 2024-03-06 121/week @ 2024-03-13 45/week @ 2024-03-20

367 downloads per month
Used in syscall-linux-raw

Unlicense

29KB
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

~1.5–2.1MB
~47K SLoC