|
1 | 1 | use std::mem::MaybeUninit;
|
2 | 2 |
|
3 |
| -use crate::arm_asm::{Add, Immediate, OpCode, Register, Ret, Udf}; |
4 |
| -use region::{alloc, page, protect, Allocation, Protection}; |
5 |
| - |
6 |
| -use libc::{sigaction, SIGILL}; |
| 3 | +use crate::arm_asm::{OpCode, Udf}; |
| 4 | +use region::{alloc, protect, Allocation, Protection}; |
7 | 5 |
|
8 | 6 | #[derive(Clone)]
|
9 | 7 | pub struct Marker {
|
@@ -108,64 +106,25 @@ impl JitPage {
|
108 | 106 | }
|
109 | 107 | }
|
110 | 108 |
|
111 |
| -macro_rules! invoke { |
112 |
| - ($jit:expr, $fn_type:ty $(, $args:expr)*) => { |
113 |
| - $jit.run(|ptr: *const _| { |
114 |
| - let func: $fn_type = std::mem::transmute_copy(&ptr); |
115 |
| - func($($args),*) |
116 |
| - }) |
117 |
| - }; |
118 |
| -} |
119 |
| - |
120 |
| -unsafe fn handler(signal: libc::c_int, siginfo: *const libc::siginfo_t, _: *const libc::c_void) { |
121 |
| - let siginfo = &*siginfo; |
122 |
| - println!("Handled signal: {}!", signal); |
123 |
| - println!("Address: {:?}", siginfo.si_addr()); |
124 |
| - libc::exit(libc::EXIT_FAILURE); |
125 |
| -} |
126 |
| - |
127 |
| -unsafe fn register_sigill_handler() { |
128 |
| - let fn_ptr: unsafe fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) = handler; |
129 |
| - let action: libc::sighandler_t = std::mem::transmute_copy(&fn_ptr); |
130 |
| - let act = sigaction { |
131 |
| - sa_mask: 0, |
132 |
| - sa_flags: 0, |
133 |
| - sa_sigaction: action, |
134 |
| - }; |
135 |
| - |
136 |
| - let result = sigaction(SIGILL, &act as *const _, std::ptr::null_mut()); |
137 |
| - if result < 0 { |
138 |
| - panic!("Error registering hander: {}", result); |
139 |
| - } |
140 |
| -} |
141 |
| - |
142 |
| -pub fn run_demo() { |
143 |
| - unsafe { register_sigill_handler() }; |
144 |
| - |
145 |
| - let mut jit_page = JitPage::allocate(page::size()).unwrap(); |
146 |
| - |
147 |
| - jit_page.populate(|opcode_stream| { |
148 |
| - opcode_stream.push_opcode( |
149 |
| - Add::new(Register::X0, Register::X0) |
150 |
| - .with_immediate(Immediate::new(24)) |
151 |
| - .generate(), |
152 |
| - ); |
153 |
| - opcode_stream.push_opcode(Ret::new().generate()); |
154 |
| - }); |
155 |
| - |
156 |
| - let val = unsafe { invoke!(jit_page, extern "C" fn(u64) -> u64, 10) }; |
157 |
| - println!("Value {}", val); |
158 |
| -} |
159 |
| - |
160 | 109 | #[cfg(all(test, target_arch = "aarch64"))]
|
161 | 110 | mod test {
|
162 | 111 | use super::*;
|
163 | 112 | use crate::arm_asm::{
|
164 |
| - Adc, And, Branch, Condition, Ldrd, MemoryAccessMode, Mov, MovShift, Movk, Movn, Movz, Mrs, |
165 |
| - Msr, OpSize, Or, RegShift, Sbc, SetF8, SignedImmediate, Strd, Sub, Sxtb, Xor, NZCV, |
| 113 | + Adc, Add, And, Branch, Condition, Immediate, Ldrd, MemoryAccessMode, Mov, MovShift, Movk, |
| 114 | + Movn, Movz, Mrs, Msr, OpSize, Or, RegShift, Register, Ret, Sbc, SetF8, SignedImmediate, |
| 115 | + Strd, Sub, Sxtb, Xor, NZCV, |
166 | 116 | };
|
167 | 117 | use region::page;
|
168 | 118 |
|
| 119 | + macro_rules! invoke { |
| 120 | + ($jit:expr, $fn_type:ty $(, $args:expr)*) => { |
| 121 | + $jit.run(|ptr: *const _| { |
| 122 | + let func: $fn_type = std::mem::transmute_copy(&ptr); |
| 123 | + func($($args),*) |
| 124 | + }) |
| 125 | + }; |
| 126 | + } |
| 127 | + |
169 | 128 | #[test]
|
170 | 129 | fn test_add_immediate() {
|
171 | 130 | let mut jit_page = JitPage::allocate(page::size()).unwrap();
|
|
0 commit comments