blob: e8df2ad4ce8c9e6bc77a85615bea6faeb4078405 [file] [log] [blame]
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -07001/*
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_READ_BARRIER_H_
18#define ART_RUNTIME_READ_BARRIER_H_
19
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070022#include "base/macros.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "base/mutex.h"
Andreas Gampedcc528d2017-12-07 13:37:10 -080024#include "base/runtime_debug.h"
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070025#include "gc_root.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080026#include "jni.h"
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070027#include "mirror/object_reference.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070028#include "offsets.h"
Andreas Gampe217488a2017-09-18 08:34:42 -070029#include "read_barrier_config.h"
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070030
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070031namespace art {
32namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080033class Object;
34template<typename MirrorType> class HeapReference;
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070035} // namespace mirror
Mathieu Chartiere401d142015-04-22 13:56:20 -070036class ArtMethod;
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070037
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070038class ReadBarrier {
39 public:
Andreas Gampeaea05c12017-05-19 08:45:02 -070040 // Enable the to-space invariant checks. This is slow and happens very often. Do not enable in
41 // fast-debug environment.
42 DECLARE_RUNTIME_DEBUG_FLAG(kEnableToSpaceInvariantChecks);
43
44 // Enable the read barrier checks. This is slow and happens very often. Do not enable in
45 // fast-debug environment.
46 DECLARE_RUNTIME_DEBUG_FLAG(kEnableReadBarrierInvariantChecks);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080047
Hans Boehmcc55e1d2017-07-27 15:28:07 -070048 // Return the reference at ref_addr, invoking read barrier as appropriate.
49 // Ref_addr is an address within obj.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080050 // It's up to the implementation whether the given field gets updated whereas the return value
51 // must be an updated reference unless kAlwaysUpdateField is true.
Hans Boehmcc55e1d2017-07-27 15:28:07 -070052 template <typename MirrorType,
53 bool kIsVolatile,
54 ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080055 bool kAlwaysUpdateField = false>
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070056 ALWAYS_INLINE static MirrorType* Barrier(
57 mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070059
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070060 // It's up to the implementation whether the given root gets updated
61 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080062 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070063 ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root,
64 GcRootSource* gc_root_source = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080066
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070067 // It's up to the implementation whether the given root gets updated
68 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080069 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070070 ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root,
71 GcRootSource* gc_root_source = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070073
Nicolas Geoffray13056a12017-05-11 11:48:28 +000074 // Return the mirror Object if it is marked, or null if not.
75 template <typename MirrorType>
76 ALWAYS_INLINE static MirrorType* IsMarked(MirrorType* ref)
77 REQUIRES_SHARED(Locks::mutator_lock_);
78
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080079 static bool IsDuringStartup();
80
81 // Without the holder object.
82 static void AssertToSpaceInvariant(mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080084 AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
85 }
86 // With the holder object.
87 static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
88 mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070089 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070090 // With GcRootSource.
91 static void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080093
Roland Levillaina78f5b62017-09-29 13:50:44 +010094 // Without the holder object, and only with the read barrier configuration (no-op otherwise).
95 static void MaybeAssertToSpaceInvariant(mirror::Object* ref)
96 REQUIRES_SHARED(Locks::mutator_lock_) {
97 if (kUseReadBarrier) {
98 AssertToSpaceInvariant(ref);
99 }
100 }
101
Hiroshi Yamauchi2e5de782016-01-29 12:06:36 -0800102 // ALWAYS_INLINE on this caused a performance regression b/26744236.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 static mirror::Object* Mark(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800104
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700105 static constexpr uint32_t WhiteState() {
106 return white_state_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800107 }
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700108 static constexpr uint32_t GrayState() {
109 return gray_state_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800110 }
111
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700112 // fake_address_dependency will be zero which should be bitwise-or'ed with the address of the
113 // subsequent load to prevent the reordering of the read barrier bit load and the subsequent
114 // object reference load (from one of `obj`'s fields).
115 // *fake_address_dependency will be set to 0.
116 ALWAYS_INLINE static bool IsGray(mirror::Object* obj, uintptr_t* fake_address_dependency)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700117 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800118
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700119 // This uses a load-acquire to load the read barrier bit internally to prevent the reordering of
120 // the read barrier bit load and the subsequent load.
121 ALWAYS_INLINE static bool IsGray(mirror::Object* obj)
122 REQUIRES_SHARED(Locks::mutator_lock_);
123
124 static bool IsValidReadBarrierState(uint32_t rb_state) {
125 return rb_state == white_state_ || rb_state == gray_state_;
126 }
127
128 static constexpr uint32_t white_state_ = 0x0; // Not marked.
129 static constexpr uint32_t gray_state_ = 0x1; // Marked, but not marked through. On mark stack.
130 static constexpr uint32_t rb_state_mask_ = 0x1; // The low bits for white|gray.
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700131};
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700132
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700133} // namespace art
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700134
135#endif // ART_RUNTIME_READ_BARRIER_H_