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 #pragma once
17 #include <cutils/native_handle.h>
18 #include <limits>
19 #include <type_traits>
20
21 #include <fmq/MQDescriptorBase.h>
22
23 namespace android {
24 namespace details {
25
26 using hardware::MQFlavor;
27
28 template <typename T, MQFlavor flavor, typename BackendTypes>
29 struct AidlMQDescriptorShimBase {
30 typedef typename BackendTypes::SynchronizedReadWriteType SynchronizedReadWriteType;
31 typedef typename BackendTypes::UnsynchronizedWriteType UnsynchronizedWriteType;
32
33 // Takes ownership of handle
34 AidlMQDescriptorShimBase(const std::vector<android::hardware::GrantorDescriptor>& grantors,
35 native_handle_t* nHandle, size_t size);
36
37 // Takes ownership of handle
38 AidlMQDescriptorShimBase(
39 const typename BackendTypes::template MQDescriptorType<
40 T, typename std::conditional<flavor == hardware::kSynchronizedReadWrite,
41 SynchronizedReadWriteType,
42 UnsynchronizedWriteType>::type>& desc);
43
44 // Takes ownership of handle
45 AidlMQDescriptorShimBase(size_t bufferSize, native_handle_t* nHandle, size_t messageSize,
46 bool configureEventFlag = false);
47
AidlMQDescriptorShimBaseAidlMQDescriptorShimBase48 explicit AidlMQDescriptorShimBase(const AidlMQDescriptorShimBase& other)
49 : AidlMQDescriptorShimBase(0, nullptr, 0) {
50 *this = other;
51 }
52 AidlMQDescriptorShimBase& operator=(const AidlMQDescriptorShimBase& other);
53
54 ~AidlMQDescriptorShimBase();
55
56 size_t getSize() const;
57
58 size_t getQuantum() const;
59
60 uint32_t getFlags() const;
61
isHandleValidAidlMQDescriptorShimBase62 bool isHandleValid() const { return mHandle != nullptr; }
countGrantorsAidlMQDescriptorShimBase63 size_t countGrantors() const { return mGrantors.size(); }
64
grantorsAidlMQDescriptorShimBase65 inline const std::vector<android::hardware::GrantorDescriptor>& grantors() const {
66 return mGrantors;
67 }
68
handleAidlMQDescriptorShimBase69 inline const ::native_handle_t* handle() const { return mHandle; }
70
handleAidlMQDescriptorShimBase71 inline ::native_handle_t* handle() { return mHandle; }
72
73 static const size_t kOffsetOfGrantors;
74 static const size_t kOffsetOfHandle;
75
76 private:
77 std::vector<android::hardware::GrantorDescriptor> mGrantors;
78 native_handle_t* mHandle = nullptr;
79 uint32_t mQuantum = 0;
80 uint32_t mFlags = 0;
81 };
82
83 template <typename T, MQFlavor flavor, typename BackendTypes>
AidlMQDescriptorShimBase(const typename BackendTypes::template MQDescriptorType<T,typename std::conditional<flavor==hardware::kSynchronizedReadWrite,SynchronizedReadWriteType,UnsynchronizedWriteType>::type> & desc)84 AidlMQDescriptorShimBase<T, flavor, BackendTypes>::AidlMQDescriptorShimBase(
85 const typename BackendTypes::template MQDescriptorType<
86 T, typename std::conditional<flavor == hardware::kSynchronizedReadWrite,
87 SynchronizedReadWriteType,
88 UnsynchronizedWriteType>::type>& desc)
89 : mQuantum(desc.quantum), mFlags(desc.flags) {
90 if (desc.quantum < 0 || desc.flags < 0) {
91 // MQDescriptor uses signed integers, but the values must be positive.
92 hardware::details::logError("Invalid MQDescriptor. Values must be positive. quantum: " +
93 std::to_string(desc.quantum) +
94 ". flags: " + std::to_string(desc.flags));
95 return;
96 }
97
98 mGrantors.resize(desc.grantors.size());
99 for (size_t i = 0; i < desc.grantors.size(); ++i) {
100 if (desc.grantors[i].offset < 0 || desc.grantors[i].extent < 0 ||
101 desc.grantors[i].fdIndex < 0) {
102 // GrantorDescriptor uses signed integers, but the values must be positive.
103 // Return before setting up the native_handle to make this invalid.
104 hardware::details::logError(
105 "Invalid MQDescriptor grantors. Values must be positive. Grantor index: " +
106 std::to_string(i) + ". offset: " + std::to_string(desc.grantors[i].offset) +
107 ". extent: " + std::to_string(desc.grantors[i].extent));
108 return;
109 }
110 mGrantors[i].flags = 0;
111 mGrantors[i].fdIndex = desc.grantors[i].fdIndex;
112 mGrantors[i].offset = desc.grantors[i].offset;
113 mGrantors[i].extent = desc.grantors[i].extent;
114 }
115
116 mHandle = native_handle_create(desc.handle.fds.size() /* num fds */,
117 desc.handle.ints.size() /* num ints */);
118 if (mHandle == nullptr) {
119 hardware::details::logError("Null native_handle_t");
120 return;
121 }
122 int data_index = 0;
123 for (const auto& fd : desc.handle.fds) {
124 mHandle->data[data_index] = dup(fd.get());
125 data_index++;
126 }
127 for (const auto& data_int : desc.handle.ints) {
128 mHandle->data[data_index] = data_int;
129 data_index++;
130 }
131 }
132
133 template <typename T, MQFlavor flavor, typename BackendTypes>
AidlMQDescriptorShimBase(const std::vector<android::hardware::GrantorDescriptor> & grantors,native_handle_t * nhandle,size_t size)134 AidlMQDescriptorShimBase<T, flavor, BackendTypes>::AidlMQDescriptorShimBase(
135 const std::vector<android::hardware::GrantorDescriptor>& grantors, native_handle_t* nhandle,
136 size_t size)
137 : mGrantors(grantors),
138 mHandle(nhandle),
139 mQuantum(static_cast<uint32_t>(size)),
140 mFlags(flavor) {}
141
142 template <typename T, MQFlavor flavor, typename BackendTypes>
143 AidlMQDescriptorShimBase<T, flavor, BackendTypes>&
144 AidlMQDescriptorShimBase<T, flavor, BackendTypes>::operator=(
145 const AidlMQDescriptorShimBase& other) {
146 mGrantors = other.mGrantors;
147 if (mHandle != nullptr) {
148 native_handle_close(mHandle);
149 native_handle_delete(mHandle);
150 mHandle = nullptr;
151 }
152 mQuantum = other.mQuantum;
153 mFlags = other.mFlags;
154
155 if (other.mHandle != nullptr) {
156 mHandle = native_handle_create(other.mHandle->numFds, other.mHandle->numInts);
157
158 for (int i = 0; i < other.mHandle->numFds; ++i) {
159 mHandle->data[i] = dup(other.mHandle->data[i]);
160 }
161
162 memcpy(&mHandle->data[other.mHandle->numFds], &other.mHandle->data[other.mHandle->numFds],
163 static_cast<size_t>(other.mHandle->numInts) * sizeof(int));
164 }
165
166 return *this;
167 }
168
169 template <typename T, MQFlavor flavor, typename BackendTypes>
AidlMQDescriptorShimBase(size_t bufferSize,native_handle_t * nHandle,size_t messageSize,bool configureEventFlag)170 AidlMQDescriptorShimBase<T, flavor, BackendTypes>::AidlMQDescriptorShimBase(
171 size_t bufferSize, native_handle_t* nHandle, size_t messageSize, bool configureEventFlag)
172 : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) {
173 /*
174 * TODO(b/165674950) Since AIDL does not support unsigned integers, it can only support
175 * The offset of EventFlag word needs to fit into an int32_t in MQDescriptor. This word comes
176 * after the readPtr, writePtr, and dataBuffer.
177 */
178 bool overflow = bufferSize > std::numeric_limits<uint64_t>::max() -
179 (sizeof(hardware::details::RingBufferPosition) +
180 sizeof(hardware::details::RingBufferPosition));
181 uint64_t largestOffset = hardware::details::alignToWordBoundary(
182 sizeof(hardware::details::RingBufferPosition) +
183 sizeof(hardware::details::RingBufferPosition) + bufferSize);
184 if (overflow || largestOffset > std::numeric_limits<int32_t>::max() ||
185 messageSize > std::numeric_limits<int32_t>::max()) {
186 hardware::details::logError(
187 "Queue size is too large. Message size: " + std::to_string(messageSize) +
188 " bytes. Data buffer size: " + std::to_string(bufferSize) + " bytes. Max size: " +
189 std::to_string(std::numeric_limits<int32_t>::max()) + " bytes.");
190 return;
191 }
192
193 /*
194 * If configureEventFlag is true, allocate an additional spot in mGrantor
195 * for containing the fd and offset for mmapping the EventFlag word.
196 */
197 mGrantors.resize(configureEventFlag ? hardware::details::kMinGrantorCountForEvFlagSupport
198 : hardware::details::kMinGrantorCount);
199
200 size_t memSize[] = {
201 sizeof(hardware::details::RingBufferPosition), /* memory to be allocated for read
202 * pointer counter
203 */
204 sizeof(hardware::details::RingBufferPosition), /* memory to be allocated for write
205 pointer counter */
206 bufferSize, /* memory to be allocated for data buffer */
207 sizeof(std::atomic<uint32_t>) /* memory to be allocated for EventFlag word */
208 };
209
210 /*
211 * Create a default grantor descriptor for read, write pointers and
212 * the data buffer. fdIndex parameter is set to 0 by default and
213 * the offset for each grantor is contiguous.
214 */
215 for (size_t grantorPos = 0, offset = 0; grantorPos < mGrantors.size();
216 offset += memSize[grantorPos++]) {
217 mGrantors[grantorPos] = {
218 0 /* grantor flags */, 0 /* fdIndex */,
219 static_cast<uint32_t>(hardware::details::alignToWordBoundary(offset)),
220 memSize[grantorPos]};
221 }
222 }
223
224 template <typename T, MQFlavor flavor, typename BackendTypes>
~AidlMQDescriptorShimBase()225 AidlMQDescriptorShimBase<T, flavor, BackendTypes>::~AidlMQDescriptorShimBase() {
226 if (mHandle != nullptr) {
227 native_handle_close(mHandle);
228 native_handle_delete(mHandle);
229 }
230 }
231
232 template <typename T, MQFlavor flavor, typename BackendTypes>
getSize()233 size_t AidlMQDescriptorShimBase<T, flavor, BackendTypes>::getSize() const {
234 if (mGrantors.size() > hardware::details::DATAPTRPOS) {
235 return mGrantors[hardware::details::DATAPTRPOS].extent;
236 } else {
237 return 0;
238 }
239 }
240
241 template <typename T, MQFlavor flavor, typename BackendTypes>
getQuantum()242 size_t AidlMQDescriptorShimBase<T, flavor, BackendTypes>::getQuantum() const {
243 return mQuantum;
244 }
245
246 template <typename T, MQFlavor flavor, typename BackendTypes>
getFlags()247 uint32_t AidlMQDescriptorShimBase<T, flavor, BackendTypes>::getFlags() const {
248 return mFlags;
249 }
250
251 template <typename T, MQFlavor flavor, typename BackendTypes>
toString(const AidlMQDescriptorShimBase<T,flavor,BackendTypes> & q)252 std::string toString(const AidlMQDescriptorShimBase<T, flavor, BackendTypes>& q) {
253 std::string os;
254 if (flavor & hardware::kSynchronizedReadWrite) {
255 os += "fmq_sync";
256 }
257 if (flavor & hardware::kUnsynchronizedWrite) {
258 os += "fmq_unsync";
259 }
260 os += " {" + toString(q.grantors().size()) + " grantor(s), " +
261 "size = " + toString(q.getSize()) + ", .handle = " + toString(q.handle()) +
262 ", .quantum = " + toString(q.getQuantum()) + "}";
263 return os;
264 }
265
266 } // namespace details
267 } // namespace android
268