1 /* 2 * Copyright (c) 2021 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_INSTRUCTIONS_MAP_H_ 17 #define PANDA_VERIFICATION_CFLOW_INSTRUCTIONS_MAP_H_ 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 pc_curr = reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_curr) + 1); 32 if (pc_curr == pc_next) { 33 return true; 34 } 35 pc_next = reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_next) - 1); 36 return AddrMap_.Mark(pc_curr, pc_next); 37 } PutInstruction(const void * pc_ptr,size_t sz)38 bool PutInstruction(const void *pc_ptr, size_t sz) 39 { 40 return PutInstruction(pc_ptr, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_ptr) + sz)); 41 } MarkCodeBlock(const void * pc_start,const void * pc_end)42 bool MarkCodeBlock(const void *pc_start, const void *pc_end) 43 { 44 return AddrMap_.Mark(pc_start, pc_end); 45 } MarkCodeBlock(const void * pc_start,size_t sz)46 bool MarkCodeBlock(const void *pc_start, size_t sz) 47 { 48 return AddrMap_.Mark(pc_start, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_start) + sz - 1)); 49 } ClearCodeBlock(const void * pc_start,const void * pc_end)50 bool ClearCodeBlock(const void *pc_start, const void *pc_end) 51 { 52 return AddrMap_.Clear(pc_start, pc_end); 53 } ClearCodeBlock(const void * pc_start,size_t sz)54 bool ClearCodeBlock(const void *pc_start, size_t sz) 55 { 56 return AddrMap_.Clear(pc_start, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_start) + sz - 1)); 57 } 58 InstructionsMap(const void * ptr_start,const void * ptr_end)59 InstructionsMap(const void *ptr_start, const void *ptr_end) : AddrMap_ {ptr_start, ptr_end} {} 60 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_VERIFICATION_CFLOW_INSTRUCTIONS_MAP_H_ 92