• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JOB_QUEUE_JOB_H_
17 #define PANDA_VERIFIER_JOB_QUEUE_JOB_H_
18 
19 #include "verification/jobs/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:
Job(Method & method,const CachedMethod & cached_method,const MethodOptions & options)32     Job(Method &method, const CachedMethod &cached_method, const MethodOptions &options)
33         : method_ {method}, cached_method_ {cached_method}, options_ {options}
34     {
35     }
36 
37     ~Job() = default;
38 
39     NO_COPY_SEMANTIC(Job);
40 
IsFieldPresentForOffset(uint32_t offset)41     bool IsFieldPresentForOffset(uint32_t offset) const
42     {
43         return fields_.count(offset) != 0;
44     }
45 
IsMethodPresentForOffset(uint32_t offset)46     bool IsMethodPresentForOffset(uint32_t offset) const
47     {
48         return methods_.count(offset) != 0;
49     }
50 
IsClassPresentForOffset(uint32_t offset)51     bool IsClassPresentForOffset(uint32_t offset) const
52     {
53         return classes_.count(offset) != 0;
54     }
55 
GetField(uint32_t offset)56     const CachedField &GetField(uint32_t offset) const
57     {
58         return fields_.at(offset);
59     }
60 
GetMethod(uint32_t offset)61     const CachedMethod &GetMethod(uint32_t offset) const
62     {
63         return methods_.at(offset);
64     }
65 
GetClass(uint32_t offset)66     const CachedClass &GetClass(uint32_t offset) const
67     {
68         return classes_.at(offset);
69     }
70 
JobCachedMethod()71     const CachedMethod &JobCachedMethod() const
72     {
73         return cached_method_;
74     }
75 
JobMethodCflow()76     const CflowMethodInfo &JobMethodCflow() const
77     {
78         return *cflow_info_;
79     }
80 
81     template <typename Handler>
ForAllCachedClasses(Handler && handler)82     void ForAllCachedClasses(Handler &&handler) const
83     {
84         for (const auto &item : classes_) {
85             handler(item.second.get());
86         }
87     }
88 
89     template <typename Handler>
ForAllCachedMethods(Handler && handler)90     void ForAllCachedMethods(Handler &&handler) const
91     {
92         for (const auto &item : methods_) {
93             handler(item.second.get());
94         }
95     }
96 
97     template <typename Handler>
ForAllCachedFields(Handler && handler)98     void ForAllCachedFields(Handler &&handler) const
99     {
100         for (const auto &item : fields_) {
101             handler(item.second.get());
102         }
103     }
104 
Options()105     const auto &Options() const
106     {
107         return options_;
108     }
109 
110     bool DoChecks(LibCache &cache, PandaTypes &types);
111 
112 private:
113     Method &method_;
114     const CachedMethod &cached_method_;
115     const MethodOptions &options_;
116     PandaUniquePtr<CflowMethodInfo> cflow_info_;
117 
118     // TODO(vdyadov): store file_id for double check during verification
119     // offset -> cache item
120     PandaUnorderedMap<uint32_t, std::reference_wrapper<const CachedField>> fields_;
121     PandaUnorderedMap<uint32_t, std::reference_wrapper<const CachedMethod>> methods_;
122     PandaUnorderedMap<uint32_t, std::reference_wrapper<const CachedClass>> classes_;
123 
124     bool ResolveIdentifiers(LibCache &cache);
125 
126     bool UpdateTypes(PandaTypes &types) const;
127 
128     bool Verify(PandaTypes &types) const;
129 
AddField(uint32_t offset,const CachedField & cached_field)130     void AddField(uint32_t offset, const CachedField &cached_field)
131     {
132         fields_.emplace(offset, std::cref(cached_field));
133     }
134 
AddMethod(uint32_t offset,const CachedMethod & cached_method)135     void AddMethod(uint32_t offset, const CachedMethod &cached_method)
136     {
137         methods_.emplace(offset, std::cref(cached_method));
138     }
139 
AddClass(uint32_t offset,const CachedClass & cached_class)140     void AddClass(uint32_t offset, const CachedClass &cached_class)
141     {
142         classes_.emplace(offset, std::cref(cached_class));
143     }
144 };
145 }  // namespace panda::verifier
146 
147 #endif  //! PANDA_VERIFIER_JOB_QUEUE_JOB_H_
148