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_JOB_QUEUE_JOB_H_ 17 #define PANDA_VERIFICATION_JOB_QUEUE_JOB_H_ 18 19 #include "verification/job_queue/cache.h" 20 #include "verification/cflow/cflow_info.h" 21 #include "verification/verification_options.h" 22 23 #include "runtime/include/method.h" 24 25 #include <cstdint> 26 #include <functional> 27 #include <optional> 28 29 namespace panda::verifier { 30 class Job { 31 public: 32 using CachedField = CacheOfRuntimeThings::CachedField; 33 using CachedMethod = CacheOfRuntimeThings::CachedMethod; 34 using CachedClass = CacheOfRuntimeThings::CachedClass; 35 Job(Method & method,const CachedMethod & ch_method,const VerificationOptions::MethodOptionsConfig::MethodOptions & options)36 Job(Method &method, const CachedMethod &ch_method, 37 const VerificationOptions::MethodOptionsConfig::MethodOptions &options) 38 : method_to_be_verified {method}, cached_method {std::cref(ch_method)}, method_options {options} 39 { 40 } 41 42 ~Job() = default; 43 AddField(uint32_t offset,const CachedField & cached_field)44 void AddField(uint32_t offset, const CachedField &cached_field) 45 { 46 fields.emplace(offset, std::cref(cached_field)); 47 } 48 AddMethod(uint32_t offset,const CachedMethod & ch_method)49 void AddMethod(uint32_t offset, const CachedMethod &ch_method) 50 { 51 methods.emplace(offset, std::cref(ch_method)); 52 } 53 AddClass(uint32_t offset,const CachedClass & ch_method)54 void AddClass(uint32_t offset, const CachedClass &ch_method) 55 { 56 classes.emplace(offset, std::cref(ch_method)); 57 } 58 IsFieldPresentForOffset(uint32_t offset)59 bool IsFieldPresentForOffset(uint32_t offset) const 60 { 61 return fields.count(offset) != 0; 62 } 63 IsMethodPresentForOffset(uint32_t offset)64 bool IsMethodPresentForOffset(uint32_t offset) const 65 { 66 return methods.count(offset) != 0; 67 } 68 IsClassPresentForOffset(uint32_t offset)69 bool IsClassPresentForOffset(uint32_t offset) const 70 { 71 return classes.count(offset) != 0; 72 } 73 GetField(uint32_t offset)74 const CachedField &GetField(uint32_t offset) const 75 { 76 return fields.at(offset); 77 } 78 GetMethod(uint32_t offset)79 const CachedMethod &GetMethod(uint32_t offset) const 80 { 81 return methods.at(offset); 82 } 83 GetClass(uint32_t offset)84 const CachedClass &GetClass(uint32_t offset) const 85 { 86 return classes.at(offset); 87 } 88 TakeNext()89 Job *TakeNext() 90 { 91 auto nxt = next; 92 next = nullptr; 93 return nxt; 94 } 95 SetNext(Job * nxt)96 void SetNext(Job *nxt) 97 { 98 next = nxt; 99 } 100 JobCachedMethod()101 const CachedMethod &JobCachedMethod() const 102 { 103 return cached_method; 104 } 105 JobMethod()106 Method &JobMethod() const 107 { 108 return method_to_be_verified; 109 } 110 JobMethodCflow()111 const CflowMethodInfo &JobMethodCflow() const 112 { 113 return *cflow_info; 114 } 115 SetMethodCflowInfo(PandaUniquePtr<CflowMethodInfo> && cflow)116 void SetMethodCflowInfo(PandaUniquePtr<CflowMethodInfo> &&cflow) 117 { 118 cflow_info = std::move(cflow); 119 } 120 121 template <typename Handler> ForAllCachedClasses(Handler && handler)122 void ForAllCachedClasses(Handler &&handler) const 123 { 124 for (const auto &item : classes) { 125 handler(item.second.get()); 126 } 127 } 128 129 template <typename Handler> ForAllCachedMethods(Handler && handler)130 void ForAllCachedMethods(Handler &&handler) const 131 { 132 for (const auto &item : methods) { 133 handler(item.second.get()); 134 } 135 } 136 137 template <typename Handler> ForAllCachedFields(Handler && handler)138 void ForAllCachedFields(Handler &&handler) const 139 { 140 for (const auto &item : fields) { 141 handler(item.second.get()); 142 } 143 } 144 Options()145 const auto &Options() const 146 { 147 return method_options; 148 } 149 150 private: 151 Job *next {nullptr}; 152 Method &method_to_be_verified; 153 std::reference_wrapper<const CachedMethod> cached_method; 154 const VerificationOptions::MethodOptionsConfig::MethodOptions &method_options; 155 PandaUniquePtr<CflowMethodInfo> cflow_info; 156 157 // offset -> cache item 158 PandaUnorderedMap<uint32_t, std::reference_wrapper<const CachedField>> fields; 159 PandaUnorderedMap<uint32_t, std::reference_wrapper<const CachedMethod>> methods; 160 PandaUnorderedMap<uint32_t, std::reference_wrapper<const CachedClass>> classes; 161 }; 162 } // namespace panda::verifier 163 164 #endif // PANDA_VERIFICATION_JOB_QUEUE_JOB_H_ 165