1 /*
2  * Copyright (C) 2016 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_METHOD_HANDLE_IMPL_H_
18 #define ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_
19 
20 #include "art_field.h"
21 #include "art_method.h"
22 #include "class.h"
23 #include "method_type.h"
24 #include "obj_ptr.h"
25 #include "object.h"
26 
27 namespace art {
28 
29 struct MethodHandleOffsets;
30 struct MethodHandleImplOffsets;
31 class ReflectiveValueVisitor;
32 
33 namespace mirror {
34 
35 // C++ mirror of java.lang.invoke.MethodHandle
36 class MANAGED MethodHandle : public Object {
37  public:
38   MIRROR_CLASS("Ljava/lang/invoke/MethodHandle;");
39 
40   // Defines the behaviour of a given method handle. The behaviour
41   // of a handle of a given kind is identical to the dex bytecode behaviour
42   // of the equivalent instruction.
43   //
44   // NOTE: These must be kept in sync with the constants defined in
45   // java.lang.invoke.MethodHandle.
46   enum Kind {
47     kInvokeVirtual = 0,
48     kInvokeSuper,
49     kInvokeDirect,
50     kInvokeStatic,
51     kInvokeInterface,
52     kInvokeTransform,
53     kInvokeCallSiteTransform,
54     kInvokeVarHandle,
55     kInvokeVarHandleExact,
56     kInstanceGet,
57     kInstancePut,
58     kStaticGet,
59     kStaticPut,
60     kLastValidKind = kStaticPut,
61     kFirstAccessorKind = kInstanceGet,
62     kLastAccessorKind = kStaticPut,
63     kLastInvokeKind = kInvokeVarHandleExact
64   };
65 
GetHandleKind()66   Kind GetHandleKind() REQUIRES_SHARED(Locks::mutator_lock_) {
67     const int32_t handle_kind = GetField32(OFFSET_OF_OBJECT_MEMBER(MethodHandle, handle_kind_));
68     DCHECK(handle_kind >= 0 &&
69            handle_kind <= static_cast<int32_t>(Kind::kLastValidKind));
70     return static_cast<Kind>(handle_kind);
71   }
72 
73   ALWAYS_INLINE ObjPtr<mirror::MethodType> GetMethodType() REQUIRES_SHARED(Locks::mutator_lock_);
74 
75   ALWAYS_INLINE ObjPtr<mirror::MethodType> GetNominalType() REQUIRES_SHARED(Locks::mutator_lock_);
76 
GetTargetField()77   ArtField* GetTargetField() REQUIRES_SHARED(Locks::mutator_lock_) {
78     return reinterpret_cast<ArtField*>(
79         GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
80   }
81 
GetTargetMethod()82   ArtMethod* GetTargetMethod() REQUIRES_SHARED(Locks::mutator_lock_) {
83     return reinterpret_cast<ArtMethod*>(
84         GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
85   }
86 
87   // Gets the return type for a named invoke method, or nullptr if the invoke method is not
88   // supported.
89   static const char* GetReturnTypeDescriptor(const char* invoke_method_name);
90 
91   // Used when classes become structurally obsolete to change the MethodHandle to refer to the new
92   // method or field.
93   void VisitTarget(ReflectiveValueVisitor* v) REQUIRES(Locks::mutator_lock_);
94 
95  protected:
96   void Initialize(uintptr_t art_field_or_method, Kind kind, Handle<MethodType> method_type)
97       REQUIRES_SHARED(Locks::mutator_lock_);
98 
99  private:
100   HeapReference<mirror::MethodHandle> cached_spread_invoker_;
101   HeapReference<mirror::MethodType> nominal_type_;
102   HeapReference<mirror::MethodType> method_type_;
103   uint32_t handle_kind_;
104   uint64_t art_field_or_method_;
105 
106  private:
CachedSpreadInvokerOffset()107   static MemberOffset CachedSpreadInvokerOffset() {
108     return MemberOffset(OFFSETOF_MEMBER(MethodHandle, cached_spread_invoker_));
109   }
NominalTypeOffset()110   static MemberOffset NominalTypeOffset() {
111     return MemberOffset(OFFSETOF_MEMBER(MethodHandle, nominal_type_));
112   }
MethodTypeOffset()113   static MemberOffset MethodTypeOffset() {
114     return MemberOffset(OFFSETOF_MEMBER(MethodHandle, method_type_));
115   }
ArtFieldOrMethodOffset()116   static MemberOffset ArtFieldOrMethodOffset() {
117     return MemberOffset(OFFSETOF_MEMBER(MethodHandle, art_field_or_method_));
118   }
HandleKindOffset()119   static MemberOffset HandleKindOffset() {
120     return MemberOffset(OFFSETOF_MEMBER(MethodHandle, handle_kind_));
121   }
122 
123   friend struct art::MethodHandleOffsets;  // for verifying offset information
124   DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandle);
125 };
126 
127 // C++ mirror of java.lang.invoke.MethodHandleImpl
128 class MANAGED MethodHandleImpl : public MethodHandle {
129  public:
130   MIRROR_CLASS("Ljava/lang/invoke/MethodHandleImpl;");
131 
132   static ObjPtr<mirror::MethodHandleImpl> Create(Thread* const self,
133                                                  uintptr_t art_field_or_method,
134                                                  MethodHandle::Kind kind,
135                                                  Handle<MethodType> method_type)
136       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
137 
138  private:
InfoOffset()139   static MemberOffset InfoOffset() {
140     return MemberOffset(OFFSETOF_MEMBER(MethodHandleImpl, info_));
141   }
142 
143   HeapReference<mirror::Object> info_;  // Unused by the runtime.
144 
145   friend struct art::MethodHandleImplOffsets;  // for verifying offset information
146   DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandleImpl);
147 };
148 
149 }  // namespace mirror
150 }  // namespace art
151 
152 #endif  // ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_
153