C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 07 Jan 2024 19:22:01 -0300
On Saturday, 6 January 2024 09:05:04 -03 Frederick Virchanza Gotham via Std-
Proposals wrote:
> If there were any implementations of C++ in existence today that
> passed the 'this' pointer in a separate register, I figured it would
> be one of the four implementations that stored the address of the
> return value in a separate register. I haven't looked into it.

There are, it's called Visual Studio for 32-bit x86. It passes the this
pointer in the ECX register, while the regular arguments go on the stack as
usual. It's called the __thiscall calling convention, as opposed to __cdecl or
the other ones.

Given that MSVC 32-bit x86 still only uses 4-byte-aligned stacks, it would be
possible to add a thunk that pops the return address, then the this pointer,
loads the latter into ECX, pushes the address back to the stack, then jumps to
the function entry point. It would not be possible with GCC because that has
moved to 16-byte-aligned stacks.

Provided that the Controlflow Enforcement Technology (CET) shadow stack and
branch tracking do permit the above. I think there's no problem because we are
indeed returning to the address that was the call point. But I'm not going to
investigate, even to see if Windows has enabled CET on 32-bit.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2024-01-07 22:22:04