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_VERIFICATION_VERIFICATION_CONTEXT_HPP 17 #define PANDA_VERIFICATION_VERIFICATION_CONTEXT_HPP 18 19 #include "libpandabase/macros.h" 20 #include "runtime/include/method.h" 21 #include "runtime/include/method-inl.h" 22 23 #include "verification/absint/exec_context.h" 24 #include "verification/cflow/cflow_info.h" 25 #include "verification/jobs/job.h" 26 #include "verification/plugins.h" 27 #include "verification/type/type_system.h" 28 #include "verification/util/lazy.h" 29 #include "verification/util/callable.h" 30 #include "verification/value/variables.h" 31 32 #include <functional> 33 34 namespace panda::verifier { 35 using CallIntoRuntimeHandler = callable<void(callable<void()>)>; 36 37 class VerificationContext { 38 public: 39 using Var = Variables::Var; 40 VerificationContext(TypeSystem * typeSystem,Job const * job,Type methodClassType)41 VerificationContext(TypeSystem *typeSystem, Job const *job, Type methodClassType) 42 : types_ {typeSystem}, 43 job_ {job}, 44 methodClassType_ {methodClassType}, 45 execCtx_ {CflowInfo().GetAddrStart(), CflowInfo().GetAddrEnd(), typeSystem}, 46 plugin_ {plugin::GetLanguagePlugin(job->JobMethod()->GetClass()->GetSourceLang())} 47 { 48 Method const *method = job->JobMethod(); 49 // set checkpoints for reg_context storage 50 // start of method is checkpoint too 51 ExecCtx().SetCheckPoint(CflowInfo().GetAddrStart()); 52 uint8_t const *start = CflowInfo().GetAddrStart(); 53 uint8_t const *end = CflowInfo().GetAddrEnd(); 54 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 55 for (uint8_t const *pc = start; pc < end; pc++) { 56 if (CflowInfo().IsFlagSet(pc, CflowMethodInfo::JUMP_TARGET)) { 57 ExecCtx().SetCheckPoint(pc); 58 } 59 } 60 method->EnumerateCatchBlocks([&](uint8_t const *tryStart, uint8_t const *tryEnd, 61 panda_file::CodeDataAccessor::CatchBlock const &catchBlock) { 62 auto catchStart = reinterpret_cast<uint8_t const *>(reinterpret_cast<uintptr_t>(method->GetInstructions()) + 63 static_cast<uintptr_t>(catchBlock.GetHandlerPc())); 64 ExecCtx().SetCheckPoint(tryStart); 65 ExecCtx().SetCheckPoint(tryEnd); 66 ExecCtx().SetCheckPoint(catchStart); 67 return true; 68 }); 69 } 70 71 ~VerificationContext() = default; 72 DEFAULT_MOVE_SEMANTIC(VerificationContext); 73 DEFAULT_COPY_SEMANTIC(VerificationContext); 74 GetJob()75 Job const *GetJob() const 76 { 77 return job_; 78 } 79 CflowInfo()80 const CflowMethodInfo &CflowInfo() const 81 { 82 return job_->JobMethodCflow(); 83 } 84 GetMethod()85 Method const *GetMethod() const 86 { 87 return job_->JobMethod(); 88 } 89 GetMethodClass()90 Type GetMethodClass() const 91 { 92 return methodClassType_; 93 } 94 ExecCtx()95 ExecContext &ExecCtx() 96 { 97 return execCtx_; 98 } 99 ExecCtx()100 const ExecContext &ExecCtx() const 101 { 102 return execCtx_; 103 } 104 GetTypeSystem()105 TypeSystem *GetTypeSystem() 106 { 107 return types_; 108 } 109 NewVar()110 Var NewVar() 111 { 112 return types_->NewVar(); 113 } 114 ReturnType()115 Type ReturnType() const 116 { 117 return returnType_; 118 } 119 SetReturnType(Type const * type)120 void SetReturnType(Type const *type) 121 { 122 returnType_ = *type; 123 } 124 GetPlugin()125 plugin::Plugin const *GetPlugin() 126 { 127 return plugin_; 128 } 129 130 private: 131 TypeSystem *types_; 132 Job const *job_; 133 Type returnType_; 134 Type methodClassType_; 135 ExecContext execCtx_; 136 plugin::Plugin const *plugin_; 137 }; 138 } // namespace panda::verifier 139 140 #endif // !PANDA_VERIFICATION_VERIFICATION_CONTEXT_HPP 141