java-native-access / java-native-access/jna

Cannot pass String as JNI jstring parameter

Open
#1,099 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
8.9k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

  1. Version of JNA and related jars
    JNA 5.3.1

  2. Version and vendor of the java virtual machine
    Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 1.8.0_181

  3. Operating system
    Ubuntu 16.04.6

  4. System architecture (CPU type, bitness of the JVM)
    Intel x64, x64 JVM

  5. Complete description of the problem
    I cannot pass String objects to a JNI method for invocation, (even using JniEnv.CURRENT and setting OPTION_ALLOW_OBJECTS), since a String is always converted to NativeString.

    When trying to call JNI methods, JNA invokes convertArgument() just before C method invocation, and String is converted to NativeString before allowObjects is checked. This means that it is impossible to pass the Java String (jstring) object to C (which accepts jstring as a parameter type). For example, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name);

  6. Steps to reproduce

    public
    interface JVM extends com.sun.jna.Library {
         Class JVM_FindLoadedClass(JNIEnv env, Object classLoader, String name);
    }


    Map options = new HashMap();
    options.put(Library.OPTION_ALLOW_OBJECTS, Boolean.TRUE);

    JVM libjvm = Native.load("jvm", JVM.class, options);
    jvm.JVM_FindLoadedClass(JNIEnv.CURRENT, classLoader, className);

It is entirely likely that I don't know what I'm doing, after reading through the documentation and studying the source -- I do not know how to pass a String as a jstring object (not a char* object) to native code. I've tried type converters, but those didn't get me anywhere, since String is always converted to NativeString as an argument...

Because the Function constructor is package scope, as is invoke, I cannot overload invoke to not convert arguments, so I am unable to modify the method invocation (outside of rewriting the class definition at runtime via javassist or something similar)

One solution I thought of, while trying to figure this out, is modify the convertArgument() method so that the check for allowObjects occurs sooner, allowing all Java objects to passed without conversion -- so the check for allowObjects is after JNA specific object types (struct, etc), but before the Java object type conversion (mostly string), instead of where currently is (which is last)....

NOTE: Loading the JVM library for Windows 32 bit, or MACOS is slightly different. The above only loads it on Linux, specifically Ubuntu 16.04.6 x64 with an Oracle JVM (from my testing, this is currently true for all released Oracle JVM's on Linux, but I haven't tested every combination....)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Function's argument conversion through convertArgument(), especially the handling of String, OPTION_ALLOW_OBJECTS, and JniEnv.CURRENT. Verify how a Java String is currently converted before invocation and determine whether jstring objects can pass through without breaking JNA-specific conversions. Done means the reported JVM_FindLoadedClass call can receive a Java String as jstring, with coverage for the conversion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.