• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "patchFix.h"
17 #include <compiler/core/pandagen.h>
18 #include <ir/expressions/literal.h>
19 
20 namespace panda::es2panda::util {
21 
22 const std::string EXTERNAL_ATTRIBUTE = "external";
23 
ProcessFunction(const compiler::PandaGen * pg,panda::pandasm::Function * func,LiteralBuffers & literalBuffers)24 void PatchFix::ProcessFunction(const compiler::PandaGen *pg, panda::pandasm::Function *func,
25     LiteralBuffers &literalBuffers)
26 {
27     if (generateSymbolFile_) {
28         DumpFunctionInfo(pg, func, literalBuffers);
29         return;
30     }
31 
32     if (generatePatch_ || IsHotReload()) {
33         HandleFunction(pg, func, literalBuffers);
34         return;
35     }
36 }
37 
ProcessModule(const std::string & recordName,std::vector<panda::pandasm::LiteralArray::Literal> & moduleBuffer)38 void PatchFix::ProcessModule(const std::string &recordName,
39     std::vector<panda::pandasm::LiteralArray::Literal> &moduleBuffer)
40 {
41     if (generateSymbolFile_) {
42         DumpModuleInfo(recordName, moduleBuffer);
43         return;
44     }
45 
46     if (generatePatch_ || IsHotReload()) {
47         ValidateModuleInfo(recordName, moduleBuffer);
48         return;
49     }
50 }
51 
ProcessJsonContentRecord(const std::string & recordName,const std::string & jsonFileContent)52 void PatchFix::ProcessJsonContentRecord(const std::string &recordName, const std::string &jsonFileContent)
53 {
54     if (generateSymbolFile_) {
55         DumpJsonContentRecInfo(recordName, jsonFileContent);
56         return;
57     }
58 
59     if (generatePatch_ || IsHotReload()) {
60         ValidateJsonContentRecInfo(recordName, jsonFileContent);
61         return;
62     }
63 }
64 
DumpModuleInfo(const std::string & recordName,std::vector<panda::pandasm::LiteralArray::Literal> & moduleBuffer)65 void PatchFix::DumpModuleInfo(const std::string &recordName,
66     std::vector<panda::pandasm::LiteralArray::Literal> &moduleBuffer)
67 {
68     std::stringstream ss;
69     ss << recordName << SymbolTable::SECOND_LEVEL_SEPERATOR;
70     ss << Helpers::GetHashString(ConvertLiteralToString(moduleBuffer)) << std::endl;
71     symbolTable_->FillSymbolTable(ss);
72 }
73 
ValidateModuleInfo(const std::string & recordName,std::vector<panda::pandasm::LiteralArray::Literal> & moduleBuffer)74 void PatchFix::ValidateModuleInfo(const std::string &recordName,
75     std::vector<panda::pandasm::LiteralArray::Literal> &moduleBuffer)
76 {
77     auto it = originModuleInfo_->find(recordName);
78     if (!IsHotReload() && it == originModuleInfo_->end()) {
79         errMsg_ << "[Patch] Found new import/export expression in " + recordName + ", not supported!" << std::endl;
80         patchError_ = true;
81         return;
82     }
83 
84     if (!IsHotReload() && Helpers::GetHashString(ConvertLiteralToString(moduleBuffer)) != it->second) {
85         errMsg_ << "[Patch] Found import/export expression changed in " + recordName + ", not supported!" <<
86             std::endl;
87         patchError_ = true;
88         return;
89     }
90 }
91 
DumpJsonContentRecInfo(const std::string & recordName,const std::string & jsonFileContent)92 void PatchFix::DumpJsonContentRecInfo(const std::string &recordName, const std::string &jsonFileContent)
93 {
94     std::stringstream ss;
95     ss << recordName << SymbolTable::SECOND_LEVEL_SEPERATOR;
96     ss << Helpers::GetHashString(jsonFileContent) << std::endl;
97     symbolTable_->FillSymbolTable(ss);
98 }
99 
ValidateJsonContentRecInfo(const std::string & recordName,const std::string & jsonFileContent)100 void PatchFix::ValidateJsonContentRecInfo(const std::string &recordName, const std::string &jsonFileContent)
101 {
102     auto it = originModuleInfo_->find(recordName);
103     if (!IsHotReload() && it == originModuleInfo_->end()) {
104         errMsg_ << "[Patch] Found new import/require json file expression in " + recordName +
105             ", not supported!" << std::endl;
106         patchError_ = true;
107         return;
108     }
109 
110     if (!IsHotReload() && Helpers::GetHashString(jsonFileContent) != it->second) {
111         errMsg_ << "[Patch] Found imported/required json file content changed in " + recordName +
112             ", not supported!" << std::endl;
113         patchError_ = true;
114         return;
115     }
116 }
117 
IsAnonymousOrSpecialOrDuplicateFunction(const std::string & funcName)118 bool PatchFix::IsAnonymousOrSpecialOrDuplicateFunction(const std::string &funcName)
119 {
120     if (util::Helpers::IsDefaultApiVersion(targetApiVersion_, targetApiSubVersion_)) {
121         return funcName.find(binder::Binder::ANONYMOUS_SPECIAL_DUPLICATE_FUNCTION_SPECIFIER) != std::string::npos;
122     }
123     // Function name is like: #scopes^1#functionname^1
124     // Special function name is which includes "\\" or ".", the name should be transformed into "".
125     // Anonymous function name is "", it's the same with special function name here.
126     // Duplicate function name includes "^" after the last "#".
127     auto pos = funcName.find_last_of(Helpers::FUNC_NAME_SEPARATOR);
128     if (pos == std::string::npos) {
129         return false;
130     }
131 
132     if (pos == funcName.size() - 1) {
133         return true;
134     }
135 
136     auto posOfDuplicateSep = funcName.find_last_of(Helpers::DUPLICATED_SEPERATOR);
137     if (posOfDuplicateSep != std::string::npos && posOfDuplicateSep > pos) {
138         return true;
139     }
140 
141     return false;
142 }
143 
GetLiteralIdxFromStringId(const std::string & stringId)144 int64_t PatchFix::GetLiteralIdxFromStringId(const std::string &stringId)
145 {
146     auto recordPrefix = recordName_ + "_";
147     auto idxStr = stringId.substr(recordPrefix.size());
148     return std::atoi(idxStr.c_str());
149 }
150 
CollectFunctionsWithDefinedClasses(std::string funcName,std::string className)151 void PatchFix::CollectFunctionsWithDefinedClasses(std::string funcName, std::string className)
152 {
153     auto funcInfo = funcDefinedClasses_.find(funcName);
154     if (funcInfo != funcDefinedClasses_.end()) {
155         funcInfo->second.push_back(className);
156         return;
157     }
158     std::vector<std::string> funcDefinedClasses = {className};
159     funcDefinedClasses_.insert({funcName, funcDefinedClasses});
160 }
161 
GenerateFunctionAndClassHash(panda::pandasm::Function * func,LiteralBuffers & literalBuffers)162 std::vector<std::pair<std::string, std::string>> PatchFix::GenerateFunctionAndClassHash(panda::pandasm::Function *func,
163     LiteralBuffers &literalBuffers)
164 {
165     std::stringstream ss;
166     std::vector<std::pair<std::string, std::string>> hashList;
167 
168     ss << ".function any " << func->name << '(';
169 
170     for (uint32_t i = 0; i < func->GetParamsNum(); i++) {
171         ss << "any a" << std::to_string(i);
172         if (i != func->GetParamsNum() - 1) {
173             ss << ", ";
174         }
175     }
176     ss << ") {" << std::endl;
177 
178     for (const auto &ins : func->ins) {
179         ss << (ins->IsLabel() ? "" : "\t") << ins->ToString("", true, func->GetTotalRegs()) << " ";
180         if (ins->opcode == panda::pandasm::Opcode::CREATEARRAYWITHBUFFER ||
181             ins->opcode == panda::pandasm::Opcode::CREATEOBJECTWITHBUFFER) {
182             int64_t bufferIdx = GetLiteralIdxFromStringId(ins->GetId(0));
183             ss << ExpandLiteral(bufferIdx, literalBuffers) << " ";
184         } else if (ins->opcode == panda::pandasm::Opcode::DEFINECLASSWITHBUFFER) {
185             CollectFunctionsWithDefinedClasses(func->name, ins->GetId(0));
186             int64_t bufferIdx = GetLiteralIdxFromStringId(ins->GetId(1));
187             std::string literalStr = ExpandLiteral(bufferIdx, literalBuffers);
188             auto classHash = Helpers::GetHashString(literalStr);
189             hashList.push_back(std::pair<std::string, std::string>(ins->GetId(0), classHash));
190             CollectClassMemberFunctions(ins->GetId(0), bufferIdx, literalBuffers);
191         }
192         ss << " ";
193     }
194 
195     ss << "}" << std::endl;
196 
197     for (const auto &ct : func->catch_blocks) {
198         ss << ".catchall " << ct.try_begin_label << ", " << ct.try_end_label << ", " << ct.catch_begin_label
199             << std::endl;
200     }
201 
202     auto funcHash = Helpers::GetHashString(ss.str());
203     hashList.push_back(std::pair<std::string, std::string>(func->name, funcHash));
204     return hashList;
205 }
206 
ConvertLiteralToString(std::vector<panda::pandasm::LiteralArray::Literal> & literalBuffer)207 std::string PatchFix::ConvertLiteralToString(std::vector<panda::pandasm::LiteralArray::Literal> &literalBuffer)
208 {
209     std::stringstream ss;
210     int count = 0;
211     for (auto &literal : literalBuffer) {
212         ss << "{" << "index: " << count++ << " ";
213         ss << "tag: " << static_cast<std::underlying_type<panda::es2panda::ir::LiteralTag>::type>(literal.tag_);
214         ss << " ";
215         std::string val;
216         std::visit([&val](auto&& element) {
217             val += "val: ";
218             val += element;
219             val += " ";
220         }, literal.value_);
221         ss << val;
222         ss << "},";
223     }
224 
225     return ss.str();
226 }
227 
ExpandLiteral(int64_t bufferIdx,PatchFix::LiteralBuffers & literalBuffers)228 std::string PatchFix::ExpandLiteral(int64_t bufferIdx, PatchFix::LiteralBuffers &literalBuffers)
229 {
230     for (auto &litPair : literalBuffers) {
231         if (litPair.first == bufferIdx) {
232             return ConvertLiteralToString(litPair.second);
233         }
234     }
235 
236     return "";
237 }
238 
GetLiteralMethods(int64_t bufferIdx,PatchFix::LiteralBuffers & literalBuffers)239 std::vector<std::string> PatchFix::GetLiteralMethods(int64_t bufferIdx, PatchFix::LiteralBuffers &literalBuffers)
240 {
241     std::vector<std::string> methods;
242     for (auto &litPair : literalBuffers) {
243         if (litPair.first != bufferIdx) {
244             continue;
245         }
246         for (auto &literal : litPair.second) {
247             switch (literal.tag_) {
248                 case panda::panda_file::LiteralTag::METHOD:
249                 case panda::panda_file::LiteralTag::GENERATORMETHOD:
250                 case panda::panda_file::LiteralTag::ASYNCGENERATORMETHOD: {
251                     methods.push_back(std::get<std::string>(literal.value_));
252                     break;
253                 }
254                 default:
255                     break;
256             }
257         }
258     }
259 
260     return methods;
261 }
262 
CollectClassMemberFunctions(const std::string & className,int64_t bufferIdx,PatchFix::LiteralBuffers & literalBuffers)263 void PatchFix::CollectClassMemberFunctions(const std::string &className, int64_t bufferIdx,
264     PatchFix::LiteralBuffers &literalBuffers)
265 {
266     std::vector<std::string> classMemberFunctions = GetLiteralMethods(bufferIdx, literalBuffers);
267     classMemberFunctions.push_back(className);
268     classMemberFunctions_.insert({className, classMemberFunctions});
269 }
270 
IsScopeValidToPatchLexical(binder::VariableScope * scope) const271 bool PatchFix::IsScopeValidToPatchLexical(binder::VariableScope *scope) const
272 {
273     if (IsDumpSymbolTable()) {
274         return false;
275     }
276 
277     CHECK_NOT_NULL(scope);
278     if (!scope->IsFunctionVariableScope()) {
279         return false;
280     }
281 
282     auto funcName = scope->AsFunctionVariableScope()->InternalName();
283     if (std::string(funcName) != funcMain0_) {
284         return false;
285     }
286     return true;
287 }
288 
AllocSlotfromPatchEnv(const std::string & variableName)289 void PatchFix::AllocSlotfromPatchEnv(const std::string &variableName)
290 {
291     if (!topScopeLexEnvs_.count(variableName)) {
292         topScopeLexEnvs_[variableName] = topScopeIdx_++;
293     }
294 }
295 
GetSlotIdFromSymbolTable(const std::string & variableName)296 uint32_t PatchFix::GetSlotIdFromSymbolTable(const std::string &variableName)
297 {
298     auto functionIter = originFunctionInfo_->find(funcMain0_);
299     if (functionIter != originFunctionInfo_->end()) {
300         for (const auto &lexenv : functionIter->second.lexenv) {
301             if (lexenv.second.first == variableName) {
302                 return lexenv.first;
303             }
304         }
305     }
306     return UINT32_MAX;
307 }
308 
GetEnvSizeOfFuncMain0()309 uint32_t PatchFix::GetEnvSizeOfFuncMain0()
310 {
311     auto functionIter = originFunctionInfo_->find(funcMain0_);
312     ASSERT(functionIter != originFunctionInfo_->end());
313     return functionIter->second.lexenv.size();
314 }
315 
GetPatchLexicalIdx(const std::string & variableName)316 uint32_t PatchFix::GetPatchLexicalIdx(const std::string &variableName)
317 {
318     ASSERT(topScopeLexEnvs_.count(variableName));
319     return topScopeLexEnvs_[variableName];
320 }
321 
IsFunctionOrClassDefineIns(panda::pandasm::Ins * ins)322 bool IsFunctionOrClassDefineIns(panda::pandasm::Ins *ins)
323 {
324     if (ins->opcode == panda::pandasm::Opcode::DEFINEMETHOD ||
325         ins->opcode == panda::pandasm::Opcode::DEFINEFUNC ||
326         ins->opcode == panda::pandasm::Opcode::DEFINECLASSWITHBUFFER) {
327         return true;
328     }
329     return false;
330 }
331 
IsStPatchVarIns(panda::pandasm::Ins * ins)332 bool IsStPatchVarIns(panda::pandasm::Ins *ins)
333 {
334     return ins->opcode == panda::pandasm::Opcode::WIDE_STPATCHVAR;
335 }
336 
CollectFuncDefineIns(panda::pandasm::Function * func)337 void PatchFix::CollectFuncDefineIns(panda::pandasm::Function *func)
338 {
339     for (size_t i = 0; i < func->ins.size(); ++i) {
340         if (IsFunctionOrClassDefineIns(func->ins[i].get())) {
341             funcDefineIns_.push_back(func->ins[i].get());  // push define ins
342             funcDefineIns_.push_back(func->ins[i + 1].get());  // push store ins
343         }
344     }
345 }
346 
HandleModifiedClasses(panda::pandasm::Program * prog)347 void PatchFix::HandleModifiedClasses(panda::pandasm::Program *prog)
348 {
349     for (auto &cls: classMemberFunctions_) {
350         for (auto &func: cls.second) {
351             if (!prog->function_table.at(func).metadata->IsForeign()) {
352                 modifiedClassNames_.insert(cls.first);
353                 break;
354             }
355         }
356     }
357 
358     for (auto &cls: modifiedClassNames_) {
359         auto &memberFunctions = classMemberFunctions_[cls];
360         for (auto &func: memberFunctions) {
361             if (prog->function_table.at(func).metadata->IsForeign()) {
362                 prog->function_table.at(func).metadata->RemoveAttribute(EXTERNAL_ATTRIBUTE);
363             }
364         }
365     }
366 }
367 
HandleModifiedDefinedClassFunc(panda::pandasm::Program * prog)368 void PatchFix::HandleModifiedDefinedClassFunc(panda::pandasm::Program *prog)
369 {
370     for (auto &funcInfo: funcDefinedClasses_) {
371         for (auto &definedClass: funcInfo.second) {
372             if (modifiedClassNames_.count(definedClass) &&
373                 prog->function_table.at(funcInfo.first).metadata->IsForeign()) {
374                 prog->function_table.at(funcInfo.first).metadata->RemoveAttribute(EXTERNAL_ATTRIBUTE);
375             }
376         }
377     }
378 }
379 
AddHeadAndTailInsForPatchFuncMain0(std::vector<panda::pandasm::InsPtr> & ins)380 void PatchFix::AddHeadAndTailInsForPatchFuncMain0(std::vector<panda::pandasm::InsPtr> &ins)
381 {
382     auto returnUndefined = new pandasm::Returnundefined();
383     if (ins.size() == 0) {
384         ins.emplace_back(returnUndefined);
385         return;
386     }
387 
388     auto newLexenv = new pandasm::Newlexenv(long(ins.size() / 2));  // each new function has 2 ins: define and stor
389     ins.emplace(ins.begin(), newLexenv);
390     ins.emplace_back(returnUndefined);
391 }
392 
AddTailInsForPatchFuncMain1(std::vector<panda::pandasm::InsPtr> & ins)393 void PatchFix::AddTailInsForPatchFuncMain1(std::vector<panda::pandasm::InsPtr> &ins)
394 {
395     auto returnUndefined = new pandasm::Returnundefined();
396     ins.emplace_back(returnUndefined);
397 }
398 
CreateFunctionPatchMain0AndMain1(panda::pandasm::Function & patchFuncMain0,panda::pandasm::Function & patchFuncMain1)399 void PatchFix::CreateFunctionPatchMain0AndMain1(panda::pandasm::Function &patchFuncMain0,
400     panda::pandasm::Function &patchFuncMain1)
401 {
402     const size_t defaultParamCount = 3;
403     patchFuncMain0.params.reserve(defaultParamCount);
404     patchFuncMain1.params.reserve(defaultParamCount);
405     for (uint32_t i = 0; i < defaultParamCount; ++i) {
406         patchFuncMain0.params.emplace_back(panda::pandasm::Type("any", 0), patchFuncMain0.language);
407         patchFuncMain1.params.emplace_back(panda::pandasm::Type("any", 0), patchFuncMain1.language);
408     }
409 
410     std::vector<panda::pandasm::InsPtr> patchMain0DefineIns;
411     std::vector<panda::pandasm::InsPtr> patchMain1DefineIns;
412 
413     for (size_t i = 0; i < funcDefineIns_.size(); ++i) {
414         if (IsFunctionOrClassDefineIns(funcDefineIns_[i])) {
415             auto name = funcDefineIns_[i]->GetId(0);
416             if (newFuncNames_.count(name) && IsStPatchVarIns(funcDefineIns_[i + 1])) {
417                 patchMain0DefineIns.emplace_back(funcDefineIns_[i]->DeepCopy());
418                 patchMain0DefineIns.emplace_back(funcDefineIns_[i + 1]->DeepCopy());
419                 continue;
420             }
421             if (patchFuncNames_.count(name) || modifiedClassNames_.count(name)) {
422                 patchMain1DefineIns.emplace_back(funcDefineIns_[i]->DeepCopy());
423                 continue;
424             }
425         }
426     }
427 
428     AddHeadAndTailInsForPatchFuncMain0(patchMain0DefineIns);
429     AddTailInsForPatchFuncMain1(patchMain1DefineIns);
430 
431     patchFuncMain0.ins = std::move(patchMain0DefineIns);
432     patchFuncMain1.ins = std::move(patchMain1DefineIns);
433 
434     patchFuncMain0.return_type = panda::pandasm::Type("any", 0);
435     patchFuncMain1.return_type = panda::pandasm::Type("any", 0);
436 }
437 
Finalize(panda::pandasm::Program ** prog)438 void PatchFix::Finalize(panda::pandasm::Program **prog)
439 {
440     if (IsDumpSymbolTable() || IsColdReload()) {
441         return;
442     }
443 
444     HandleModifiedClasses(*prog);
445 
446     HandleModifiedDefinedClassFunc(*prog);
447 
448     if (patchError_) {
449         *prog = nullptr;
450         errMsg_ << "[Patch] Found unsupported change in file, will not generate patch!" << std::endl;
451         std::cerr << errMsg_.str();
452         return;
453     }
454 
455     if (IsHotReload() || IsColdFix()) {
456         return;
457     }
458 
459     panda::pandasm::Function patchFuncMain0(patchMain0_, (*prog)->lang);
460     panda::pandasm::Function patchFuncMain1(patchMain1_, (*prog)->lang);
461     CreateFunctionPatchMain0AndMain1(patchFuncMain0, patchFuncMain1);
462 
463     (*prog)->function_table.emplace(patchFuncMain0.name, std::move(patchFuncMain0));
464     (*prog)->function_table.emplace(patchFuncMain1.name, std::move(patchFuncMain1));
465 }
466 
CompareLexenv(const std::string & funcName,const compiler::PandaGen * pg,SymbolTable::OriginFunctionInfo & bytecodeInfo)467 bool PatchFix::CompareLexenv(const std::string &funcName, const compiler::PandaGen *pg,
468     SymbolTable::OriginFunctionInfo &bytecodeInfo)
469 {
470     auto &lexicalVarNameAndTypes = pg->TopScope()->GetLexicalVarNameAndTypes();
471     auto &lexenv = bytecodeInfo.lexenv;
472     if (funcName != funcMain0_) {
473         if (lexenv.size() != lexicalVarNameAndTypes.size()) {
474             errMsg_ << "[Patch] Found lexical variable added or removed in " + funcName + ", not supported!"
475                 << std::endl;
476             patchError_ = true;
477             return false;
478         }
479         for (auto &variable: lexicalVarNameAndTypes) {
480             auto varSlot = variable.first;
481             auto lexenvIter = lexenv.find(varSlot);
482             if (lexenvIter == lexenv.end()) {
483                 errMsg_ << "[Patch] Found new lexical variable added in function " + funcName + ", not supported!"
484                     << std::endl;
485                 patchError_ = true;
486                 return false;
487             }
488 
489             auto &lexInfo = lexenvIter->second;
490             if (!IsColdFix() && (std::string(variable.second.first) != lexInfo.first ||
491                                  variable.second.second != lexInfo.second)) {
492                 errMsg_ << "[Patch] Found lexical variable changed in function " + funcName + ", not supported!"
493                     << std::endl;
494                 patchError_ = true;
495                 return false;
496             }
497         }
498     }
499     return true;
500 }
501 
CompareClassHash(std::vector<std::pair<std::string,std::string>> & hashList,SymbolTable::OriginFunctionInfo & bytecodeInfo)502 bool PatchFix::CompareClassHash(std::vector<std::pair<std::string, std::string>> &hashList,
503     SymbolTable::OriginFunctionInfo &bytecodeInfo)
504 {
505     auto &classInfo = bytecodeInfo.classHash;
506     for (size_t i = 0; i < hashList.size() - 1; ++i) {
507         auto &className = hashList[i].first;
508         auto classIter = classInfo.find(className);
509         if (!IsHotReload() && classIter != classInfo.end() && classIter->second != hashList[i].second) {
510             if (IsColdFix()) {
511                 modifiedClassNames_.insert(className);
512                 continue;
513             } else {
514                 ASSERT(IsHotFix());
515                 errMsg_ << "[Patch] Found class " + hashList[i].first + " changed, not supported!" << std::endl;
516             }
517             patchError_ = true;
518             return false;
519         }
520     }
521     return true;
522 }
523 
CheckAndRestoreSpecialFunctionName(uint32_t globalIndexForSpecialFunc,std::string & funcInternalName,std::string recordName)524 void PatchFix::CheckAndRestoreSpecialFunctionName(uint32_t globalIndexForSpecialFunc, std::string &funcInternalName,
525     std::string recordName)
526 {
527     auto it = originRecordHashFunctionNames_->find(recordName);
528     if (it != originRecordHashFunctionNames_->end()) {
529         if (it->second.size() == 0 || globalIndexForSpecialFunc > it->second.size()) {
530             // anonymous, special or duplicate function added
531             errMsg_ << "[Patch] Found new anonymous, special(containing '.' or '\\') or duplicate name function "
532                     + funcInternalName + " not supported!" << std::endl;
533             patchError_ = true;
534             return;
535         }
536         std::string originalName = it->second.at(std::to_string(globalIndexForSpecialFunc));
537         // special name function in the same position must have the same real function name as original
538         if (originalName.substr(originalName.find_last_of("#")) !=
539             funcInternalName.substr(funcInternalName.find_last_of("#"))) {
540             errMsg_ << "[Patch] Found new anonymous, special(containing '.' or '\\') or duplicate name function "
541                     + funcInternalName + " not supported!" << std::endl;
542             patchError_ = true;
543             return;
544         }
545         funcInternalName = originalName;
546     }
547 }
548 
HandleFunction(const compiler::PandaGen * pg,panda::pandasm::Function * func,LiteralBuffers & literalBuffers)549 void PatchFix::HandleFunction(const compiler::PandaGen *pg, panda::pandasm::Function *func,
550     LiteralBuffers &literalBuffers)
551 {
552     std::string funcName = func->name;
553     auto originFunction = originFunctionInfo_->find(funcName);
554     if (originFunction == originFunctionInfo_->end()) {
555         if ((!util::Helpers::IsDefaultApiVersion(targetApiVersion_, targetApiSubVersion_)) &&
556             IsHotFix() &&
557             IsAnonymousOrSpecialOrDuplicateFunction(funcName)) {
558             errMsg_ << "[Patch] Found new anonymous, special(containing '.' or '\\') or duplicate name function "
559                       + funcName + " not supported!" << std::endl;
560             patchError_ = true;
561             return;
562         }
563         newFuncNames_.insert(funcName);
564         CollectFuncDefineIns(func);
565         return;
566     }
567 
568     auto &bytecodeInfo = originFunction->second;
569     if (!CompareLexenv(funcName, pg, bytecodeInfo)) {
570         return;
571     }
572 
573     auto hashList = GenerateFunctionAndClassHash(func, literalBuffers);
574     if (!CompareClassHash(hashList, bytecodeInfo)) {
575         return;
576     }
577 
578     if (IsHotReload()) {
579         return;
580     }
581 
582     auto funcHash = hashList.back().second;
583 
584     if (funcName == funcMain0_) {
585         if (IsHotFix()) {
586             func->metadata->SetAttribute(EXTERNAL_ATTRIBUTE);
587         } else {
588             patchFuncNames_.insert(funcName);
589         }
590     } else {
591         if (funcHash == bytecodeInfo.funcHash) {
592             func->metadata->SetAttribute(EXTERNAL_ATTRIBUTE);
593         } else {
594             patchFuncNames_.insert(funcName);
595         }
596     }
597 
598     CollectFuncDefineIns(func);
599 }
600 
DumpFunctionInfo(const compiler::PandaGen * pg,panda::pandasm::Function * func,PatchFix::LiteralBuffers & literalBuffers)601 void PatchFix::DumpFunctionInfo(const compiler::PandaGen *pg, panda::pandasm::Function *func,
602     PatchFix::LiteralBuffers &literalBuffers)
603 {
604     std::stringstream ss;
605 
606     ss << pg->InternalName();
607     ss << SymbolTable::SECOND_LEVEL_SEPERATOR << pg->InternalName() << SymbolTable::SECOND_LEVEL_SEPERATOR;
608 
609     std::vector<std::pair<std::string, std::string>> hashList = GenerateFunctionAndClassHash(func, literalBuffers);
610     ss << hashList.back().second << SymbolTable::SECOND_LEVEL_SEPERATOR;
611 
612     if (util::Helpers::IsDefaultApiVersion(targetApiVersion_, targetApiSubVersion_)) {
613         auto internalNameStr = pg->InternalName().Mutf8();
614         if (internalNameStr.find("#") != std::string::npos) {
615             ss << (pg->Binder()->SpecialFuncNameIndexMap()).at(internalNameStr) << SymbolTable::SECOND_LEVEL_SEPERATOR;
616         } else {
617             // index 0 for all the normal name functions
618             ss << "0" << SymbolTable::SECOND_LEVEL_SEPERATOR;
619         }
620     }
621 
622     ss << SymbolTable::FIRST_LEVEL_SEPERATOR;
623     for (size_t i = 0; i < hashList.size() - 1; ++i) {
624         ss << hashList[i].first << SymbolTable::SECOND_LEVEL_SEPERATOR << hashList[i].second <<
625             SymbolTable::SECOND_LEVEL_SEPERATOR;
626     }
627     ss << SymbolTable::SECOND_LEVEL_SEPERATOR << SymbolTable::FIRST_LEVEL_SEPERATOR;
628 
629     for (auto &variable: pg->TopScope()->GetLexicalVarNameAndTypes()) {
630         ss << variable.second.first << SymbolTable::SECOND_LEVEL_SEPERATOR
631            << variable.first << SymbolTable::SECOND_LEVEL_SEPERATOR
632            << variable.second.second << SymbolTable::SECOND_LEVEL_SEPERATOR;
633     }
634     ss << SymbolTable::SECOND_LEVEL_SEPERATOR << std::endl;
635 
636     symbolTable_->FillSymbolTable(ss);
637 }
638 
IsAdditionalVarInPatch(uint32_t slot)639 bool PatchFix::IsAdditionalVarInPatch(uint32_t slot)
640 {
641     return slot == UINT32_MAX;
642 }
643 
IsDumpSymbolTable() const644 bool PatchFix::IsDumpSymbolTable() const
645 {
646     return patchFixKind_ == PatchFixKind::DUMPSYMBOLTABLE;
647 }
648 
IsHotFix() const649 bool PatchFix::IsHotFix() const
650 {
651     return patchFixKind_ == PatchFixKind::HOTFIX;
652 }
653 
IsColdFix() const654 bool PatchFix::IsColdFix() const
655 {
656     return patchFixKind_ == PatchFixKind::COLDFIX;
657 }
658 
IsHotReload() const659 bool PatchFix::IsHotReload() const
660 {
661     return patchFixKind_ == PatchFixKind::HOTRELOAD;
662 }
663 
IsColdReload() const664 bool PatchFix::IsColdReload() const
665 {
666     return patchFixKind_ == PatchFixKind::COLDRELOAD;
667 }
668 
669 } // namespace panda::es2panda::util
670