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

Commit 45024c67 authored by Aaron Okano's avatar Aaron Okano
Browse files

binder: Add Java bindings for setInheritRt

This change adds Java functions for two features:

1. A wrapper for setInheritRt, which is available in libbinder but was
   previously not exposed to Java code.
2. A wrapper for setGlobalInheritRt to enable setInheritRt across all
   Binder instances within a process.

Flag: android.server.allow_system_server_inherit_rt
bug: 397169625
Change-Id: I2689dcd742f7bb1db74871dda0f491aceae713f0
parent bc15153a
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -1259,6 +1259,26 @@ public class Binder implements IBinder {

    private final native void setExtensionNative(@Nullable IBinder extension);

    /**
     * Set whether or not this binder node will inherit real-time priority.
     * This should be called immediately when the object is created.
     *
     * @hide
     */
    public final native void setInheritRt(boolean inheritRt);

    /**
     * Set default RT inheritance for Binders in this process
     *
     * Any binder objects sent out of the process before this is called will
     * not use the updated value. Can be overridden by setInheritRt. Defaults
     * to false if this function is not called.
     *
     * @hide
     */
    @CriticalNative
    public static final native void setGlobalInheritRt(boolean enabled);

    /**
     * Default implementation rewinds the parcels and calls onTransact. On
     * the remote side, transact calls into the binder to do the IPC.
+25 −0
Original line number Diff line number Diff line
@@ -496,6 +496,9 @@ public:
                    b.get()->setExtension(extensionFromJava);
                }
            }
            if (mInheritRt) {
                b.get()->setInheritRt(mInheritRt);
            }
            mBinder = b;
            ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n",
                 b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount());
@@ -529,6 +532,15 @@ public:
        }
    }

    void setInheritRt(bool inheritRt) {
        AutoMutex _l(mLock);
        mInheritRt = inheritRt;
        sp<JavaBBinder> b = mBinder.promote();
        if (b != nullptr) {
            b.get()->setInheritRt(inheritRt);
        }
    }

private:
    Mutex           mLock;
    wp<JavaBBinder> mBinder;
@@ -538,6 +550,7 @@ private:
    // sp here (avoid recreating it)
    bool            mVintf = false;
    bool            mSetExtensionCalled = false;
    bool            mInheritRt = false;
};

// ----------------------------------------------------------------------------
@@ -1262,6 +1275,15 @@ static void android_os_Binder_setExtension(JNIEnv* env, jobject obj, jobject ext
    jbh->setExtension(extension);
}

static void android_os_Binder_setInheritRt(JNIEnv* env, jobject obj, jboolean inheritRt) {
    JavaBBinderHolder* jbh = (JavaBBinderHolder*) env->GetLongField(obj, gBinderOffsets.mObject);
    jbh->setInheritRt(inheritRt);
}

static void android_os_Binder_setGlobalInheritRt(CRITICAL_JNI_PARAMS_COMMA jboolean enabled) {
    BBinder::setGlobalInheritRt(enabled);
}

// ----------------------------------------------------------------------------

// clang-format off
@@ -1290,6 +1312,8 @@ static const JNINativeMethod gBinderMethods[] = {
    { "getCallingWorkSourceUid", "()I", (void*)android_os_Binder_getCallingWorkSourceUid },
    // @CriticalNative
    { "clearCallingWorkSource", "()J", (void*)android_os_Binder_clearCallingWorkSource },
    // @CriticalNative
    { "setGlobalInheritRt", "(Z)V", (void*)android_os_Binder_setGlobalInheritRt },
    { "restoreCallingWorkSource", "(J)V", (void*)android_os_Binder_restoreCallingWorkSource },
    { "markVintfStability", "()V", (void*)android_os_Binder_markVintfStability},
    { "forceDowngradeToSystemStability", "()V", (void*)android_os_Binder_forceDowngradeToSystemStability},
@@ -1298,6 +1322,7 @@ static const JNINativeMethod gBinderMethods[] = {
    { "getNativeFinalizer", "()J", (void*)android_os_Binder_getNativeFinalizer },
    { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable },
    { "setExtensionNative", "(Landroid/os/IBinder;)V", (void*)android_os_Binder_setExtension },
    { "setInheritRt", "(Z)V", (void*)android_os_Binder_setInheritRt },
};
// clang-format on