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