Skip to content
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

Prevent illegal access exceptions, when retrieving options for private library interfaces with an instance field #1284

Conversation

fkistner
Copy link
Contributor

@fkistner fkistner commented Dec 3, 2020

System info
  1. Version of JNA and related jars: latest master
  2. Version and vendor of the java virtual machine: Amazon Corretto 11.0.9.1
  3. Operating system: macOS 10.15.7 (19H2)
  4. System architecture (CPU type, bitness of the JVM): x86_64

JNA encounters IllegalAccessExceptions, when trying to retrieve library options from private Library interfaces that contain an instance field. Since direct mapped classes may retrieve options for the first time, only when a callback is passed or returned, this can happen rather unexpectedly.

This change suppresses the IllegalAccessExceptions by calling setAccessible like it is already done, when looking up other fields of the library interface via reflection.

Example stack traces (see PrivateLibraryInfoTest.java and PrivateDirectCallbacksTest.java)
java.lang.IllegalArgumentException: Could not access instance of interface com.sun.jna.different_package.PrivateDirectCallbacksTest$PrivateWithCallbackArgumentTestLibrary (java.lang.IllegalAccessException: Class com.sun.jna.Native can not access a member of class com.sun.jna.different_package.PrivateDirectCallbacksTest$PrivateWithCallbackArgumentTestLibrary with modifiers "public static final")

	at com.sun.jna.Native.loadLibraryInstance(Native.java:673)
	at com.sun.jna.Native.getLibraryOptions(Native.java:737)
	at com.sun.jna.CallbackReference.getFunctionPointer(CallbackReference.java:455)
	at com.sun.jna.different_package.PrivateDirectCallbacksTest$DirectPrivateWithCallbackArgumentTestLibrary.callVoidCallback(Native Method)

java.lang.ExceptionInInitializerError
	at com.sun.jna.different_package.PrivateDirectCallbacksTest$PrivateWithCallbackReturnTestLibrary.<clinit>(PrivateDirectCallbacksTest.java:39)
	at com.sun.jna.different_package.PrivateDirectCallbacksTest.testInvokeCallback(PrivateDirectCallbacksTest.java:63)
…
Caused by: java.lang.IllegalArgumentException: Could not access instance of interface com.sun.jna.different_package.PrivateDirectCallbacksTest$PrivateWithCallbackReturnTestLibrary (java.lang.IllegalAccessException: Class com.sun.jna.Native can not access a member of class com.sun.jna.different_package.PrivateDirectCallbacksTest$PrivateWithCallbackReturnTestLibrary with modifiers "public static final")
	at com.sun.jna.Native.loadLibraryInstance(Native.java:673)
	at com.sun.jna.Native.getLibraryOptions(Native.java:737)
	at com.sun.jna.Native.getTypeMapper(Native.java:799)
	at com.sun.jna.Structure$FFIType.get(Structure.java:2151)
	at com.sun.jna.Structure$FFIType.get(Structure.java:2146)
	at com.sun.jna.Native.register(Native.java:1793)

@fkistner fkistner force-pushed the pr-private-interface-callbacks branch from d47af88 to 5323018 Compare December 3, 2020 15:54
…brary interfaces with an instance field

 Since direct mapped classes may retrieve options for the first time, only when a callback is passed or returned, this could happen unexpectedly.
@fkistner fkistner force-pushed the pr-private-interface-callbacks branch from 5323018 to 3f19a5c Compare December 4, 2020 01:15
@matthiasblaesing
Copy link
Member

Merged via 578220e - thank you!

@fkistner fkistner deleted the pr-private-interface-callbacks branch January 13, 2021 12:08
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.

2 participants