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