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 #include "zlib.h"
18
19 namespace panda::ecmascript::pgo {
20 using StringHelper = base::StringHelper;
21 bool PGOProfilerHeader::strictMatch_ = true;
22 bool PGOProfilerHeader::checksumListHasAbcId_ = true;
23
BuildFromLegacy(void * buffer,PGOProfilerHeader ** header)24 bool PGOProfilerHeader::BuildFromLegacy(void *buffer, PGOProfilerHeader **header)
25 {
26 if (buffer == nullptr || header == nullptr) {
27 LOG_ECMA(ERROR) << "buffer or header is null!";
28 return false;
29 }
30 auto *inHeader = reinterpret_cast<PGOProfilerHeaderLegacy *>(buffer);
31 size_t desSize = Size(inHeader->GetSectionNumber());
32 if (desSize > LastSize()) {
33 LOG_ECMA(ERROR) << "header size error, expected size is less than " << LastSize() << ", but got " << desSize;
34 return false;
35 }
36 Build(header, desSize);
37 // copy header base.
38 if (memcpy_s(*header, sizeof(FileHeaderBase), inHeader, sizeof(FileHeaderBase)) != EOK) {
39 LOG_ECMA(FATAL) << "PGOProfilerHeader BuildFromLegacy copy header base memcpy failed!";
40 UNREACHABLE();
41 }
42 // skip elastic header field, and copy section info from incoming buffer.
43 auto sectionSize = desSize - sizeof(FileHeaderElastic);
44 if (memcpy_s(&((*header)->sectionNumber_), sectionSize, &(inHeader->GetSectionNumber()), sectionSize) != EOK) {
45 LOG_ECMA(FATAL) << "PGOProfilerHeader BuildFromLegacy copy section info memcpy failed!";
46 UNREACHABLE();
47 }
48 return true;
49 }
50
BuildFromElastic(void * buffer,size_t bufferSize,PGOProfilerHeader ** header)51 bool PGOProfilerHeader::BuildFromElastic(void *buffer, size_t bufferSize, PGOProfilerHeader **header)
52 {
53 auto *inHeader = reinterpret_cast<PGOProfilerHeader *>(buffer);
54 if (!inHeader->Verify(buffer, bufferSize)) {
55 return false;
56 }
57 size_t desSize = inHeader->Size();
58 if (desSize > LastSize()) {
59 LOG_ECMA(ERROR) << "header size error, expected size is less than " << LastSize() << ", but got " << desSize;
60 return false;
61 }
62 Build(header, desSize);
63 if (memcpy_s(*header, desSize, inHeader, desSize) != EOK) {
64 LOG_ECMA(FATAL) << "PGOProfilerHeader BuildFromElastic memcpy failed!";
65 UNREACHABLE();
66 }
67 return true;
68 }
69
ParseFromBinary(void * buffer,size_t bufferSize,PGOProfilerHeader ** header)70 bool PGOProfilerHeader::ParseFromBinary(void *buffer, size_t bufferSize, PGOProfilerHeader **header)
71 {
72 auto *inHeaderBase = reinterpret_cast<FileHeaderBase *>(buffer);
73 if (inHeaderBase->VerifyVersion("ap file", LAST_VERSION, IsStrictMatch())) {
74 if (!inHeaderBase->CompatibleVerify(ELASTIC_HEADER_MINI_VERSION)) {
75 return BuildFromLegacy(buffer, header);
76 }
77 return BuildFromElastic(buffer, bufferSize, header);
78 }
79 return false;
80 }
81
VerifyFileSize(size_t bufferSize) const82 bool PGOProfilerHeader::VerifyFileSize(size_t bufferSize) const
83 {
84 if (!SupportFileSize()) {
85 return true;
86 }
87 if (GetFileSize() != bufferSize) {
88 LOG_ECMA(ERROR) << "Verify ap file's file size failed. size: " << std::hex << bufferSize << " vs "
89 << GetFileSize();
90 return false;
91 }
92 return true;
93 }
94
VerifyConsistency(void * buffer,size_t bufferSize) const95 bool PGOProfilerHeader::VerifyConsistency(void *buffer, size_t bufferSize) const
96 {
97 if (!SupportFileConsistency()) {
98 return true;
99 }
100 uint32_t checksum = adler32(0, reinterpret_cast<const Bytef *>(buffer) + MAGIC_SIZE, VERSION_SIZE);
101 checksum = adler32(checksum, reinterpret_cast<const Bytef *>(buffer) + CHECKSUM_END_OFFSET,
102 bufferSize - CHECKSUM_END_OFFSET);
103 if (checksum != GetChecksum()) {
104 LOG_ECMA(ERROR) << "Verify ap file's consistency failed. checksum: " << std::hex << checksum << " vs "
105 << std::hex << GetChecksum();
106 return false;
107 }
108 return true;
109 }
110
ProcessToBinary(std::fstream & fileStream) const111 void PGOProfilerHeader::ProcessToBinary(std::fstream &fileStream) const
112 {
113 fileStream.seekp(0);
114 if (base::FileHeaderBase::CompatibleVerify(ELASTIC_HEADER_MINI_VERSION)) {
115 fileStream.write(reinterpret_cast<const char *>(this), Size());
116 } else {
117 // copy header base.
118 fileStream.write(reinterpret_cast<const char *>(this), sizeof(FileHeaderBase));
119 // skip elastic header field, and copy section info from incoming buffer.
120 auto sectionSize = Size() - sizeof(FileHeaderElastic);
121 fileStream.write(reinterpret_cast<const char *>(§ionNumber_), sectionSize);
122 }
123 }
124
ProcessToText(std::ofstream & stream) const125 bool PGOProfilerHeader::ProcessToText(std::ofstream &stream) const
126 {
127 if (!Verify()) {
128 return false;
129 }
130 stream << DumpUtils::VERSION_HEADER << InternalGetVersion() << DumpUtils::NEW_LINE;
131 stream << "Compatible an file version: " << ConvToStr(GetCompatibleAnVersion()) << DumpUtils::NEW_LINE;
132 if (SupportFileConsistency()) {
133 stream << "FileSize: " << GetFileSize() << ", HeaderSize: " << GetHeaderSize() << ", Checksum: " << std::hex
134 << GetChecksum() << DumpUtils::NEW_LINE;
135 }
136 return true;
137 }
138 } // namespace panda::ecmascript::pgo
139