1 /** 2 * Copyright (c) 2021-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 _PANDA_VERIFIER_INSTRUCTIONS_MAP_HPP 17 #define _PANDA_VERIFIER_INSTRUCTIONS_MAP_HPP 18 19 #include "util/addr_map.h" 20 21 #include <cstdint> 22 23 namespace panda::verifier { 24 class InstructionsMap { 25 public: PutInstruction(const void * pc_curr,const void * pc_next)26 bool PutInstruction(const void *pc_curr, const void *pc_next) 27 { 28 if (!AddrMap_.IsInAddressSpace(pc_curr)) { 29 return false; 30 } 31 // why + 1: those byte addresses are marked that stays in between bytecode starts 32 pc_curr = reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_curr) + 1); 33 if (pc_curr == pc_next) { 34 // the bytecode is one-byte length => no marks 35 return true; 36 } 37 pc_next = reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_next) - 1); 38 return AddrMap_.Mark(pc_curr, pc_next); 39 } PutInstruction(const void * pc_ptr,size_t sz)40 bool PutInstruction(const void *pc_ptr, size_t sz) 41 { 42 return PutInstruction(pc_ptr, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_ptr) + sz)); 43 } MarkCodeBlock(const void * pc_start,const void * pc_end)44 bool MarkCodeBlock(const void *pc_start, const void *pc_end) 45 { 46 return AddrMap_.Mark(pc_start, pc_end); 47 } MarkCodeBlock(const void * pc_start,size_t sz)48 bool MarkCodeBlock(const void *pc_start, size_t sz) 49 { 50 return AddrMap_.Mark(pc_start, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_start) + sz - 1)); 51 } ClearCodeBlock(const void * pc_start,const void * pc_end)52 bool ClearCodeBlock(const void *pc_start, const void *pc_end) 53 { 54 return AddrMap_.Clear(pc_start, pc_end); 55 } ClearCodeBlock(const void * pc_start,size_t sz)56 bool ClearCodeBlock(const void *pc_start, size_t sz) 57 { 58 return AddrMap_.Clear(pc_start, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_start) + sz - 1)); 59 } InstructionsMap(const void * ptr_start,const void * ptr_end)60 InstructionsMap(const void *ptr_start, const void *ptr_end) : AddrMap_ {ptr_start, ptr_end} {} InstructionsMap(const void * ptr_start,size_t size)61 InstructionsMap(const void *ptr_start, size_t size) 62 : InstructionsMap(ptr_start, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(ptr_start) + size - 1)) 63 { 64 } 65 InstructionsMap() = delete; 66 ~InstructionsMap() = default; 67 CanJumpTo(const void * pc_target_ptr)68 bool CanJumpTo(const void *pc_target_ptr) const 69 { 70 return !AddrMap_.HasMark(pc_target_ptr); 71 } 72 73 template <typename PtrType> AddrStart()74 PtrType AddrStart() const 75 { 76 return AddrMap_.AddrStart<PtrType>(); 77 } 78 79 template <typename PtrType> AddrEnd()80 PtrType AddrEnd() const 81 { 82 return AddrMap_.AddrEnd<PtrType>(); 83 } 84 85 private: 86 AddrMap AddrMap_; 87 friend class JumpsMap; 88 }; 89 } // namespace panda::verifier 90 91 #endif // !_PANDA_VERIFIER_INSTRUCTIONS_MAP_HPP 92