iqiyi / xHook

🔥 A PLT hook library for Android native ELF.
Other
4.07k stars 759 forks source link

请问这里的函数返回值为什么是void*,而非void? #93

Closed Valkierja closed 3 years ago

Valkierja commented 3 years ago
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <test.h>

#define PAGE_START(addr) ((addr) & PAGE_MASK)
#define PAGE_END(addr)   (PAGE_START(addr) + PAGE_SIZE)

void *my_malloc(size_t size)
{
    printf("%zu bytes memory are allocated by libtest.so\n", size);
    return malloc(size);
}

void hook()
{
    char       line[512];
    FILE      *fp;
    uintptr_t  base_addr = 0;
    uintptr_t  addr;

    //find base address of libtest.so
    if(NULL == (fp = fopen("/proc/self/maps", "r"))) return;
    while(fgets(line, sizeof(line), fp))
    {
        if(NULL != strstr(line, "libtest.so") &&
           sscanf(line, "%"PRIxPTR"-%*lx %*4s 00000000", &base_addr) == 1)
            break;
    }
    fclose(fp);
    if(0 == base_addr) return;

    //the absolute address
    addr = base_addr + 0x3f90;

    //add write permission
    mprotect((void *)PAGE_START(addr), PAGE_SIZE, PROT_READ | PROT_WRITE);

    //replace the function address
    *(void **)addr = my_malloc;

    //clear instruction cache
    __builtin___clear_cache((void *)PAGE_START(addr), (void *)PAGE_END(addr));
}

int main()
{
    hook();

    say_hello();
    return 0;
}

节选自(https://github.com/iqiyi/xHook/blob/master/docs/overview/android_plt_hook_overview.zh-CN.md)

函数malloc返回值是void my_malloc return了一个malloc 为什么my_malloc的返回值不是void? 按道理来讲是都可以吧,这里空指针是为了类型安全么 楼主比较菜,不太理解这里,感谢各位大大~

ratsafalig commented 3 years ago

https://www.tutorialspoint.com/c_standard_library/c_function_malloc.htm