1 /** 2 * Copyright (c) 2021-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 PANDA_VERIFICATION_CFLOW_INFO_HPP_ 17 #define PANDA_VERIFICATION_CFLOW_INFO_HPP_ 18 19 #include "macros.h" 20 #include "verification/verification_status.h" 21 22 #include "runtime/include/class.h" 23 #include "runtime/include/mem/panda_containers.h" 24 #include "runtime/include/mem/panda_smart_pointers.h" 25 26 #include <cstdint> 27 #include <optional> 28 29 namespace ark::verifier { 30 class Job; 31 32 enum class InstructionType { NORMAL, JUMP, COND_JUMP, RETURN, THROW }; 33 34 class CflowMethodInfo { 35 public: 36 enum Flag : uint8_t { INSTRUCTION = 1, EXCEPTION_SOURCE = 2, EXCEPTION_HANDLER = 4, JUMP_TARGET = 8 }; 37 38 CflowMethodInfo() = delete; CflowMethodInfo(uint8_t const * addrStart,size_t codeSize)39 CflowMethodInfo(uint8_t const *addrStart, size_t codeSize) 40 : addrStart_ {addrStart}, 41 addrEnd_ {addrStart + codeSize} // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 42 { 43 flags_.resize(codeSize); 44 } 45 ~CflowMethodInfo() = default; 46 NO_COPY_SEMANTIC(CflowMethodInfo); 47 NO_MOVE_SEMANTIC(CflowMethodInfo); 48 GetAddrStart()49 uint8_t const *GetAddrStart() const 50 { 51 return addrStart_; 52 } 53 GetAddrEnd()54 uint8_t const *GetAddrEnd() const 55 { 56 return addrEnd_; 57 } 58 IsAddrValid(uint8_t const * addr)59 bool IsAddrValid(uint8_t const *addr) const 60 { 61 return addrStart_ <= addr && addr < addrEnd_; 62 } 63 IsFlagSet(uint8_t const * addr,Flag flag)64 bool IsFlagSet(uint8_t const *addr, Flag flag) const 65 { 66 ASSERT(addr >= addrStart_); 67 ASSERT(addr < addrEnd_); 68 return ((flags_[addr - addrStart_] & flag) != 0); 69 } 70 SetFlag(uint8_t const * addr,Flag flag)71 void SetFlag(uint8_t const *addr, Flag flag) 72 { 73 ASSERT(addr >= addrStart_); 74 ASSERT(addr < addrEnd_); 75 flags_[addr - addrStart_] |= flag; 76 } 77 ClearFlag(uint8_t const * addr,Flag flag)78 void ClearFlag(uint8_t const *addr, Flag flag) 79 { 80 ASSERT(addr >= addrStart_); 81 ASSERT(addr < addrEnd_); 82 flags_[addr - addrStart_] &= static_cast<uint8_t>(~flag); 83 } 84 GetHandlerStartAddresses()85 PandaVector<uint8_t const *> const *GetHandlerStartAddresses() const 86 { 87 return &handlerStartAddresses_; 88 } 89 90 private: 91 uint8_t const *addrStart_; 92 uint8_t const *addrEnd_; 93 PandaVector<uint8_t> flags_; 94 PandaVector<uint8_t const *> handlerStartAddresses_; 95 96 VerificationStatus FillCodeMaps(Method const *method); 97 VerificationStatus ProcessCatchBlocks(Method const *method); 98 99 friend PandaUniquePtr<CflowMethodInfo> GetCflowMethodInfo(Method const *method); 100 }; 101 102 PandaUniquePtr<CflowMethodInfo> GetCflowMethodInfo(Method const *method); 103 } // namespace ark::verifier 104 105 #endif // !PANDA_VERIFICATION_CFLOW_INFO_HPP_ 106