1 /*
2 * Copyright (c) 2022 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 #include "ash_mem_utils.h"
17
18 #include <string>
19
20 #include "hilog/log.h"
21 #include "string_ex.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HiView-SharedMemory-Util" };
27 constexpr char ASH_MEM_NAME[] = "HiSysEventService SharedMemory";
28 constexpr int32_t ASH_MEM_SIZE = 1024 * 769; // 769k
29
ParseAllStringItemSize(const std::vector<std::u16string> & data,std::vector<std::string> & translatedData,std::vector<uint32_t> & allSize)30 void ParseAllStringItemSize(const std::vector<std::u16string>& data, std::vector<std::string>& translatedData,
31 std::vector<uint32_t>& allSize)
32 {
33 for (auto& item : data) {
34 auto translated = Str16ToStr8(item);
35 translatedData.emplace_back(translated);
36 allSize.emplace_back(strlen(translated.c_str()) + 1);
37 }
38 }
39 }
40
GetAshmem()41 sptr<Ashmem> AshMemUtils::GetAshmem()
42 {
43 auto ashmem = Ashmem::CreateAshmem(ASH_MEM_NAME, ASH_MEM_SIZE);
44 if (ashmem == nullptr) {
45 HiLog::Error(LABEL, "ashmem init failed.");
46 return ashmem;
47 }
48 if (!ashmem->MapReadAndWriteAshmem()) {
49 HiLog::Error(LABEL, "ashmem map failed.");
50 return ashmem;
51 }
52 HiLog::Info(LABEL, "ashmem init succeed.");
53 return ashmem;
54 }
55
CloseAshmem(sptr<Ashmem> ashmem)56 void AshMemUtils::CloseAshmem(sptr<Ashmem> ashmem)
57 {
58 if (ashmem != nullptr) {
59 ashmem->UnmapAshmem();
60 ashmem->CloseAshmem();
61 }
62 HiLog::Info(LABEL, "ashmem closed.");
63 }
64
WriteBulkData(MessageParcel & parcel,const std::vector<std::u16string> & src)65 sptr<Ashmem> AshMemUtils::WriteBulkData(MessageParcel& parcel, const std::vector<std::u16string>& src)
66 {
67 std::vector<std::string> allData;
68 std::vector<uint32_t> allSize;
69 ParseAllStringItemSize(src, allData, allSize);
70 if (!parcel.WriteUInt32Vector(allSize)) {
71 HiLog::Error(LABEL, "writing allSize array failed.");
72 return nullptr;
73 }
74 auto ashmem = GetAshmem();
75 if (ashmem == nullptr) {
76 return nullptr;
77 }
78 uint32_t offset = 0;
79 for (uint32_t i = 0; i < allData.size(); i++) {
80 auto translated = allData[i].c_str();
81 if (!ashmem->WriteToAshmem(translated, strlen(translated), offset)) {
82 HiLog::Error(LABEL, "writing ashmem failed.");
83 CloseAshmem(ashmem);
84 return nullptr;
85 }
86 offset += allSize[i];
87 }
88 if (!parcel.WriteAshmem(ashmem)) {
89 HiLog::Error(LABEL, "writing ashmem failed.");
90 CloseAshmem(ashmem);
91 return nullptr;
92 }
93 return ashmem;
94 }
95
ReadBulkData(MessageParcel & parcel,std::vector<std::u16string> & dest)96 bool AshMemUtils::ReadBulkData(MessageParcel& parcel, std::vector<std::u16string>& dest)
97 {
98 std::vector<uint32_t> allSize;
99 if (!parcel.ReadUInt32Vector(&allSize)) {
100 HiLog::Error(LABEL, "reading allSize array failed.");
101 return false;
102 }
103 auto ashmem = parcel.ReadAshmem();
104 if (ashmem == nullptr) {
105 HiLog::Error(LABEL, "reading ashmem failed.");
106 return false;
107 }
108 bool ret = ashmem->MapReadAndWriteAshmem();
109 if (!ret) {
110 HiLog::Error(LABEL, "mapping read only ashmem failed.");
111 CloseAshmem(ashmem);
112 return false;
113 }
114 uint32_t offset = 0;
115 for (uint32_t i = 0; i < allSize.size(); i++) {
116 auto origin = ashmem->ReadFromAshmem(allSize[i], offset);
117 dest.emplace_back(Str8ToStr16(std::string(reinterpret_cast<const char*>(origin))));
118 offset += allSize[i];
119 }
120 CloseAshmem(ashmem);
121 return true;
122 }
123 } // namespace HiviewDFX
124 } // namespace OHOS
125