Skip to content

Commit 7c166e1

Browse files
author
Steven Moreland
committed
libbinder: don't hold global locks for callbacks..
... that can be set by other libraries :) Bug: 354286280 Bug: 199683153 Bug: 352692435 Test: boot Change-Id: I8b8b9a243336a45af50fffbddcab13808a4a1bdc
1 parent e2f79ab commit 7c166e1

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

libs/binder/BpBinder.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,12 @@ void BpBinder::ObjectManager::kill()
160160

161161
// ---------------------------------------------------------------------------
162162

163-
sp<BpBinder> BpBinder::create(int32_t handle) {
163+
sp<BpBinder> BpBinder::create(int32_t handle, std::function<void()>* postTask) {
164164
if constexpr (!kEnableKernelIpc) {
165165
LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
166166
return nullptr;
167167
}
168+
LOG_ALWAYS_FATAL_IF(postTask == nullptr, "BAD STATE");
168169

169170
int32_t trackedUid = -1;
170171
if (sCountByUidEnabled) {
@@ -183,7 +184,11 @@ sp<BpBinder> BpBinder::create(int32_t handle) {
183184
ALOGE("Still too many binder proxy objects sent to uid %d from uid %d (%d proxies "
184185
"held)",
185186
getuid(), trackedUid, trackedValue);
186-
if (sLimitCallback) sLimitCallback(trackedUid);
187+
188+
if (sLimitCallback) {
189+
*postTask = [=]() { sLimitCallback(trackedUid); };
190+
}
191+
187192
sLastLimitCallbackMap[trackedUid] = trackedValue;
188193
}
189194
} else {
@@ -197,7 +202,11 @@ sp<BpBinder> BpBinder::create(int32_t handle) {
197202
ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
198203
getuid(), trackedUid, trackedValue);
199204
sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
200-
if (sLimitCallback) sLimitCallback(trackedUid);
205+
206+
if (sLimitCallback) {
207+
*postTask = [=]() { sLimitCallback(trackedUid); };
208+
}
209+
201210
sLastLimitCallbackMap[trackedUid] = trackedValue & COUNTING_VALUE_MASK;
202211
if (sBinderProxyThrottleCreate) {
203212
ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"

libs/binder/ProcessState.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ extern sp<BBinder> the_context_object;
311311
sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
312312
{
313313
sp<IBinder> result;
314+
std::function<void()> postTask;
314315

315316
std::unique_lock<std::mutex> _l(mLock);
316317

@@ -358,7 +359,7 @@ sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
358359
return nullptr;
359360
}
360361

361-
sp<BpBinder> b = BpBinder::PrivateAccessor::create(handle);
362+
sp<BpBinder> b = BpBinder::PrivateAccessor::create(handle, &postTask);
362363
e->binder = b.get();
363364
if (b) e->refs = b->getWeakRefs();
364365
result = b;
@@ -371,6 +372,10 @@ sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
371372
}
372373
}
373374

375+
_l.unlock();
376+
377+
if (postTask) postTask();
378+
374379
return result;
375380
}
376381

libs/binder/include/binder/BpBinder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ class BpBinder : public IBinder {
134134
friend class ::android::RpcState;
135135
explicit PrivateAccessor(const BpBinder* binder) : mBinder(binder) {}
136136

137-
static sp<BpBinder> create(int32_t handle) { return BpBinder::create(handle); }
137+
static sp<BpBinder> create(int32_t handle, std::function<void()>* postTask) {
138+
return BpBinder::create(handle, postTask);
139+
}
138140
static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address) {
139141
return BpBinder::create(session, address);
140142
}
@@ -156,7 +158,7 @@ class BpBinder : public IBinder {
156158
friend PrivateAccessor;
157159
friend class sp<BpBinder>;
158160

159-
static sp<BpBinder> create(int32_t handle);
161+
static sp<BpBinder> create(int32_t handle, std::function<void()>* postTask);
160162
static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address);
161163

162164
struct BinderHandle {

0 commit comments

Comments
 (0)