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_MIRROR_OBJECT_REFERENCE_H_
18 #define ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_
19
20 #include <array>
21 #include <string_view>
22
23 #include "base/atomic.h"
24 #include "base/casts.h"
25 #include "base/locks.h" // For Locks::mutator_lock_.
26 #include "heap_poisoning.h"
27 #include "obj_ptr.h"
28 #include "runtime_globals.h"
29
30 namespace art {
31 namespace mirror {
32
33 class Object;
34
35 // Classes shared with the managed side of the world need to be packed so that they don't have
36 // extra platform specific padding.
37 #define MANAGED PACKED(4)
38 #define MIRROR_CLASS(desc) \
39 static_assert(::art::mirror::IsMirroredDescriptor(desc), \
40 desc " is not a known mirror class. Please update" \
41 " IsMirroredDescriptor to include it!")
42
IsMirroredDescriptor(std::string_view desc)43 constexpr bool IsMirroredDescriptor(std::string_view desc) {
44 if (desc[0] != 'L') {
45 // All primitives and arrays are mirrored
46 return true;
47 }
48 #define MIRROR_DESCRIPTORS(vis) \
49 vis("Ljava/lang/Class;") \
50 vis("Ljava/lang/ClassLoader;") \
51 vis("Ljava/lang/ClassNotFoundException;") \
52 vis("Ljava/lang/DexCache;") \
53 vis("Ljava/lang/Object;") \
54 vis("Ljava/lang/StackFrameInfo;") \
55 vis("Ljava/lang/StackTraceElement;") \
56 vis("Ljava/lang/String;") \
57 vis("Ljava/lang/Throwable;") \
58 vis("Ljava/lang/invoke/ArrayElementVarHandle;") \
59 vis("Ljava/lang/invoke/ByteArrayViewVarHandle;") \
60 vis("Ljava/lang/invoke/ByteBufferViewVarHandle;") \
61 vis("Ljava/lang/invoke/CallSite;") \
62 vis("Ljava/lang/invoke/FieldVarHandle;") \
63 vis("Ljava/lang/invoke/StaticFieldVarHandle;") \
64 vis("Ljava/lang/invoke/MethodHandle;") \
65 vis("Ljava/lang/invoke/MethodHandleImpl;") \
66 vis("Ljava/lang/invoke/MethodHandles$Lookup;") \
67 vis("Ljava/lang/invoke/MethodType;") \
68 vis("Ljava/lang/invoke/VarHandle;") \
69 vis("Ljava/lang/ref/FinalizerReference;") \
70 vis("Ljava/lang/ref/Reference;") \
71 vis("Ljava/lang/reflect/AccessibleObject;") \
72 vis("Ljava/lang/reflect/Constructor;") \
73 vis("Ljava/lang/reflect/Executable;") \
74 vis("Ljava/lang/reflect/Field;") \
75 vis("Ljava/lang/reflect/Method;") \
76 vis("Ljava/lang/reflect/Proxy;") \
77 vis("Ldalvik/system/ClassExt;") \
78 vis("Ldalvik/system/EmulatedStackFrame;")
79 // TODO: Once we are C++ 20 we can just have a constexpr array and std::find.
80 // constexpr std::array<std::string_view, 28> kMirrorTypes{
81 // // Fill in
82 // };
83 // return std::find(kMirrorTypes.begin(), kMirrorTypes.end(), desc) != kMirrorTypes.end();
84 #define CHECK_DESCRIPTOR(descriptor) \
85 if (std::string_view(descriptor) == desc) { \
86 return true; \
87 }
88 MIRROR_DESCRIPTORS(CHECK_DESCRIPTOR)
89 #undef CHECK_DESCRIPTOR
90 return false;
91 #undef MIRROR_DESCRIPTORS
92 }
93
94 template<bool kPoisonReferences, class MirrorType>
95 class PtrCompression {
96 public:
97 // Compress reference to its bit representation.
Compress(MirrorType * mirror_ptr)98 static uint32_t Compress(MirrorType* mirror_ptr) {
99 uint32_t as_bits = reinterpret_cast32<uint32_t>(mirror_ptr);
100 return kPoisonReferences ? -as_bits : as_bits;
101 }
102
103 // Uncompress an encoded reference from its bit representation.
Decompress(uint32_t ref)104 static MirrorType* Decompress(uint32_t ref) {
105 uint32_t as_bits = kPoisonReferences ? -ref : ref;
106 return reinterpret_cast32<MirrorType*>(as_bits);
107 }
108
109 // Convert an ObjPtr to a compressed reference.
110 static uint32_t Compress(ObjPtr<MirrorType> ptr) REQUIRES_SHARED(Locks::mutator_lock_);
111 };
112
113 // Value type representing a reference to a mirror::Object of type MirrorType.
114 template<bool kPoisonReferences, class MirrorType>
115 class MANAGED ObjectReference {
116 private:
117 using Compression = PtrCompression<kPoisonReferences, MirrorType>;
118
119 public:
120 /*
121 * Returns a pointer to the mirror of the managed object this reference is for.
122 *
123 * This does NOT return the current object (which isn't derived from, and
124 * therefor cannot be a mirror::Object) as a mirror pointer. Instead, this
125 * returns a pointer to the mirror of the managed object this refers to.
126 *
127 * TODO (chriswailes): Rename to GetPtr().
128 */
AsMirrorPtr()129 MirrorType* AsMirrorPtr() const {
130 return Compression::Decompress(reference_);
131 }
132
Assign(MirrorType * other)133 void Assign(MirrorType* other) {
134 reference_ = Compression::Compress(other);
135 }
136
137 void Assign(ObjPtr<MirrorType> ptr) REQUIRES_SHARED(Locks::mutator_lock_);
138
Clear()139 void Clear() {
140 reference_ = 0;
141 DCHECK(IsNull());
142 }
143
IsNull()144 bool IsNull() const {
145 return reference_ == 0;
146 }
147
FromMirrorPtr(MirrorType * mirror_ptr)148 static ObjectReference<kPoisonReferences, MirrorType> FromMirrorPtr(MirrorType* mirror_ptr)
149 REQUIRES_SHARED(Locks::mutator_lock_) {
150 return ObjectReference<kPoisonReferences, MirrorType>(mirror_ptr);
151 }
152
153 protected:
ObjectReference(MirrorType * mirror_ptr)154 explicit ObjectReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_)
155 : reference_(Compression::Compress(mirror_ptr)) {
156 }
ObjectReference()157 ObjectReference() : reference_(0u) {
158 DCHECK(IsNull());
159 }
160
161 // The encoded reference to a mirror::Object.
162 uint32_t reference_;
163 };
164
165 // References between objects within the managed heap.
166 // Similar API to ObjectReference, but not a value type. Supports atomic access.
167 template<class MirrorType>
168 class MANAGED HeapReference {
169 private:
170 using Compression = PtrCompression<kPoisonHeapReferences, MirrorType>;
171
172 public:
HeapReference()173 HeapReference() REQUIRES_SHARED(Locks::mutator_lock_) : HeapReference(nullptr) {}
174
175 template <bool kIsVolatile = false>
AsMirrorPtr()176 MirrorType* AsMirrorPtr() const REQUIRES_SHARED(Locks::mutator_lock_) {
177 return Compression::Decompress(
178 kIsVolatile ? reference_.load(std::memory_order_seq_cst) : reference_.LoadJavaData());
179 }
180
181 template <bool kIsVolatile = false>
Assign(MirrorType * other)182 void Assign(MirrorType* other) REQUIRES_SHARED(Locks::mutator_lock_) {
183 if (kIsVolatile) {
184 reference_.store(Compression::Compress(other), std::memory_order_seq_cst);
185 } else {
186 reference_.StoreJavaData(Compression::Compress(other));
187 }
188 }
189
190 template <bool kIsVolatile = false>
191 void Assign(ObjPtr<MirrorType> ptr) REQUIRES_SHARED(Locks::mutator_lock_);
192
Clear()193 void Clear() {
194 reference_.StoreJavaData(0);
195 DCHECK(IsNull());
196 }
197
IsNull()198 bool IsNull() const {
199 return reference_.LoadJavaData() == 0;
200 }
201
FromMirrorPtr(MirrorType * mirror_ptr)202 static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr)
203 REQUIRES_SHARED(Locks::mutator_lock_) {
204 return HeapReference<MirrorType>(mirror_ptr);
205 }
206
207 bool CasWeakRelaxed(MirrorType* old_ptr, MirrorType* new_ptr)
208 REQUIRES_SHARED(Locks::mutator_lock_);
209
210 private:
HeapReference(MirrorType * mirror_ptr)211 explicit HeapReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_)
212 : reference_(Compression::Compress(mirror_ptr)) {}
213
214 // The encoded reference to a mirror::Object. Atomically updateable.
215 Atomic<uint32_t> reference_;
216 };
217
218 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize,
219 "heap reference size does not match");
220
221 // Standard compressed reference used in the runtime. Used for StackReference and GC roots.
222 template<class MirrorType>
223 class MANAGED CompressedReference : public mirror::ObjectReference<false, MirrorType> {
224 public:
225 CompressedReference<MirrorType>()
226 : mirror::ObjectReference<false, MirrorType>() {}
227
FromMirrorPtr(MirrorType * p)228 static CompressedReference<MirrorType> FromMirrorPtr(MirrorType* p)
229 REQUIRES_SHARED(Locks::mutator_lock_) {
230 return CompressedReference<MirrorType>(p);
231 }
232
FromVRegValue(uint32_t vreg_value)233 static CompressedReference<MirrorType> FromVRegValue(uint32_t vreg_value) {
234 CompressedReference<MirrorType> result;
235 result.reference_ = vreg_value;
236 return result;
237 }
238
AsVRegValue()239 uint32_t AsVRegValue() const {
240 return this->reference_;
241 }
242
243 private:
CompressedReference(MirrorType * p)244 explicit CompressedReference(MirrorType* p) REQUIRES_SHARED(Locks::mutator_lock_)
245 : ObjectReference<false, MirrorType>(p) {}
246 };
247
248 } // namespace mirror
249 } // namespace art
250
251 #endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_
252