1 /*
2  * Copyright (C) 2020 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/hidl/base/1.0/IBase.h>
23 #include <hidl/HidlSupport.h>
24 #include <nnapi/Result.h>
25 
26 #include "HandleError.h"
27 
28 #include <algorithm>
29 #include <functional>
30 #include <mutex>
31 #include <vector>
32 
33 namespace android::hardware::neuralnetworks::utils {
34 
serviceDied(uint64_t,const wp<hidl::base::V1_0::IBase> &)35 void DeathRecipient::serviceDied(uint64_t /*cookie*/, const wp<hidl::base::V1_0::IBase>& /*who*/) {
36     std::lock_guard guard(mMutex);
37     std::for_each(mObjects.begin(), mObjects.end(),
38                   [](IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
39     mObjects.clear();
40     mIsDeadObject = true;
41 }
42 
add(IProtectedCallback * killable) const43 void DeathRecipient::add(IProtectedCallback* killable) const {
44     CHECK(killable != nullptr);
45     std::lock_guard guard(mMutex);
46     if (mIsDeadObject) {
47         killable->notifyAsDeadObject();
48     } else {
49         mObjects.push_back(killable);
50     }
51 }
52 
remove(IProtectedCallback * killable) const53 void DeathRecipient::remove(IProtectedCallback* killable) const {
54     CHECK(killable != nullptr);
55     std::lock_guard guard(mMutex);
56     const auto newEnd = std::remove(mObjects.begin(), mObjects.end(), killable);
57     mObjects.erase(newEnd, mObjects.end());
58 }
59 
create(sp<hidl::base::V1_0::IBase> object)60 nn::GeneralResult<DeathHandler> DeathHandler::create(sp<hidl::base::V1_0::IBase> 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 deathRecipient = sp<DeathRecipient>::make();
66 
67     const auto ret = object->linkToDeath(deathRecipient, /*cookie=*/0);
68     const bool success = HANDLE_TRANSPORT_FAILURE(ret);
69     if (!success) {
70         return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "IBase::linkToDeath returned false";
71     }
72 
73     return DeathHandler(std::move(object), std::move(deathRecipient));
74 }
75 
DeathHandler(sp<hidl::base::V1_0::IBase> object,sp<DeathRecipient> deathRecipient)76 DeathHandler::DeathHandler(sp<hidl::base::V1_0::IBase> object, sp<DeathRecipient> deathRecipient)
77     : mObject(std::move(object)), mDeathRecipient(std::move(deathRecipient)) {
78     CHECK(mObject != nullptr);
79     CHECK(mDeathRecipient != nullptr);
80 }
81 
~DeathHandler()82 DeathHandler::~DeathHandler() {
83     if (mObject != nullptr && mDeathRecipient != nullptr) {
84         const auto successful = mObject->unlinkToDeath(mDeathRecipient).isOk();
85         if (!successful) {
86             LOG(ERROR) << "IBase::linkToDeath failed";
87         }
88     }
89 }
90 
protectCallback(IProtectedCallback * killable) const91 [[nodiscard]] base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
92         IProtectedCallback* killable) const {
93     CHECK(killable != nullptr);
94     mDeathRecipient->add(killable);
95     return base::make_scope_guard(
96             [deathRecipient = mDeathRecipient, killable] { deathRecipient->remove(killable); });
97 }
98 
protectCallbackForLifetimeOfDeathHandler(IProtectedCallback * killable) const99 void DeathHandler::protectCallbackForLifetimeOfDeathHandler(IProtectedCallback* killable) const {
100     CHECK(killable != nullptr);
101     mDeathRecipient->add(killable);
102 }
103 
104 }  // namespace android::hardware::neuralnetworks::utils
105