Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
replace a C function at runtime , for unit test。
#include <stdint.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include "patch_elf.h"
using namespace std;
int print_op(void * addr,int leng){
unsigned char * op=(unsigned char *) addr;
cout<<endl<<"addr:"<<addr<<" code:"<<endl;
for(int i=0;i<leng;++i){
cout<<"0x"<<hex<<(unsigned int)op[i]<<" ";
return 0;
int patch_func(void * original,void * mock){
<<endl<<__func__<<" ,i am going to patch "<<original<<" to "<<mock
//rax 用于保存函数调用的返回值,所以可以占用
//4010e1: b8 20 0c 40 00 mov $0x400c20,%eax
//4010e6: ff e0 jmpq *%rax
uint32_t addr=(uint32_t)(uint64_t)mock;
const int code_len=7;
char inject_code[code_len]={0xb8,0x00,0x00,0x00,0x00,0xff,0xe0};
const size_t length = sysconf(_SC_PAGESIZE);
void * code_addr= (void*) ( ( (long) original/length)*length );
int ret=mprotect(code_addr, length, PROT_READ | PROT_WRITE | PROT_EXEC);
if ( 0!=ret ) {
cerr<<"mprotect failed! ret="<<ret<<endl;
memcpy( original,inject_code,code_len);
ret=mprotect(code_addr, length, PROT_READ | PROT_EXEC);
if ( 0!=ret ) {
cerr<<"mprotect failed! ret="<<ret<<endl;
// <<endl<<endl;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.