blob: 4a7ac08895b7669eb25dc9f0e70b56935f6dd61b [file] [log] [blame]
Lev Proleev900c28a2021-01-26 19:40:20 +00001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ProtectCallback.h"
18
19#include <android-base/logging.h>
20#include <android-base/scopeguard.h>
21#include <android-base/thread_annotations.h>
22#include <android/binder_auto_utils.h>
23#include <android/binder_interface_utils.h>
24#include <nnapi/Result.h>
Lev Proleev900c28a2021-01-26 19:40:20 +000025
26#include <algorithm>
27#include <functional>
Devin Mooredef7a3c2024-04-09 21:50:41 +000028#include <map>
Lev Proleev900c28a2021-01-26 19:40:20 +000029#include <memory>
30#include <mutex>
31#include <vector>
32
33#include "Utils.h"
34
35namespace aidl::android::hardware::neuralnetworks::utils {
36
Devin Mooredef7a3c2024-04-09 21:50:41 +000037namespace {
38
39// Only dereference the cookie if it's valid (if it's in this set)
40// Only used with ndk
41std::mutex sCookiesMutex;
42uintptr_t sCookieKeyCounter GUARDED_BY(sCookiesMutex) = 0;
43std::map<uintptr_t, std::weak_ptr<DeathMonitor>> sCookies GUARDED_BY(sCookiesMutex);
44
45} // namespace
46
Lev Proleev900c28a2021-01-26 19:40:20 +000047void DeathMonitor::serviceDied() {
48 std::lock_guard guard(mMutex);
49 std::for_each(mObjects.begin(), mObjects.end(),
Michael Butlere8645c32021-10-15 18:42:32 -070050 [](IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
Lev Proleev900c28a2021-01-26 19:40:20 +000051}
52
53void DeathMonitor::serviceDied(void* cookie) {
Devin Mooredef7a3c2024-04-09 21:50:41 +000054 std::shared_ptr<DeathMonitor> monitor;
55 {
56 std::lock_guard<std::mutex> guard(sCookiesMutex);
57 if (auto it = sCookies.find(reinterpret_cast<uintptr_t>(cookie)); it != sCookies.end()) {
58 monitor = it->second.lock();
59 sCookies.erase(it);
60 } else {
61 LOG(INFO)
62 << "Service died, but cookie is no longer valid so there is nothing to notify.";
63 return;
64 }
65 }
66 if (monitor) {
67 LOG(INFO) << "Notifying DeathMonitor from serviceDied.";
68 monitor->serviceDied();
69 } else {
70 LOG(INFO) << "Tried to notify DeathMonitor from serviceDied but could not promote.";
71 }
Lev Proleev900c28a2021-01-26 19:40:20 +000072}
73
Michael Butlere8645c32021-10-15 18:42:32 -070074void DeathMonitor::add(IProtectedCallback* killable) const {
Lev Proleev900c28a2021-01-26 19:40:20 +000075 CHECK(killable != nullptr);
76 std::lock_guard guard(mMutex);
77 mObjects.push_back(killable);
78}
79
Michael Butlere8645c32021-10-15 18:42:32 -070080void DeathMonitor::remove(IProtectedCallback* killable) const {
Lev Proleev900c28a2021-01-26 19:40:20 +000081 CHECK(killable != nullptr);
82 std::lock_guard guard(mMutex);
83 const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable);
84 mObjects.erase(removedIter);
85}
86
Devin Mooredef7a3c2024-04-09 21:50:41 +000087DeathMonitor::~DeathMonitor() {
88 // lock must be taken so object is not used in OnBinderDied"
89 std::lock_guard<std::mutex> guard(sCookiesMutex);
90 sCookies.erase(kCookieKey);
91}
92
Lev Proleev900c28a2021-01-26 19:40:20 +000093nn::GeneralResult<DeathHandler> DeathHandler::create(std::shared_ptr<ndk::ICInterface> object) {
94 if (object == nullptr) {
95 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
96 << "utils::DeathHandler::create must have non-null object";
97 }
Devin Mooredef7a3c2024-04-09 21:50:41 +000098
99 std::shared_ptr<DeathMonitor> deathMonitor;
100 {
101 std::lock_guard<std::mutex> guard(sCookiesMutex);
102 deathMonitor = std::make_shared<DeathMonitor>(sCookieKeyCounter++);
103 sCookies[deathMonitor->getCookieKey()] = deathMonitor;
104 }
105
Lev Proleev900c28a2021-01-26 19:40:20 +0000106 auto deathRecipient = ndk::ScopedAIBinder_DeathRecipient(
107 AIBinder_DeathRecipient_new(DeathMonitor::serviceDied));
108
109 // If passed a local binder, AIBinder_linkToDeath will do nothing and return
110 // STATUS_INVALID_OPERATION. We ignore this case because we only use local binders in tests
111 // where this is not an error.
112 if (object->isRemote()) {
Devin Mooredef7a3c2024-04-09 21:50:41 +0000113 const auto ret = ndk::ScopedAStatus::fromStatus(
114 AIBinder_linkToDeath(object->asBinder().get(), deathRecipient.get(),
115 reinterpret_cast<void*>(deathMonitor->getCookieKey())));
Lev Proleev900c28a2021-01-26 19:40:20 +0000116 HANDLE_ASTATUS(ret) << "AIBinder_linkToDeath failed";
117 }
118
119 return DeathHandler(std::move(object), std::move(deathRecipient), std::move(deathMonitor));
120}
121
122DeathHandler::DeathHandler(std::shared_ptr<ndk::ICInterface> object,
123 ndk::ScopedAIBinder_DeathRecipient deathRecipient,
124 std::shared_ptr<DeathMonitor> deathMonitor)
125 : kObject(std::move(object)),
126 kDeathRecipient(std::move(deathRecipient)),
127 kDeathMonitor(std::move(deathMonitor)) {
128 CHECK(kObject != nullptr);
129 CHECK(kDeathRecipient.get() != nullptr);
130 CHECK(kDeathMonitor != nullptr);
131}
132
133DeathHandler::~DeathHandler() {
134 if (kObject != nullptr && kDeathRecipient.get() != nullptr && kDeathMonitor != nullptr) {
Devin Mooredef7a3c2024-04-09 21:50:41 +0000135 const auto ret = ndk::ScopedAStatus::fromStatus(
136 AIBinder_unlinkToDeath(kObject->asBinder().get(), kDeathRecipient.get(),
137 reinterpret_cast<void*>(kDeathMonitor->getCookieKey())));
Lev Proleev900c28a2021-01-26 19:40:20 +0000138 const auto maybeSuccess = handleTransportError(ret);
139 if (!maybeSuccess.ok()) {
140 LOG(ERROR) << maybeSuccess.error().message;
141 }
142 }
143}
144
145[[nodiscard]] ::android::base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
Michael Butlere8645c32021-10-15 18:42:32 -0700146 IProtectedCallback* killable) const {
Lev Proleev900c28a2021-01-26 19:40:20 +0000147 CHECK(killable != nullptr);
148 kDeathMonitor->add(killable);
149 return ::android::base::make_scope_guard(
150 [deathMonitor = kDeathMonitor, killable] { deathMonitor->remove(killable); });
151}
152
153} // namespace aidl::android::hardware::neuralnetworks::utils