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