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 | |
| 17 | #ifndef ART_SRC_RUNTIME_SUPPORT_H_ |
| 18 | #define ART_SRC_RUNTIME_SUPPORT_H_ |
| 19 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 21 | #include "dex_file.h" |
| 22 | #include "dex_verifier.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 23 | #include "invoke_type.h" |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 24 | #include "object.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 25 | #include "object_utils.h" |
| 26 | #include "thread.h" |
| 27 | |
| 28 | extern "C" void art_proxy_invoke_handler(); |
| 29 | extern "C" void art_work_around_app_jni_bugs(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 33 | class Array; |
| 34 | class Class; |
| 35 | class Field; |
| 36 | class Method; |
| 37 | class Object; |
| 38 | |
| 39 | // Helpers to give consistent descriptive exception messages |
| 40 | void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed); |
| 41 | void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer, |
| 42 | Class* accessed, |
| 43 | const Method* caller, |
| 44 | const Method* called, |
| 45 | InvokeType type); |
| 46 | void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self, |
| 47 | const Method* referrer, |
| 48 | const Method* interface_method, |
| 49 | Object* this_object); |
| 50 | void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed); |
| 51 | void ThrowNewIllegalAccessErrorFinalField(Thread* self, const Method* referrer, Field* accessed); |
| 52 | |
| 53 | void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed); |
| 54 | void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read); |
| 55 | void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller, uint32_t method_idx, |
| 56 | InvokeType type); |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 57 | void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* caller, uint32_t dex_pc); |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 58 | void ThrowVerificationError(Thread* self, const Method* method, int32_t kind, int32_t ref); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 59 | |
| 60 | std::string FieldNameFromIndex(const Method* method, uint32_t ref, |
| 61 | verifier::VerifyErrorRefType ref_type, bool access); |
| 62 | std::string MethodNameFromIndex(const Method* method, uint32_t ref, |
| 63 | verifier::VerifyErrorRefType ref_type, bool access); |
| 64 | |
| 65 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 66 | // cannot be resolved, throw an error. If it can, use it to create an instance. |
| 67 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 68 | // check. |
| 69 | static inline Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self, |
| 70 | bool access_check) { |
| 71 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 72 | Runtime* runtime = Runtime::Current(); |
| 73 | if (UNLIKELY(klass == NULL)) { |
| 74 | klass = runtime->GetClassLinker()->ResolveType(type_idx, method); |
| 75 | if (klass == NULL) { |
| 76 | DCHECK(self->IsExceptionPending()); |
| 77 | return NULL; // Failure |
| 78 | } |
| 79 | } |
| 80 | if (access_check) { |
| 81 | if (UNLIKELY(!klass->IsInstantiable())) { |
| 82 | self->ThrowNewException("Ljava/lang/InstantiationError;", |
| 83 | PrettyDescriptor(klass).c_str()); |
| 84 | return NULL; // Failure |
| 85 | } |
| 86 | Class* referrer = method->GetDeclaringClass(); |
| 87 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 88 | ThrowNewIllegalAccessErrorClass(self, referrer, klass); |
| 89 | return NULL; // Failure |
| 90 | } |
| 91 | } |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 92 | if (!runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 93 | DCHECK(self->IsExceptionPending()); |
| 94 | return NULL; // Failure |
| 95 | } |
| 96 | return klass->AllocObject(); |
| 97 | } |
| 98 | |
| 99 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 100 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
| 101 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 102 | // check. |
| 103 | static inline Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 104 | Thread* self, bool access_check) { |
| 105 | if (UNLIKELY(component_count < 0)) { |
| 106 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", |
| 107 | component_count); |
| 108 | return NULL; // Failure |
| 109 | } |
| 110 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 111 | if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve |
| 112 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 113 | if (klass == NULL) { // Error |
| 114 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 115 | return NULL; // Failure |
| 116 | } |
| 117 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 118 | } |
| 119 | if (access_check) { |
| 120 | Class* referrer = method->GetDeclaringClass(); |
| 121 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 122 | ThrowNewIllegalAccessErrorClass(self, referrer, klass); |
| 123 | return NULL; // Failure |
| 124 | } |
| 125 | } |
| 126 | return Array::Alloc(klass, component_count); |
| 127 | } |
| 128 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 129 | extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
Ian Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 130 | Thread* self, bool access_check); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 131 | |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 132 | extern Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self, |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 133 | bool is_static, bool is_primitive, bool is_set, |
| 134 | size_t expected_size); |
| 135 | |
| 136 | // Fast path field resolution that can't throw exceptions |
| 137 | static inline Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive, |
| 138 | size_t expected_size, bool is_set) { |
| 139 | Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx); |
| 140 | if (UNLIKELY(resolved_field == NULL)) { |
| 141 | return NULL; |
| 142 | } |
| 143 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 144 | // Check class is initiliazed or initializing |
| 145 | if (UNLIKELY(!fields_class->IsInitializing())) { |
| 146 | return NULL; |
| 147 | } |
| 148 | Class* referring_class = referrer->GetDeclaringClass(); |
| 149 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
| 150 | !referring_class->CanAccessMember(fields_class, |
| 151 | resolved_field->GetAccessFlags()) || |
| 152 | (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) { |
| 153 | // illegal access |
| 154 | return NULL; |
| 155 | } |
| 156 | FieldHelper fh(resolved_field); |
| 157 | if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || |
| 158 | fh.FieldSize() != expected_size)) { |
| 159 | return NULL; |
| 160 | } |
| 161 | return resolved_field; |
| 162 | } |
| 163 | |
| 164 | // Fast path method resolution that can't throw exceptions |
| 165 | static inline Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer, |
| 166 | bool access_check, InvokeType type) { |
| 167 | bool is_direct = type == kStatic || type == kDirect; |
| 168 | if (UNLIKELY(this_object == NULL && !is_direct)) { |
| 169 | return NULL; |
| 170 | } |
| 171 | Method* resolved_method = |
| 172 | referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx); |
| 173 | if (UNLIKELY(resolved_method == NULL)) { |
| 174 | return NULL; |
| 175 | } |
| 176 | if (access_check) { |
| 177 | Class* methods_class = resolved_method->GetDeclaringClass(); |
| 178 | Class* referring_class = referrer->GetDeclaringClass(); |
| 179 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 180 | !referring_class->CanAccessMember(methods_class, |
| 181 | resolved_method->GetAccessFlags()))) { |
| 182 | // potential illegal access |
| 183 | return NULL; |
| 184 | } |
| 185 | } |
| 186 | if (type == kInterface) { // Most common form of slow path dispatch. |
| 187 | return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 188 | } else if (is_direct) { |
| 189 | return resolved_method; |
| 190 | } else if (type == kSuper) { |
| 191 | return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()-> |
| 192 | Get(resolved_method->GetMethodIndex()); |
| 193 | } else { |
| 194 | DCHECK(type == kVirtual); |
| 195 | return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | extern Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer, |
| 200 | Thread* self, bool access_check, InvokeType type); |
| 201 | |
Elliott Hughes | f3778f6 | 2012-01-26 14:14:35 -0800 | [diff] [blame] | 202 | extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self, |
| 203 | bool can_run_clinit, bool verify_access); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 204 | |
| 205 | static inline String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) { |
| 206 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 207 | return class_linker->ResolveString(string_idx, referrer); |
| 208 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 209 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 210 | extern void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception); |
| 211 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 212 | } // namespace art |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 213 | |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 214 | #endif // ART_SRC_RUNTIME_SUPPORT_H_ |