Anonymous user
Machine code: Difference between revisions
Enhanced Rust
(Added Rust) |
(Enhanced Rust) |
||
Line 817:
This is heavily inspired by https://www.jonathanturner.org/2015/12/building-a-simple-jit-in-rust.html<br />
Hence, only working on Linux (the only other way to disable memory execution protection on other OSes was to use other crates, which kind of defeats the purpose.)
<lang Rust>extern crate libc;
use std::mem;▼
const PAGE_SIZE: usize = 4096;▼
#[cfg(all(
target_os = "linux",
any(target_pointer_width = "32", target_pointer_width = "64")
))]
fn main() {
let bytes: [u8; 9] = [0x8b, 0x44, 0x24, 0x04, 0x03, 0x44, 0x24, 0x08, 0xc3];▼
let (bytes, size): (Vec<u8>, usize) = if cfg!(target_pointer_width = "32") {
(
9,
)
} else {
(vec![0x48, 0x89, 0xf8, 0x48, 0x01, 0xf0, 0xc3], 7)
};
let f: fn(u8, u8) -> u8 = unsafe {
let mut page: *mut libc::c_void =
libc::posix_memalign(&mut page,
libc::mprotect(
page,
Line 836 ⟶ 844:
libc::PROT_EXEC | libc::PROT_READ | libc::PROT_WRITE,
);
let contents: *mut u8 =
ptr::copy(bytes.as_ptr(), contents, 9);
mem::transmute(contents)
};
println!("Returned value: {}", return_value);
assert_eq!(return_value, 19);
}
#[cfg(any(
not(target_os = "linux"),
not(any(target_pointer_width = "32", target_pointer_width = "64"))
))]
fn main() {
println!("Not supported on this platform.");
}
</lang>
|