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