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