xref: /aosp_15_r20/art/runtime/obj_ptr-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_OBJ_PTR_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_OBJ_PTR_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <ostream>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
23*795d594fSAndroid Build Coastguard Worker #include "obj_ptr.h"
24*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
GetCurrentTrimedCookie()29*795d594fSAndroid Build Coastguard Worker inline uintptr_t ObjPtr<MirrorType>::GetCurrentTrimedCookie() {
30*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
31*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(self == nullptr)) {
32*795d594fSAndroid Build Coastguard Worker     return kCookieMask;
33*795d594fSAndroid Build Coastguard Worker   }
34*795d594fSAndroid Build Coastguard Worker   return self->GetPoisonObjectCookie() & kCookieMask;
35*795d594fSAndroid Build Coastguard Worker }
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
IsValid()38*795d594fSAndroid Build Coastguard Worker inline bool ObjPtr<MirrorType>::IsValid() const {
39*795d594fSAndroid Build Coastguard Worker   if (!kObjPtrPoisoning || IsNull()) {
40*795d594fSAndroid Build Coastguard Worker     return true;
41*795d594fSAndroid Build Coastguard Worker   }
42*795d594fSAndroid Build Coastguard Worker   return GetCookie() == GetCurrentTrimedCookie();
43*795d594fSAndroid Build Coastguard Worker }
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
AssertValid()46*795d594fSAndroid Build Coastguard Worker inline void ObjPtr<MirrorType>::AssertValid() const {
47*795d594fSAndroid Build Coastguard Worker   if (kObjPtrPoisoning) {
48*795d594fSAndroid Build Coastguard Worker     CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie "
49*795d594fSAndroid Build Coastguard Worker         << GetCurrentTrimedCookie() << " but got " << GetCookie();
50*795d594fSAndroid Build Coastguard Worker   }
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
Encode(MirrorType * ptr)54*795d594fSAndroid Build Coastguard Worker inline uintptr_t ObjPtr<MirrorType>::Encode(MirrorType* ptr) {
55*795d594fSAndroid Build Coastguard Worker   uintptr_t ref = reinterpret_cast<uintptr_t>(ptr);
56*795d594fSAndroid Build Coastguard Worker   DCHECK_ALIGNED(ref, kObjectAlignment);
57*795d594fSAndroid Build Coastguard Worker   if (kObjPtrPoisoning && ref != 0) {
58*795d594fSAndroid Build Coastguard Worker     DCHECK_LE(ref, 0xFFFFFFFFU);
59*795d594fSAndroid Build Coastguard Worker     ref >>= kObjectAlignmentShift;
60*795d594fSAndroid Build Coastguard Worker     // Put cookie in high bits.
61*795d594fSAndroid Build Coastguard Worker     ref |= GetCurrentTrimedCookie() << kCookieShift;
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker   return ref;
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
67*795d594fSAndroid Build Coastguard Worker template <typename Type,
68*795d594fSAndroid Build Coastguard Worker           typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */>
ObjPtr(Type * ptr)69*795d594fSAndroid Build Coastguard Worker inline ObjPtr<MirrorType>::ObjPtr(Type* ptr)
70*795d594fSAndroid Build Coastguard Worker     : reference_(Encode(static_cast<MirrorType*>(ptr))) {
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
74*795d594fSAndroid Build Coastguard Worker template <typename Type,
75*795d594fSAndroid Build Coastguard Worker           typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */>
ObjPtr(const ObjPtr<Type> & other)76*795d594fSAndroid Build Coastguard Worker inline ObjPtr<MirrorType>::ObjPtr(const ObjPtr<Type>& other)
77*795d594fSAndroid Build Coastguard Worker     : reference_(other.reference_) {
78*795d594fSAndroid Build Coastguard Worker   if (kObjPtrPoisoningValidateOnCopy) {
79*795d594fSAndroid Build Coastguard Worker     AssertValid();
80*795d594fSAndroid Build Coastguard Worker   }
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
84*795d594fSAndroid Build Coastguard Worker template <typename Type,
85*795d594fSAndroid Build Coastguard Worker           typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */>
86*795d594fSAndroid Build Coastguard Worker inline ObjPtr<MirrorType>& ObjPtr<MirrorType>::operator=(const ObjPtr<Type>& other) {
87*795d594fSAndroid Build Coastguard Worker   reference_ = other.reference_;
88*795d594fSAndroid Build Coastguard Worker   if (kObjPtrPoisoningValidateOnCopy) {
89*795d594fSAndroid Build Coastguard Worker     AssertValid();
90*795d594fSAndroid Build Coastguard Worker   }
91*795d594fSAndroid Build Coastguard Worker   return *this;
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
95*795d594fSAndroid Build Coastguard Worker OBJPTR_INLINE ObjPtr<MirrorType>& ObjPtr<MirrorType>::operator=(MirrorType* ptr) {
96*795d594fSAndroid Build Coastguard Worker   Assign(ptr);
97*795d594fSAndroid Build Coastguard Worker   return *this;
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
Assign(MirrorType * ptr)101*795d594fSAndroid Build Coastguard Worker inline void ObjPtr<MirrorType>::Assign(MirrorType* ptr) {
102*795d594fSAndroid Build Coastguard Worker   reference_ = Encode(ptr);
103*795d594fSAndroid Build Coastguard Worker }
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
106*795d594fSAndroid Build Coastguard Worker inline MirrorType* ObjPtr<MirrorType>::operator->() const {
107*795d594fSAndroid Build Coastguard Worker   return Ptr();
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
Ptr()111*795d594fSAndroid Build Coastguard Worker inline MirrorType* ObjPtr<MirrorType>::Ptr() const {
112*795d594fSAndroid Build Coastguard Worker   AssertValid();
113*795d594fSAndroid Build Coastguard Worker   return PtrUnchecked();
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
117*795d594fSAndroid Build Coastguard Worker template <typename SourceType>
DownCast(ObjPtr<SourceType> ptr)118*795d594fSAndroid Build Coastguard Worker inline ObjPtr<MirrorType> ObjPtr<MirrorType>::DownCast(ObjPtr<SourceType> ptr) {
119*795d594fSAndroid Build Coastguard Worker   static_assert(std::is_base_of_v<SourceType, MirrorType>,
120*795d594fSAndroid Build Coastguard Worker                 "Target type must be a subtype of source type");
121*795d594fSAndroid Build Coastguard Worker   return static_cast<MirrorType*>(ptr.Ptr());
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
125*795d594fSAndroid Build Coastguard Worker template <typename SourceType>
DownCast(SourceType * ptr)126*795d594fSAndroid Build Coastguard Worker inline ObjPtr<MirrorType> ObjPtr<MirrorType>::DownCast(SourceType* ptr) {
127*795d594fSAndroid Build Coastguard Worker   static_assert(std::is_base_of_v<SourceType, MirrorType>,
128*795d594fSAndroid Build Coastguard Worker                 "Target type must be a subtype of source type");
129*795d594fSAndroid Build Coastguard Worker   return static_cast<MirrorType*>(ptr);
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker 
132*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
operator()133*795d594fSAndroid Build Coastguard Worker size_t HashObjPtr::operator()(const ObjPtr<MirrorType>& ptr) const {
134*795d594fSAndroid Build Coastguard Worker   return std::hash<MirrorType*>()(ptr.Ptr());
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker template<class MirrorType1, class MirrorType2>
138*795d594fSAndroid Build Coastguard Worker inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
139*795d594fSAndroid Build Coastguard Worker                         std::is_base_of_v<MirrorType2, MirrorType1>, bool>
140*795d594fSAndroid Build Coastguard Worker operator==(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) {
141*795d594fSAndroid Build Coastguard Worker   return lhs.Ptr() == rhs.Ptr();
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker template<class MirrorType1, class MirrorType2>
145*795d594fSAndroid Build Coastguard Worker inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
146*795d594fSAndroid Build Coastguard Worker                         std::is_base_of_v<MirrorType2, MirrorType1>, bool>
147*795d594fSAndroid Build Coastguard Worker operator==(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) {
148*795d594fSAndroid Build Coastguard Worker   return lhs == rhs.Ptr();
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker 
151*795d594fSAndroid Build Coastguard Worker template<class MirrorType1, class MirrorType2>
152*795d594fSAndroid Build Coastguard Worker inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
153*795d594fSAndroid Build Coastguard Worker                         std::is_base_of_v<MirrorType2, MirrorType1>, bool>
154*795d594fSAndroid Build Coastguard Worker operator==(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) {
155*795d594fSAndroid Build Coastguard Worker   return lhs.Ptr() == rhs;
156*795d594fSAndroid Build Coastguard Worker }
157*795d594fSAndroid Build Coastguard Worker 
158*795d594fSAndroid Build Coastguard Worker template<class MirrorType1, class MirrorType2>
159*795d594fSAndroid Build Coastguard Worker inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
160*795d594fSAndroid Build Coastguard Worker                         std::is_base_of_v<MirrorType2, MirrorType1>, bool>
161*795d594fSAndroid Build Coastguard Worker operator!=(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) {
162*795d594fSAndroid Build Coastguard Worker   return !(lhs == rhs);
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker template<class MirrorType1, class MirrorType2>
166*795d594fSAndroid Build Coastguard Worker inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
167*795d594fSAndroid Build Coastguard Worker                         std::is_base_of_v<MirrorType2, MirrorType1>, bool>
168*795d594fSAndroid Build Coastguard Worker operator!=(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) {
169*795d594fSAndroid Build Coastguard Worker   return !(lhs == rhs);
170*795d594fSAndroid Build Coastguard Worker }
171*795d594fSAndroid Build Coastguard Worker 
172*795d594fSAndroid Build Coastguard Worker template<class MirrorType1, class MirrorType2>
173*795d594fSAndroid Build Coastguard Worker inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
174*795d594fSAndroid Build Coastguard Worker                         std::is_base_of_v<MirrorType2, MirrorType1>, bool>
175*795d594fSAndroid Build Coastguard Worker operator!=(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) {
176*795d594fSAndroid Build Coastguard Worker   return !(lhs == rhs);
177*795d594fSAndroid Build Coastguard Worker }
178*795d594fSAndroid Build Coastguard Worker 
179*795d594fSAndroid Build Coastguard Worker template<class MirrorType>
180*795d594fSAndroid Build Coastguard Worker inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr) {
181*795d594fSAndroid Build Coastguard Worker   // May be used for dumping bad pointers, do not use the checked version.
182*795d594fSAndroid Build Coastguard Worker   return os << ptr.PtrUnchecked();
183*795d594fSAndroid Build Coastguard Worker }
184*795d594fSAndroid Build Coastguard Worker 
185*795d594fSAndroid Build Coastguard Worker }  // namespace art
186*795d594fSAndroid Build Coastguard Worker 
187*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_OBJ_PTR_INL_H_
188