1 /* 2 * Copyright (c) 2022-2024 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_PATCH_PATCH_LOADER_H 17 #define ECMASCRIPT_PATCH_PATCH_LOADER_H 18 19 #include "ecmascript/jspandafile/js_pandafile.h" 20 #include "ecmascript/js_tagged_value.h" 21 #include "ecmascript/method.h" 22 #include "ecmascript/mem/c_containers.h" 23 #include "ecmascript/napi/include/jsnapi.h" 24 25 namespace panda::ecmascript { 26 using PatchErrorCode = panda::JSNApi::PatchErrorCode; 27 using JSRecordInfo = JSPandaFile::JSRecordInfo; 28 using LiteralDataAccessor = panda_file::LiteralDataAccessor; 29 using LiteralValue = panda_file::LiteralDataAccessor::LiteralValue; 30 using LiteralTag = panda_file::LiteralTag; 31 class ConstantPool; 32 class JSThread; 33 34 struct BaseMethodIndex { 35 uint32_t constpoolNum {UINT32_MAX}; 36 uint32_t constpoolIndex {UINT32_MAX}; 37 uint32_t literalIndex {UINT32_MAX}; 38 struct Hash { operatorBaseMethodIndex::Hash39 std::size_t operator()(const BaseMethodIndex &baseMethodIndex) const 40 { 41 return std::hash<uint32_t>{}(baseMethodIndex.constpoolNum) ^ (std::hash<uint32_t>{}( 42 baseMethodIndex.constpoolIndex) << 1) ^ std::hash<uint32_t>{}(baseMethodIndex.literalIndex); 43 } 44 }; 45 46 bool operator==(const BaseMethodIndex &baseMethodIndex) const 47 { 48 return constpoolNum == baseMethodIndex.constpoolNum && constpoolIndex == baseMethodIndex.constpoolIndex && 49 literalIndex == baseMethodIndex.literalIndex; 50 } 51 }; 52 53 struct PatchMethodIndex { 54 CString recordName; 55 CString className; 56 CString methodName; 57 struct Hash { operatorPatchMethodIndex::Hash58 std::size_t operator()(const PatchMethodIndex &patchMethodIndex) const 59 { 60 return std::hash<CString>{}(patchMethodIndex.recordName) ^ 61 std::hash<CString>{}(patchMethodIndex.className) ^ std::hash<CString>{}(patchMethodIndex.methodName); 62 } 63 }; 64 65 bool operator==(const PatchMethodIndex &patchMethodIndex) const 66 { 67 return recordName == patchMethodIndex.recordName && className == patchMethodIndex.className && 68 methodName == patchMethodIndex.methodName; 69 } 70 }; 71 72 struct PatchInfo { 73 // patch file name. 74 CString patchFileName; 75 // patch methodLiterals for load patch, <recordName, <methodName, MethodLiteral>> 76 CUnorderedMap<PatchMethodIndex, MethodLiteral*, PatchMethodIndex::Hash> patchMethodLiterals; 77 // base method info for unload patch, <BaseMethodIndex, base MethodLiteral> 78 CUnorderedMap<BaseMethodIndex, MethodLiteral *, BaseMethodIndex::Hash> baseMethodInfo; 79 // save base constpool in global for avoid gc. 80 CVector<JSHandle<JSTaggedValue>> baseConstpools; 81 // patch replaced recordNames. 82 CUnorderedSet<CString> replacedRecordNames; 83 // patch replaced methods. 84 CUnorderedMap<EntityId, CString> replacedPatchMethods; 85 }; 86 87 enum class StageOfHotReload : int32_t { 88 BEGIN_EXECUTE_PATCHMAIN = -1, // -1: For intercepting Evaluate() 89 INITIALIZE_STAGE_OF_HOTRELOAD, // 0 : initialize stageOfHotreload_ in ecma_context.h 90 LOAD_END_EXECUTE_PATCHMAIN, // 1: for Interceptint get module var 91 UNLOAD_END_EXECUTE_PATCHMAIN // 2 :for execute abc normally 92 }; 93 94 enum class StageOfColdReload : int32_t { 95 NOT_COLD_RELOAD, 96 IS_COLD_RELOAD 97 }; 98 99 class PatchLoader { 100 public: 101 PatchLoader() = default; 102 ~PatchLoader() = default; 103 NO_COPY_SEMANTIC(PatchLoader); 104 NO_MOVE_SEMANTIC(PatchLoader); 105 106 static PatchErrorCode LoadPatchInternal(JSThread *thread, const JSPandaFile *baseFile, 107 const JSPandaFile *patchFile, PatchInfo &patchInfo, 108 const CMap<uint32_t, CString> &baseClassInfo); 109 static PatchErrorCode UnloadPatchInternal(JSThread *thread, const CString &patchFileName, 110 const CString &baseFileName, PatchInfo &patchInfo); 111 112 static MethodLiteral *FindSameMethod(PatchInfo &patchInfo, const JSPandaFile *baseFile, 113 EntityId baseMethodId, const CMap<uint32_t, CString> &baseClassInfo); 114 static void ExecuteFuncOrPatchMain( 115 JSThread *thread, const JSPandaFile *jsPandaFile, const PatchInfo &patchInfo, bool loadPatch = true); 116 static CMap<uint32_t, CString> CollectClassInfo(const JSPandaFile *jsPandaFile); 117 static void UpdateModuleForColdPatch( 118 JSThread *thread, EntityId methodId, CString &recordName, bool hasModule = true); 119 static void UpdateJSFunction(JSThread *thread, PatchInfo &patchInfo); 120 121 private: 122 static PatchInfo GeneratePatchInfo(const JSPandaFile *patchFile); 123 static CString GetRealName(const JSPandaFile *jsPandaFile, EntityId entityId, CString &className); 124 static void FindAndReplaceSameMethod(JSThread *thread, 125 const JSPandaFile *baseFile, 126 const JSPandaFile *patchFile, 127 PatchInfo &patchInfo, 128 const CMap<uint32_t, CString> &baseClassInfo); 129 static void SaveBaseMethodInfo(PatchInfo &patchInfo, const JSPandaFile *baseFile, 130 EntityId baseMethodId, const BaseMethodIndex &indexs); 131 static void ReplaceMethod(JSThread *thread, 132 Method *destMethod, 133 MethodLiteral *srcMethodLiteral, 134 JSTaggedValue srcConstpool); 135 136 static void ClearPatchInfo(JSThread *thread, const CString &patchFileName); 137 138 static Method *GetPatchMethod(JSThread *thread, 139 const BaseMethodIndex &methodIndex, const JSTaggedValue baseConstpool); 140 static void FindAndReplaceClassLiteral(JSThread *thread, const JSPandaFile *baseFile, 141 const JSPandaFile *patchFile, JSTaggedValue constpoolValue, 142 PatchInfo &patchInfo, uint32_t constpoolIndex, 143 uint32_t constpoolNum, const CMap<uint32_t, CString> &baseClassInfo); 144 }; 145 } // namespace panda::ecmascript 146 #endif // ECMASCRIPT_PATCH_PATCH_LOADER_H 147