|
| 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,102 @@ 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 | + ($fn_name:ident, $test_fn1:ident, $test_fn2:ident, $test_ret_val1:expr, $test_ret_val2:expr) => { |
| 106 | + #[test] |
| 107 | + fn $fn_name() { |
| 108 | + #[allow(unused_unsafe)] |
| 109 | + unsafe { |
| 110 | + let ret1 = $test_fn1(1, 2, 3, 4, 5, 6); |
| 111 | + assert_eq!(ret1, $test_ret_val1); |
| 112 | + |
| 113 | + let ret2 = $test_fn2(1, 2, 3, 4, 5, 6); |
| 114 | + assert_eq!(ret2, $test_ret_val2); |
| 115 | + |
| 116 | + let ptest1 = $test_fn1 as *const () as GuestAddr; |
| 117 | + let ptest2 = $test_fn2 as *const () as GuestAddr; |
| 118 | + info!("pfn: {:#x}", ptest1); |
| 119 | + let aligned_pfn = ptest1 & !0xfff; |
| 120 | + info!("aligned_pfn: {:#x}", aligned_pfn); |
| 121 | + LinuxMmap::protect( |
| 122 | + aligned_pfn, |
| 123 | + 0x4096, |
| 124 | + MmapProt::READ | MmapProt::WRITE | MmapProt::EXEC, |
| 125 | + ) |
| 126 | + .unwrap(); |
| 127 | + |
| 128 | + RawPatch::patch(ptest1, ptest2).unwrap(); |
| 129 | + let ret = $test_fn1(1, 2, 3, 4, 5, 6); |
| 130 | + assert_eq!(ret, $test_ret_val2); |
| 131 | + } |
| 132 | + } |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + define_test_function!(test_arm1, 0xdeadface); |
| 137 | + define_test_function!(test_arm2, 0xd00df00d); |
| 138 | + define_test_function!(test_arm3, 0xfeeddeaf); |
| 139 | + define_test_function!( |
| 140 | + target_feature(enable = "thumb-mode"), |
| 141 | + test_thumb1, |
| 142 | + 0xcafebabe |
| 143 | + ); |
| 144 | + define_test_function!( |
| 145 | + target_feature(enable = "thumb-mode"), |
| 146 | + test_thumb2, |
| 147 | + 0xbeeffade |
| 148 | + ); |
| 149 | + define_test_function!( |
| 150 | + target_feature(enable = "thumb-mode"), |
| 151 | + test_thumb3, |
| 152 | + 0xdeedcede |
| 153 | + ); |
| 154 | + |
| 155 | + define_test!(test_patch_arm_to_arm, test_arm2, test_arm1, 0xd00df00d, 0xdeadface); |
| 156 | + define_test!(test_patch_arm_to_thumb, test_arm3, test_thumb1, 0xfeeddeaf, 0xcafebabe); |
| 157 | + define_test!(test_patch_thumb_to_arm, test_thumb3, test_arm1, 0xdeedcede, 0xdeadface); |
| 158 | + define_test!(test_patch_thumb_to_thumb, test_thumb2, test_thumb1, 0xbeeffade, 0xcafebabe); |
| 159 | +} |
0 commit comments