Skip to content

8319589: Attach from root to a user java process not supported in Mac #25824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sercher
Copy link
Contributor

@sercher sercher commented Jun 16, 2025

Hi all,

I would like to propose a fix for JDK-8319589. This will allow jcmd and jps running as root to get the complete list of JVMs running by all users, and to attach from root to non-root JVMs. Previously, JDK-8197387 introduced the same approach on Linux.

This change affects macOS, that uses "secure" per-user temporary directories. It only affects JVMs running as root, the behavior in non-privileged JVMs remains unchanged.

Jcmd and jps rely on LocalVmManager to get the initial list of the local VMs. The LocalVmManager uses sun.jvmstat.PlatformSupport to get the list of temp directories, where it searches for user's PerfData directory such as "hsperfdata_<username>". In macosx the temp directories are per-user, the temp path is returned by confstr(_CS_DARWIN_USER_TEMP_DIR). The per-user directories are mode 700 and so they are read-protected from non-privileged users and can be accessed by the owner and the root.

Both jps and jcmd (HotSpotAttachProvider) create MonitoredVm objects, that have PerfDataBuffer that performs attachment to the target. Only the attachable VMs are listed in jcmd output.

The proposed patch changes the list of directories returned by the PlatformSupport#getTemporaryDirectories() in VMs running as root. The list is later used in VirtualMachineImpl (jdk.attach). It changes also the way mmap_attach_shared() searches for hsperfdata_<username>/<pid> files to map the shared memory. Mmap_attach_shared() and VirtualMachineImpl (via PlatformSupport) list the content of /var/folders, where the temp directories are located, more specificly the temp directories are /var/folders/<BUCKET>/<ENCODED_UUID_UID>/T as hinted in [1]. The full list is returned by newly added PlatformSupportImpl#getTemporaryDirectories().

The attaching client's VirtualMachineImpl needs the target process's temp directory to find .java<pid> and create .attach<pid> files. It uses the list returned by PlatformSupportImpl#getTemporaryDirectories() and the ProcessHandle of the target process to search for user's PerfData directory, e.g. hsperfdata_<username>, which is in the target process's temp directory, exactly where it expects to see the .java<pid> in return on sending SIGQUIT to the target VM.

Mmap_attach_shared() traverses the /var/folders in get_user_tmp_dir() and looks for a hsperfdata_<username> folder. If that folder is found in /var/folders/*/*/T, that means the temp folder corresponds to the <username> and to the JVM being attached to.

The patch is tested against the range of macOS versions, the earliest version tested is 10.13 High Sierra (as 11u backport), the latest version tested is 15.5 Sequoia.

Testing:

  • jtreg hotspot/jdk tier1 passed
  • jtreg tier2, tier3 no regresstions
  • gtest:all passed

[1] https://github.com/apple-oss-distributions/Libc/blob/Libc-997.90.3/darwin/_dirhelper.c#L210


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8319589: Attach from root to a user java process not supported in Mac (Enhancement - P3)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25824/head:pull/25824
$ git checkout pull/25824

Update a local copy of the PR:
$ git checkout pull/25824
$ git pull https://git.openjdk.org/jdk.git pull/25824/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25824

View PR using the GUI difftool:
$ git pr show -t 25824

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25824.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 16, 2025

👋 Welcome back schernyshev! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jun 16, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 16, 2025
@openjdk
Copy link

openjdk bot commented Jun 16, 2025

@sercher The following labels will be automatically applied to this pull request:

  • hotspot-runtime
  • serviceability

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added serviceability serviceability-dev@openjdk.org hotspot-runtime hotspot-runtime-dev@openjdk.org labels Jun 16, 2025
@mlbridge
Copy link

mlbridge bot commented Jun 16, 2025

Webrevs

@@ -127,6 +130,80 @@ static void save_memory_to_file(char* addr, size_t size) {
// simple file apis.
//

#ifdef __APPLE__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit too much non-posix code in the posix file IMO. I'd rather see a MACOS_ONLY call later on to something defined in os_bsd.cpp for macOS.

if (user != null) {
for (String dir : platformSupport.getTemporaryDirectories(pid)) {
Path fullPath = Path.of(dir, HSPERFDATA_PREFIX + user, String.valueOf(pid));
if(Files.exists(fullPath)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(Files.exists(fullPath)) {
if (Files.exists(fullPath)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime hotspot-runtime-dev@openjdk.org rfr Pull request is ready for review serviceability serviceability-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

3 participants