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