C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Interceptor Function (preserve stack and all registers)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 14 Apr 2026 23:00:27 +0100
On Tue, Apr 14, 2026 at 6:36 PM Thiago Macieira wrote:
>
> The first thing anyone is going to ask is to get access to the arguments of the
> call, so they can be logged.
>
> The second is going to be to change the arguments.


The main idea of an 'interceptor' is that it's agnostic to the target
function's parameters and return type.

So if you're asking what the arguments are, well they can be provided
if you specify the type, for example:

    std::get_arg<double>( 1 );

The above will get the value of xmm2 on x86_64. Of course it won't be
perfect if the stack is involved, because if you have many arguments
pushed onto the stack, and if you don't know how big the previous
stack argument was (e.g. 8 bytes Vs 16 bytes), then you won't know
where the Nth argument is on the stack.

But yeah you could log it something like:

    cout << get_arg<uintptr_t>( 0 ) << get_arg<uintptr_t>( 1 ); <<
get_arg<uintptr_t>( 2 ); << endl;


> > Some x86_64 processors only have SSE, some have AVX-256, and some have
> > AVX-512, so at runtime I need to execute the CPUID instruction to find out.
>
> Or you can use IFUNC and make a call instead. Push the target address onto the
> stack and JMP to the saver-redirector-restorer.



Not familiar with this . . . let me chat to ChatGPT about it for a
while and I'll get back to you.



> > kmovq %k0, 520(%rsp)
> > kmovq %k1, 528(%rsp)
> > kmovq %k2, 536(%rsp)
> > kmovq %k3, 544(%rsp)
> > kmovq %k4, 552(%rsp)
> > kmovq %k5, 560(%rsp)
> > kmovq %k6, 568(%rsp)
> > kmovq %k7, 576(%rsp)
> > jmp .Lpush_done\@
>
> Why are you saving the k regs? They are all scratch registers, aren't they?


ChatGPT told me they need to be preserved but I'll look into it to confirm.

Received on 2026-04-14 22:00:42