|
| 1 | +#![cfg_attr(target_arch = "arm", feature(arm_target_feature))] |
| 2 | + |
1 | 3 | #[cfg(test)] |
2 | 4 | #[cfg(feature = "libc")] |
| 5 | +#[cfg(not(target_arch = "arm"))] |
3 | 6 | mod tests { |
4 | 7 | use asan::{ |
5 | 8 | GuestAddr, |
@@ -55,3 +58,132 @@ mod tests { |
55 | 58 | assert_eq!(ret, 0xd00df00d); |
56 | 59 | } |
57 | 60 | } |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +#[cfg(feature = "libc")] |
| 64 | +#[cfg(target_arch = "arm")] |
| 65 | +mod tests { |
| 66 | + use asan::{ |
| 67 | + GuestAddr, |
| 68 | + mmap::{Mmap, MmapProt, linux::LinuxMmap}, |
| 69 | + patch::{Patch, raw::RawPatch}, |
| 70 | + }; |
| 71 | + use log::info; |
| 72 | + |
| 73 | + macro_rules! define_test_function { |
| 74 | + ($fn_name:ident, $ret_val:expr) => { |
| 75 | + define_test_function!([], $fn_name, $ret_val); |
| 76 | + }; |
| 77 | + |
| 78 | + ($attr:meta, $fn_name:ident, $ret_val:expr) => { |
| 79 | + define_test_function!([$attr], $fn_name, $ret_val); |
| 80 | + }; |
| 81 | + |
| 82 | + ([$($attr:meta)*], $fn_name:ident, $ret_val:expr) => { |
| 83 | + #[unsafe(no_mangle)] |
| 84 | + $(#[$attr])* |
| 85 | + extern "C" fn $fn_name( |
| 86 | + a1: usize, |
| 87 | + a2: usize, |
| 88 | + a3: usize, |
| 89 | + a4: usize, |
| 90 | + a5: usize, |
| 91 | + a6: usize, |
| 92 | + ) -> usize { |
| 93 | + assert_eq!(a1, 1); |
| 94 | + assert_eq!(a2, 2); |
| 95 | + assert_eq!(a3, 3); |
| 96 | + assert_eq!(a4, 4); |
| 97 | + assert_eq!(a5, 5); |
| 98 | + assert_eq!(a6, 6); |
| 99 | + return $ret_val; |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + macro_rules! define_test { |
| 105 | + ( |
| 106 | + $fn_name:ident, |
| 107 | + $test_fn1:ident, |
| 108 | + $test_fn2:ident, |
| 109 | + $test_ret_val1:expr, |
| 110 | + $test_ret_val2:expr |
| 111 | + ) => { |
| 112 | + #[test] |
| 113 | + fn $fn_name() { |
| 114 | + #[allow(unused_unsafe)] |
| 115 | + unsafe { |
| 116 | + let ret1 = $test_fn1(1, 2, 3, 4, 5, 6); |
| 117 | + assert_eq!(ret1, $test_ret_val1); |
| 118 | + |
| 119 | + let ret2 = $test_fn2(1, 2, 3, 4, 5, 6); |
| 120 | + assert_eq!(ret2, $test_ret_val2); |
| 121 | + |
| 122 | + let ptest1 = $test_fn1 as *const () as GuestAddr; |
| 123 | + let ptest2 = $test_fn2 as *const () as GuestAddr; |
| 124 | + info!("pfn: {:#x}", ptest1); |
| 125 | + let aligned_pfn = ptest1 & !0xfff; |
| 126 | + info!("aligned_pfn: {:#x}", aligned_pfn); |
| 127 | + LinuxMmap::protect( |
| 128 | + aligned_pfn, |
| 129 | + 0x4096, |
| 130 | + MmapProt::READ | MmapProt::WRITE | MmapProt::EXEC, |
| 131 | + ) |
| 132 | + .unwrap(); |
| 133 | + |
| 134 | + RawPatch::patch(ptest1, ptest2).unwrap(); |
| 135 | + let ret = $test_fn1(1, 2, 3, 4, 5, 6); |
| 136 | + assert_eq!(ret, $test_ret_val2); |
| 137 | + } |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + define_test_function!(test_arm1, 0xdeadface); |
| 143 | + define_test_function!(test_arm2, 0xd00df00d); |
| 144 | + define_test_function!(test_arm3, 0xfeeddeaf); |
| 145 | + define_test_function!( |
| 146 | + target_feature(enable = "thumb-mode"), |
| 147 | + test_thumb1, |
| 148 | + 0xcafebabe |
| 149 | + ); |
| 150 | + define_test_function!( |
| 151 | + target_feature(enable = "thumb-mode"), |
| 152 | + test_thumb2, |
| 153 | + 0xbeeffade |
| 154 | + ); |
| 155 | + define_test_function!( |
| 156 | + target_feature(enable = "thumb-mode"), |
| 157 | + test_thumb3, |
| 158 | + 0xdeedcede |
| 159 | + ); |
| 160 | + |
| 161 | + define_test!( |
| 162 | + test_patch_arm_to_arm, |
| 163 | + test_arm2, |
| 164 | + test_arm1, |
| 165 | + 0xd00df00d, |
| 166 | + 0xdeadface |
| 167 | + ); |
| 168 | + define_test!( |
| 169 | + test_patch_arm_to_thumb, |
| 170 | + test_arm3, |
| 171 | + test_thumb1, |
| 172 | + 0xfeeddeaf, |
| 173 | + 0xcafebabe |
| 174 | + ); |
| 175 | + define_test!( |
| 176 | + test_patch_thumb_to_arm, |
| 177 | + test_thumb3, |
| 178 | + test_arm1, |
| 179 | + 0xdeedcede, |
| 180 | + 0xdeadface |
| 181 | + ); |
| 182 | + define_test!( |
| 183 | + test_patch_thumb_to_thumb, |
| 184 | + test_thumb2, |
| 185 | + test_thumb1, |
| 186 | + 0xbeeffade, |
| 187 | + 0xcafebabe |
| 188 | + ); |
| 189 | +} |
0 commit comments