C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Monitor recursion by checking the frame pointer

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 17 Apr 2023 21:42:31 +0100
On Mon, Apr 17, 2023 at 8:46 PM Thiago Macieira wrote:
>
> On Monday, 17 April 2023 07:50:10 PDT Frederick Virchanza Gotham wrote:
>>
> > Anyway, the use of "std::frame_pointer" inside a function would be an
> > indicator to the compiler to actually use the frame pointer. If it
> > really doesn't want to use the frame pointer, it can return nullptr.
>
> Then the feature is as good as useless. A compliant implementation can simply
> return nullptr everywhere.
>
> And then how does this work for your needs?


On second thought you're right.

For this to make sense, any function which invokes 'std::frame_pointer' must:
(a) Be a real function (i.e. not inline code)
(b) Upon being entered, immediately set up the stack frame with either
"enter" or "push rbp; mov rbp,rsp"

If the implementation is unwilling or unable to do both A and B, for
example if it was compiled with "-fomit-frame-pointer" or if the
function was marked "__attribute__((always_inline))", then the
compiler shall immediately terminate compilation and issue a
diagnostic.

I was also thinking..... if we were to have access to the return
address, then we could query whether the return address is within a
given function, for example:

void Monkey(void)
{
    // check whether we were invoked from 'Func'
    if ( std::code_address_is_within_function(Func,
std::return_address) ) return;

    // Do something else
}

void Func(void)
{
    Monkey();
}

int main(void)
{
    Func();
}

The linker would need to know the address of each function and also
the size of the function's machine code.

Received on 2023-04-17 20:42:44