1 /*
2 * Copyright (c) 2023 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 "ecmascript/pgo_profiler/ap_file/pgo_file_info.h"
17 namespace panda::ecmascript::pgo {
18 using StringHelper = base::StringHelper;
19 bool PGOProfilerHeader::strictMatch_ = true;
20
BuildFromLegacy(void * buffer,PGOProfilerHeader ** header)21 bool PGOProfilerHeader::BuildFromLegacy(void *buffer, PGOProfilerHeader **header)
22 {
23 auto *inHeader = reinterpret_cast<PGOProfilerHeaderLegacy *>(buffer);
24 size_t desSize = Size(inHeader->GetSectionNumber());
25 if (desSize > LastSize()) {
26 LOG_ECMA(ERROR) << "header size error, expected size is less than " << LastSize() << ", but got " << desSize;
27 return false;
28 }
29 Build(header, desSize);
30 // copy header base.
31 if (memcpy_s(*header, sizeof(FileHeaderBase), inHeader, sizeof(FileHeaderBase)) != EOK) {
32 UNREACHABLE();
33 }
34 // skip elastic header field, and copy section info from incoming buffer.
35 auto sectionSize = desSize - sizeof(FileHeaderElastic);
36 if (memcpy_s(&((*header)->sectionNumber_), sectionSize, &(inHeader->GetSectionNumber()), sectionSize) != EOK) {
37 UNREACHABLE();
38 }
39 return true;
40 }
41
BuildFromElastic(void * buffer,size_t bufferSize,PGOProfilerHeader ** header)42 bool PGOProfilerHeader::BuildFromElastic(void *buffer, size_t bufferSize, PGOProfilerHeader **header)
43 {
44 auto *inHeader = reinterpret_cast<PGOProfilerHeader *>(buffer);
45 if (!inHeader->Verify(buffer, bufferSize)) {
46 return false;
47 }
48 size_t desSize = inHeader->Size();
49 if (desSize > LastSize()) {
50 LOG_ECMA(ERROR) << "header size error, expected size is less than " << LastSize() << ", but got " << desSize;
51 return false;
52 }
53 Build(header, desSize);
54 if (memcpy_s(*header, desSize, inHeader, desSize) != EOK) {
55 UNREACHABLE();
56 }
57 return true;
58 }
59
ParseFromBinary(void * buffer,size_t bufferSize,PGOProfilerHeader ** header)60 bool PGOProfilerHeader::ParseFromBinary(void *buffer, size_t bufferSize, PGOProfilerHeader **header)
61 {
62 auto *inHeaderBase = reinterpret_cast<FileHeaderBase *>(buffer);
63 if (inHeaderBase->VerifyVersion("ap file", LAST_VERSION, IsStrictMatch())) {
64 if (!inHeaderBase->CompatibleVerify(ELASTIC_HEADER_MINI_VERSION)) {
65 return BuildFromLegacy(buffer, header);
66 }
67 return BuildFromElastic(buffer, bufferSize, header);
68 }
69 return false;
70 }
71
VerifyFileSize(size_t bufferSize) const72 bool PGOProfilerHeader::VerifyFileSize(size_t bufferSize) const
73 {
74 if (!SupportFileSize()) {
75 return true;
76 }
77 if (GetFileSize() != bufferSize) {
78 LOG_ECMA(ERROR) << "Verify ap file's file size failed. size: " << std::hex << bufferSize << " vs "
79 << GetFileSize();
80 return false;
81 }
82 return true;
83 }
84
VerifyConsistency(void * buffer,size_t bufferSize) const85 bool PGOProfilerHeader::VerifyConsistency(void *buffer, size_t bufferSize) const
86 {
87 if (!SupportFileConsistency()) {
88 return true;
89 }
90 uint32_t checksum = adler32(0, reinterpret_cast<const Bytef *>(buffer) + MAGIC_SIZE, VERSION_SIZE);
91 checksum = adler32(checksum, reinterpret_cast<const Bytef *>(buffer) + CHECKSUM_END_OFFSET,
92 bufferSize - CHECKSUM_END_OFFSET);
93 if (checksum != GetChecksum()) {
94 LOG_ECMA(ERROR) << "Verify ap file's consistency failed. checksum: " << std::hex << checksum << " vs "
95 << std::hex << GetChecksum();
96 return false;
97 }
98 return true;
99 }
100
ProcessToBinary(std::fstream & fileStream) const101 void PGOProfilerHeader::ProcessToBinary(std::fstream &fileStream) const
102 {
103 fileStream.seekp(0);
104 if (base::FileHeaderBase::CompatibleVerify(ELASTIC_HEADER_MINI_VERSION)) {
105 fileStream.write(reinterpret_cast<const char *>(this), Size());
106 } else {
107 // copy header base.
108 fileStream.write(reinterpret_cast<const char *>(this), sizeof(FileHeaderBase));
109 // skip elastic header field, and copy section info from incoming buffer.
110 auto sectionSize = Size() - sizeof(FileHeaderElastic);
111 fileStream.write(reinterpret_cast<const char *>(§ionNumber_), sectionSize);
112 }
113 }
114
ParseFromText(std::ifstream & stream)115 bool PGOProfilerHeader::ParseFromText(std::ifstream &stream)
116 {
117 std::string header;
118 if (std::getline(stream, header)) {
119 if (header.empty()) {
120 return false;
121 }
122 auto index = header.find(DumpUtils::BLOCK_START);
123 if (index == std::string::npos) {
124 return false;
125 }
126 auto version = header.substr(index + 1);
127 if (!InternalSetVersion(version)) {
128 return false;
129 }
130 if (!Verify()) {
131 return false;
132 }
133 if (!base::FileHeaderBase::CompatibleVerify(ELASTIC_HEADER_MINI_VERSION)) {
134 auto *pandaInfoSection = GetPandaInfoSection();
135 if (pandaInfoSection == nullptr) {
136 return false;
137 }
138 pandaInfoSection->offset_ -= sizeof(PGOProfilerHeader) - sizeof(PGOProfilerHeaderLegacy);
139 }
140 return true;
141 }
142 return false;
143 }
144
ProcessToText(std::ofstream & stream) const145 bool PGOProfilerHeader::ProcessToText(std::ofstream &stream) const
146 {
147 if (!Verify()) {
148 return false;
149 }
150 stream << DumpUtils::VERSION_HEADER << InternalGetVersion() << DumpUtils::NEW_LINE;
151 stream << "Compatible an file version: " << ConvToStr(GetCompatibleAnVersion()) << DumpUtils::NEW_LINE;
152 if (SupportFileConsistency()) {
153 stream << "FileSize: " << GetFileSize() << " ,HeaderSize: " << GetHeaderSize() << " ,Checksum: " << std::hex
154 << GetChecksum() << DumpUtils::NEW_LINE;
155 }
156 return true;
157 }
158 } // namespace panda::ecmascript::pgo