• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
25 
26 #include <algorithm>
27 #include <functional>
28 #include <memory>
29 #include <mutex>
30 #include <vector>
31 
32 #include "Utils.h"
33 
34 namespace aidl::android::hardware::neuralnetworks::utils {
35 
serviceDied()36 void DeathMonitor::serviceDied() {
37     std::lock_guard guard(mMutex);
38     std::for_each(mObjects.begin(), mObjects.end(),
39                   [](IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
40 }
41 
serviceDied(void * cookie)42 void DeathMonitor::serviceDied(void* cookie) {
43     auto deathMonitor = static_cast<DeathMonitor*>(cookie);
44     deathMonitor->serviceDied();
45 }
46 
add(IProtectedCallback * killable) const47 void DeathMonitor::add(IProtectedCallback* killable) const {
48     CHECK(killable != nullptr);
49     std::lock_guard guard(mMutex);
50     mObjects.push_back(killable);
51 }
52 
remove(IProtectedCallback * killable) const53 void DeathMonitor::remove(IProtectedCallback* killable) const {
54     CHECK(killable != nullptr);
55     std::lock_guard guard(mMutex);
56     const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable);
57     mObjects.erase(removedIter);
58 }
59 
create(std::shared_ptr<ndk::ICInterface> object)60 nn::GeneralResult<DeathHandler> DeathHandler::create(std::shared_ptr<ndk::ICInterface> object) {
61     if (object == nullptr) {
62         return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
63                << "utils::DeathHandler::create must have non-null object";
64     }
65     auto deathMonitor = std::make_shared<DeathMonitor>();
66     auto deathRecipient = ndk::ScopedAIBinder_DeathRecipient(
67             AIBinder_DeathRecipient_new(DeathMonitor::serviceDied));
68 
69     // If passed a local binder, AIBinder_linkToDeath will do nothing and return
70     // STATUS_INVALID_OPERATION. We ignore this case because we only use local binders in tests
71     // where this is not an error.
72     if (object->isRemote()) {
73         const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_linkToDeath(
74                 object->asBinder().get(), deathRecipient.get(), deathMonitor.get()));
75         HANDLE_ASTATUS(ret) << "AIBinder_linkToDeath failed";
76     }
77 
78     return DeathHandler(std::move(object), std::move(deathRecipient), std::move(deathMonitor));
79 }
80 
DeathHandler(std::shared_ptr<ndk::ICInterface> object,ndk::ScopedAIBinder_DeathRecipient deathRecipient,std::shared_ptr<DeathMonitor> deathMonitor)81 DeathHandler::DeathHandler(std::shared_ptr<ndk::ICInterface> object,
82                            ndk::ScopedAIBinder_DeathRecipient deathRecipient,
83                            std::shared_ptr<DeathMonitor> deathMonitor)
84     : kObject(std::move(object)),
85       kDeathRecipient(std::move(deathRecipient)),
86       kDeathMonitor(std::move(deathMonitor)) {
87     CHECK(kObject != nullptr);
88     CHECK(kDeathRecipient.get() != nullptr);
89     CHECK(kDeathMonitor != nullptr);
90 }
91 
~DeathHandler()92 DeathHandler::~DeathHandler() {
93     if (kObject != nullptr && kDeathRecipient.get() != nullptr && kDeathMonitor != nullptr) {
94         const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_unlinkToDeath(
95                 kObject->asBinder().get(), kDeathRecipient.get(), kDeathMonitor.get()));
96         const auto maybeSuccess = handleTransportError(ret);
97         if (!maybeSuccess.ok()) {
98             LOG(ERROR) << maybeSuccess.error().message;
99         }
100     }
101 }
102 
protectCallback(IProtectedCallback * killable) const103 [[nodiscard]] ::android::base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
104         IProtectedCallback* killable) const {
105     CHECK(killable != nullptr);
106     kDeathMonitor->add(killable);
107     return ::android::base::make_scope_guard(
108             [deathMonitor = kDeathMonitor, killable] { deathMonitor->remove(killable); });
109 }
110 
111 }  // namespace aidl::android::hardware::neuralnetworks::utils
112