xref: /aosp_15_r20/art/runtime/reflective_value_visitor.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2019 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_REFLECTIVE_VALUE_VISITOR_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_REFLECTIVE_VALUE_VISITOR_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <array>
23*795d594fSAndroid Build Coastguard Worker #include <compare>
24*795d594fSAndroid Build Coastguard Worker #include <functional>
25*795d594fSAndroid Build Coastguard Worker #include <stack>
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include "android-base/macros.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
30*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/value_object.h"
33*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
34*795d594fSAndroid Build Coastguard Worker #include "jni.h"
35*795d594fSAndroid Build Coastguard Worker #include "mirror/dex_cache.h"
36*795d594fSAndroid Build Coastguard Worker #include "obj_ptr.h"
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker class ArtField;
41*795d594fSAndroid Build Coastguard Worker class ArtMethod;
42*795d594fSAndroid Build Coastguard Worker class BaseReflectiveHandleScope;
43*795d594fSAndroid Build Coastguard Worker class Thread;
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker class ReflectionSourceInfo;
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker class ReflectiveValueVisitor : public ValueObject {
48*795d594fSAndroid Build Coastguard Worker  public:
~ReflectiveValueVisitor()49*795d594fSAndroid Build Coastguard Worker   virtual ~ReflectiveValueVisitor() {}
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker   virtual ArtMethod* VisitMethod(ArtMethod* in, const ReflectionSourceInfo& info)
52*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
53*795d594fSAndroid Build Coastguard Worker   virtual ArtField* VisitField(ArtField* in, const ReflectionSourceInfo& info)
54*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker   // Give it an entrypoint through operator() to interact with things that expect lambda-like things
57*795d594fSAndroid Build Coastguard Worker   template <typename T,
58*795d594fSAndroid Build Coastguard Worker             typename = typename std::enable_if<std::is_same_v<T, ArtField> ||
59*795d594fSAndroid Build Coastguard Worker                                                std::is_same_v<T, ArtMethod>>>
operator()60*795d594fSAndroid Build Coastguard Worker   T* operator()(T* t, const ReflectionSourceInfo& info) REQUIRES_SHARED(Locks::mutator_lock_) {
61*795d594fSAndroid Build Coastguard Worker     if constexpr (std::is_same_v<T, ArtField>) {
62*795d594fSAndroid Build Coastguard Worker       return VisitField(t, info);
63*795d594fSAndroid Build Coastguard Worker     } else {
64*795d594fSAndroid Build Coastguard Worker       static_assert(std::is_same_v<T, ArtMethod>, "Expected ArtField or ArtMethod");
65*795d594fSAndroid Build Coastguard Worker       return VisitMethod(t, info);
66*795d594fSAndroid Build Coastguard Worker     }
67*795d594fSAndroid Build Coastguard Worker   }
68*795d594fSAndroid Build Coastguard Worker };
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker template <typename FieldVis, typename MethodVis>
71*795d594fSAndroid Build Coastguard Worker class FunctionReflectiveValueVisitor : public ReflectiveValueVisitor {
72*795d594fSAndroid Build Coastguard Worker  public:
FunctionReflectiveValueVisitor(FieldVis fv,MethodVis mv)73*795d594fSAndroid Build Coastguard Worker   FunctionReflectiveValueVisitor(FieldVis fv, MethodVis mv) : fv_(fv), mv_(mv) {}
VisitField(ArtField * in,const ReflectionSourceInfo & info)74*795d594fSAndroid Build Coastguard Worker   ArtField* VisitField(ArtField* in, const ReflectionSourceInfo& info) override
75*795d594fSAndroid Build Coastguard Worker       REQUIRES(Locks::mutator_lock_) {
76*795d594fSAndroid Build Coastguard Worker     return fv_(in, info);
77*795d594fSAndroid Build Coastguard Worker   }
VisitMethod(ArtMethod * in,const ReflectionSourceInfo & info)78*795d594fSAndroid Build Coastguard Worker   ArtMethod* VisitMethod(ArtMethod* in, const ReflectionSourceInfo& info) override
79*795d594fSAndroid Build Coastguard Worker       REQUIRES(Locks::mutator_lock_) {
80*795d594fSAndroid Build Coastguard Worker     return mv_(in, info);
81*795d594fSAndroid Build Coastguard Worker   }
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker  private:
84*795d594fSAndroid Build Coastguard Worker   FieldVis fv_;
85*795d594fSAndroid Build Coastguard Worker   MethodVis mv_;
86*795d594fSAndroid Build Coastguard Worker };
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker enum ReflectionSourceType {
89*795d594fSAndroid Build Coastguard Worker   kSourceUnknown = 0,
90*795d594fSAndroid Build Coastguard Worker   kSourceJavaLangReflectExecutable,
91*795d594fSAndroid Build Coastguard Worker   kSourceJavaLangReflectField,
92*795d594fSAndroid Build Coastguard Worker   kSourceJavaLangInvokeMethodHandle,
93*795d594fSAndroid Build Coastguard Worker   kSourceJavaLangInvokeFieldVarHandle,
94*795d594fSAndroid Build Coastguard Worker   kSourceThreadHandleScope,
95*795d594fSAndroid Build Coastguard Worker   kSourceJniFieldId,
96*795d594fSAndroid Build Coastguard Worker   kSourceJniMethodId,
97*795d594fSAndroid Build Coastguard Worker   kSourceDexCacheResolvedMethod,
98*795d594fSAndroid Build Coastguard Worker   kSourceDexCacheResolvedField,
99*795d594fSAndroid Build Coastguard Worker   kSourceMiscInternal,
100*795d594fSAndroid Build Coastguard Worker };
101*795d594fSAndroid Build Coastguard Worker EXPORT std::ostream& operator<<(std::ostream& os, ReflectionSourceType type);
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker class ReflectionSourceInfo : public ValueObject {
104*795d594fSAndroid Build Coastguard Worker  public:
~ReflectionSourceInfo()105*795d594fSAndroid Build Coastguard Worker   virtual ~ReflectionSourceInfo() {}
106*795d594fSAndroid Build Coastguard Worker   // Thread id 0 is for non thread roots.
ReflectionSourceInfo(ReflectionSourceType type)107*795d594fSAndroid Build Coastguard Worker   explicit ReflectionSourceInfo(ReflectionSourceType type) : type_(type) {}
Describe(std::ostream & os)108*795d594fSAndroid Build Coastguard Worker   virtual void Describe(std::ostream& os) const {
109*795d594fSAndroid Build Coastguard Worker     os << "Type=" << type_;
110*795d594fSAndroid Build Coastguard Worker   }
111*795d594fSAndroid Build Coastguard Worker 
GetType()112*795d594fSAndroid Build Coastguard Worker   ReflectionSourceType GetType() const {
113*795d594fSAndroid Build Coastguard Worker     return type_;
114*795d594fSAndroid Build Coastguard Worker   }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker  private:
117*795d594fSAndroid Build Coastguard Worker   const ReflectionSourceType type_;
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ReflectionSourceInfo);
120*795d594fSAndroid Build Coastguard Worker };
121*795d594fSAndroid Build Coastguard Worker inline std::ostream& operator<<(std::ostream& os, const ReflectionSourceInfo& info) {
122*795d594fSAndroid Build Coastguard Worker   info.Describe(os);
123*795d594fSAndroid Build Coastguard Worker   return os;
124*795d594fSAndroid Build Coastguard Worker }
125*795d594fSAndroid Build Coastguard Worker 
126*795d594fSAndroid Build Coastguard Worker class EXPORT ReflectiveHandleScopeSourceInfo : public ReflectionSourceInfo {
127*795d594fSAndroid Build Coastguard Worker  public:
ReflectiveHandleScopeSourceInfo(BaseReflectiveHandleScope * source)128*795d594fSAndroid Build Coastguard Worker   explicit ReflectiveHandleScopeSourceInfo(BaseReflectiveHandleScope* source)
129*795d594fSAndroid Build Coastguard Worker       : ReflectionSourceInfo(kSourceThreadHandleScope), source_(source) {}
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker   void Describe(std::ostream& os) const override;
132*795d594fSAndroid Build Coastguard Worker 
133*795d594fSAndroid Build Coastguard Worker  private:
134*795d594fSAndroid Build Coastguard Worker   BaseReflectiveHandleScope* source_;
135*795d594fSAndroid Build Coastguard Worker };
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker // TODO Maybe give this the ability to retrieve the type and ref, if it's useful.
138*795d594fSAndroid Build Coastguard Worker class HeapReflectiveSourceInfo : public ReflectionSourceInfo {
139*795d594fSAndroid Build Coastguard Worker  public:
HeapReflectiveSourceInfo(ReflectionSourceType t,mirror::Object * src)140*795d594fSAndroid Build Coastguard Worker   HeapReflectiveSourceInfo(ReflectionSourceType t, mirror::Object* src)
141*795d594fSAndroid Build Coastguard Worker       : ReflectionSourceInfo(t), src_(src) {}
142*795d594fSAndroid Build Coastguard Worker   void Describe(std::ostream& os) const override;
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker  private:
145*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Object> src_;
146*795d594fSAndroid Build Coastguard Worker };
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker // TODO Maybe give this the ability to retrieve the id if it's useful.
149*795d594fSAndroid Build Coastguard Worker template <typename T,
150*795d594fSAndroid Build Coastguard Worker           typename = typename std::enable_if_t<std::is_same_v<T, jmethodID> ||
151*795d594fSAndroid Build Coastguard Worker                                                std::is_same_v<T, jfieldID>>>
152*795d594fSAndroid Build Coastguard Worker class JniIdReflectiveSourceInfo : public ReflectionSourceInfo {
153*795d594fSAndroid Build Coastguard Worker  public:
JniIdReflectiveSourceInfo(T id)154*795d594fSAndroid Build Coastguard Worker   explicit JniIdReflectiveSourceInfo(T id)
155*795d594fSAndroid Build Coastguard Worker       : ReflectionSourceInfo(std::is_same_v<T, jmethodID> ? kSourceJniMethodId : kSourceJniFieldId),
156*795d594fSAndroid Build Coastguard Worker         id_(id) {}
157*795d594fSAndroid Build Coastguard Worker   void Describe(std::ostream& os) const override;
158*795d594fSAndroid Build Coastguard Worker 
159*795d594fSAndroid Build Coastguard Worker  private:
160*795d594fSAndroid Build Coastguard Worker   T id_;
161*795d594fSAndroid Build Coastguard Worker };
162*795d594fSAndroid Build Coastguard Worker 
163*795d594fSAndroid Build Coastguard Worker class DexCacheSourceInfo : public ReflectionSourceInfo {
164*795d594fSAndroid Build Coastguard Worker  public:
DexCacheSourceInfo(ReflectionSourceType type,size_t index,ObjPtr<mirror::DexCache> cache)165*795d594fSAndroid Build Coastguard Worker   explicit DexCacheSourceInfo(ReflectionSourceType type,
166*795d594fSAndroid Build Coastguard Worker                               size_t index,
167*795d594fSAndroid Build Coastguard Worker                               ObjPtr<mirror::DexCache> cache)
168*795d594fSAndroid Build Coastguard Worker       : ReflectionSourceInfo(type), index_(index), cache_(cache) {}
169*795d594fSAndroid Build Coastguard Worker 
Describe(std::ostream & os)170*795d594fSAndroid Build Coastguard Worker   void Describe(std::ostream& os) const override REQUIRES(Locks::mutator_lock_) {
171*795d594fSAndroid Build Coastguard Worker     ReflectionSourceInfo::Describe(os);
172*795d594fSAndroid Build Coastguard Worker     os << " index=" << index_ << " cache_=" << cache_.PtrUnchecked()
173*795d594fSAndroid Build Coastguard Worker        << " files=" << *cache_->GetDexFile();
174*795d594fSAndroid Build Coastguard Worker   }
175*795d594fSAndroid Build Coastguard Worker 
176*795d594fSAndroid Build Coastguard Worker  private:
177*795d594fSAndroid Build Coastguard Worker   size_t index_;
178*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::DexCache> cache_;
179*795d594fSAndroid Build Coastguard Worker };
180*795d594fSAndroid Build Coastguard Worker }  // namespace art
181*795d594fSAndroid Build Coastguard Worker 
182*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_REFLECTIVE_VALUE_VISITOR_H_
183