C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A drift for c++ decorators;

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 28 Oct 2024 09:49:58 +0000
On Sun, Oct 27, 2024 at 11:01 PM Thiago Macieira wrote:
>
> On Sunday 27 October 2024 15:41:26 Pacific Daylight Time Frederick Virchanza
> Gotham via Std-Proposals wrote:
> > Next let's say we have a variable called 'obj' of type 'MyClass', and
> > we want to tweak this object so that every invocation of any of its
> > methods gets intercepted (we want to lock a mutex before the call, and
> > unlock the mutex after the call). Well. . . here's what you could do:
> > (1) Copy the MyClass VTable
> > (2) Edit the copied VTable so that each function pointer is replaced
> > with a pointer to an interceptor
> > (3) Write the machine code for each interceptor into a buffer of
> > executable memory
> > (4) Change the VTable pointer inside the object 'obj'
>
> How about using reflection?


I'd appreciate an example -- even theoretical psuedocode.

By the way my previous GodBolt code writes machine code into a buffer
of executable memory . . . I was thinking I would be able to do away
with the allocating (and permission setting) of memory if I were to
write just one function something like:

Func:
    call Increment
    call Increment
    call Increment
    call Increment
    call Increment
    [ rest of function goes here ]

So the first interceptor's address will be "&Func", and the second
one's address will be "&Func + 8" and the third will be "&Func + 16".
And so the when the rest of the function code is reached, the value of
the incremented thread-local variable is checked to determine which
method is being intercepted. This means we don't have to change the
permissions on a page of dynamically-allocated memory.

Received on 2024-10-28 09:50:07