detour 0.9.0

A cross-platform function detouring (inline hooking) library for x86, x86-64 and AArch64
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Tests of the public API.
use detour::{Error, GenericDetour, RawDetour, Result, static_detour};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

mod common;

/// A runtime zero, which distinguishes otherwise identical functions (which
/// LLVM would merge, causing concurrent tests to hook the same function).
macro_rules! unique {
  () => {
    std::hint::black_box(0) * line!() as i32
  };
}

type FnAdd = extern "C" fn(i32, i32) -> i32;

#[inline(never)]
extern "C" fn sub_detour(x: i32, y: i32) -> i32 {
  std::hint::black_box(x) - y
}

mod raw {
  use super::*;

  #[test]
  fn basic() -> Result<()> {
    #[inline(never)]
    extern "C" fn add(x: i32, y: i32) -> i32 {
      std::hint::black_box(x) + y + unique!()
    }

    // SAFETY: The functions share the same signature.
    let hook = unsafe { RawDetour::new(add as *const (), sub_detour as *const ())? };

    assert_eq!(add(10, 5), 15);
    assert!(!hook.is_enabled());

    // SAFETY: No other thread is executing `add`.
    unsafe { hook.enable()? };
    {
      assert!(hook.is_enabled());

      // The `add` function is hooked, but can be called using the trampoline
      // SAFETY: The trampoline shares the target's signature.
      let trampoline: FnAdd = unsafe { std::mem::transmute(hook.trampoline()) };

      assert_eq!(trampoline(10, 5), 15);
      assert_eq!(add(10, 5), 5);
    }
    // SAFETY: No other thread is executing `add`.
    unsafe { hook.disable()? };

    assert!(!hook.is_enabled());
    assert_eq!(add(10, 5), 15);
    Ok(())
  }

  #[test]
  fn reference_arguments() -> Result<()> {
    // Functions with higher-ranked lifetimes are only supported by raw detours
    #[inline(never)]
    fn length(value: &str) -> usize {
      std::hint::black_box(value).len()
    }

    fn zero(_: &str) -> usize {
      0
    }

    // SAFETY: The functions share the same signature.
    let hook = unsafe { RawDetour::new(length as *const (), zero as *const ())? };
    // SAFETY: No other thread is executing `length`.
    unsafe { hook.enable()? };
    assert_eq!(length("detour"), 0);

    // SAFETY: The trampoline shares the target's signature.
    let original: fn(&str) -> usize = unsafe { std::mem::transmute(hook.trampoline()) };
    assert_eq!(original("detour"), 6);
    Ok(())
  }

  #[test]
  fn drop_restores_target() -> Result<()> {
    #[inline(never)]
    extern "C" fn mul(x: i32, y: i32) -> i32 {
      std::hint::black_box(x) * y
    }

    {
      // SAFETY: The functions share the same signature.
      let hook = unsafe { RawDetour::new(mul as *const (), sub_detour as *const ())? };
      // SAFETY: No other thread is executing `mul`.
      unsafe { hook.enable()? };
      assert_eq!(mul(3, 3), 0);
    }

    assert_eq!(mul(3, 3), 9);
    Ok(())
  }

  #[cfg(target_pointer_width = "64")]
  #[test]
  fn distant_detour_uses_relay() -> Result<()> {
    #[inline(never)]
    extern "C" fn ret5() -> i32 {
      std::hint::black_box(5)
    }

    // Beyond ±2 GiB (x86-64), or ±128 MiB (AArch64)
    let distance = if cfg!(target_arch = "aarch64") {
      1 << 28
    } else {
      1 << 32
    };
    let far = common::far_ret10(ret5 as *const () as usize, distance);

    // SAFETY: Both functions share the same signature.
    let hook = unsafe { RawDetour::new(ret5 as *const (), far as *const ())? };
    // SAFETY: No other thread is executing `ret5`.
    unsafe { hook.enable()? };
    assert_eq!(ret5(), 10);

    // SAFETY: The trampoline shares the target's signature.
    let original: extern "C" fn() -> i32 = unsafe { std::mem::transmute(hook.trampoline()) };
    assert_eq!(original(), 5);

    // SAFETY: No other thread is executing `ret5`.
    unsafe { hook.disable()? };
    assert_eq!(ret5(), 5);
    Ok(())
  }

  #[test]
  fn many_detours() -> Result<()> {
    macro_rules! targets {
      ($($name:ident = $value:literal),*) => {{
        $(
          #[inline(never)]
          extern "C" fn $name(x: i32, y: i32) -> i32 {
            std::hint::black_box(x) + y + $value
          }
        )*
        [$($name as FnAdd),*]
      }};
    }

    let targets = targets!(
      a = 1,
      b = 2,
      c = 3,
      d = 4,
      e = 5,
      f = 6,
      g = 7,
      h = 8,
      i = 9,
      j = 10,
      k = 11,
      l = 12,
      m = 13,
      n = 14,
      o = 15,
      p = 16
    );
    let expected: Vec<_> = targets.iter().map(|target| target(1, 1)).collect();

    let hooks = targets
      .iter()
      // SAFETY: The functions share the same signature.
      .map(|target| unsafe { GenericDetour::<FnAdd>::new(*target, sub_detour) })
      .collect::<Result<Vec<_>>>()?;

    for hook in &hooks {
      // SAFETY: No other thread is executing the targets.
      unsafe { hook.enable()? };
    }

    for (index, (target, hook)) in targets.iter().zip(&hooks).enumerate() {
      assert_eq!(target(1, 1), 0);
      assert_eq!(hook.call(1, 1), expected[index]);
    }

    drop(hooks);
    let restored: Vec<_> = targets.iter().map(|target| target(1, 1)).collect();
    assert_eq!(restored, expected);
    Ok(())
  }
}

mod generic {
  use super::*;

  #[test]
  fn toggling_is_idempotent() -> Result<()> {
    #[inline(never)]
    extern "C" fn add(x: i32, y: i32) -> i32 {
      std::hint::black_box(x) + y + unique!()
    }

    // SAFETY: The functions share the same signature.
    let hook = unsafe { GenericDetour::<FnAdd>::new(add, sub_detour)? };
    // SAFETY: No other thread is executing `add`.
    unsafe {
      hook.enable()?;
      hook.enable()?;
      assert_eq!(add(3, 1), 2);
      hook.disable()?;
      hook.disable()?;
    }
    assert_eq!(add(3, 1), 4);
    Ok(())
  }

  #[test]
  fn memory_is_reused() -> Result<()> {
    #[inline(never)]
    extern "C" fn add(x: i32, y: i32) -> i32 {
      std::hint::black_box(x) + y + unique!()
    }

    // Trampolines are released upon drop, so this must not exhaust memory
    for _ in 0..10_000 {
      // SAFETY: The functions share the same signature.
      let hook = unsafe { GenericDetour::<FnAdd>::new(add, sub_detour)? };
      // SAFETY: No other thread is executing `add`.
      unsafe { hook.enable()? };
      assert_eq!(add(3, 1), 2);
    }
    assert_eq!(add(3, 1), 4);
    Ok(())
  }

  #[test]
  fn concurrent_creation() {
    if common::is_translated() {
      eprintln!("skipped: concurrent code modification is unreliable under Rosetta 2");
      return;
    }

    macro_rules! targets {
      ($($name:ident = $value:literal),*) => {{
        $(
          #[inline(never)]
          extern "C" fn $name(x: i32, y: i32) -> i32 {
            std::hint::black_box(x) * y + $value
          }
        )*
        [$($name as FnAdd),*]
      }};
    }

    let targets = targets!(
      a = 100,
      b = 200,
      c = 300,
      d = 400,
      e = 500,
      f = 600,
      g = 700,
      h = 800
    );
    let threads: Vec<_> = targets
      .into_iter()
      .map(|target| {
        std::thread::spawn(move || -> Result<()> {
          for _ in 0..50 {
            // SAFETY: Each thread detours a distinct target.
            let hook = unsafe { GenericDetour::<FnAdd>::new(target, sub_detour)? };
            // SAFETY: See above.
            unsafe { hook.enable()? };
            assert_eq!(target(5, 2), 3);
            assert_eq!(hook.call(5, 2), hook.call(0, 0) + 10);
          }
          Ok(())
        })
      })
      .collect();

    for thread in threads {
      thread
        .join()
        .expect("thread panicked")
        .expect("detour failed");
    }
  }

  #[test]
  fn basic() -> Result<()> {
    #[inline(never)]
    extern "C" fn add(x: i32, y: i32) -> i32 {
      std::hint::black_box(x) + y + unique!()
    }

    // SAFETY: The functions share the same signature.
    let hook = unsafe { GenericDetour::<FnAdd>::new(add, sub_detour)? };

    assert_eq!(add(10, 5), 15);
    assert_eq!(hook.call(10, 5), 15);
    // SAFETY: No other thread is executing `add`.
    unsafe { hook.enable()? };
    {
      assert_eq!(hook.call(10, 5), 15);
      assert_eq!(add(10, 5), 5);
    }
    // SAFETY: No other thread is executing `add`.
    unsafe { hook.disable()? };
    assert_eq!(hook.call(10, 5), 15);
    assert_eq!(add(10, 5), 15);
    Ok(())
  }

  #[test]
  fn unsafe_target_with_safe_detour() -> Result<()> {
    #[inline(never)]
    unsafe extern "C" fn value() -> u64 {
      std::hint::black_box(1)
    }

    extern "C" fn detour() -> u64 {
      2
    }

    // SAFETY: The functions share the same signature.
    let hook = unsafe {
      GenericDetour::<unsafe extern "C" fn() -> u64>::new(value, detour as extern "C" fn() -> u64)?
    };
    // SAFETY: No other thread is executing `value`.
    unsafe {
      hook.enable()?;
      assert_eq!(value(), 2);
      assert_eq!(hook.call(), 1);
    }
    Ok(())
  }

  #[test]
  fn is_send_and_sync() {
    fn assert<T: Send + Sync>() {}
    assert::<GenericDetour<fn()>>();
    assert::<RawDetour>();
    assert::<detour::StaticDetour<fn()>>();
  }
}

mod statik {
  use super::*;

  #[inline(never)]
  unsafe extern "C" fn add(x: i32, y: i32) -> i32 {
    std::hint::black_box(x) + y + unique!()
  }

  #[inline(never)]
  fn counter(value: u64) -> u64 {
    std::hint::black_box(value)
  }

  #[inline(never)]
  extern "system" fn negate(value: i64, scale: i64) -> i64 {
    -std::hint::black_box(value) * scale
  }

  static_detour! {
    #[doc = "Test with attributes"]
    pub static DetourAdd: unsafe extern "C" fn(i32, i32) -> i32;
    static DetourCounter: fn(u64) -> u64;
    pub(crate) static DetourNegate: extern "system" fn(i64, i64,) -> i64;
    static DetourUninitialized: fn();
  }

  #[test]
  fn basic() -> Result<()> {
    // SAFETY: No other thread is executing `add`.
    unsafe {
      DetourAdd.initialize(add, |x, y| x - y)?;

      assert_eq!(add(10, 5), 15);
      assert!(!DetourAdd.is_enabled());

      DetourAdd.enable()?;
      {
        assert!(DetourAdd.is_enabled());
        assert_eq!(DetourAdd.call(10, 5), 15);
        assert_eq!(add(10, 5), 5);
      }
      DetourAdd.disable()?;

      assert!(!DetourAdd.is_enabled());
      assert_eq!(DetourAdd.call(10, 5), 15);
      assert_eq!(add(10, 5), 15);

      assert!(matches!(
        DetourAdd.initialize(add, |x, y| x * y),
        Err(Error::AlreadyInitialized)
      ));
    }
    Ok(())
  }

  #[test]
  fn stateful_closures() -> Result<()> {
    let calls = Arc::new(AtomicUsize::new(0));
    let counted = calls.clone();

    // SAFETY: No other thread is executing `counter`.
    unsafe {
      DetourCounter
        .initialize(counter, move |value| {
          counted.fetch_add(1, Ordering::SeqCst);
          // The original can be called from within the detour
          DetourCounter.call(value) * 2
        })?
        .enable()?;
    }

    assert_eq!(counter(4), 8);
    assert_eq!(counter(5), 10);
    assert_eq!(calls.load(Ordering::SeqCst), 2);

    DetourCounter.set_detour(|value| value + 1);
    assert_eq!(counter(4), 5);
    assert_eq!(calls.load(Ordering::SeqCst), 2);

    // SAFETY: No other thread is executing `counter`.
    unsafe { DetourCounter.disable()? };
    assert_eq!(counter(4), 4);
    Ok(())
  }

  #[test]
  fn system_abi_and_trailing_comma() -> Result<()> {
    // SAFETY: No other thread is executing `negate`.
    unsafe { DetourNegate.initialize(negate, |x, y| x + y)?.enable()? };
    assert_eq!(negate(2, 3), 5);
    assert_eq!(DetourNegate.call(2, 3), -6);
    Ok(())
  }

  #[test]
  fn uninitialized() {
    assert!(!DetourUninitialized.is_enabled());
    // SAFETY: The detour is not initialized.
    let result = unsafe { DetourUninitialized.enable() };
    assert!(matches!(result, Err(Error::NotInitialized)));
    assert!(std::panic::catch_unwind(|| DetourUninitialized.call()).is_err());
  }
}

/// Toggles a detour whilst another thread continuously calls the target.
///
/// A single AArch64 instruction is replaced atomically, so this is safe; on
/// x86 the patch is not guaranteed to be atomic.
#[cfg(target_arch = "aarch64")]
#[test]
fn toggle_whilst_executing() -> Result<()> {
  use std::sync::atomic::AtomicBool;

  #[inline(never)]
  extern "C" fn ret1() -> i32 {
    std::hint::black_box(1)
  }

  extern "C" fn ret2() -> i32 {
    2
  }

  // SAFETY: The functions share the same signature.
  let hook = unsafe { GenericDetour::<extern "C" fn() -> i32>::new(ret1, ret2)? };
  let done = Arc::new(AtomicBool::new(false));

  let calls = Arc::new(AtomicUsize::new(0));

  let worker = std::thread::spawn({
    let (done, calls) = (done.clone(), calls.clone());
    move || {
      let mut results = [0usize; 3];
      while !done.load(Ordering::Relaxed) {
        results[ret1() as usize] += 1;
        calls.fetch_add(1, Ordering::Relaxed);
      }
      results
    }
  });

  // Ensure the worker is executing the target before it is patched
  while calls.load(Ordering::Relaxed) == 0 {
    std::thread::yield_now();
  }

  for _ in 0..200 {
    // SAFETY: Replacing a single instruction is atomic on AArch64.
    unsafe {
      hook.enable()?;
      hook.disable()?;
    }
  }

  done.store(true, Ordering::Relaxed);
  let results = worker.join().expect("worker thread panicked");
  // Every call must return either the original, or the detoured, result
  assert_eq!(results[0], 0);
  assert!(results[1] + results[2] > 0);
  Ok(())
}