1 /* 2 * Copyright (c) 2023 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 MAPLEIR_VERIFICATION_PHASE_H 17 #define MAPLEIR_VERIFICATION_PHASE_H 18 #include "class_hierarchy.h" 19 #include "verify_pragma_info.h" 20 21 namespace maple { 22 using ClassVerifyPragmas = MapleUnorderedMap<std::string, std::vector<const VerifyPragmaInfo *>>; 23 24 class VerifyResult { 25 public: VerifyResult(const MIRModule & module,const KlassHierarchy & klassHierarchy,MemPool & memPool)26 VerifyResult(const MIRModule &module, const KlassHierarchy &klassHierarchy, MemPool &memPool) 27 : module(module), 28 klassHierarchy(klassHierarchy), 29 allocator(&memPool), 30 classesCorrectness(allocator.Adapter()), 31 classesPragma(allocator.Adapter()) 32 { 33 } 34 35 ~VerifyResult() = default; 36 GetKlassHierarchy()37 const KlassHierarchy &GetKlassHierarchy() const 38 { 39 return klassHierarchy; 40 } 41 GetMIRModule()42 const MIRModule &GetMIRModule() const 43 { 44 return module; 45 } 46 GetCurrentFunction()47 const MIRFunction *GetCurrentFunction() const 48 { 49 return module.GetFunctionList().front(); 50 } 51 GetCurrentClassName()52 const std::string &GetCurrentClassName() const 53 { 54 return GetCurrentFunction()->GetClassType()->GetName(); 55 } 56 GetDeferredClassesPragma()57 const ClassVerifyPragmas &GetDeferredClassesPragma() const 58 { 59 return classesPragma; 60 } 61 62 void AddPragmaVerifyError(const std::string &className, std::string errMsg); 63 void AddPragmaAssignableCheck(const std::string &className, std::string fromType, std::string toType); 64 void AddPragmaExtendFinalCheck(const std::string &className); 65 void AddPragmaOverrideFinalCheck(const std::string &className); 66 GetResultMap()67 const MapleUnorderedMap<std::string, bool> &GetResultMap() const 68 { 69 return classesCorrectness; 70 } SetClassCorrectness(const std::string & className,bool result)71 void SetClassCorrectness(const std::string &className, bool result) 72 { 73 classesCorrectness[className] = result; 74 } 75 HasErrorNotDeferred()76 bool HasErrorNotDeferred() const 77 { 78 for (auto &classResult : classesCorrectness) { 79 if (!classResult.second) { 80 if (classesPragma.find(classResult.first) == classesPragma.end()) { 81 // Verify result is not OK, but has no deferred check or verify error in runtime 82 return true; 83 } 84 } 85 } 86 return false; 87 } 88 89 private: 90 bool HasVerifyError(const std::vector<const VerifyPragmaInfo *> &pragmaInfoPtrVec) const; 91 bool HasSamePragmaInfo(const std::vector<const VerifyPragmaInfo *> &pragmaInfoPtrVec, 92 const VerifyPragmaInfo &verifyPragmaInfo) const; 93 94 const MIRModule &module; 95 const KlassHierarchy &klassHierarchy; 96 MapleAllocator allocator; 97 // classesCorrectness<key: className, value: correctness>, correctness is true only if the class is verified OK 98 MapleUnorderedMap<std::string, bool> classesCorrectness; 99 // classesPragma<key: className, value: PragmaInfo for deferred check> 100 ClassVerifyPragmas classesPragma; 101 }; 102 103 class VerificationPhaseResult : public AnalysisResult { 104 public: VerificationPhaseResult(MemPool & mp,const VerifyResult & verifyResult)105 VerificationPhaseResult(MemPool &mp, const VerifyResult &verifyResult) 106 : AnalysisResult(&mp), verifyResult(verifyResult) 107 { 108 } 109 ~VerificationPhaseResult() = default; 110 GetDeferredClassesPragma()111 const ClassVerifyPragmas &GetDeferredClassesPragma() const 112 { 113 return verifyResult.GetDeferredClassesPragma(); 114 } 115 116 private: 117 const VerifyResult &verifyResult; 118 }; 119 120 #ifdef NOT_USED 121 class DoVerification : public ModulePhase { 122 public: DoVerification(ModulePhaseID id)123 explicit DoVerification(ModulePhaseID id) : ModulePhase(id) {} 124 125 AnalysisResult *Run(MIRModule *module, ModuleResultMgr *mgr) override; PhaseName()126 std::string PhaseName() const override 127 { 128 return "verification"; 129 } 130 131 ~DoVerification() = default; 132 133 private: 134 void VerifyModule(MIRModule &module, VerifyResult &result) const; 135 void DeferredCheckFinalClassAndMethod(VerifyResult &result) const; 136 bool IsLazyBindingOrDecouple(const KlassHierarchy &klassHierarchy) const; 137 bool NeedRuntimeFinalCheck(const KlassHierarchy &klassHierarchy, const std::string &className) const; 138 void CheckExtendFinalClass(VerifyResult &result) const; 139 }; 140 #endif 141 } // namespace maple 142 #endif // MAPLEIR_VERIFICATION_PHASE_H 143