1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "ADynamicInstrumentationManager"
#include <android-base/properties.h>
#include <android/dynamic_instrumentation_manager.h>
#include <android/os/instrumentation/BnOffsetCallback.h>
#include <android/os/instrumentation/ExecutableMethodFileOffsets.h>
#include <android/os/instrumentation/IDynamicInstrumentationManager.h>
#include <android/os/instrumentation/MethodDescriptor.h>
#include <android/os/instrumentation/TargetProcess.h>
#include <binder/Binder.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <utils/StrongPointer.h>
#include <future>
#include <mutex>
#include <optional>
#include <string>
#include <vector>
namespace android::dynamicinstrumentationmanager {
using android::os::instrumentation::BnOffsetCallback;
using android::os::instrumentation::ExecutableMethodFileOffsets;
// Global instance of IDynamicInstrumentationManager, service is obtained only on first use.
static std::mutex mLock;
static sp<os::instrumentation::IDynamicInstrumentationManager> mService;
sp<os::instrumentation::IDynamicInstrumentationManager> getService() {
std::lock_guard<std::mutex> scoped_lock(mLock);
if (mService == nullptr || !IInterface::asBinder(mService)->isBinderAlive()) {
sp<IBinder> binder =
defaultServiceManager()->waitForService(String16("dynamic_instrumentation"));
mService = interface_cast<os::instrumentation::IDynamicInstrumentationManager>(binder);
}
return mService;
}
} // namespace android::dynamicinstrumentationmanager
using namespace android;
using namespace dynamicinstrumentationmanager;
struct ADynamicInstrumentationManager_TargetProcess {
uid_t uid;
uid_t pid;
std::string processName;
ADynamicInstrumentationManager_TargetProcess(uid_t uid, pid_t pid, const char* processName)
: uid(uid), pid(pid), processName(processName) {}
};
ADynamicInstrumentationManager_TargetProcess* ADynamicInstrumentationManager_TargetProcess_create(
uid_t uid, pid_t pid, const char* processName) {
return new ADynamicInstrumentationManager_TargetProcess(uid, pid, processName);
}
void ADynamicInstrumentationManager_TargetProcess_destroy(
const ADynamicInstrumentationManager_TargetProcess* instance) {
if (instance == nullptr) return;
delete instance;
}
struct ADynamicInstrumentationManager_MethodDescriptor {
std::string fqcn;
std::string methodName;
std::vector<std::string> fqParameters;
ADynamicInstrumentationManager_MethodDescriptor(const char* fqcn, const char* methodName,
const char* fullyQualifiedParameters[],
size_t numParameters)
: fqcn(fqcn), methodName(methodName) {
std::vector<std::string> fqParameters;
fqParameters.reserve(numParameters);
std::copy_n(fullyQualifiedParameters, numParameters, std::back_inserter(fqParameters));
this->fqParameters = std::move(fqParameters);
}
};
ADynamicInstrumentationManager_MethodDescriptor*
ADynamicInstrumentationManager_MethodDescriptor_create(const char* fullyQualifiedClassName,
const char* methodName,
const char* fullyQualifiedParameters[],
size_t numParameters) {
return new ADynamicInstrumentationManager_MethodDescriptor(fullyQualifiedClassName, methodName,
fullyQualifiedParameters,
numParameters);
}
void ADynamicInstrumentationManager_MethodDescriptor_destroy(
const ADynamicInstrumentationManager_MethodDescriptor* instance) {
if (instance == nullptr) return;
delete instance;
}
struct ADynamicInstrumentationManager_ExecutableMethodFileOffsets {
std::string containerPath;
uint64_t containerOffset;
uint64_t methodOffset;
};
ADynamicInstrumentationManager_ExecutableMethodFileOffsets*
ADynamicInstrumentationManager_ExecutableMethodFileOffsets_create() {
return new ADynamicInstrumentationManager_ExecutableMethodFileOffsets();
}
const char* ADynamicInstrumentationManager_ExecutableMethodFileOffsets_getContainerPath(
const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) {
return instance->containerPath.c_str();
}
uint64_t ADynamicInstrumentationManager_ExecutableMethodFileOffsets_getContainerOffset(
const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) {
return instance->containerOffset;
}
uint64_t ADynamicInstrumentationManager_ExecutableMethodFileOffsets_getMethodOffset(
const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) {
return instance->methodOffset;
}
void ADynamicInstrumentationManager_ExecutableMethodFileOffsets_destroy(
const ADynamicInstrumentationManager_ExecutableMethodFileOffsets* instance) {
if (instance == nullptr) return;
delete instance;
}
class ResultCallback : public BnOffsetCallback {
public:
::android::binder::Status onResult(
const ::std::optional<ExecutableMethodFileOffsets>& offsets) override {
promise_.set_value(offsets);
return android::binder::Status::ok();
}
std::optional<ExecutableMethodFileOffsets> waitForResult() {
std::future<std::optional<ExecutableMethodFileOffsets>> futureResult =
promise_.get_future();
auto futureStatus = futureResult.wait_for(
std::chrono::seconds(1 * android::base::HwTimeoutMultiplier()));
if (futureStatus == std::future_status::ready) {
return futureResult.get();
} else {
return std::nullopt;
}
}
private:
std::promise<std::optional<ExecutableMethodFileOffsets>> promise_;
};
int32_t ADynamicInstrumentationManager_getExecutableMethodFileOffsets(
const ADynamicInstrumentationManager_TargetProcess* targetProcess,
const ADynamicInstrumentationManager_MethodDescriptor* methodDescriptor,
const ADynamicInstrumentationManager_ExecutableMethodFileOffsets** out) {
android::os::instrumentation::TargetProcess targetProcessParcel;
targetProcessParcel.uid = targetProcess->uid;
targetProcessParcel.pid = targetProcess->pid;
targetProcessParcel.processName = targetProcess->processName;
android::os::instrumentation::MethodDescriptor methodDescriptorParcel;
methodDescriptorParcel.fullyQualifiedClassName = methodDescriptor->fqcn;
methodDescriptorParcel.methodName = methodDescriptor->methodName;
methodDescriptorParcel.fullyQualifiedParameters = methodDescriptor->fqParameters;
sp<os::instrumentation::IDynamicInstrumentationManager> service = getService();
if (service == nullptr) {
return INVALID_OPERATION;
}
android::sp<ResultCallback> resultCallback = android::sp<ResultCallback>::make();
binder_status_t result =
service->getExecutableMethodFileOffsets(targetProcessParcel, methodDescriptorParcel,
resultCallback)
.exceptionCode();
if (result != OK) {
return result;
}
std::optional<ExecutableMethodFileOffsets> offsets = resultCallback->waitForResult();
if (offsets != std::nullopt) {
auto* value = new ADynamicInstrumentationManager_ExecutableMethodFileOffsets();
value->containerPath = offsets->containerPath;
value->containerOffset = offsets->containerOffset;
value->methodOffset = offsets->methodOffset;
*out = value;
} else {
*out = nullptr;
}
return result;
}
|