1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_VERIFIER_CLASS_VERIFIER_H_
18 #define ART_RUNTIME_VERIFIER_CLASS_VERIFIER_H_
19 
20 #include <string>
21 
22 #include <android-base/macros.h>
23 #include <android-base/thread_annotations.h>
24 
25 #include "base/locks.h"
26 #include "handle.h"
27 #include "obj_ptr.h"
28 #include "verifier/method_verifier.h"
29 #include "verifier/reg_type_cache.h"
30 #include "verifier_enums.h"
31 
32 namespace art {
33 
34 class ClassLinker;
35 class CompilerCallbacks;
36 class DexFile;
37 class RootVisitor;
38 class Thread;
39 
40 namespace dex {
41 struct ClassDef;
42 }  // namespace dex
43 
44 namespace mirror {
45 class Class;
46 class DexCache;
47 class ClassLoader;
48 }  // namespace mirror
49 
50 namespace verifier {
51 
52 class VerifierDeps;
53 
54 // Verifier that ensures the complete class is OK.
55 class ClassVerifier {
56  public:
57   // Redo verification on a loaded class. This is for use by class redefinition. This must be called
58   // with all methods already having all of kAccDontCompile and kAccCountLocks and not having
59   // kAccSkipAccessChecks. This will remove some of these flags from the method. The caller must
60   // ensure this cannot race with other changes to the verification class flags.
61   static FailureKind ReverifyClass(Thread* self,
62                                    ObjPtr<mirror::Class> klass,
63                                    HardFailLogMode log_level,
64                                    uint32_t api_level,
65                                    std::string* error)
66       REQUIRES_SHARED(Locks::mutator_lock_);
67   // Verify a class. Returns "kNoFailure" on success.
68   static FailureKind VerifyClass(Thread* self,
69                                  VerifierDeps* verifier_deps,
70                                  ObjPtr<mirror::Class> klass,
71                                  CompilerCallbacks* callbacks,
72                                  bool allow_soft_failures,
73                                  HardFailLogMode log_level,
74                                  uint32_t api_level,
75                                  std::string* error)
76       REQUIRES_SHARED(Locks::mutator_lock_);
77   static FailureKind VerifyClass(Thread* self,
78                                  VerifierDeps* verifier_deps,
79                                  const DexFile* dex_file,
80                                  Handle<mirror::DexCache> dex_cache,
81                                  Handle<mirror::ClassLoader> class_loader,
82                                  const dex::ClassDef& class_def,
83                                  CompilerCallbacks* callbacks,
84                                  bool allow_soft_failures,
85                                  HardFailLogMode log_level,
86                                  uint32_t api_level,
87                                  std::string* error)
88       REQUIRES_SHARED(Locks::mutator_lock_);
89 
90   static void Init(ClassLinker* class_linker) REQUIRES_SHARED(Locks::mutator_lock_);
91   static void Shutdown();
92 
93   static void VisitStaticRoots(RootVisitor* visitor)
94       REQUIRES_SHARED(Locks::mutator_lock_);
95 
96  private:
97   static FailureKind CommonVerifyClass(Thread* self,
98                                        VerifierDeps* verifier_deps,
99                                        ObjPtr<mirror::Class> klass,
100                                        CompilerCallbacks* callbacks,
101                                        VerifierCallback* verifier_callback,
102                                        bool allow_soft_failures,
103                                        HardFailLogMode log_level,
104                                        uint32_t api_level,
105                                        std::string* error)
106       REQUIRES_SHARED(Locks::mutator_lock_);
107 
108   static FailureKind VerifyClass(Thread* self,
109                                  VerifierDeps* verifier_deps,
110                                  const DexFile* dex_file,
111                                  Handle<mirror::DexCache> dex_cache,
112                                  Handle<mirror::ClassLoader> class_loader,
113                                  const dex::ClassDef& class_def,
114                                  CompilerCallbacks* callbacks,
115                                  VerifierCallback* verifier_callback,
116                                  bool allow_soft_failures,
117                                  HardFailLogMode log_level,
118                                  uint32_t api_level,
119                                  std::string* error)
120       REQUIRES_SHARED(Locks::mutator_lock_);
121   DISALLOW_COPY_AND_ASSIGN(ClassVerifier);
122 };
123 
124 }  // namespace verifier
125 }  // namespace art
126 
127 #endif  // ART_RUNTIME_VERIFIER_CLASS_VERIFIER_H_
128