blob: 3f3eb1c2eda0ec8cc5965e96e8c423d732c6ff2b [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
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#pragma once
17
18#include <android-base/unique_fd.h>
19#include <binder/IBinder.h>
20#include <binder/Parcel.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000021#include <binder/RpcSession.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000022
23#include <map>
24#include <queue>
25
26namespace android {
27
28struct RpcWireHeader;
29
30/**
31 * Log a lot more information about RPC calls, when debugging issues. Usually,
32 * you would want to enable this in only one process. If repeated issues require
33 * a specific subset of logs to debug, this could be broken up like
34 * IPCThreadState's.
35 */
36#define SHOULD_LOG_RPC_DETAIL false
37
38#if SHOULD_LOG_RPC_DETAIL
39#define LOG_RPC_DETAIL(...) ALOGI(__VA_ARGS__)
40#else
41#define LOG_RPC_DETAIL(...) ALOGV(__VA_ARGS__) // for type checking
42#endif
43
44/**
45 * Abstracts away management of ref counts and the wire format from
Steven Morelandbdb53ab2021-05-05 17:57:41 +000046 * RpcSession
Steven Moreland5553ac42020-11-11 02:14:45 +000047 */
48class RpcState {
49public:
50 RpcState();
51 ~RpcState();
52
Steven Moreland7c5e6c22021-05-01 02:55:20 +000053 // TODO(b/182940634): combine some special transactions into one "getServerInfo" call?
Steven Morelandbdb53ab2021-05-05 17:57:41 +000054 sp<IBinder> getRootObject(const base::unique_fd& fd, const sp<RpcSession>& session);
55 status_t getMaxThreads(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Morelandf137de92021-04-24 01:54:26 +000056 size_t* maxThreadsOut);
Steven Morelandbdb53ab2021-05-05 17:57:41 +000057 status_t getSessionId(const base::unique_fd& fd, const sp<RpcSession>& session,
58 int32_t* sessionIdOut);
Steven Moreland5553ac42020-11-11 02:14:45 +000059
60 [[nodiscard]] status_t transact(const base::unique_fd& fd, const RpcAddress& address,
61 uint32_t code, const Parcel& data,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000062 const sp<RpcSession>& session, Parcel* reply, uint32_t flags);
Steven Moreland5553ac42020-11-11 02:14:45 +000063 [[nodiscard]] status_t sendDecStrong(const base::unique_fd& fd, const RpcAddress& address);
64 [[nodiscard]] status_t getAndExecuteCommand(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +000065 const sp<RpcSession>& session);
Steven Moreland5553ac42020-11-11 02:14:45 +000066
67 /**
68 * Called by Parcel for outgoing binders. This implies one refcount of
69 * ownership to the outgoing binder.
70 */
Steven Morelandbdb53ab2021-05-05 17:57:41 +000071 [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
72 RpcAddress* outAddress);
Steven Moreland5553ac42020-11-11 02:14:45 +000073
74 /**
75 * Called by Parcel for incoming binders. This either returns the refcount
76 * to the process, if this process already has one, or it takes ownership of
77 * that refcount
78 */
Steven Morelandbdb53ab2021-05-05 17:57:41 +000079 sp<IBinder> onBinderEntering(const sp<RpcSession>& session, const RpcAddress& address);
Steven Moreland5553ac42020-11-11 02:14:45 +000080
81 size_t countBinders();
82 void dump();
83
84private:
85 /**
Steven Morelandbdb53ab2021-05-05 17:57:41 +000086 * Called when reading or writing data to a session fails to clean up
87 * data associated with the session in order to cleanup binders.
Steven Moreland5553ac42020-11-11 02:14:45 +000088 * Specifically, we have a strong dependency cycle, since BpBinder is
89 * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
90 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +000091 * BpBinder -> RpcSession -> RpcState
Steven Moreland5553ac42020-11-11 02:14:45 +000092 * ^-----------------------------/
93 *
94 * In the success case, eventually all refcounts should be propagated over
Steven Morelandbdb53ab2021-05-05 17:57:41 +000095 * the session, though this could also be called to eagerly cleanup
96 * the session.
Steven Moreland5553ac42020-11-11 02:14:45 +000097 *
Steven Morelandbdb53ab2021-05-05 17:57:41 +000098 * WARNING: RpcState is responsible for calling this when the session is
Steven Moreland5553ac42020-11-11 02:14:45 +000099 * no longer recoverable.
100 */
101 void terminate();
102
103 [[nodiscard]] bool rpcSend(const base::unique_fd& fd, const char* what, const void* data,
104 size_t size);
105 [[nodiscard]] bool rpcRec(const base::unique_fd& fd, const char* what, void* data, size_t size);
106
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000107 [[nodiscard]] status_t waitForReply(const base::unique_fd& fd, const sp<RpcSession>& session,
108 Parcel* reply);
Steven Moreland5553ac42020-11-11 02:14:45 +0000109 [[nodiscard]] status_t processServerCommand(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000110 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000111 const RpcWireHeader& command);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000112 [[nodiscard]] status_t processTransact(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000113 const RpcWireHeader& command);
114 [[nodiscard]] status_t processTransactInternal(const base::unique_fd& fd,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000115 const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000116 std::vector<uint8_t>&& transactionData);
117 [[nodiscard]] status_t processDecStrong(const base::unique_fd& fd,
118 const RpcWireHeader& command);
119
120 struct BinderNode {
121 // Two cases:
122 // A - local binder we are serving
123 // B - remote binder, we are sending transactions to
124 wp<IBinder> binder;
125
126 // if timesSent > 0, this will be equal to binder.promote()
127 sp<IBinder> sentRef;
128
129 // Number of times we've sent this binder out of process, which
130 // translates to an implicit strong count. A client must send RPC binder
131 // socket's dec ref for each time it is sent out of process in order to
132 // deallocate it. Note, a proxy binder we are holding onto might be
133 // sent (this is important when the only remaining refcount of this
134 // binder is the one associated with a transaction sending it back to
135 // its server)
136 size_t timesSent = 0;
137
138 // Number of times we've received this binder, each time corresponds to
139 // a reference we hold over the wire (not a local incStrong/decStrong)
140 size_t timesRecd = 0;
141
142 // transaction ID, for async transactions
143 uint64_t asyncNumber = 0;
144
145 //
146 // CASE A - local binder we are serving
147 //
148
149 // async transaction queue, _only_ for local binder
150 struct AsyncTodo {
151 std::vector<uint8_t> data; // most convenient format, to move it here
152 uint64_t asyncNumber = 0;
153
154 bool operator<(const AsyncTodo& o) const {
155 return asyncNumber > /* !!! */ o.asyncNumber;
156 }
157 };
158 std::priority_queue<AsyncTodo> asyncTodo;
159
160 //
161 // CASE B - remote binder, we are sending transactions to
162 //
163
164 // (no additional data specific to remote binders)
165 };
166
167 std::mutex mNodeMutex;
168 bool mTerminated = false;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000169 // binders known by both sides of a session
Steven Moreland5553ac42020-11-11 02:14:45 +0000170 std::map<RpcAddress, BinderNode> mNodeForAddress;
171};
172
173} // namespace android