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

Commit 0186839f authored by Steven Moreland's avatar Steven Moreland
Browse files

Binder: avoid GC while holding lock

The JavaBBinder constructor may trigger a GC. This
causes deadlocks.

Though, I also think, usually when you create a
binder object, you just send it out, and so maybe
we should always create the local object instead
of doing it lazily like this, but idk the history
behind that, I've been thinking of changing it.

Bug: 360067751
Test: boot, TH
Change-Id: I97963c5e7b859d4ad474c715bb657b9baa19fabf
parent a35b1f13
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
@@ -465,11 +465,26 @@ class JavaBBinderHolder
{
public:
    sp<JavaBBinder> get(JNIEnv* env, jobject obj)
    {
        sp<JavaBBinder> b;
        {
            AutoMutex _l(mLock);
        sp<JavaBBinder> b = mBinder.promote();
        if (b == NULL) {
            // must take lock to promote because we set the same wp<>
            // on another thread.
            b = mBinder.promote();
        }

        if (b) return b;

        // b/360067751: constructor may trigger GC, so call outside lock
        b = new JavaBBinder(env, obj);

        {
            AutoMutex _l(mLock);
            // if it was constructed on another thread in the meantime,
            // return that. 'b' will just get destructed.
            if (sp<JavaBBinder> b2 = mBinder.promote(); b2) return b2;

            if (mVintf) {
                ::android::internal::Stability::markVintf(b.get());
            }