1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include <sys/mman.h>
18 #include <unistd.h>
19 #include "ashmem.h"
20 #include "avsession_errors.h"
21 #include "avsession_log.h"
22 #include "securec.h"
23 #include "av_shared_memory_base.h"
24
25 namespace {
26 static std::atomic<uint64_t> g_uniqueSharedMemoryID = 0;
27 }
28
29 namespace OHOS {
30 namespace AVSession {
31 struct AVSharedMemoryBaseImpl : public AVSharedMemoryBase {
32 public:
AVSharedMemoryBaseImplOHOS::AVSession::AVSharedMemoryBaseImpl33 AVSharedMemoryBaseImpl(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
34 : AVSharedMemoryBase(fd, size, flags, name) {}
35 };
36
Marshalling(Parcel & out) const37 bool AVSharedMemoryBase::Marshalling(Parcel& out) const
38 {
39 return WriteToParcel(static_cast<MessageParcel&>(out));
40 }
41
Unmarshalling(Parcel & in)42 AVSharedMemoryBase* AVSharedMemoryBase::Unmarshalling(Parcel& in)
43 {
44 MessageParcel& parcel = static_cast<MessageParcel&>(in);
45 int32_t fd = parcel.ReadFileDescriptor();
46 CHECK_AND_RETURN_RET_LOG(fd >= 0, nullptr, "read fd is invalid");
47
48 int32_t size = parcel.ReadInt32();
49 uint32_t flags = parcel.ReadUint32();
50 std::string name = parcel.ReadString();
51
52 AVSharedMemoryBase* memory = new (std::nothrow) AVSharedMemoryBaseImpl(fd, size, flags, name);
53 CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "create memory fail");
54 int32_t ret = memory->Init();
55 (void)::close(fd);
56 bool isErr = ret != static_cast<int32_t>(AVSESSION_SUCCESS) || memory->GetBase() == nullptr;
57 if (isErr) {
58 SLOGE("create memory failed");
59 delete memory;
60 return nullptr;
61 }
62 return memory;
63 }
64
WriteToParcel(MessageParcel & out) const65 bool AVSharedMemoryBase::WriteToParcel(MessageParcel& out) const
66 {
67 int32_t fd = GetFd();
68 CHECK_AND_RETURN_RET_LOG(fd >= 0, false, "write fd is invalid");
69
70 int32_t size = GetSize();
71 bool res = out.WriteFileDescriptor(fd);
72 CHECK_AND_RETURN_RET_LOG(res, false, "write fd failed");
73 return out.WriteInt32(size) && out.WriteUint32(GetFlags()) && out.WriteString(GetName());
74 }
75
CreateFromLocal(int32_t size,uint32_t flags,const std::string & name)76 std::shared_ptr<AVSharedMemoryBase> AVSharedMemoryBase::CreateFromLocal(
77 int32_t size, uint32_t flags, const std::string &name)
78 {
79 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
80 int32_t ret = memory->Init();
81 if (ret != static_cast<int32_t>(AVSESSION_SUCCESS)) {
82 SLOGE("Create AVSharedMemoryBase failed, ret = %{public}d", ret);
83 return nullptr;
84 }
85
86 return memory;
87 }
88
CreateFromRemote(int32_t fd,int32_t size,uint32_t flags,const std::string & name)89 std::shared_ptr<AVSharedMemoryBase> AVSharedMemoryBase::CreateFromRemote(
90 int32_t fd, int32_t size, uint32_t flags, const std::string &name)
91 {
92 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBaseImpl>(fd, size, flags, name);
93 CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "create memory fail");
94 int32_t ret = memory->Init();
95 if (ret != static_cast<int32_t>(AVSESSION_SUCCESS)) {
96 SLOGE("Create AVSharedMemoryBase failed, ret = %{public}d", ret);
97 return nullptr;
98 }
99 return memory;
100 }
101
AVSharedMemoryBase(int32_t size,uint32_t flags,const std::string & name)102 AVSharedMemoryBase::AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name)
103 : base_(nullptr), capacity_(size), flags_(flags), name_(name), fd_(-1), size_(0)
104 {
105 SLOGD("AVSharedMemoryBase size in name = %{public}s", name_.c_str());
106 uniqueSharedMemoryID_ = g_uniqueSharedMemoryID++;
107 }
108
AVSharedMemoryBase(int32_t fd,int32_t size,uint32_t flags,const std::string & name)109 AVSharedMemoryBase::AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
110 : base_(nullptr), capacity_(size), flags_(flags), name_(name), fd_(dup(fd)), size_(0)
111 {
112 SLOGD("AVSharedMemoryBase fd in, name = %{public}s", name_.c_str());
113 uniqueSharedMemoryID_ = g_uniqueSharedMemoryID++;
114 }
115
~AVSharedMemoryBase()116 AVSharedMemoryBase::~AVSharedMemoryBase()
117 {
118 SLOGD("~AVSharedMemoryBase, name = %{public}s", name_.c_str());
119 Close();
120 }
121
Init(bool isMapVirAddr)122 int32_t AVSharedMemoryBase::Init(bool isMapVirAddr)
123 {
124 if (capacity_ <= 0) {
125 SLOGE("size is invalid, size = %{public}d", capacity_);
126 return static_cast<int32_t>(ERR_INVALID_PARAM);
127 }
128
129 bool isRemote = false;
130 if (fd_ > 0) {
131 int size = AshmemGetSize(fd_);
132 if (size != capacity_) {
133 SLOGE("size not equal capacity_, size = %{public}d, capacity_ = %{public}d", size, capacity_);
134 return static_cast<int32_t>(ERR_INVALID_PARAM);
135 }
136 isRemote = true;
137 } else {
138 fd_ = AshmemCreate(name_.c_str(), static_cast<size_t>(capacity_));
139 if (fd_ <= 0) {
140 SLOGE("fd is invalid, fd = %{public}d", fd_);
141 return static_cast<int32_t>(ERR_INVALID_PARAM);
142 }
143 }
144 if (isMapVirAddr) {
145 int32_t ret = MapMemory(isRemote);
146 if (ret != static_cast<int32_t>(AVSESSION_SUCCESS)) {
147 SLOGE("MapMemory failed, ret = %{public}d", ret);
148 return static_cast<int32_t>(ERR_INVALID_PARAM);
149 }
150 }
151 return static_cast<int32_t>(AVSESSION_SUCCESS);
152 }
153
MapMemory(bool isRemote)154 int32_t AVSharedMemoryBase::MapMemory(bool isRemote)
155 {
156 unsigned int prot = PROT_READ | PROT_WRITE;
157 if (isRemote && (flags_ & FLAGS_READ_ONLY)) {
158 prot &= ~PROT_WRITE;
159 }
160
161 int result = AshmemSetProt(fd_, static_cast<int>(prot));
162 if (result < 0) {
163 SLOGE("AshmemSetProt failed, result = %{public}d", result);
164 return static_cast<int32_t>(AVSESSION_ERROR);
165 }
166
167 void *addr = ::mmap(nullptr, static_cast<size_t>(capacity_), static_cast<int>(prot), MAP_SHARED, fd_, 0);
168 if (addr == MAP_FAILED) {
169 SLOGE("mmap failed, please check params");
170 return static_cast<int32_t>(AVSESSION_ERROR);
171 }
172
173 base_ = reinterpret_cast<uint8_t*>(addr);
174 return static_cast<int32_t>(AVSESSION_SUCCESS);
175 }
176
Close()177 void AVSharedMemoryBase::Close() noexcept
178 {
179 if (base_ != nullptr) {
180 (void)::munmap(base_, static_cast<size_t>(capacity_));
181 base_ = nullptr;
182 capacity_ = 0;
183 flags_ = 0;
184 size_ = 0;
185 }
186 if (fd_ > 0) {
187 (void)::close(fd_);
188 fd_ = -1;
189 }
190 }
191
Write(const uint8_t * in,int32_t writeSize,int32_t position)192 int32_t AVSharedMemoryBase::Write(const uint8_t *in, int32_t writeSize, int32_t position)
193 {
194 if (in == nullptr) {
195 SLOGE("Input buffer is nullptr");
196 return 0;
197 }
198 if (writeSize <= 0) {
199 SLOGE("Input writeSize:%{public}d is invalid", writeSize);
200 return 0;
201 }
202 int32_t start = 0;
203 if (position == INVALID_POSITION) {
204 start = size_;
205 } else {
206 start = std::min(position, capacity_);
207 }
208 int32_t unusedSize = capacity_ - start;
209 int32_t length = std::min(writeSize, unusedSize);
210 SLOGD("write data,length:%{public}d, start:%{public}d, name:%{public}s", length, start, name_.c_str());
211 if ((length + start) > capacity_) {
212 SLOGE("Write out of bounds, length:%{public}d, "
213 "start:%{public}d, capacity_:%{public}d", length, start, capacity_);
214 return 0;
215 }
216 uint8_t *dstPtr = base_ + start;
217 if (dstPtr == nullptr) {
218 SLOGE("Inner dstPtr is nullptr");
219 return 0;
220 }
221 auto error = memcpy_s(dstPtr, length, in, length);
222 if (error != EOK) {
223 SLOGE("Inner memcpy_s failed,name:%{public}s, %{public}s", name_.c_str(), strerror(error));
224 return 0;
225 }
226 size_ = start + length;
227 return length;
228 }
229
Read(uint8_t * out,int32_t readSize,int32_t position)230 int32_t AVSharedMemoryBase::Read(uint8_t *out, int32_t readSize, int32_t position)
231 {
232 if (out == nullptr) {
233 SLOGE("Input buffer is nullptr");
234 return 0;
235 }
236 if (readSize <= 0) {
237 SLOGE("Input readSize:%{public}d is invalid", readSize);
238 return 0;
239 }
240 int32_t start = 0;
241 int32_t maxLength = size_;
242 if (position != INVALID_POSITION) {
243 start = std::min(position, size_);
244 maxLength = size_ - start;
245 }
246 int32_t length = std::min(readSize, maxLength);
247 if ((length + start) > capacity_) {
248 SLOGE("Read out of bounds, length:%{public}d, "
249 "start:%{public}d, capacity_:%{public}d", length, start, capacity_);
250 return 0;
251 }
252 uint8_t *srcPtr = base_ + start;
253 if (srcPtr == nullptr) {
254 SLOGE("Inner srcPtr is nullptr");
255 return 0;
256 }
257 auto error = memcpy_s(out, length, srcPtr, length);
258 if (error != EOK) {
259 SLOGE("Inner memcpy_s failed,name:%{public}s, %{public}s", name_.c_str(), strerror(error));
260 return 0;
261 }
262 return length;
263 }
264
ClearUsedSize()265 void AVSharedMemoryBase::ClearUsedSize()
266 {
267 size_ = 0;
268 }
269
GetUsedSize() const270 int32_t AVSharedMemoryBase::GetUsedSize() const
271 {
272 return size_;
273 }
274 } // namespace AVSession
275 } // namespace OHOS