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

Commit 666e72c8 authored by Parth Sane's avatar Parth Sane Committed by Android (Google) Code Review
Browse files

Merge "Ensure thread is attached to JVM before sending binder metrics" into main

parents 3c8ccdc0 818faf06
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