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

Commit 00ba9e8c authored by Vishnu Nair's avatar Vishnu Nair
Browse files

BBQ: Attach calling thread to jvm if needed

When BBQ is destroyed from a native thread created by
the app, getEnv fails because the calling thread is not
attached to the jvm. To fix this, check the result when
getting the env from the jvm and attach the current
thread if needed.

Test: app in bug doesnt crash when resumed
Fixes: 236780458
Change-Id: Ib82b9b372167df60ad230ac309099db9f4a1a1fc
parent 18acc531
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -41,7 +41,12 @@ struct {

static JNIEnv* getenv(JavaVM* vm) {
    JNIEnv* env;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
    auto result = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
    if (result == JNI_EDETACHED) {
        if (vm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) {
            LOG_ALWAYS_FATAL("Failed to AttachCurrentThread!");
        }
    } else if (result != JNI_OK) {
        LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", vm);
    }
    return env;
@@ -60,15 +65,15 @@ public:
    }

    ~TransactionHangCallbackWrapper() {
        if (mTransactionHangObject) {
            getenv()->DeleteGlobalRef(mTransactionHangObject);
        if (mTransactionHangObject != nullptr) {
            getenv(mVm)->DeleteGlobalRef(mTransactionHangObject);
            mTransactionHangObject = nullptr;
        }
    }

    void onTransactionHang(bool isGpuHang) {
        if (mTransactionHangObject) {
            getenv()->CallVoidMethod(mTransactionHangObject,
            getenv(mVm)->CallVoidMethod(mTransactionHangObject,
                                        gTransactionHangCallback.onTransactionHang, isGpuHang);
        }
    }
@@ -76,12 +81,6 @@ public:
private:
    JavaVM* mVm;
    jobject mTransactionHangObject;

    JNIEnv* getenv() {
        JNIEnv* env;
        mVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
        return env;
    }
};

static jlong nativeCreate(JNIEnv* env, jclass clazz, jstring jName,