Skip to content

Replace usage of SecurityManager and AccessController#doPrivileged#1638

Merged
matthiasblaesing merged 1 commit into
java-native-access:masterfrom
matthiasblaesing:replace_security_manager
May 11, 2026
Merged

Replace usage of SecurityManager and AccessController#doPrivileged#1638
matthiasblaesing merged 1 commit into
java-native-access:masterfrom
matthiasblaesing:replace_security_manager

Conversation

@matthiasblaesing
Copy link
Copy Markdown
Member

The SecurityManager class is deprecated for removal since JDK 17. Its getClassContext method is used to optain the calling class when Native#register and Native#unregister are used without the target class being passed in. With JDK 9 the StalkWalker was introduced to make that information available. This change makes both methods available using method handles and prefers the StalkWalker codepath as that is the path expected to be invoked in most cases (on JDK 9+).

The AccessController#doProvileged call is replaced by a method handle, assuming, that in an environment where WebStart is available, AccessController is also available.

Closes: #1636

@matthiasblaesing matthiasblaesing force-pushed the replace_security_manager branch 2 times, most recently from f6b09c3 to 9b5e1ad Compare July 15, 2025 18:27
Copy link
Copy Markdown
Contributor

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

Dug into this and compared to other implementations. LGTM. Only thing I'd add is comments explaining some choices.

stackWalkerFilterBuilder = new java.util.function.Function<Stream<Object>, Class<?>>() {
@Override
public Class<?> apply(Stream<Object> t) {
Object stackFrame = t.skip(2).findFirst().get();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This works but probably deserves a comment on why 2 (vs. old [3]). I'm wondering if there's a better approach by looking for the first stack frame that's outside of c.s.j.Native.

Comment thread CHANGES.md Outdated
--------
* [#1671](https://github.com/java-native-access/jna/pull/1671): Add `isRISCV` to `c.s.j.Platform` - [@Glavo](https://github.com/Glavo).
* [#1672](https://github.com/java-native-access/jna/pull/1672): Add `CFLocale`, `CFLocaleCopyCurrent`, `CFCFDateFormatter`, `CFDateFormatterStyle`, `CFDateFormatterCreate` and `CFDateFormatterGetFormat` to `c.s.j.p.mac.CoreFoundation` - [@dbwiddis](https://github.com/dbwiddis).
* [#1636](https://github.com/java-native-access/jna/issues/1636): Drop hard dependency on java.lang.SecurityManager/java.security.AccessController - [@matthiasblaesing](https://github.com/matthiasblaesing).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Needs moved to 5.19.0 :)

@matthiasblaesing matthiasblaesing force-pushed the replace_security_manager branch from 9b5e1ad to e2c1f02 Compare May 10, 2026 16:16
@matthiasblaesing
Copy link
Copy Markdown
Member Author

@dbwiddis thanks - I added some words about the logic. Does it make sense to you?

@dbwiddis
Copy link
Copy Markdown
Contributor

@dbwiddis thanks - I added some words about the logic. Does it make sense to you?

Looks great!

For clarity, I did compare the JDK9+ implementation vs. other known good fixes for this. The StackWalker was new to me but looks sane and your new comment makes it very clear!

The SecurityManager class is deprecated for removal since JDK 17. Its
getClassContext method is used to optain the calling class when
Native#register and Native#unregister are used without the target class
being passed in. With JDK 9 the StalkWalker was introduced to make
that information available. This change makes both methods available
using method handles and prefers the StalkWalker codepath as that is
the path expected to be invoked in most cases (on JDK 9+).

The AccessController#doProvileged call is replaced by a method handle,
assuming, that in an environment where WebStart is available,
AccessController is also available.

Closes: java-native-access#1636
@matthiasblaesing matthiasblaesing force-pushed the replace_security_manager branch from e2c1f02 to f5f17d5 Compare May 11, 2026 17:28
@matthiasblaesing matthiasblaesing merged commit c59d194 into java-native-access:master May 11, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SecurityManager is deprecated for removal

2 participants