C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 14 Apr 2026 16:11:24 -0700
On Tuesday, 14 April 2026 15:00:27 Pacific Daylight Time Frederick Virchanza
Gotham via Std-Proposals wrote:
> 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.

It might be your idea. But I am saying this is the first thing someone will
ask.

https://www.google.com/search?q=how+to+intercept+open+on+linux

The AI answer shows the LD_PRELOAD trick which gets the arguments and can
decide to pass them on or not. The first link to StackOverflow also does.

> 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 );

Much simpler to just allow (or even require) the interceptor function to have
the same signature as the intercepted function. This has the added benefit that
no extra state-saving is required, because the compiler knows *exactly* what
parameters need saving and where they exist.

> 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.

In your case, you'd probably read that from the saved buffer, not from the
registers.

> 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.

ChatGPT is not good for coding / programming. I've found that GitHub Copilot
is much better at it, with the Claude model. Google Gemini appears to be good
too.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-04-14 23:11:32