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

Commit 818faf06 authored by Parth Sane's avatar Parth Sane
Browse files

Ensure thread is attached to JVM before sending binder metrics

Bug: 429487202
Test: atest binderUnitTest
Flag: build.RELEASE_LIBBINDER_BINDER_OBSERVER
Change-Id: I4db94e0a4e147381cc53de25eb9f323b3afb3a46
parent ba1ebe47
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -74,9 +74,11 @@ void BinderStatsPusher::aggregateStatsLocked(const std::vector<BinderCallData>&
    // to the VM then skip pushing. This is required since StatsBootstrap is
    // a Java service and needs a JNI interface to be called from native code.
    bool isProcessSystemServer = IInterface::asBinder(service)->localBinder() != nullptr;
    if (isProcessSystemServer && getJavaVM() == nullptr) {
    if (isProcessSystemServer) {
        if (!isThreadAttachedToJVM()) {
            return;
        }
    }
    // Clear calling identity if this is called from system server. This
    // will allow libStatsBootstrap to verify calling uid correctly.
    int64_t callingIdentity;
+12 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ namespace {
static void* getJavaVM() {
    return nullptr;
}
bool isThreadAttachedToJVM() {
    return false;
}
#else
static JavaVM* getJavaVM() {
    static auto fn = reinterpret_cast<decltype(&AndroidRuntimeGetJavaVM)>(
@@ -30,5 +33,14 @@ static JavaVM* getJavaVM() {
    if (fn == nullptr) return nullptr;
    return fn();
}

bool isThreadAttachedToJVM() {
    JNIEnv* env = nullptr;
    JavaVM* vm = getJavaVM();
    if (vm == nullptr || vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4) < 0) {
        return false;
    }
    return env != nullptr;
}
#endif
} // namespace