Skip to content

8358799: Refactor os::jvm_path() #25675

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

Closed

Conversation

calvinccheung
Copy link
Member

@calvinccheung calvinccheung commented Jun 6, 2025

The linux, bsd, and aix versions of os::jvm_path() are very similar and can be combined into one in os_posix.cpp.

Passed tiers 1 - 3 tests.
However, tests were not run on the aix platform since it is not one of the Oracle supported platforms.


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-8358799: Refactor os::jvm_path() (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25675

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@calvinccheung calvinccheung marked this pull request as ready for review June 6, 2025 16:35
@bridgekeeper
Copy link

bridgekeeper bot commented Jun 6, 2025

👋 Welcome back ccheung! 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 6, 2025

@calvinccheung This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8358799: Refactor os::jvm_path()

Reviewed-by: dholmes, jsjolen

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 24 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

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

openjdk bot commented Jun 6, 2025

@calvinccheung The following label will be automatically applied to this pull request:

  • hotspot-runtime

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

@mlbridge
Copy link

mlbridge bot commented Jun 6, 2025

Webrevs

@snake66
Copy link
Contributor

snake66 commented Jun 8, 2025

Not a reviewer or anything, but I'm all for this change. It will help my BSD porting efforts as well.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Good idea but I think we can go further.

Thanks

Comment on lines 1079 to 1099
#ifdef AIX
Dl_info dlinfo;
int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
assert(ret != 0, "cannot locate libjvm");
char* rp = os::realpath((char *)dlinfo.dli_fname, buf, buflen);
assert(rp != nullptr, "error in realpath(): maybe the 'path' argument is too long?");
#else
char dli_fname[MAXPATHLEN];
dli_fname[0] = '\0';
bool ret = dll_address_to_library_name(
CAST_FROM_FN_PTR(address, os::jvm_path),
dli_fname, sizeof(dli_fname), nullptr);
assert(ret, "cannot locate libjvm");
char *rp = nullptr;
if (ret && dli_fname[0] != '\0') {
rp = os::realpath(dli_fname, buf, buflen);
}
if (rp == nullptr) {
return;
}
#endif // AIX
Copy link
Member

Choose a reason for hiding this comment

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

Given both chunks use os::realpath and otherwise only differ in error handling around realpath, I think you can reduce this further. E.g. something like

  char* fname;
#ifdef AIX
  Dl_info dlinfo;
  int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
  assert(ret != 0, "cannot locate libjvm");
  fname = dlinfo.dli_fname;
#else
  char dli_fname[MAXPATHLEN];
  dli_fname[0] = '\0';
  bool ret = dll_address_to_library_name(
                                         CAST_FROM_FN_PTR(address, os::jvm_path),
                                         dli_fname, sizeof(dli_fname), nullptr);
  assert(ret, "cannot locate libjvm");
  fname = dli_fname;
#endif
  char* rp = nullprtr;
  ...

I would also be asking the AIX folk if there is a reason they use dladdr directly instead of os::dll_address_to_library_name?

Copy link
Member

Choose a reason for hiding this comment

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

AIX is very different in this regard. There is a hand-crafted partial implementation of dladdr in src/hotspot/os/aix/porting_aix.cpp. I'm not sure this is the reason, just pointing out that this is a typical weakness of AIX where it differs from other unix systems wrt to dladdr.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the PR based on David's comment and have left the dladdr alone for now.

Comment on lines 1080 to 1084
Dl_info dlinfo;
int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
assert(ret != 0, "cannot locate libjvm");
char* rp = os::realpath((char *)dlinfo.dli_fname, buf, buflen);
assert(rp != nullptr, "error in realpath(): maybe the 'path' argument is too long?");
Copy link
Contributor

Choose a reason for hiding this comment

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

Pre-existing: We should return if ret == 0 and rp == nullptr, not continue as if it didn't fail.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added the check for ret.

if (buflen < MAXPATHLEN) {
assert(false, "must use a large-enough buffer");
buf[0] = '\0';
return;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need release the memory of buf before return, such like

free(buf);

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because the caller of the function still references buf after the function returns, e.g. in os::init_system_properties_values().

Copy link
Member

Choose a reason for hiding this comment

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

More specifically, buf is owned by the caller and we have no idea what it is so we have no business "releasing" it.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

I'd still like some input from AIX folk on whether we can do better here, but in lieu of that this seems good enough to me.

Thanks

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 10, 2025
Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

I'm also OK with this.

@calvinccheung
Copy link
Member Author

Thanks @dholmes-ora @jdksjolen for the review.

/integrate

@openjdk
Copy link

openjdk bot commented Jun 10, 2025

Going to push as commit 500a3a2.
Since your change was applied there have been 24 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 10, 2025
@openjdk openjdk bot closed this Jun 10, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 10, 2025
@openjdk
Copy link

openjdk bot commented Jun 10, 2025

@calvinccheung Pushed as commit 500a3a2.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@calvinccheung calvinccheung deleted the 8358799-refactor-jvm-path branch June 10, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime [email protected] integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

6 participants