Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 22 | #include "base/macros.h" |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 23 | #include "obj_ptr.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 24 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 25 | namespace art { |
| 26 | |
| 27 | namespace mirror { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 28 | class Class; |
| 29 | class Object; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 30 | } // namespace mirror |
| 31 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 32 | // How we want to check the heap's correctness. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 33 | enum VerifyObjectMode { |
| 34 | kVerifyObjectModeDisabled, // Heap verification is disabled. |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 35 | kVerifyObjectModeFast, // Check heap accesses quickly by using VerifyClassClass. |
| 36 | kVerifyObjectModeAll // Check heap accesses thoroughly. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 51 | static constexpr VerifyObjectFlags kDefaultVerifyFlags = kVerifyNone; |
| 52 | static constexpr VerifyObjectMode kVerifyObjectSupport = |
| 53 | kDefaultVerifyFlags != 0 ? kVerifyObjectModeFast : kVerifyObjectModeDisabled; |
| 54 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 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 |
| 60 | static inline void VerifyObject(ObjPtr<mirror::Object> obj) NO_THREAD_SAFETY_ANALYSIS { |
| 61 | if (kVerifyObjectSupport > kVerifyObjectModeDisabled && obj != nullptr) { |
| 62 | VerifyObjectImpl(obj); |
| 63 | } |
| 64 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 65 | |
Mathieu Chartier | 9911128 | 2018-06-19 12:30:56 -0700 | [diff] [blame] | 66 | inline constexpr VerifyObjectFlags RemoveThisFlags(VerifyObjectFlags flags) { |
| 67 | return static_cast<VerifyObjectFlags>(flags & ~kVerifyThis); |
| 68 | } |
| 69 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 70 | // Check that c.getClass() == c.getClass().getClass(). |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 71 | ALWAYS_INLINE bool VerifyClassClass(ObjPtr<mirror::Class> c) NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 72 | |
| 73 | } // namespace art |
| 74 | |
| 75 | #endif // ART_RUNTIME_VERIFY_OBJECT_H_ |