C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow conversion of memfunc pointers to func pointers

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Tue, 9 Jan 2024 00:34:00 +0100
Why is it important to dethunk or to recognize as a thunk?   For comparison/function object or address identity?   For speed?   -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mo 08.01.2024 21:43 Betreff:Re: [std-proposals] Allow conversion of memfunc pointers to func pointers An:std-proposals_at_[hidden]; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; On Monday, January 8, 2024, Thiago Macieira wrote: But again pointer-equality problems because the entry point of the function you de-virtualised may be that of the thunk instead of the actual function.       So maybe we could use 'std::dethunk' to de-thunk the function pointer.      q = std::dethunk( p );     if ( q == p )  cout << "It wasn't a thunk\n";     else cout << "Actual function address: 0x" << hex << (void*)q << endl;  So then the question is how do we identify thunks at runtime? Well we could binary-search for the address in a global constexpr array of thunk addresses. Or alternatively we could precede every thunk with 8 identifying bytes something like:      't' 'h' 'u' 'n' 'k' '\0' '\0' '\0'  Or maybe just identify the machine code of a thunk generated from the following assembler:      add rdi, 8     jmp ActualFunction  If it adds a constant to RDI and immediately jumps to a constant address then assume it to be a thunk (although this could be problematic if a non-thunk function which begins with a loop (and is misidentified as a thunk)).  I would really like to see a function to retrieve the function pointer from the vtable:      class MyClass {  . . . };     MyClass myobj;     void (*p)(void) = std::devirtualise( &MyClass::SomeMethod, &myobj );  so then you could chain them:      void (*p)(void) = std::dethunk( std::devirtualise( &MyClass::SomeMethod, &myobj ) );  On computers that don't use thunks, "std::dethunk" shall be optimised away to a nop.    -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-01-08 23:34:03