Giter Site home page Giter Site logo

trampolinehook's Introduction

TrampolineHook

A solution for centralized method redirection.

You can read articles about underlying implementation of this project.

  1. 基于桥的全量方法Hook方案 - 探究苹果主线程检查实现
  2. 基于桥的全量方法 Hook 方案(2) - 全新升级
  3. 基于桥的全量方法 Hook 方案(3)- 开源 TrampolineHook
  4. TrampolineHook - 解决栈污染问题支持变参 Hook

1. Usage

The interface of TrampolineHook is very very simple. Only two steps are required.

  1. Create the global interceptor with your centralized function as follows:
// Suppose you have  function defined as

// C Format
void myInterceptor
{
    // bla bla bla
}

// Create the inteceptor with your interceptor function
THInterceptor *interceptor = [[THInterceptor alloc] initWithRedirectionFunction:(IMP)myInterceptor];
  1. Intercept any function you want no matter what the method signature it is.
// Suppose you want to intercept the call of - [UIView initWithFrame:]
Method m = class_getInstanceMethod([UIView class], @selector(initWithFrame:));
IMP imp = method_getImplementation(m);

// Intercept the imp
THInterceptorResult *result = [interceptor interceptFunction:imp];

// You can check the result.state to find whether the inteception is successfully carried out or not.
result.state == THInterceptStateSuccess

2. How to debug

The debug of interception is not very easy.

Remember always use the result.replacedAddress returned from interceptFunction for breakpoint.

3. Example

There is one typical example associated with this open source project called MainThreadChecker。It is the rewritten version of the implementation in Apple libMainThreadChecker.dylib

It is almost the same as the one of Apple based on my own reverse engineering. No Private APIs used.

The usage of MainThreadChecker is quite easy.

+ (void)load 
{
    [MTInitializer enableMainThreadChecker]
}

4. TODO

  • API Stability.
  • Varadic Argument Interceptor.
  • More examples.
  • Performance Benchmark.

You can view the Project tab to follow the process of this project.

5.Reference

The great works listed below inspired me a lot during the development of TrampolineHook.

6. License

Copyright 2020 @SatanWoo

Checkout License

trampolinehook's People

Contributors

satanwoo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.