kubo / plthook

Hook function calls by replacing PLT(Procedure Linkage Table) entries.
762 stars 156 forks source link

Conversion from function pointer to void* isn't portable #8

Closed othias closed 5 years ago

othias commented 6 years ago

Hi :)

Thank you for your library, one minor nitpick though: the plthook_replace function takes the function pointer as a void*, and that conversion isn't guaranteed to work per C11 standard, 6.3.2.3 §1 :

"A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer."

So the conversion from/to void* is only guaranteed to work for object pointers, it says nothing about function pointers. However, (C11 standard, 6.3.2.3 §8) :

"A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer."

So an easy fix could be to change void to void ()(void), which could be typedef'd to something like plthook_func or similar.

Thanks!

kubo commented 5 years ago

Thanks for pointing it. I confirmed what you quoted in the C11 final draft.

However I don't change the pointer type. That's because: (1) plthook itself isn't portable. It works only on limited platforms. So I don't care whether the conversion is portable or not on unsupported platforms. (2) This tool is based on the technique which isn't guaranteed to work; changing PLT entries in a running process. So I don't care whether the conversion is guaranteed or not as long as it works fine. It is small issue compared with the tool's basis, (3) Other tools, such as Microsoft Detours, use void *.

Well, I'll change my thought when a real-world compiler cannot convert void* to a pointer to a function or vise versa.