├── .gitignore ├── Cargo.toml ├── Makefile ├── README.md └── src ├── bin.rs └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | jit 2 | Cargo.lock 3 | target 4 | src/main 5 | src/jit 6 | main 7 | *~ 8 | \#*\# 9 | \#* 10 | .#* 11 | *.x86 12 | *.llvm 13 | *.out 14 | *.boot 15 | *.rustc 16 | *.cmx 17 | *.dll 18 | *.def 19 | *.exe 20 | *.a 21 | *.rlib 22 | *.so 23 | *.dylib 24 | *.orig 25 | *.cmo 26 | *.cmi 27 | *.dSYM 28 | *.d 29 | *.o 30 | *.ll 31 | *.bc 32 | *.aux 33 | *.cp 34 | *.fn 35 | *.ky 36 | *.cps 37 | *.log 38 | *.pdf 39 | *.epub 40 | *.html 41 | *.pg 42 | *.toc 43 | *.tp 44 | *.vr 45 | *.patch 46 | *.diff 47 | *.rej 48 | *.swp 49 | *.swo 50 | *.tmp 51 | *.pyc 52 | *.elc 53 | .hg/ 54 | .hgignore 55 | .cproject 56 | .project 57 | .valgrindrc 58 | lexer.ml 59 | TAGS 60 | TAGS.emacs 61 | TAGS.vi 62 | version.ml 63 | version.texi 64 | config.mk 65 | /rt/ 66 | /rustllvm/ 67 | /test/ 68 | /build/ 69 | /inst/ 70 | /mingw-build/ 71 | src/.DS_Store 72 | /tmp/ 73 | /stage0/ 74 | /dl/ 75 | /stage1/ 76 | /stage2/ 77 | /stage3/ 78 | *.bz2 79 | /doc/html 80 | /doc/latex 81 | /doc/std 82 | /doc/extra 83 | /doc/green 84 | /doc/native 85 | /doc/rustc 86 | /doc/syntax 87 | /nd/ 88 | /llvm/ 89 | version.md 90 | *.tex 91 | keywords.md 92 | x86_64-apple-darwin/ 93 | x86_64-unknown-linux-gnu/ 94 | i686-unknown-linux-gnu/ 95 | tmp.*.rs 96 | config.stamp 97 | .DS_Store 98 | src/etc/dl 99 | i686-pc-mingw32/ 100 | src/librustc/lib/llvmdeps.rs 101 | target/ 102 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "jitter" 3 | version = "0.1.0" 4 | license = "MIT" 5 | authors = ["Daniel Fagnan "] 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | mkdir -p target 3 | rustc --test --out-dir target src/lib.rs 4 | ./target/jiter 5 | 6 | bin: 7 | mkdir -p target 8 | rustc --out-dir target src/bin.rs 9 | ./target/bin 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JITer 2 | 3 | JITer gives you the ability to dynamically JIT (just-in-time compile) machine code. Right now, it's a work in progress, and you have to express your machine code in a vector of hexadecimals representing the instructions. 4 | 5 | # Usage 6 | 7 | Not Ready 8 | 9 | # License 10 | 11 | MIT -------------------------------------------------------------------------------- /src/bin.rs: -------------------------------------------------------------------------------- 1 | #![crate_id = "jitbin"] 2 | 3 | extern crate jit; 4 | 5 | fn main() { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | use std::mem; 4 | use std::ptr; 5 | use std::os::{MemoryMap, MapReadable, MapWritable}; 6 | 7 | unsafe fn transmute_harder(from: T) -> U { 8 | let mut to: U = mem::uninitialized(); 9 | let p = &mut to as *mut U; 10 | *p = ptr::read(&from as *const _ as *const U); 11 | mem::forget(from); 12 | to 13 | } 14 | 15 | /// A function type in the context of a JIT-compiler. 16 | pub struct Function { 17 | map: Option, 18 | func: Option 19 | } 20 | 21 | impl Function { 22 | 23 | /// Allocate a new `Function`. We don't have enough information 24 | /// to reserve the memory needed or the function itself. 25 | pub fn new() -> Function { 26 | Function { 27 | map: None, 28 | func: None 29 | } 30 | } 31 | 32 | } 33 | 34 | /// Dynamically compile a function down to machine code. This will compile the 35 | /// contents (x86 instructions) and return a function that you can call normally. 36 | /// 37 | /// For security reasons, you're unable to write to the memory mapped region after 38 | /// (or during) this function. 39 | fn jit_func(region: &MemoryMap, contents: &[u8]) -> T { 40 | unsafe { 41 | ptr::copy_memory(region.data(), contents.as_ptr(), region.len()); 42 | libc::mprotect(region.data() as *mut libc::c_void, 43 | region.len() as libc::size_t, 44 | libc::PROT_EXEC | libc::PROT_READ as libc::c_int); 45 | 46 | for i in std::iter::range(0, contents.len()) { 47 | assert_eq!(*(contents.as_ptr().offset(i as int)), 48 | *region.data().offset(i as int)); 49 | } 50 | 51 | transmute_harder(region.data()) 52 | } 53 | } 54 | 55 | #[test] 56 | fn test_jit_func() { 57 | 58 | let contents = [ 59 | 0x48, 0x89, 0xf8, // mov %rdi, %rax 60 | 0x48, 0x83, 0xc0, 0x04, // add $4, %rax 61 | 0xc3 // ret 62 | ]; 63 | 64 | let region = match MemoryMap::new(contents.len(), &[MapReadable, MapWritable]) { 65 | Ok(map) => map, 66 | Err(err) => panic!(err) 67 | }; 68 | 69 | let Add = jit_func:: int>(®ion, contents.as_slice()); 70 | assert_eq!(Add(4), 8); 71 | println!("value: {}", Add(4)); 72 | } 73 | --------------------------------------------------------------------------------