Android JNI blocks on native to Java calls - android

I have a pretty basic question regarding JNI calls to Java in Android NDK. I have no problems with making the actual calls, but I am not sure whether the Java call is blocked or not when it is called from C.
My question is specifically whether the c call blocks while the Java method is executed, or if it returns immediately. The reason I am asking is that I pass a byteArray to Java and I wonder if Java has access to it after the call is initiated.

JNI calls are like nornal function call. Your native code continues running when JNI call (and Java function) returns.

You should be careful about your selection of JNI calls, some can become blocking. For example getting a Critical pointer to a byte array and not releasing it can block the jvm from continuing to operate.

Related

AttachCurrentThread crash in multi threads JNI after moving to Android L release

I have implemented a service which is invoked by system server during the boot-up.
My service has JNI implementation which creates another thread.
The reason for having another thread is to have the capability of canceling operation while monitoring the call back.
This mechanism worked well in Android Kitkat release but it crashes in Android L.
Between two threads, I stored the JavaVM* that I get from GetJavaVM(env) to a static global variable. Of course, this shared data is protected by pthread mutex.
I have tried the following so far, but all of them below still crashed :
1) Used JavaVM* I get from JNI onLoad() function by store it to global
2) In the new thread, as there is only one JavaVM running on Android, get the vm from calling android::AndroidRuntime::getJavaVM();
3) Stored the vm information in the main thread after calling NewGlobalRef(). And saved that reference to the shared data. The new thread used the reference from NewGlobalRef().
Does anyone know what is significantly changed in JNI environment on Android L release?
UPDATE :
Debugged further and the solution I mentioned 1) or 2) should have worked.
The actual issue was due to the garbage collection running more frequently. So the HAL pointer I kept was not valid any more...
These links were helpful!!!
https://developer.android.com/guide/practices/verifying-apps-art.html
http://developer.android.com/training/articles/perf-jni.html
Thanks for all the comments!
What has changed with L release is the move to ART that is less flexible than Dalvik regarding errors.
It's perfectly fine to share JavaVM* across threads, you should keep it this way.
However, what are you doing later with this JavaVM* ?
JNIEnv* has to be retrieved and used from the same thread and must not be used across threads. To use JNIEnv*, a thread must have been attached to the VM (using AttachCurrentThread).
Threads also have to be detached using DetachCurrentThread before they exit.

Android : Logging all API calls

I'm working on a collage project about security in Android. One part of the project attempts to capture and log all API function called by the selected APK. This can't be done with a external programs so in the project we are working with the Android source code to modify the ROM.
At the present time we only have two possible solutions:
DVM JNI Bridge
The API is Java code so, obviously, the Dalvik Virtual Machine needs a bridge to execute JNI code. The function which handle all cases is dvmCallJNIMethod(const u4* args, JValue* pResult, const Method* method, Thread* self). In this function we can get the name and the class of the called function.
This function can log all the JNI code executed, which includes API calls. But there is no easy way to distinct between private calls and API calls. And, if we wanted to execute some code depending on the risk of the API call executed, we would have to create a huge and inefficient switch.
API Framework
another solution To log all API calls is creating a new interface for the framework. With a new logging class and a simple inheritance should be easy to log all calls and add a risk parameter. But it would mean changing a lot of code. Also, Java code has worst performance than C, so it might not be the most efficient way.
More over, we would like to ask you a few questions about Android DVM and the API.
1.Which is exactly the call flow between DVM and the API?
2.Could be the DVM monitor a good idea to log the calls?
3.Which role have the shared libraries in all of this?
4.Are all API calls really Java code?
Thanks for your time.

Android: How to read assets from another thread in C++

I'm writing code in C++ for Android. The main code is written in Java, but I would like to start another thread invoked from JNI call and access assets from C++. However, when I pass AAssetsManager from getAssets(), it works only within scope of JNI function call. When other thread tries to use, I get an invalid pointer. I also made sure that the object AAssetsManager is not garbage collected by writing it to a static variable. Do you have any advice on how I could make it work?
In other works, I would like to access AAssetManager and cache in other object.
Thanks
It's not enough to protect the assetManager object from GC. You need a global reference:
gAssetManager = env->NewGlobalRef(assetManager);
If you wanna access Java VM by native thread, you have to call AttachCurrentThread() to attach native thread to VM. Have you made this? If not, you can refer to Oracle's JNI docs.

Native multithreading and JNI

I have an Android application which consists of some native threads (not attached to JVM) which need to be able to call methods of a Java object.
The way in which I was intending to do this was to create a JNI function which I call from the relevant Java object which allows me to obtain and cache the required java object method ID's, JNIEnv and object references in a static native data structure so that my native threads can (thread safely) access the required methods (e.g. using (*env)->CallVoidMethod(env, this, JavaMethodID, ...), etc;
I'm not convinced this approach is going to work, since I read that the JNIEnv pointer can't be shared between threads, and that only threads which are attached to the JVM can do this kind of thing...
Is this a viable approach?
in JNI_OnLoad, cache JavaVM*. That's the only thing persistent and valid across threads.
as soon as you set up some native thread, call AttachCurrentThread and obtain JNIEnv*, which is valid only for that single thread.
with JavaVM* and JNIEnv*, look up your jclasses, jobjects and jmethodIDs. These are still valid only for the single thread you have attached to.
convert jclasses and jobjects to global references, so that it persists across threads. jmethodIDs do not need to be globalized, they are not jobjects.
On any further native threads, you again need to call AttachCurrentThread to obtain a valid JNIEnv* for that thread.
Don't forget to delete the created global references when you don't need them anymore (in JNI_OnUnload at the latest)

Android NDK: JNI "main" to deal with messages?

I'm trying to build an Android application that uses an existing C library for some background operation (i.e. does some radio scans, tunes to stations etc). As a stand-alone C executable, the main-loop can deal with message handling from lower levels (hardware components). My understanding is that using JNI, no main function is required because
1) a shared library is created and
2) the shared library is "alive" for as long as the java thread that loaded it is alive.
So assuming that the C library uses multiple threads: where should then the message handling that normally is done in the initial main-loop be done? Is it as simple as by calling C functions that are declared together with the JNI functions?
Re 2) library is "alive" in the meaning of persisting in the memory. But it does not do anything on its own. If you need the library to "do something" even if no functions are being called through JNI, then you need a separate native thread of course. You can create the thread and start a message loop within a regular JNI function call (init method or use JNI_OnLoad for that purpose). It will keep on running when the JNI function call returns. You then also need a teardown method which stops the thread and tears it down (JNI_OnUnload can be used for that)

Categories

Resources