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 #ifndef ECMASCRIPT_PATCH_QUICK_FIX_MANAGER_H 16 #define ECMASCRIPT_PATCH_QUICK_FIX_MANAGER_H 17 18 #include "ecmascript/napi/include/jsnapi_internals.h" 19 #include "ecmascript/patch/patch_loader.h" 20 #include "ecmascript/module/js_module_source_text.h" 21 22 namespace panda::ecmascript { 23 class QuickFixManager { 24 public: 25 QuickFixManager() = default; 26 ~QuickFixManager(); 27 28 void RegisterQuickFixQueryFunc(const std::function<bool(std::string baseFileName, 29 std::string &patchFileName, 30 uint8_t **patchBuffer, 31 size_t &patchSize)> callBack); 32 void LoadPatchIfNeeded(JSThread *thread, const JSPandaFile *baseFile); 33 PatchErrorCode LoadPatch(JSThread *thread, const std::string &patchFileName, const std::string &baseFileName); 34 PatchErrorCode LoadPatch(JSThread *thread, 35 const std::string &patchFileName, uint8_t *patchBuffer, size_t patchSize, 36 const std::string &baseFileName, uint8_t *baseBuffer, size_t baseSize); 37 PatchErrorCode UnloadPatch(JSThread *thread, const std::string &patchFileName); 38 bool IsQuickFixCausedException(JSThread *thread, 39 const JSHandle<JSTaggedValue> &exceptionInfo, 40 const std::string &patchFileName); 41 JSTaggedValue CheckAndGetPatch(JSThread *thread, const JSPandaFile *baseFile, EntityId baseMethodId); 42 CString GetBaseFileName(const JSHandle<SourceTextModule> &module); 43 private: 44 // check whether the callback is registered. HasQueryQuickFixInfoFunc()45 bool HasQueryQuickFixInfoFunc() const 46 { 47 return callBack_ != nullptr; 48 } 49 50 CUnorderedSet<CString> ParseStackInfo(const CString &stackInfo); 51 52 // key: baseFileName 53 CMap<CString, PatchInfo> methodInfos_ {}; 54 std::function<bool(std::string baseFileName, 55 std::string &patchFileName, 56 uint8_t **patchBuffer, 57 size_t &patchSize)> callBack_; 58 CMap<uint32_t, CString> baseClassInfo_ {}; 59 // key: patch file name, value: base file name 60 CMap<CString, CString> patchAndBaseFileNameMap_; 61 std::set<CString> checkedFiles_ {}; 62 }; 63 } // namespace panda::ecmascript 64 #endif // ECMASCRIPT_PATCH_QUICK_FIX_MANAGER_H