Date: Sat, 02 Aug 2025 15:22:30 -0700
On Saturday, 2 August 2025 13:34:03 Pacific Daylight Time connor horman via
Std-Proposals wrote:
> There is a way usually on an OS to determine whether or not a particular
> address is mapped (linux, for example, you can examine `/proc/self/map`).
Actually, no.
It might tell you that accessing the memory won't produce a SIGSEGV (assuming
you access it correctly), but you may still get a SIGBUS if this is a file-
mapped memory and the file is too small. So you also need to have an open file
handle to the file in question or a means to open one (/proc/self/map_files on
Linux) so you can stat(2) it to find out if it is big enough.
In a multithreaded application, any information you get about a pointer may be
stale by the time you use it. And in a multi-process OS, any information you
get about a file may also be stale, unless you have means to force a file not to
shrink -- Linux only supports file sealing on memfds.
Therefore, this is a TOCTOU problem. There's no reliable way to get the
correct information cross-platform and there's no way make it reliable to use.
And this was in no way what the OP was asking for.
Std-Proposals wrote:
> There is a way usually on an OS to determine whether or not a particular
> address is mapped (linux, for example, you can examine `/proc/self/map`).
Actually, no.
It might tell you that accessing the memory won't produce a SIGSEGV (assuming
you access it correctly), but you may still get a SIGBUS if this is a file-
mapped memory and the file is too small. So you also need to have an open file
handle to the file in question or a means to open one (/proc/self/map_files on
Linux) so you can stat(2) it to find out if it is big enough.
In a multithreaded application, any information you get about a pointer may be
stale by the time you use it. And in a multi-process OS, any information you
get about a file may also be stale, unless you have means to force a file not to
shrink -- Linux only supports file sealing on memfds.
Therefore, this is a TOCTOU problem. There's no reliable way to get the
correct information cross-platform and there's no way make it reliable to use.
And this was in no way what the OP was asking for.
-- Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org Principal Engineer - Intel Platform & System Engineering
Received on 2025-08-02 22:22:34