打桩@[TOC](这里写自定义目
 /*• linux_stub.c
• 
• Created on: 2020-5-28
• Author: xxxx
 */#include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <memory.h>
 #include <errno.h>
 #include <limits.h>
 #include <sys/mman.h>
 #include “linux_stub.h”
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <memory.h>
 #include <errno.h>
 #include <limits.h>
 #include <sys/mman.h>
 #include “linux_stub.h”// 绝对跳转指令长度
 #define CODESIZE 5U
 #define STUB_MAX_BACKUP_NUM 128typedef struct tag_func_stub
 {
 void *fn;
 unsigned char code_buf[CODESIZE];
 }func_stub;static long pagesize = -1;
 func_stub astStubBackups[STUB_MAX_BACKUP_NUM] = {{NULL, {0}}};
 static inline void pageof(const void p)
 {
 long pagesize;
 pagesize = sysconf(_SC_PAGE_SIZE);
 return (void *)((unsigned long)p & ~(pagesize - 1));
 }/*****************************************************************************
 Function : 打桩接口
 Description : 参数1:函数容器,记录打桩前函数信息,便于后期清桩
 参数2:被打桩接口,可以是c函数也可以是c++函数,形式比如 (void*)func或者GetClassFnAddress(&ClassName::func)
 参数3:打桩接管的函数,可以是c函数也可以是c++函数,形式比如:(void*)Stubfunc或者GetClassFnAddress(&StubClassName::Stubfunc)
 Note :
 author :
 *****************************************************************************/
 void stub_set(func_stub *pstub, void *fn, void *fn_stub)
 {
 pstub->fn = fn;
 memcpy(pstub->code_buf, fn, CODESIZE);if (-1 == mprotect(pageof(fn), pagesize * 2, PROT_READ | PROT_WRITE | PROT_EXEC))
{
    perror("mprotect to w+r+x faild");
    exit(errno);
}

*(unsigned char *)fn = (unsigned char)0xE9;
*(unsigned int *)((unsigned char *)fn + 1) = (unsigned char *)fn_stub - (unsigned char *)fn - CODESIZE;

if (-1 == mprotect(pageof(fn), pagesize * 2, PROT_READ | PROT_EXEC))
{
    perror("mprotect to r+x failed");
    exit(errno);
}

return;}
/*****************************************************************************
 Function : 清除某个函数的打桩
 Description : 参数1:打桩接口的参数1
 Note :
 author :
 *****************************************************************************/
 void stub_reset(func_stub *pstub)
 {
 if (NULL == pstub->fn)
 {
 return;
 }if (-1 == mprotect(pageof(pstub->fn), pagesize * 2, PROT_READ | PROT_WRITE | PROT_EXEC))
{
    perror("mprotect to w+r+x faild");
    exit(errno);
}

memcpy(pstub->fn, pstub->code_buf, CODESIZE);

if (-1 == mprotect(pageof(pstub->fn), pagesize * 2, PROT_READ | PROT_EXEC))
{
    perror("mprotect to r+x failed");
    exit(errno);
}
memset(pstub, 0, sizeof(func_stub)); 
return;}
/*****************************************************************************
 Function : 初始化
 Description : 主要是获取页面大小
 Note :
 author :
 *****************************************************************************/
 int stub_init(void)
 {
 int ret;pagesize = sysconf(_SC_PAGE_SIZE);

ret = 0;
if (pagesize < 0)
{
    perror("get system _SC_PAGE_SIZE configure failed");
    ret = -1;
}       
return ret;}
int SetFuncStub(CommonFunc fnOldFunc, CommonFunc fnNewFunc)
 {
 int i, iFree = -1;for (i = 0; i < STUB_MAX_BACKUP_NUM; i++)
{
    if (astStubBackups[i].fn == NULL && iFree == -1)
    {
        iFree = i;
    }
    else if (astStubBackups[i].fn == fnOldFunc)
    {
        // 已经备份过了,返回成功
        return 0;
    }
}

if (iFree != -1)
{
    if (pagesize < 0)
    {
        if (stub_init() < 0)
        {
            return -1;
        }
    }
    stub_set(&(astStubBackups[iFree]), fnOldFunc, fnNewFunc);
    return 0;
}   
return -1;}
void ClearAllFuncStubs(void)
 {
 int i;for (i = 0; i < STUB_MAX_BACKUP_NUM; i++)
{
    if (astStubBackups[i].fn != NULL)
    {
        stub_reset(&astStubBackups[i]);
        astStubBackups[i].fn = NULL;
        memset(astStubBackups[i].code_buf, 0, CODESIZE);
    }
}}
int SetFuncStubNone(CommonFunc fnOldFunc)
 {
 int i;for (i = 0; i < STUB_MAX_BACKUP_NUM; i++)
{
    if (astStubBackups[i].fn == fnOldFunc)
    {
        stub_reset(&astStubBackups[i]);
        astStubBackups[i].fn = NULL;
        memset(astStubBackups[i].code_buf, 0, CODESIZE);
        return 0;
    }
}
return -1;}
/*
• linux_stub.h
• 
• Created on: 2020-5-28
• Author: xxxx
 */#ifndef LINUX_STUB_H
 #define LINUX_STUB_H#include <stdint.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <errno.h>typedef void (*CommonFunc)(void);
 void ClearAllFuncStubs(void);
 int SetFuncStub(CommonFunc fnOldFunc, CommonFunc fnNewFunc);
 int SetFuncStubNone(CommonFunc fnOldFunc);/设置桩函数,oldFunc为原函数,newFunc为桩函数/
 #define SET_FUNC_STUB(oldFunc,newFunc) SetFuncStub((CommonFunc)oldFunc, (CommonFunc)newFunc)
 /清除所有函数的桩函数/
 #define CLEAR_ALL_FUNC_STUBS ClearAllFuncStubs
 /清除指定函数桩函数/
 #define SET_FUNC_STUB_NONE(oldFunc) SetFuncStubNone((CommonFunc)oldFunc)
 #endif
 录标题)