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_EXCEPTION_SOURCE_MAP_HPP 17 #define _PANDA_VERIFIER_EXCEPTION_SOURCE_MAP_HPP 18 19 #include "util/addr_map.h" 20 21 #include <cstdint> 22 23 namespace panda::verifier { 24 class ExceptionSourceMap { 25 public: PutExceptionSource(const void * pc)26 bool PutExceptionSource(const void *pc) 27 { 28 return Map_.Mark(pc, pc); 29 } PutExceptionSourceRange(const void * pc_start,const void * pc_end)30 bool PutExceptionSourceRange(const void *pc_start, const void *pc_end) 31 { 32 return Map_.Mark(pc_start, pc_end); 33 } PutExceptionSourceRange(const void * pc_start,size_t sz)34 bool PutExceptionSourceRange(const void *pc_start, size_t sz) 35 { 36 return Map_.Mark(pc_start, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_start) + sz - 1)); 37 } ClearExceptionSource(const void * pc)38 bool ClearExceptionSource(const void *pc) 39 { 40 return Map_.Clear(pc, pc); 41 } ClearExceptionSourceRange(const void * pc_start,const void * pc_end)42 bool ClearExceptionSourceRange(const void *pc_start, const void *pc_end) 43 { 44 return Map_.Clear(pc_start, pc_end); 45 } ClearExceptionSourceRange(const void * pc_start,size_t sz)46 bool ClearExceptionSourceRange(const void *pc_start, size_t sz) 47 { 48 return Map_.Clear(pc_start, reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(pc_start) + sz - 1)); 49 } ExceptionSourceMap(const void * ptr_start,const void * ptr_end)50 ExceptionSourceMap(const void *ptr_start, const void *ptr_end) : Map_ {ptr_start, ptr_end} {} ExceptionSourceMap(const void * ptr_start,size_t size)51 ExceptionSourceMap(const void *ptr_start, size_t size) 52 : ExceptionSourceMap(ptr_start, 53 reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(ptr_start) + size - 1)) 54 { 55 } 56 ExceptionSourceMap() = delete; 57 ~ExceptionSourceMap() = default; 58 IsExceptionSource(const void * pc)59 bool IsExceptionSource(const void *pc) const 60 { 61 return Map_.HasMark(pc); 62 } 63 64 template <typename Handler> ForSourcesInRange(const void * from,const void * to,Handler && handler)65 void ForSourcesInRange(const void *from, const void *to, Handler &&handler) const 66 { 67 Map_.EnumerateMarksInScope<const uint8_t *>(from, to, std::move(handler)); 68 } 69 70 private: 71 AddrMap Map_; 72 }; 73 74 } // namespace panda::verifier 75 76 #endif // !_PANDA_VERIFIER_EXCEPTION_SOURCE_MAP_HPP 77