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