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