@@ -19,7 +19,7 @@ impl Patch for RawPatch {
19
19
if target == destination {
20
20
Err ( RawPatchError :: IdentityPatch ( target) ) ?;
21
21
}
22
- let patch = Self :: get_patch ( target, destination) ? ;
22
+ let patch = Self :: get_patch ( target, destination) ;
23
23
24
24
// Mask the thumb mode indicator bit
25
25
#[ cfg( target_arch = "arm" ) ]
@@ -34,108 +34,88 @@ impl Patch for RawPatch {
34
34
35
35
impl RawPatch {
36
36
#[ cfg( target_arch = "x86_64" ) ]
37
- fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
37
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Vec < u8 > {
38
38
// mov rax, 0xdeadfacef00dd00d
39
39
// jmp rax
40
- let insns = [
41
- [ 0x48 , 0xb8 , 0x0d , 0xd0 , 0x0d , 0xf0 , 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
42
- [ 0xff , 0xe0 ] . to_vec ( ) ,
40
+ let addr = destination. to_ne_bytes ( ) ;
41
+ #[ rustfmt:: skip]
42
+ let insns: & [ & [ u8 ] ] = & [
43
+ & [ 0x48 , 0xb8 ] , & addr,
44
+ & [ 0xff , 0xe0 ] ,
43
45
] ;
44
- let addr = destination. to_le_bytes ( ) ;
45
- let insn0_mod = [
46
- insns[ 0 ] [ 0 ] ,
47
- insns[ 0 ] [ 1 ] ,
48
- addr[ 0 ] ,
49
- addr[ 1 ] ,
50
- addr[ 2 ] ,
51
- addr[ 3 ] ,
52
- addr[ 4 ] ,
53
- addr[ 5 ] ,
54
- addr[ 6 ] ,
55
- addr[ 7 ] ,
56
- ]
57
- . to_vec ( ) ;
58
- let insns_mod = [ & insn0_mod, & insns[ 1 ] ] ;
59
- Ok ( insns_mod. into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
46
+ insns. concat ( )
60
47
}
61
48
62
49
#[ cfg( target_arch = "x86" ) ]
63
- fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
50
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Vec < u8 > {
64
51
// mov eax, 0xdeadface
65
52
// jmp eax
66
- let insns = [
67
- [ 0xb8 , 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
68
- [ 0xff , 0xe0 ] . to_vec ( ) ,
53
+ let addr = destination. to_ne_bytes ( ) ;
54
+ #[ rustfmt:: skip]
55
+ let insns: & [ & [ u8 ] ] = & [
56
+ & [ 0xb8 ] , & addr,
57
+ & [ 0xff , 0xe0 ] ,
69
58
] ;
70
- let addr = destination. to_le_bytes ( ) ;
71
- let insn0_mod = [ insns[ 0 ] [ 0 ] , addr[ 0 ] , addr[ 1 ] , addr[ 2 ] , addr[ 3 ] ] . to_vec ( ) ;
72
- let insns_mod = [ & insn0_mod, & insns[ 1 ] ] ;
73
- Ok ( insns_mod. into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
59
+ insns. concat ( )
74
60
}
75
61
76
62
#[ cfg( target_arch = "arm" ) ]
77
- fn get_patch ( target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
63
+ fn get_patch ( target : GuestAddr , destination : GuestAddr ) -> Vec < u8 > {
64
+ let addr = destination. to_ne_bytes ( ) ;
78
65
// If our target is in thumb mode
79
- if target & 1 == 1 {
66
+ #[ rustfmt:: skip]
67
+ let insns: & [ & [ u8 ] ] = if target & 1 == 1 {
80
68
// ldr ip, [pc, #2]
81
69
// bx ip
82
70
// .long 0xdeadface
83
- let insns = [
84
- [ 0xdf , 0xf8 , 0x02 , 0xc0 ] . to_vec ( ) ,
85
- [ 0x60 , 0x47 ] . to_vec ( ) ,
86
- [ 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
87
- ] ;
88
- let addr = destination. to_ne_bytes ( ) . to_vec ( ) ;
89
- let insns_mod = [ & insns[ 0 ] , & insns[ 1 ] , & addr] ;
90
- Ok ( insns_mod. into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
71
+ & [
72
+ & [ 0xdf , 0xf8 , 0x02 , 0xc0 ] ,
73
+ & [ 0x60 , 0x47 ] ,
74
+ & addr,
75
+ ]
91
76
} else {
92
77
// ldr ip, [pc]
93
78
// bx ip
94
79
// .long 0xdeadface
95
- let insns = [
96
- [ 0x00 , 0xc0 , 0x9f , 0xe5 ] . to_vec ( ) ,
97
- [ 0x1c , 0xff , 0x2f , 0xe1 ] . to_vec ( ) ,
98
- [ 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
99
- ] ;
100
- let addr = destination. to_ne_bytes ( ) . to_vec ( ) ;
101
- let insns_mod = [ & insns[ 0 ] , & insns[ 1 ] , & addr] ;
102
- Ok ( insns_mod. into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
103
- }
80
+ & [
81
+ & [ 0x00 , 0xc0 , 0x9f , 0xe5 ] ,
82
+ & [ 0x1c , 0xff , 0x2f , 0xe1 ] ,
83
+ & addr,
84
+ ]
85
+ } ;
86
+ insns. concat ( )
104
87
}
105
88
106
89
#[ cfg( target_arch = "aarch64" ) ]
107
- fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
90
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Vec < u8 > {
108
91
// ldr x16, #8
109
92
// br x16
110
93
// .quad 0xdeadfacef00dd00d
111
- let insns = [
112
- [ 0x50 , 0x00 , 0x00 , 0x58 ] . to_vec ( ) ,
113
- [ 0x00 , 0x02 , 0x1f , 0xd6 ] . to_vec ( ) ,
114
- [ 0x0d , 0xd0 , 0x0d , 0xf0 ] . to_vec ( ) ,
115
- [ 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
94
+ let addr = destination. to_ne_bytes ( ) ;
95
+ #[ rustfmt:: skip]
96
+ let insns: & [ & [ u8 ] ] = & [
97
+ & [ 0x50 , 0x00 , 0x00 , 0x58 ] ,
98
+ & [ 0x00 , 0x02 , 0x1f , 0xd6 ] ,
99
+ & addr
116
100
] ;
117
- let addr = destination. to_ne_bytes ( ) . to_vec ( ) ;
118
- let insns_mod = [ & insns[ 0 ] , & insns[ 1 ] , & addr] ;
119
- Ok ( insns_mod. into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
101
+ insns. concat ( )
120
102
}
121
103
122
104
#[ cfg( target_arch = "powerpc" ) ]
123
- fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
105
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Vec < u8 > {
124
106
// lis 12, 0xdead
125
107
// ori 12, 12, 0xface
126
108
// mtctr 12
127
109
// bctr
128
- let insns = [
129
- [ 0x3d , 0x80 , 0xde , 0xad ] . to_vec ( ) ,
130
- [ 0x61 , 0x8c , 0xfa , 0xce ] . to_vec ( ) ,
131
- [ 0x7d , 0x89 , 0x03 , 0xa6 ] . to_vec ( ) ,
132
- [ 0x4e , 0x80 , 0x04 , 0x20 ] . to_vec ( ) ,
110
+ let addr = destination. to_ne_bytes ( ) ;
111
+ #[ rustfmt:: skip]
112
+ let insns: & [ & [ u8 ] ] = & [
113
+ & [ 0x3d , 0x80 ] , & addr[ ..2 ] ,
114
+ & [ 0x61 , 0x8c ] , & addr[ 2 ..] ,
115
+ & [ 0x7d , 0x89 , 0x03 , 0xa6 ] ,
116
+ & [ 0x4e , 0x80 , 0x04 , 0x20 ] ,
133
117
] ;
134
- let addr = destination. to_be_bytes ( ) . to_vec ( ) ;
135
- let insn0_mod = [ insns[ 0 ] [ 0 ] , insns[ 0 ] [ 1 ] , addr[ 0 ] , addr[ 1 ] ] . to_vec ( ) ;
136
- let insn1_mod = [ insns[ 1 ] [ 0 ] , insns[ 1 ] [ 1 ] , addr[ 2 ] , addr[ 3 ] ] . to_vec ( ) ;
137
- let insns_mod = [ & insn0_mod, & insn1_mod, & insns[ 2 ] , & insns[ 3 ] ] ;
138
- Ok ( insns_mod. into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
118
+ insns. concat ( )
139
119
}
140
120
}
141
121
0 commit comments