1 /*
2  * Copyright (C) 2014 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_VERIFY_OBJECT_H_
18 #define ART_RUNTIME_VERIFY_OBJECT_H_
19 
20 #include <stdint.h>
21 
22 #include "base/macros.h"
23 #include "obj_ptr.h"
24 
25 namespace art {
26 
27 namespace mirror {
28 class Class;
29 class Object;
30 }  // namespace mirror
31 
32 // How we want to check the heap's correctness.
33 enum VerifyObjectMode {
34   kVerifyObjectModeDisabled,  // Heap verification is disabled.
35   kVerifyObjectModeFast,  // Check heap accesses quickly by using VerifyClassClass.
36   kVerifyObjectModeAll  // Check heap accesses thoroughly.
37 };
38 
39 enum VerifyObjectFlags {
40   kVerifyNone = 0x0,
41   // Verify self when we are doing an operation.
42   kVerifyThis = 0x1,
43   // Verify reads from objects.
44   kVerifyReads = 0x2,
45   // Verify writes to objects.
46   kVerifyWrites = 0x4,
47   // Verify all things.
48   kVerifyAll = kVerifyThis | kVerifyReads | kVerifyWrites,
49 };
50 
51 static constexpr VerifyObjectFlags kDefaultVerifyFlags = kVerifyNone;
52 static constexpr VerifyObjectMode kVerifyObjectSupport =
53     kDefaultVerifyFlags != 0 ? kVerifyObjectModeFast : kVerifyObjectModeDisabled;
54 
55 // Implements the actual object checks.
56 void VerifyObjectImpl(ObjPtr<mirror::Object> obj) NO_THREAD_SAFETY_ANALYSIS;
57 
58 // Is a front to optimize out any calls if no verification is enabled.
59 ALWAYS_INLINE
VerifyObject(ObjPtr<mirror::Object> obj)60 static inline void VerifyObject(ObjPtr<mirror::Object> obj) NO_THREAD_SAFETY_ANALYSIS {
61   if (kVerifyObjectSupport > kVerifyObjectModeDisabled && obj != nullptr) {
62     VerifyObjectImpl(obj);
63   }
64 }
65 
RemoveThisFlags(VerifyObjectFlags flags)66 inline constexpr VerifyObjectFlags RemoveThisFlags(VerifyObjectFlags flags) {
67   return static_cast<VerifyObjectFlags>(flags & ~kVerifyThis);
68 }
69 
70 // Check that c.getClass() == c.getClass().getClass().
71 ALWAYS_INLINE bool VerifyClassClass(ObjPtr<mirror::Class> c) NO_THREAD_SAFETY_ANALYSIS;
72 
73 }  // namespace art
74 
75 #endif  // ART_RUNTIME_VERIFY_OBJECT_H_
76