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