Skip to content

Conversation

@wangweij
Copy link
Contributor

@wangweij wangweij commented Dec 19, 2025

Rewrite the native calls with FFM.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 2 Reviewers)

Issue

  • JDK-8277489: Rewrite JAAS UnixLoginModule with FFM (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28931

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 19, 2025

👋 Welcome back weijun! 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 Dec 19, 2025

@wangweij This change is no longer ready for integration - check the PR body for details.

@openjdk
Copy link

openjdk bot commented Dec 19, 2025

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

  • build
  • security

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 the rfr Pull request is ready for review label Dec 19, 2025
@mlbridge
Copy link

mlbridge bot commented Dec 19, 2025

Webrevs

Copy link
Member

@erikj79 erikj79 left a comment

Choose a reason for hiding this comment

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

Build change looks ok.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 19, 2025
@wangweij
Copy link
Contributor Author

/reviewers 2 reviewer

@openjdk
Copy link

openjdk bot commented Dec 19, 2025

@wangweij
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 2 Reviewers).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Dec 19, 2025
try (Arena scope = Arena.ofConfined()) {
int groupnum = getgroups(0, MemorySegment.NULL);
if (groupnum == -1) {
throw new RuntimeException("getgroups returns " + groupnum);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a must-have, but would be nice to have a FFM for errno to provide better diagnostics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. There is a Linker.Option.captureCallState("errno") feature and I'll see how to use it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like there has been prior work with this, at least with macos, e.g.: src/java.base/macosx/classes/jdk/internal/ffi/generated/errno/errno_h.java

throw new RuntimeException("getgroups returns " + groupnum);
}

var gs = scope.allocate(gid_t, groupnum);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be the size of gid_t * groupnum as the first argument and the byte alignment as the second?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is MemorySegment allocate(MemoryLayout elementLayout, long count) in SegmentAllocator.java.

var gs = scope.allocate(gid_t, groupnum);
groupnum = getgroups(groupnum, gs);
if (groupnum == -1) {
throw new RuntimeException("getgroups returns " + groupnum);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a must-have, but would be nice to have a FFM for errno to provide better diagnostics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

groups[i] = gs.getAtIndex(gid_t, i);
}

var resbuf = passwd.allocate(scope);
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't this be 1024 as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the MemorySegment allocate(SegmentAllocator allocator) method in the generated passwd.java. If I understand correctly, the 2nd argument to getpwuid_r should be the pointer to a single passwd struct and the 3rd is to a big enough memory to hold contents of extra pointers inside passwd.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, ideally the 3rd argument would be the size of platform's maximum pwd struct buffer size, which at least can found found on linux and macos with _SC_GETPW_R_SIZE_MAX using sysconf. The sysconf FFM would not be a must-have, but would be a nice addition (along with errno) as part of the core libraries in the future.

var resbuf = passwd.allocate(scope);
var pwd = scope.allocate(C_POINTER);
var pwd_buf = scope.allocate(1024);
int out = getpwuid_r(getuid(), resbuf, pwd_buf, pwd_buf.byteSize(), pwd);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: resbuf is usually name pwd, which is used to reference the passwd attributes in pwd_buf.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was using the old names in the C code. They could be confusing. I'll fix them.

username = passwd.pw_name(resbuf).getString(0);
} catch (Throwable t) {
var error = new UnsatisfiedLinkError("FFM calls failed");
error.initCause(t);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to check with getCause() here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getCause of t? I'll just pass the whole t back to the caller.

* echo "#include <unistd.h> " > $HEADER_NAME
* echo "#include <spwd.h>" >> $HEADER_NAME
*
*
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: extra line, but please ignore if jextract generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not generated.

Copy link
Contributor Author

@wangweij wangweij Dec 22, 2025

Choose a reason for hiding this comment

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

Thanks for the comments. I've pushed a new commit that removes all jextract-generated code and go fully manual. Is this better?

BTW, I haven't studied the use of errorno yet. Also, I have a TODO item at the beginning of UnixLoginModule::login. Please tell me your opinion on my concern.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks better, thank you.
Re: check for window platform earlier - yes, I think that this would be appropriate, but I haven't worked through how this would fallback to the existing windows implementation.
Re: other unix platforms - if FFM sysconf is implemented then allocation would not be left to chance on these systems. Regarding the pwd structure - I would be very surprised if the core attributes would be any different.

Copy link
Contributor

@smemery smemery left a comment

Choose a reason for hiding this comment

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

BTW, I don't believe that this change warrants new benchmarks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants