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