• 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 #ifndef ECMASCRIPT_JSPANDAFILE_QUICK_FIX_LOADER_H
17 #define ECMASCRIPT_JSPANDAFILE_QUICK_FIX_LOADER_H
18 
19 #include "ecmascript/jspandafile/program_object.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/js_thread.h"
22 #include "ecmascript/mem/c_containers.h"
23 
24 namespace panda::ecmascript {
25 using EntityId = panda_file::File::EntityId;
26 class QuickFixLoader {
27 public:
28     QuickFixLoader() = default;
29     ~QuickFixLoader();
30 
31     bool LoadPatch(JSThread *thread, const CString &patchFileName, const CString &baseFileName);
32     bool LoadPatch(JSThread *thread, const CString &patchFileName, const void *patchBuffer, size_t patchSize,
33                    const CString &baseFileName);
34     bool UnloadPatch(JSThread *thread, const CString &patchFileName);
35 
36 private:
37     bool LoadPatchInternal(JSThread *thread, const JSPandaFile *baseFile, const JSPandaFile *patchFile);
38     bool ReplaceMethod(JSThread *thread,
39                        const JSPandaFile *baseFile,
40                        const JSPandaFile *patchFile,
41                        const JSHandle<ConstantPool> &baseConstpool,
42                        const JSHandle<ConstantPool> &patchConstpool);
43     void ReplaceMethodInner(JSThread *thread,
44                             Method *destMethod,
45                             MethodLiteral *srcMethodLiteral,
46                             JSTaggedValue srcConstpool);
47 
48     void InsertBaseClassMethodInfo(uint32_t constpoolIndex, uint32_t literalIndex, MethodLiteral *base);
49     bool HasNormalMethodReplaced(uint32_t index) const;
50     bool HasClassMethodReplaced(uint32_t constpoolIndex, uint32_t literalIndex) const;
51 
52     CString GetRecordName(const JSPandaFile *jsPandaFile, EntityId methodId);
53     CVector<JSHandle<Program>> ParseAllConstpoolWithMerge(JSThread *thread, const JSPandaFile *jsPandaFile);
54     void GenerateConstpoolCache(JSThread *thread, const JSPandaFile *jsPandaFile, JSHandle<ConstantPool> constpool);
55 
56     bool ExecutePatchMain(JSThread *thread, const JSHandle<Program> &patchProgram, const JSPandaFile *patchFile);
57     bool ExecutePatchMain(JSThread *thread, const CVector<JSHandle<Program>> &programs, const JSPandaFile *patchFile);
58 
59     CUnorderedSet<CString> ParseStackInfo(const CString &stackInfo);
60 
ClearReservedInfo()61     void ClearReservedInfo()
62     {
63         reservedBaseMethodInfo_.clear();
64         reservedBaseClassInfo_.clear();
65     }
66     void ClearPatchInfo(JSThread *thread, const CString &patchFileName) const;
67 
68     bool CheckIsInvalidPatch(const JSPandaFile *baseFile, const JSPandaFile *patchFile, EcmaVM *vm) const;
69     // Check module mismatch
70     static bool CheckIsModuleMismatch(JSThread *thread, JSHandle<SourceTextModule> patchModule,
71                                       JSHandle<SourceTextModule> baseModule);
72     static bool CheckImportEntriesMismatch(JSThread *thread, JSTaggedValue patch, JSTaggedValue base);
73     static bool CheckLocalExportEntriesMismatch(JSThread *thread, JSTaggedValue patch, JSTaggedValue base);
74     static bool CheckIndirectExportEntriesMismatch(JSThread *thread, JSTaggedValue patch, JSTaggedValue base);
75     static bool CheckStarExportEntriesMismatch(JSThread *thread, JSTaggedValue patch, JSTaggedValue base);
76     static bool CheckImportEntryMismatch(ImportEntry *patch, ImportEntry *base);
77     static bool CheckLocalExportEntryMismatch(LocalExportEntry *patch, LocalExportEntry *base);
78     static bool CheckIndirectExportEntryMismatch(IndirectExportEntry *patch, IndirectExportEntry *base);
79     static bool CheckStarExportEntryMismatch(StarExportEntry *patch, StarExportEntry *base);
80 
81     CString baseFileName_;
82 
83     // For method unload patch.
84     // key: base constpool index, value: base methodLiteral.
85     CUnorderedMap<uint32_t, MethodLiteral *> reservedBaseMethodInfo_ {};
86 
87     // For class unload patch.
88     // key: base constpool index.
89     // key: class literal tagged array index, value: base methodLiteral.
90     CUnorderedMap<uint32_t, CUnorderedMap<uint32_t, MethodLiteral *>> reservedBaseClassInfo_ {};
91 };
92 }  // namespace panda::ecmascript
93 #endif // ECMASCRIPT_JSPANDAFILE_QUICK_FIX_LOADER_H
94