Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | */ |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |
| 18 | #define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame^] | 19 | #include "object_utils.h" |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 21 | #include "common_throws.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 22 | #include "dex_file.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 23 | #include "indirect_reference_table.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 24 | #include "invoke_type.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 25 | #include "jni_internal.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 26 | #include "mirror/art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/array.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 28 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/throwable.h" |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame^] | 30 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 31 | #include "thread.h" |
| 32 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 33 | namespace art { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 34 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | namespace mirror { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 36 | class Class; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 37 | class ArtField; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 38 | class Object; |
| 39 | } // namespace mirror |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 40 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 41 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 42 | // cannot be resolved, throw an error. If it can, use it to create an instance. |
| 43 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 44 | // check. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 45 | static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx, mirror::ArtMethod* method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | Thread* self, |
| 47 | bool access_check) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 48 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 50 | Runtime* runtime = Runtime::Current(); |
| 51 | if (UNLIKELY(klass == NULL)) { |
| 52 | klass = runtime->GetClassLinker()->ResolveType(type_idx, method); |
| 53 | if (klass == NULL) { |
| 54 | DCHECK(self->IsExceptionPending()); |
| 55 | return NULL; // Failure |
| 56 | } |
| 57 | } |
| 58 | if (access_check) { |
| 59 | if (UNLIKELY(!klass->IsInstantiable())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 60 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 61 | self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;", |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 62 | PrettyDescriptor(klass).c_str()); |
| 63 | return NULL; // Failure |
| 64 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | mirror::Class* referrer = method->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 66 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 67 | ThrowIllegalAccessErrorClass(referrer, klass); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 68 | return NULL; // Failure |
| 69 | } |
| 70 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 71 | if (!klass->IsInitialized() && |
| 72 | !runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 73 | DCHECK(self->IsExceptionPending()); |
| 74 | return NULL; // Failure |
| 75 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 76 | return klass->AllocObject(self); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 80 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
| 81 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 82 | // check. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 83 | static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | int32_t component_count, |
| 85 | Thread* self, bool access_check) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 87 | if (UNLIKELY(component_count < 0)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 88 | ThrowNegativeArraySizeException(component_count); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 89 | return NULL; // Failure |
| 90 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 92 | if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve |
| 93 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 94 | if (klass == NULL) { // Error |
| 95 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 96 | return NULL; // Failure |
| 97 | } |
| 98 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 99 | } |
| 100 | if (access_check) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | mirror::Class* referrer = method->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 102 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 103 | ThrowIllegalAccessErrorClass(referrer, klass); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 104 | return NULL; // Failure |
| 105 | } |
| 106 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | return mirror::Array::Alloc(self, klass, component_count); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 110 | extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | int32_t component_count, |
| 112 | Thread* self, bool access_check) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 113 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 114 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 115 | // Type of find field operation for fast and slow case. |
| 116 | enum FindFieldType { |
| 117 | InstanceObjectRead, |
| 118 | InstanceObjectWrite, |
| 119 | InstancePrimitiveRead, |
| 120 | InstancePrimitiveWrite, |
| 121 | StaticObjectRead, |
| 122 | StaticObjectWrite, |
| 123 | StaticPrimitiveRead, |
| 124 | StaticPrimitiveWrite, |
| 125 | }; |
| 126 | |
| 127 | // Slow field find that can initialize classes and may throw exceptions. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 128 | extern mirror::ArtField* FindFieldFromCode(uint32_t field_idx, const mirror::ArtMethod* referrer, |
| 129 | Thread* self, FindFieldType type, size_t expected_size, |
| 130 | bool access_check) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 131 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 132 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 133 | // Fast path field resolution that can't initialize classes or throw exceptions. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 134 | static inline mirror::ArtField* FindFieldFast(uint32_t field_idx, |
| 135 | const mirror::ArtMethod* referrer, |
| 136 | FindFieldType type, size_t expected_size) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 137 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 138 | mirror::ArtField* resolved_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 139 | referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 140 | if (UNLIKELY(resolved_field == NULL)) { |
| 141 | return NULL; |
| 142 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 144 | // Check class is initiliazed or initializing. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 145 | if (UNLIKELY(!fields_class->IsInitializing())) { |
| 146 | return NULL; |
| 147 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 148 | // Check for incompatible class change. |
| 149 | bool is_primitive; |
| 150 | bool is_set; |
| 151 | bool is_static; |
| 152 | switch (type) { |
| 153 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 154 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 155 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 156 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 157 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 158 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 159 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 160 | case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break; |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 161 | default: |
| 162 | LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings. |
| 163 | is_primitive = true; |
| 164 | is_set = true; |
| 165 | is_static = true; |
| 166 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 167 | } |
| 168 | if (UNLIKELY(resolved_field->IsStatic() != is_static)) { |
| 169 | // Incompatible class change. |
| 170 | return NULL; |
| 171 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 172 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 173 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
| 174 | !referring_class->CanAccessMember(fields_class, |
| 175 | resolved_field->GetAccessFlags()) || |
| 176 | (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 177 | // Illegal access. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
| 180 | FieldHelper fh(resolved_field); |
| 181 | if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || |
| 182 | fh.FieldSize() != expected_size)) { |
| 183 | return NULL; |
| 184 | } |
| 185 | return resolved_field; |
| 186 | } |
| 187 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 188 | // Fast path method resolution that can't throw exceptions. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 189 | static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx, |
| 190 | mirror::Object* this_object, |
| 191 | const mirror::ArtMethod* referrer, |
| 192 | bool access_check, InvokeType type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 193 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 194 | bool is_direct = type == kStatic || type == kDirect; |
| 195 | if (UNLIKELY(this_object == NULL && !is_direct)) { |
| 196 | return NULL; |
| 197 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 198 | mirror::ArtMethod* resolved_method = |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 199 | referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx); |
| 200 | if (UNLIKELY(resolved_method == NULL)) { |
| 201 | return NULL; |
| 202 | } |
| 203 | if (access_check) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 204 | // Check for incompatible class change errors and access. |
| 205 | bool icce = resolved_method->CheckIncompatibleClassChange(type); |
| 206 | if (UNLIKELY(icce)) { |
| 207 | return NULL; |
| 208 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
| 210 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 211 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 212 | !referring_class->CanAccessMember(methods_class, |
| 213 | resolved_method->GetAccessFlags()))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 214 | // Potential illegal access, may need to refine the method's class. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 215 | return NULL; |
| 216 | } |
| 217 | } |
| 218 | if (type == kInterface) { // Most common form of slow path dispatch. |
| 219 | return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 220 | } else if (is_direct) { |
| 221 | return resolved_method; |
| 222 | } else if (type == kSuper) { |
| 223 | return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()-> |
| 224 | Get(resolved_method->GetMethodIndex()); |
| 225 | } else { |
| 226 | DCHECK(type == kVirtual); |
| 227 | return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex()); |
| 228 | } |
| 229 | } |
| 230 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 231 | extern mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object, |
| 232 | mirror::ArtMethod* referrer, |
| 233 | Thread* self, bool access_check, InvokeType type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 234 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 235 | |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 236 | static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 237 | const mirror::ArtMethod* referrer, |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 238 | Thread* self, bool can_run_clinit, |
| 239 | bool verify_access) |
| 240 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 241 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 242 | mirror::Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 243 | if (UNLIKELY(klass == NULL)) { |
| 244 | CHECK(self->IsExceptionPending()); |
| 245 | return NULL; // Failure - Indicate to caller to deliver exception |
| 246 | } |
| 247 | // Perform access check if necessary. |
| 248 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 249 | if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) { |
| 250 | ThrowIllegalAccessErrorClass(referring_class, klass); |
| 251 | return NULL; // Failure - Indicate to caller to deliver exception |
| 252 | } |
| 253 | // If we're just implementing const-class, we shouldn't call <clinit>. |
| 254 | if (!can_run_clinit) { |
| 255 | return klass; |
| 256 | } |
| 257 | // If we are the <clinit> of this class, just return our storage. |
| 258 | // |
| 259 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 260 | // running. |
| 261 | if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) { |
| 262 | return klass; |
| 263 | } |
| 264 | if (!class_linker->EnsureInitialized(klass, true, true)) { |
| 265 | CHECK(self->IsExceptionPending()); |
| 266 | return NULL; // Failure - Indicate to caller to deliver exception |
| 267 | } |
| 268 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 269 | return klass; |
| 270 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 271 | |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 272 | extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 273 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 274 | static inline mirror::String* ResolveStringFromCode(const mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 275 | uint32_t string_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 276 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 277 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 278 | return class_linker->ResolveString(string_idx, referrer); |
| 279 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 280 | |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 281 | static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 282 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 283 | UNLOCK_FUNCTION(monitor_lock_) { |
| 284 | // Save any pending exception over monitor exit call. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 285 | mirror::Throwable* saved_exception = NULL; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 286 | ThrowLocation saved_throw_location; |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 287 | if (UNLIKELY(self->IsExceptionPending())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 288 | saved_exception = self->GetException(&saved_throw_location); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 289 | self->ClearException(); |
| 290 | } |
| 291 | // Decode locked object and unlock, before popping local references. |
| 292 | self->DecodeJObject(locked)->MonitorExit(self); |
| 293 | if (UNLIKELY(self->IsExceptionPending())) { |
| 294 | LOG(FATAL) << "Synchronized JNI code returning with an exception:\n" |
| 295 | << saved_exception->Dump() |
| 296 | << "\nEncountered second exception during implicit MonitorExit:\n" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 297 | << self->GetException(NULL)->Dump(); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 298 | } |
| 299 | // Restore pending exception. |
| 300 | if (saved_exception != NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 301 | self->SetException(saved_throw_location, saved_exception); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 305 | static inline void CheckReferenceResult(mirror::Object* o, Thread* self) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 306 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 307 | if (o == NULL) { |
| 308 | return; |
| 309 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 310 | mirror::ArtMethod* m = self->GetCurrentMethod(NULL); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 311 | if (o == kInvalidIndirectRefObject) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 312 | JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str()); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 313 | } |
| 314 | // Make sure that the result is an instance of the type this method was expected to return. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 315 | mirror::Class* return_type = MethodHelper(m).GetReturnType(); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 316 | |
| 317 | if (!o->InstanceOf(return_type)) { |
| 318 | JniAbortF(NULL, "attempt to return an instance of %s from %s", |
| 319 | PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str()); |
| 320 | } |
| 321 | } |
| 322 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 323 | static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
jeffhao | 373c52f | 2012-11-20 16:11:52 -0800 | [diff] [blame] | 324 | for (;;) { |
| 325 | if (thread->ReadFlag(kCheckpointRequest)) { |
| 326 | thread->RunCheckpointFunction(); |
| 327 | thread->AtomicClearFlag(kCheckpointRequest); |
| 328 | } else if (thread->ReadFlag(kSuspendRequest)) { |
| 329 | thread->FullSuspendCheck(); |
| 330 | } else { |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 336 | JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 337 | jobject rcvr_jobj, jobject interface_art_method_jobj, |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 338 | std::vector<jvalue>& args) |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 339 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 340 | |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 341 | // Entry point for deoptimization. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 342 | extern "C" void art_quick_deoptimize(); |
| 343 | static inline uintptr_t GetQuickDeoptimizationEntryPoint() { |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 344 | return reinterpret_cast<uintptr_t>(art_quick_deoptimize); |
| 345 | } |
| 346 | |
| 347 | // Return address of instrumentation stub. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 348 | extern "C" void art_quick_instrumentation_entry(void*); |
| 349 | static inline void* GetQuickInstrumentationEntryPoint() { |
| 350 | return reinterpret_cast<void*>(art_quick_instrumentation_entry); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // The return_pc of instrumentation exit stub. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 354 | extern "C" void art_quick_instrumentation_exit(); |
| 355 | static inline uintptr_t GetQuickInstrumentationExitPc() { |
| 356 | return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit); |
| 357 | } |
| 358 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 359 | extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 360 | static inline const void* GetPortableToInterpreterBridge() { |
| 361 | return reinterpret_cast<void*>(art_portable_to_interpreter_bridge); |
| 362 | } |
| 363 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 364 | extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 365 | static inline const void* GetQuickToInterpreterBridge() { |
| 366 | return reinterpret_cast<void*>(art_quick_to_interpreter_bridge); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Return address of interpreter stub. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 370 | static inline const void* GetCompiledCodeToInterpreterBridge() { |
| 371 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 372 | return GetPortableToInterpreterBridge(); |
| 373 | #else |
| 374 | return GetQuickToInterpreterBridge(); |
| 375 | #endif |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 378 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 379 | static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) { |
| 380 | return class_linker->GetPortableResolutionTrampoline(); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 383 | static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) { |
| 384 | return class_linker->GetQuickResolutionTrampoline(); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // Return address of resolution trampoline stub for defined compiler. |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 388 | static inline const void* GetResolutionTrampoline(ClassLinker* class_linker) { |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 389 | #if defined(ART_USE_PORTABLE_COMPILER) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 390 | return GetPortableResolutionTrampoline(class_linker); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 391 | #else |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 392 | return GetQuickResolutionTrampoline(class_linker); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 393 | #endif |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 396 | extern "C" void art_portable_proxy_invoke_handler(); |
| 397 | static inline const void* GetPortableProxyInvokeHandler() { |
| 398 | return reinterpret_cast<void*>(art_portable_proxy_invoke_handler); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 401 | extern "C" void art_quick_proxy_invoke_handler(); |
| 402 | static inline const void* GetQuickProxyInvokeHandler() { |
| 403 | return reinterpret_cast<void*>(art_quick_proxy_invoke_handler); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 406 | static inline const void* GetProxyInvokeHandler() { |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 407 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 408 | return GetPortableProxyInvokeHandler(); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 409 | #else |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 410 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 411 | #endif |
| 412 | } |
| 413 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 414 | extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 415 | static inline void* GetJniDlsymLookupStub() { |
| 416 | return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub); |
| 417 | } |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 418 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 419 | } // namespace art |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 420 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 421 | #endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |