@@ -184,6 +184,7 @@ pub struct PatchGuard {
184
184
ptr : * mut u8 ,
185
185
len : usize ,
186
186
data : [ u8 ; JMP_MAX_SIZE ] ,
187
+ patch : [ u8 ; JMP_MAX_SIZE ] ,
187
188
}
188
189
189
190
impl Drop for PatchGuard {
@@ -194,6 +195,20 @@ impl Drop for PatchGuard {
194
195
}
195
196
}
196
197
198
+ impl PatchGuard {
199
+ pub fn revert ( & self ) {
200
+ unsafe {
201
+ copy_to_protected_address ( self . ptr , & self . data [ ..self . len ] ) ;
202
+ }
203
+ }
204
+
205
+ pub fn restore ( & self ) {
206
+ unsafe {
207
+ copy_to_protected_address ( self . ptr , & self . patch [ ..self . len ] ) ;
208
+ }
209
+ }
210
+ }
211
+
197
212
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
198
213
const UNSAFE_LEADING_BYTES : [ u8 ; 4 ] = [
199
214
0xC3 , // ret near
@@ -238,6 +253,7 @@ macro_rules! define_patch {
238
253
ptr: target,
239
254
len,
240
255
data: original,
256
+ patch,
241
257
}
242
258
}
243
259
) ;
@@ -254,6 +270,26 @@ define_patch!(patch7(A, B, C, D, E, F, G,));
254
270
define_patch ! ( patch8( A , B , C , D , E , F , G , H , ) ) ;
255
271
define_patch ! ( patch9( A , B , C , D , E , F , G , H , I , ) ) ;
256
272
273
+ #[ macro_export]
274
+ macro_rules! disable_patch {
275
+ ( $guard: expr, async $( $body: tt) * ) => { {
276
+ $guard. revert( ) ;
277
+ let result = async { $( $body) * } . await ;
278
+ $guard. restore( ) ;
279
+ result
280
+ } } ;
281
+
282
+ ( $guard: expr, $( $body: tt) * ) => { {
283
+ $guard. revert( ) ;
284
+ let result = ( || { $( $body) * } ) ( ) ;
285
+ $guard. restore( ) ;
286
+ result
287
+ } } ;
288
+ }
289
+
290
+ unsafe impl Send for PatchGuard { }
291
+ unsafe impl Sync for PatchGuard { }
292
+
257
293
#[ cfg( test) ]
258
294
#[ inline( never) ]
259
295
fn tiny ( ) { }
0 commit comments