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