C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Translation-unit-local functions that access private class fields

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 28 Apr 2026 09:56:54 -0700
On Sunday, 26 April 2026 05:34:21 Pacific Daylight Time Sebastian Wittmeier via
Std-Proposals wrote:
> void Cube::innocentPrivateFunction() {
> CubeHelper::width = &Cube::width;
> CubeHelper::length = &Cube::length;
> CubeHelper::height = &Cube::height;
> }

While the execution of this is the same as a PLT-less GOT-based call across
libraries, the implementation is not: the pointers above will not reside in
"relro" segments of the binary. That is, they will live in writable memory,
which is less secure. Moreover, they will be laid out in memory near other
variables, which makes it possible to overwrite them via buffer overflows.

Ideally, the pointers would be `const` and would be constinit'ed. The above is
a nice hack, but I wouldn't call it a proper solution until this happens.

And thus, a sufficiently-smart LTO compiler could remove the variables
completely and directly call the target functions.

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

Received on 2026-04-28 16:57:00