RFR: 8370254: Add VM_MEMORY_JAVA mmap tag to MacOS mmap calls [v15]

Jaikiran Pai jpai at openjdk.org
Sat Nov 15 11:39:06 UTC 2025


On Sat, 15 Nov 2025 10:29:03 GMT, Thomas Stuefe <stuefe at openjdk.org> wrote:

>> src/hotspot/os/bsd/os_bsd.hpp line 43:
>> 
>>> 41: // Bsd_OS defines the interface to Bsd operating systems
>>> 42: 
>>> 43: static constexpr int bsd_mmap_fd =
>> 
>> I don't have familiarity in this area, but looking at the man page of `mmap` on my local macos, it states this:
>> 
>> 
>> void *
>> mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
>> ...
>> MAP_ANON          Map anonymous memory not associated with any specific file.  The offset argument is ignored.  Mac OS X specific: the file descriptor used for
>>                    creating MAP_ANON regions can be used to pass some Mach VM flags, and can be specified as -1 if no such flags are associated with the region.
>>                    Mach VM flags are defined in <mach/vm_statistics.h> and the ones that currently apply to mmap are:
>> 
>>                    VM_FLAGS_PURGABLE   to create Mach purgable (i.e. volatile) memory.
>> 
>>                    VM_MAKE_TAG(tag)    to associate an 8-bit tag with the region.
>>                    <mach/vm_statistics.h> defines some preset tags (with a VM_MEMORY_ prefix).  Users are encouraged to use tags between 240 and 255.  Tags are used
>>                    by tools such as vmmap(1) to help identify specific memory regions.
>> 
>> 
>> So this special value handling of `fd` value is only applicable if `MAP_ANON` is part of the `flags`.
>> Given this, should the name of constexpr be a bit more specific and the call sites, where this gets used, verify/assert that the flags indeed contains the `MAP_ANON` flag?
>> Plus, this is very macos specific, calling it `bsd_...` feels much more generic. Maybe we should consider naming it `macos_mmap_anon_fd`?
>
> @jaikiran If `MAP_ANON` is not set, a file descriptor will be given. Contract for MAP_ANON (or MAP_ANONYMOUS on Linux).
> 
> About the bsd.. naming:
> 
> I think this is okay. The source split between xxBSD and Mac is long overdue. I think there is (was?) someone from the FreeBSD foundation working on it. 
> 
> That said, I think this should not be a global constant but live in the os::BSD namespace/class.

Hello Thomas, 

> If MAP_ANON is not set, a file descriptor will be given. Contract for MAP_ANON (or MAP_ANONYMOUS on Linux).

I was thinking about call sites like this https://github.com/openjdk/jdk/pull/27868/files#diff-1f93205c2e57bee432f8fb7a0725ba1dfdbe5b901ac63010ea0b43922e34ac12R1692 :

char* addr = (char*)::mmap(requested_addr, bytes, PROT_NONE, flags, bsd_mmap_fd, 0);

where the `flags` may be defined a few lines away and it may not be easy to guarantee/spot that those `flags` have `MAP_ANON`. Using this new `bsd_mmap_fd` in such places without additional checks on `flags`, I suspect, might lead to subtle issues (if for example, those flags are updated in future)? I don't have any prior knowledge of the code that's being updated here, so my concern may not be practical after all.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/27868#discussion_r2529831333


More information about the hotspot-dev mailing list