| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 | #define LOG_TAG "ADynamicInstrumentationManager" |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 18 | #include <android-base/properties.h> |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 19 | #include <android/dynamic_instrumentation_manager.h> |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 20 | #include <android/os/instrumentation/BnOffsetCallback.h> |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 21 | #include <android/os/instrumentation/ExecutableMethodFileOffsets.h> |
| 22 | #include <android/os/instrumentation/IDynamicInstrumentationManager.h> |
| 23 | #include <android/os/instrumentation/MethodDescriptor.h> |
| 24 | #include <android/os/instrumentation/TargetProcess.h> |
| 25 | #include <binder/Binder.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <utils/Log.h> |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 28 | #include <utils/StrongPointer.h> |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 29 | |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 30 | #include <future> |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 31 | #include <mutex> |
| 32 | #include <optional> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace android::dynamicinstrumentationmanager { |
| 37 | |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 38 | using android::os::instrumentation::BnOffsetCallback; |
| 39 | using android::os::instrumentation::ExecutableMethodFileOffsets; |
| 40 | |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 41 | // Global instance of IDynamicInstrumentationManager, service is obtained only on first use. |
| 42 | static std::mutex mLock; |
| 43 | static sp<os::instrumentation::IDynamicInstrumentationManager> mService; |
| 44 | |
| 45 | sp<os::instrumentation::IDynamicInstrumentationManager> getService() { |
| 46 | std::lock_guard<std::mutex> scoped_lock(mLock); |
| 47 | if (mService == nullptr || !IInterface::asBinder(mService)->isBinderAlive()) { |
| 48 | sp<IBinder> binder = |
| 49 | defaultServiceManager()->waitForService(String16("dynamic_instrumentation")); |
| 50 | mService = interface_cast<os::instrumentation::IDynamicInstrumentationManager>(binder); |
| 51 | } |
| 52 | return mService; |
| 53 | } |
| 54 | |
| 55 | } // namespace android::dynamicinstrumentationmanager |
| 56 | |
| 57 | using namespace android; |
| 58 | using namespace dynamicinstrumentationmanager; |
| 59 | |
| 60 | struct ADynamicInstrumentationManager_TargetProcess { |
| 61 | uid_t uid; |
| 62 | uid_t pid; |
| 63 | std::string processName; |
| 64 | |
| 65 | ADynamicInstrumentationManager_TargetProcess(uid_t uid, pid_t pid, const char* processName) |
| 66 | : uid(uid), pid(pid), processName(processName) {} |
| 67 | }; |
| 68 | |
| 69 | ADynamicInstrumentationManager_TargetProcess* ADynamicInstrumentationManager_TargetProcess_create( |
| 70 | uid_t uid, pid_t pid, const char* processName) { |
| 71 | return new ADynamicInstrumentationManager_TargetProcess(uid, pid, processName); |
| 72 | } |
| 73 | |
| 74 | void ADynamicInstrumentationManager_TargetProcess_destroy( |
| Matt Gilbride | 49caa7d | 2024-11-18 17:40:37 +0000 | [diff] [blame] | 75 | const ADynamicInstrumentationManager_TargetProcess* instance) { |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 76 | delete instance; |
| 77 | } |
| 78 | |
| 79 | struct ADynamicInstrumentationManager_MethodDescriptor { |
| 80 | std::string fqcn; |
| 81 | std::string methodName; |
| 82 | std::vector<std::string> fqParameters; |
| 83 | |
| 84 | ADynamicInstrumentationManager_MethodDescriptor(const char* fqcn, const char* methodName, |
| 85 | const char* fullyQualifiedParameters[], |
| 86 | size_t numParameters) |
| 87 | : fqcn(fqcn), methodName(methodName) { |
| 88 | std::vector<std::string> fqParameters; |
| 89 | fqParameters.reserve(numParameters); |
| 90 | std::copy_n(fullyQualifiedParameters, numParameters, std::back_inserter(fqParameters)); |
| 91 | this->fqParameters = std::move(fqParameters); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | ADynamicInstrumentationManager_MethodDescriptor* |
| 96 | ADynamicInstrumentationManager_MethodDescriptor_create(const char* fullyQualifiedClassName, |
| 97 | const char* methodName, |
| 98 | const char* fullyQualifiedParameters[], |
| 99 | size_t numParameters) { |
| 100 | return new ADynamicInstrumentationManager_MethodDescriptor(fullyQualifiedClassName, methodName, |
| 101 | fullyQualifiedParameters, |
| 102 | numParameters); |
| 103 | } |
| 104 | |
| 105 | void ADynamicInstrumentationManager_MethodDescriptor_destroy( |
| Matt Gilbride | 49caa7d | 2024-11-18 17:40:37 +0000 | [diff] [blame] | 106 | const ADynamicInstrumentationManager_MethodDescriptor* instance) { |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 107 | delete instance; |
| 108 | } |
| 109 | |
| 110 | struct ADynamicInstrumentationManager_ExecutableMethodFileOffsets { |
| 111 | std::string containerPath; |
| 112 | uint64_t containerOffset; |
| 113 | uint64_t methodOffset; |
| 114 | }; |
| 115 | |
| 116 | ADynamicInstrumentationManager_ExecutableMethodFileOffsets* |
| 117 | ADynamicInstrumentationManager_ExecutableMethodFileOffsets_create() { |
| 118 | return new ADynamicInstrumentationManager_ExecutableMethodFileOffsets(); |
| 119 | } |
| 120 | |
| 121 | const char* ADynamicInstrumentationManager_ExecutableMethodFileOffsets_getContainerPath( |
| Matt Gilbride | 49caa7d | 2024-11-18 17:40:37 +0000 | [diff] [blame] | 122 | const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) { |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 123 | return instance->containerPath.c_str(); |
| 124 | } |
| 125 | |
| 126 | uint64_t ADynamicInstrumentationManager_ExecutableMethodFileOffsets_getContainerOffset( |
| Matt Gilbride | 49caa7d | 2024-11-18 17:40:37 +0000 | [diff] [blame] | 127 | const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) { |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 128 | return instance->containerOffset; |
| 129 | } |
| 130 | |
| 131 | uint64_t ADynamicInstrumentationManager_ExecutableMethodFileOffsets_getMethodOffset( |
| Matt Gilbride | 49caa7d | 2024-11-18 17:40:37 +0000 | [diff] [blame] | 132 | const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) { |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 133 | return instance->methodOffset; |
| 134 | } |
| 135 | |
| 136 | void ADynamicInstrumentationManager_ExecutableMethodFileOffsets_destroy( |
| Matt Gilbride | 49caa7d | 2024-11-18 17:40:37 +0000 | [diff] [blame] | 137 | const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) { |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 138 | delete instance; |
| 139 | } |
| 140 | |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 141 | class ResultCallback : public BnOffsetCallback { |
| 142 | public: |
| 143 | ::android::binder::Status onResult( |
| 144 | const ::std::optional<ExecutableMethodFileOffsets>& offsets) override { |
| 145 | promise_.set_value(offsets); |
| 146 | return android::binder::Status::ok(); |
| 147 | } |
| 148 | |
| 149 | std::optional<ExecutableMethodFileOffsets> waitForResult() { |
| 150 | std::future<std::optional<ExecutableMethodFileOffsets>> futureResult = |
| 151 | promise_.get_future(); |
| 152 | auto futureStatus = futureResult.wait_for( |
| 153 | std::chrono::seconds(1 * android::base::HwTimeoutMultiplier())); |
| 154 | if (futureStatus == std::future_status::ready) { |
| 155 | return futureResult.get(); |
| 156 | } else { |
| 157 | return std::nullopt; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | std::promise<std::optional<ExecutableMethodFileOffsets>> promise_; |
| 163 | }; |
| 164 | |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 165 | int32_t ADynamicInstrumentationManager_getExecutableMethodFileOffsets( |
| 166 | const ADynamicInstrumentationManager_TargetProcess* targetProcess, |
| 167 | const ADynamicInstrumentationManager_MethodDescriptor* methodDescriptor, |
| Matt Gilbride | 49caa7d | 2024-11-18 17:40:37 +0000 | [diff] [blame] | 168 | const ADynamicInstrumentationManager_ExecutableMethodFileOffsets** out) { |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 169 | android::os::instrumentation::TargetProcess targetProcessParcel; |
| 170 | targetProcessParcel.uid = targetProcess->uid; |
| 171 | targetProcessParcel.pid = targetProcess->pid; |
| 172 | targetProcessParcel.processName = targetProcess->processName; |
| 173 | |
| 174 | android::os::instrumentation::MethodDescriptor methodDescriptorParcel; |
| 175 | methodDescriptorParcel.fullyQualifiedClassName = methodDescriptor->fqcn; |
| 176 | methodDescriptorParcel.methodName = methodDescriptor->methodName; |
| 177 | methodDescriptorParcel.fullyQualifiedParameters = methodDescriptor->fqParameters; |
| 178 | |
| 179 | sp<os::instrumentation::IDynamicInstrumentationManager> service = getService(); |
| 180 | if (service == nullptr) { |
| 181 | return INVALID_OPERATION; |
| 182 | } |
| 183 | |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 184 | android::sp<ResultCallback> resultCallback = android::sp<ResultCallback>::make(); |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 185 | binder_status_t result = |
| 186 | service->getExecutableMethodFileOffsets(targetProcessParcel, methodDescriptorParcel, |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 187 | resultCallback) |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 188 | .exceptionCode(); |
| 189 | if (result != OK) { |
| 190 | return result; |
| 191 | } |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 192 | std::optional<ExecutableMethodFileOffsets> offsets = resultCallback->waitForResult(); |
| Matt Gilbride | ae83ac2 | 2024-09-24 22:48:38 +0000 | [diff] [blame] | 193 | if (offsets != std::nullopt) { |
| 194 | auto* value = new ADynamicInstrumentationManager_ExecutableMethodFileOffsets(); |
| 195 | value->containerPath = offsets->containerPath; |
| 196 | value->containerOffset = offsets->containerOffset; |
| 197 | value->methodOffset = offsets->methodOffset; |
| 198 | *out = value; |
| 199 | } else { |
| 200 | *out = nullptr; |
| 201 | } |
| 202 | |
| 203 | return result; |
| Yu-Ting Tseng | d857b66 | 2024-11-08 12:42:45 -0800 | [diff] [blame] | 204 | } |