Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 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_METHOD_HANDLES_H_ |
| 18 | #define ART_RUNTIME_METHOD_HANDLES_H_ |
| 19 | |
| 20 | #include <ostream> |
| 21 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 22 | #include "dex_instruction.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 23 | #include "handle.h" |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 24 | #include "jvalue.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 25 | #include "mirror/class.h" |
| 26 | #include "mirror/method_type.h" |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 27 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 30 | namespace mirror { |
| 31 | class MethodType; |
| 32 | } |
| 33 | |
| 34 | class ShadowFrame; |
| 35 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 36 | // Defines the behaviour of a given method handle. The behaviour |
| 37 | // of a handle of a given kind is identical to the dex bytecode behaviour |
| 38 | // of the equivalent instruction. |
| 39 | // |
| 40 | // NOTE: These must be kept in sync with the constants defined in |
| 41 | // java.lang.invoke.MethodHandle. |
| 42 | enum MethodHandleKind { |
| 43 | kInvokeVirtual = 0, |
| 44 | kInvokeSuper, |
| 45 | kInvokeDirect, |
| 46 | kInvokeStatic, |
| 47 | kInvokeInterface, |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 48 | kInvokeTransform, |
Orion Hodson | 1c87878 | 2016-11-25 15:46:49 +0000 | [diff] [blame] | 49 | kInvokeCallSiteTransform, |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 50 | kInstanceGet, |
| 51 | kInstancePut, |
| 52 | kStaticGet, |
| 53 | kStaticPut, |
| 54 | kLastValidKind = kStaticPut, |
Orion Hodson | 1c87878 | 2016-11-25 15:46:49 +0000 | [diff] [blame] | 55 | kLastInvokeKind = kInvokeCallSiteTransform |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | // Whether the given method handle kind is some variant of an invoke. |
| 59 | inline bool IsInvoke(const MethodHandleKind handle_kind) { |
| 60 | return handle_kind <= kLastInvokeKind; |
| 61 | } |
| 62 | |
Orion Hodson | 1c87878 | 2016-11-25 15:46:49 +0000 | [diff] [blame] | 63 | // Whether the given method handle kind is some variant of a tranform. |
| 64 | inline bool IsInvokeTransform(const MethodHandleKind handle_kind) { |
| 65 | return handle_kind == kInvokeTransform || handle_kind == kInvokeCallSiteTransform; |
| 66 | } |
| 67 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 68 | // Returns true if there is a possible conversion from |from| to |to| |
| 69 | // for a MethodHandle parameter. |
| 70 | bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, |
| 71 | ObjPtr<mirror::Class> to); |
| 72 | |
| 73 | // Returns true if there is a possible conversion from |from| to |to| |
| 74 | // for the return type of a MethodHandle. |
| 75 | bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, |
| 76 | ObjPtr<mirror::Class> to); |
| 77 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 78 | // Performs a conversion from type |from| to a distinct type |to| as |
| 79 | // part of conversion of |caller_type| to |callee_type|. The value to |
| 80 | // be converted is in |value|. Returns true on success and updates |
| 81 | // |value| with the converted value, false otherwise. |
| 82 | bool ConvertJValueCommon(Handle<mirror::MethodType> callsite_type, |
| 83 | Handle<mirror::MethodType> callee_type, |
| 84 | ObjPtr<mirror::Class> from, |
| 85 | ObjPtr<mirror::Class> to, |
| 86 | JValue* value) |
| 87 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 88 | |
| 89 | // Converts the value of the argument at position |index| from type |
| 90 | // expected by |callee_type| to type used by |callsite_type|. |value| |
| 91 | // represents the value to be converted. Returns true on success and |
| 92 | // updates |value|, false otherwise. |
| 93 | ALWAYS_INLINE bool ConvertArgumentValue(Handle<mirror::MethodType> callsite_type, |
| 94 | Handle<mirror::MethodType> callee_type, |
| 95 | int index, |
| 96 | JValue* value) |
| 97 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 98 | |
| 99 | // Converts the return value from return type yielded by |
| 100 | // |callee_type| to the return type yielded by |
| 101 | // |callsite_type|. |value| represents the value to be |
| 102 | // converted. Returns true on success and updates |value|, false |
| 103 | // otherwise. |
| 104 | ALWAYS_INLINE bool ConvertReturnValue(Handle<mirror::MethodType> callsite_type, |
| 105 | Handle<mirror::MethodType> callee_type, |
| 106 | JValue* value) |
| 107 | REQUIRES_SHARED(Locks::mutator_lock_); |
Narayan Kamath | da24650 | 2016-10-20 18:39:22 +0100 | [diff] [blame] | 108 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 109 | // Perform argument conversions between |callsite_type| (the type of the |
| 110 | // incoming arguments) and |callee_type| (the type of the method being |
| 111 | // invoked). These include widening and narrowing conversions as well as |
| 112 | // boxing and unboxing. Returns true on success, on false on failure. A |
| 113 | // pending exception will always be set on failure. |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 114 | // |
| 115 | // The values to be converted are read from an input source (of type G) |
| 116 | // that provides three methods : |
| 117 | // |
| 118 | // class G { |
| 119 | // // Used to read the next boolean/short/int or float value from the |
| 120 | // // source. |
| 121 | // uint32_t Get(); |
| 122 | // |
| 123 | // // Used to the read the next reference value from the source. |
| 124 | // ObjPtr<mirror::Object> GetReference(); |
| 125 | // |
| 126 | // // Used to read the next double or long value from the source. |
| 127 | // int64_t GetLong(); |
| 128 | // } |
| 129 | // |
| 130 | // After conversion, the values are written to an output sink (of type S) |
| 131 | // that provides three methods : |
| 132 | // |
| 133 | // class S { |
| 134 | // void Set(uint32_t); |
| 135 | // void SetReference(ObjPtr<mirror::Object>) |
| 136 | // void SetLong(int64_t); |
| 137 | // } |
| 138 | // |
| 139 | // The semantics and usage of the Set methods are analagous to the getter |
| 140 | // class. |
| 141 | // |
| 142 | // This method is instantiated in three different scenarions : |
| 143 | // - <S = ShadowFrameSetter, G = ShadowFrameGetter> : copying from shadow |
| 144 | // frame to shadow frame, used in a regular polymorphic non-exact invoke. |
| 145 | // - <S = EmulatedShadowFrameAccessor, G = ShadowFrameGetter> : entering into |
| 146 | // a transformer method from a polymorphic invoke. |
| 147 | // - <S = ShadowFrameStter, G = EmulatedStackFrameAccessor> : entering into |
| 148 | // a regular poly morphic invoke from a transformer method. |
| 149 | // |
| 150 | // TODO(narayan): If we find that the instantiations of this function take |
| 151 | // up too much space, we can make G / S abstract base classes that are |
| 152 | // overridden by concrete classes. |
| 153 | template <typename G, typename S> |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 154 | bool PerformConversions(Thread* self, |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 155 | Handle<mirror::MethodType> callsite_type, |
| 156 | Handle<mirror::MethodType> callee_type, |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 157 | G* getter, |
| 158 | S* setter, |
Narayan Kamath | 2cb856c | 2016-11-02 12:01:26 +0000 | [diff] [blame] | 159 | int32_t num_conversions) REQUIRES_SHARED(Locks::mutator_lock_); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 160 | |
| 161 | // A convenience wrapper around |PerformConversions|, for the case where |
| 162 | // the setter and getter are both ShadowFrame based. |
Narayan Kamath | 2cb856c | 2016-11-02 12:01:26 +0000 | [diff] [blame] | 163 | template <bool is_range> |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 164 | bool ConvertAndCopyArgumentsFromCallerFrame(Thread* self, |
| 165 | Handle<mirror::MethodType> callsite_type, |
| 166 | Handle<mirror::MethodType> callee_type, |
| 167 | const ShadowFrame& caller_frame, |
| 168 | uint32_t first_src_reg, |
| 169 | uint32_t first_dest_reg, |
| 170 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Narayan Kamath | 2cb856c | 2016-11-02 12:01:26 +0000 | [diff] [blame] | 171 | ShadowFrame* callee_frame) |
| 172 | REQUIRES_SHARED(Locks::mutator_lock_); |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 173 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 174 | // A convenience class that allows for iteration through a list of |
| 175 | // input argument registers |arg| for non-range invokes or a list of |
| 176 | // consecutive registers starting with a given based for range |
| 177 | // invokes. |
| 178 | // |
| 179 | // This is used to iterate over input arguments while performing standard |
| 180 | // argument conversions. |
| 181 | template <bool is_range> class ShadowFrameGetter { |
| 182 | public: |
| 183 | ShadowFrameGetter(size_t first_src_reg, |
| 184 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 185 | const ShadowFrame& shadow_frame) : |
| 186 | first_src_reg_(first_src_reg), |
| 187 | arg_(arg), |
| 188 | shadow_frame_(shadow_frame), |
| 189 | arg_index_(0) { |
| 190 | } |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 191 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 192 | ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 193 | const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]); |
| 194 | ++arg_index_; |
| 195 | |
| 196 | return shadow_frame_.GetVReg(next); |
| 197 | } |
| 198 | |
| 199 | ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 200 | const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]); |
| 201 | arg_index_ += 2; |
| 202 | |
| 203 | return shadow_frame_.GetVRegLong(next); |
| 204 | } |
| 205 | |
| 206 | ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 207 | const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]); |
| 208 | ++arg_index_; |
| 209 | |
| 210 | return shadow_frame_.GetVRegReference(next); |
| 211 | } |
| 212 | |
| 213 | private: |
| 214 | const size_t first_src_reg_; |
| 215 | const uint32_t (&arg_)[Instruction::kMaxVarArgRegs]; |
| 216 | const ShadowFrame& shadow_frame_; |
| 217 | size_t arg_index_; |
| 218 | }; |
| 219 | |
| 220 | // A convenience class that allows values to be written to a given shadow frame, |
| 221 | // starting at location |first_dst_reg|. |
| 222 | class ShadowFrameSetter { |
| 223 | public: |
| 224 | ShadowFrameSetter(ShadowFrame* shadow_frame, |
| 225 | size_t first_dst_reg) : |
| 226 | shadow_frame_(shadow_frame), |
| 227 | arg_index_(first_dst_reg) { |
| 228 | } |
| 229 | |
| 230 | ALWAYS_INLINE void Set(uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 231 | shadow_frame_->SetVReg(arg_index_++, value); |
| 232 | } |
| 233 | |
| 234 | ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> value) |
| 235 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 236 | shadow_frame_->SetVRegReference(arg_index_++, value.Ptr()); |
| 237 | } |
| 238 | |
| 239 | ALWAYS_INLINE void SetLong(int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 240 | shadow_frame_->SetVRegLong(arg_index_, value); |
| 241 | arg_index_ += 2; |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | ShadowFrame* shadow_frame_; |
| 246 | size_t arg_index_; |
| 247 | }; |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 248 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 249 | } // namespace art |
| 250 | |
| 251 | #endif // ART_RUNTIME_METHOD_HANDLES_H_ |