Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2e28bb23 authored by hyeeun.jun@samsung.com's avatar hyeeun.jun@samsung.com Committed by Hyeeun Jun
Browse files

Fix Deadlock Issue On AppFuseBridge

There are two locks used by AppFuseBridge.
First is it's object lock, and the second is a mutex lock in app fuse library.
There are two oppsite routines to get those locks.

  (Thread A) Got Java lock -> Try to get Native lock
  (Thread B)        Got Native lock -> Try to get Java lock

Bug : https://issuetracker.google.com/issues/145707568


Signed-off-by: default avatarhyeeun.jun@samsung.com <hyeeun.jun@samsung.com>

The order must be followed to obtain two locks.
If not, the dead lock will be caused.
Therefore we change the routine to get the mutex lock first, and the object lock later.
parent f8af6922
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -56,6 +56,15 @@ public class AppFuseBridge implements Runnable {

    public ParcelFileDescriptor addBridge(MountScope mountScope)
            throws FuseUnavailableMountException, NativeDaemonConnectorException {
        /*
        ** Dead Lock between Java lock (AppFuseBridge.java) and Native lock (FuseBridgeLoop.cc)
        **
        **  (Thread A) Got Java lock (addBrdige) -> Try to get Native lock (native_add_brdige)
        **  (Thread B)        Got Native lock (FuseBrdigeLoop.start) -> Try to get Java lock (onClosed)
        **
        ** Guarantee the lock order (native lock -> java lock) when adding Bridge.
        */
        native_lock();
        try {
            synchronized (this) {
                Preconditions.checkArgument(mScopes.indexOfKey(mountScope.mountId) < 0);
@@ -73,6 +82,7 @@ public class AppFuseBridge implements Runnable {
                return result;
            }
        } finally {
            native_unlock();
            IoUtils.closeQuietly(mountScope);
        }
    }
@@ -159,4 +169,6 @@ public class AppFuseBridge implements Runnable {
    private native void native_delete(long loop);
    private native void native_start_loop(long loop);
    private native int native_add_bridge(long loop, int mountId, int deviceId);
    private native void native_lock();
    private native void native_unlock();
}
+18 −0
Original line number Diff line number Diff line
@@ -123,6 +123,14 @@ jint com_android_server_storage_AppFuseBridge_add_bridge(
    return proxyFd[1].release();
}

void com_android_server_storage_AppFuseBridge_lock(JNIEnv* env, jobject self) {
    fuse::FuseBridgeLoop::Lock();
}

void com_android_server_storage_AppFuseBridge_unlock(JNIEnv* env, jobject self) {
    fuse::FuseBridgeLoop::Unlock();
}

const JNINativeMethod methods[] = {
    {
        "native_new",
@@ -143,6 +151,16 @@ const JNINativeMethod methods[] = {
        "native_add_bridge",
        "(JII)I",
        reinterpret_cast<void*>(com_android_server_storage_AppFuseBridge_add_bridge)
    },
    {
        "native_lock",
        "()V",
        reinterpret_cast<void*>(com_android_server_storage_AppFuseBridge_lock)
    },
    {
        "native_unlock",
        "()V",
        reinterpret_cast<void*>(com_android_server_storage_AppFuseBridge_unlock)
    }
};