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 | */ |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 16 | |
| 17 | #include "class_linker.h" |
Logan Chien | 95dfa6a | 2012-06-01 16:08:02 +0800 | [diff] [blame] | 18 | #include "compiler_runtime_func_list.h" |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 19 | #include "dex_file.h" |
| 20 | #include "dex_instruction.h" |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 21 | #include "nth_caller_visitor.h" |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 22 | #include "object.h" |
| 23 | #include "object_utils.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 24 | #include "reflection.h" |
Shih-wei Liao | b1ab7df | 2012-03-29 13:53:46 -0700 | [diff] [blame] | 25 | #include "runtime_support.h" |
Logan Chien | 95dfa6a | 2012-06-01 16:08:02 +0800 | [diff] [blame] | 26 | #include "runtime_support_func_list.h" |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 27 | #include "runtime_support_llvm.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 28 | #include "ScopedLocalRef.h" |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 29 | #include "thread.h" |
Shih-wei Liao | 66adbb7 | 2012-03-07 01:31:50 -0800 | [diff] [blame] | 30 | #include "thread_list.h" |
Logan Chien | a1beb1f | 2012-06-01 16:03:27 +0800 | [diff] [blame] | 31 | #include "utils_llvm.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 32 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 33 | #include "well_known_classes.h" |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 34 | |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 35 | #include <algorithm> |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 36 | #include <cstdarg> |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 37 | #include <stdint.h> |
| 38 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 39 | #include "asm_support.h" |
| 40 | |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 41 | namespace art { |
| 42 | |
| 43 | //---------------------------------------------------------------------------- |
| 44 | // Thread |
| 45 | //---------------------------------------------------------------------------- |
| 46 | |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 47 | // This is used by other runtime support functions, NOT FROM CODE. The REAL GetCurrentThread is |
| 48 | // implemented by IRBuilder. (So, ARM can't return R9 in this function.) |
| 49 | // TODO: Maybe remove these which are implemented by IRBuilder after refactor runtime support. |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 50 | Thread* art_get_current_thread_from_code() { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 51 | #if defined(__i386__) |
| 52 | Thread* ptr; |
Elliott Hughes | 7834cbd | 2012-05-14 18:25:16 -0700 | [diff] [blame] | 53 | __asm__ __volatile__("movl %%fs:(%1), %0" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 54 | : "=r"(ptr) // output |
| 55 | : "r"(THREAD_SELF_OFFSET) // input |
| 56 | :); // clobber |
| 57 | return ptr; |
| 58 | #else |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 59 | return Thread::Current(); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 60 | #endif |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 61 | } |
| 62 | |
TDYa127 | c147826 | 2012-06-20 20:22:27 -0700 | [diff] [blame] | 63 | void* art_set_current_thread_from_code(void* thread_object_addr) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 64 | // Nothing to be done. |
TDYa127 | c147826 | 2012-06-20 20:22:27 -0700 | [diff] [blame] | 65 | return NULL; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 66 | } |
| 67 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 68 | void art_lock_object_from_code(Object* obj, Thread* thread) { |
Shih-wei Liao | 66adbb7 | 2012-03-07 01:31:50 -0800 | [diff] [blame] | 69 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 70 | obj->MonitorEnter(thread); // May block |
| 71 | DCHECK(thread->HoldsLock(obj)); |
| 72 | // Only possible exception is NPE and is handled before entry |
| 73 | DCHECK(!thread->IsExceptionPending()); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 74 | } |
| 75 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 76 | void art_unlock_object_from_code(Object* obj, Thread* thread) { |
Shih-wei Liao | 66adbb7 | 2012-03-07 01:31:50 -0800 | [diff] [blame] | 77 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 78 | // MonitorExit may throw exception |
| 79 | obj->MonitorExit(thread); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 80 | } |
| 81 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 82 | void art_test_suspend_from_code(Thread* thread) { |
Shih-wei Liao | 66adbb7 | 2012-03-07 01:31:50 -0800 | [diff] [blame] | 83 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 84 | } |
| 85 | |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 86 | ShadowFrame* art_push_shadow_frame_from_code(Thread* thread, ShadowFrame* new_shadow_frame, |
| 87 | Method* method, uint32_t size) { |
| 88 | ShadowFrame* old_frame = thread->PushShadowFrame(new_shadow_frame); |
| 89 | new_shadow_frame->SetMethod(method); |
| 90 | new_shadow_frame->SetNumberOfReferences(size); |
| 91 | return old_frame; |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 92 | } |
| 93 | |
TDYa127 | 0de52be | 2012-05-27 20:49:31 -0700 | [diff] [blame] | 94 | void art_pop_shadow_frame_from_code(void*) { |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 95 | LOG(FATAL) << "Implemented by IRBuilder."; |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 96 | } |
| 97 | |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 98 | void art_mark_gc_card_from_code(void *, void*) { |
| 99 | LOG(FATAL) << "Implemented by IRBuilder."; |
| 100 | } |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 101 | |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 102 | //---------------------------------------------------------------------------- |
| 103 | // Exception |
| 104 | //---------------------------------------------------------------------------- |
| 105 | |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 106 | bool art_is_exception_pending_from_code() { |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 107 | LOG(FATAL) << "Implemented by IRBuilder."; |
| 108 | return false; |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void art_throw_div_zero_from_code() { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 112 | Thread* thread = art_get_current_thread_from_code(); |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 113 | thread->ThrowNewException("Ljava/lang/ArithmeticException;", |
| 114 | "divide by zero"); |
| 115 | } |
| 116 | |
Shih-wei Liao | 77065fb | 2012-06-21 11:32:19 -0700 | [diff] [blame] | 117 | void art_throw_array_bounds_from_code(int32_t index, int32_t length) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 118 | Thread* thread = art_get_current_thread_from_code(); |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 119 | thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 120 | "length=%d; index=%d", length, index); |
| 121 | } |
| 122 | |
| 123 | void art_throw_no_such_method_from_code(int32_t method_idx) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 124 | Thread* thread = art_get_current_thread_from_code(); |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 125 | // We need the calling method as context for the method_idx |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 126 | Method* method = thread->GetCurrentMethod(); |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 127 | thread->ThrowNewException("Ljava/lang/NoSuchMethodError;", |
| 128 | MethodNameFromIndex(method, |
| 129 | method_idx, |
| 130 | verifier::VERIFY_ERROR_REF_METHOD, |
| 131 | false).c_str()); |
| 132 | } |
| 133 | |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 134 | void art_throw_null_pointer_exception_from_code(uint32_t dex_pc) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 135 | Thread* thread = art_get_current_thread_from_code(); |
Shih-wei Liao | a64f157 | 2012-06-22 00:09:57 -0700 | [diff] [blame^] | 136 | NthCallerVisitor visitor(thread->GetManagedStack(), thread->GetTraceStack(), 0); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 137 | visitor.WalkStack(); |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 138 | Method* throw_method = visitor.caller; |
| 139 | ThrowNullPointerExceptionFromDexPC(thread, throw_method, dex_pc); |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 140 | } |
| 141 | |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 142 | void art_throw_stack_overflow_from_code() { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 143 | Thread* thread = art_get_current_thread_from_code(); |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 144 | if (Runtime::Current()->IsMethodTracingActive()) { |
| 145 | TraceMethodUnwindFromCode(thread); |
| 146 | } |
Elliott Hughes | 6cf2388 | 2012-06-15 15:42:07 -0700 | [diff] [blame] | 147 | thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute. |
| 148 | thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;", "stack size %s", |
| 149 | PrettySize(thread->GetStackSize()).c_str()); |
| 150 | thread->ResetDefaultStackEnd(); // Return to default stack size. |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void art_throw_exception_from_code(Object* exception) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 154 | Thread* thread = art_get_current_thread_from_code(); |
TDYa127 | 540a5b7 | 2012-04-03 18:56:08 -0700 | [diff] [blame] | 155 | if (exception == NULL) { |
| 156 | thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception"); |
| 157 | } else { |
| 158 | thread->SetException(static_cast<Throwable*>(exception)); |
| 159 | } |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 162 | void art_throw_verification_error_from_code(Method* current_method, |
| 163 | int32_t kind, |
| 164 | int32_t ref) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 165 | ThrowVerificationError(art_get_current_thread_from_code(), current_method, kind, ref); |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 168 | int32_t art_find_catch_block_from_code(Method* current_method, |
| 169 | uint32_t ti_offset) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 170 | Thread* thread = art_get_current_thread_from_code(); |
TDYa127 | 2d70217 | 2012-04-01 15:15:13 -0700 | [diff] [blame] | 171 | Class* exception_type = thread->GetException()->GetClass(); |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 172 | MethodHelper mh(current_method); |
| 173 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 174 | DCHECK_LT(ti_offset, code_item->tries_size_); |
| 175 | const DexFile::TryItem* try_item = DexFile::GetTryItems(*code_item, ti_offset); |
| 176 | |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 177 | int iter_index = 0; |
| 178 | // Iterate over the catch handlers associated with dex_pc |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 179 | for (CatchHandlerIterator it(*code_item, *try_item); it.HasNext(); it.Next()) { |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 180 | uint16_t iter_type_idx = it.GetHandlerTypeIndex(); |
| 181 | // Catch all case |
| 182 | if (iter_type_idx == DexFile::kDexNoIndex16) { |
| 183 | return iter_index; |
| 184 | } |
| 185 | // Does this catch exception type apply? |
| 186 | Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx); |
| 187 | if (iter_exception_type == NULL) { |
| 188 | // The verifier should take care of resolving all exception classes early |
| 189 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
| 190 | << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx); |
| 191 | } else if (iter_exception_type->IsAssignableFrom(exception_type)) { |
| 192 | return iter_index; |
| 193 | } |
| 194 | ++iter_index; |
| 195 | } |
| 196 | // Handler not found |
| 197 | return -1; |
| 198 | } |
| 199 | |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 200 | |
| 201 | //---------------------------------------------------------------------------- |
| 202 | // Object Space |
| 203 | //---------------------------------------------------------------------------- |
| 204 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 205 | Object* art_alloc_object_from_code(uint32_t type_idx, |
| 206 | Method* referrer, |
| 207 | Thread* thread) { |
| 208 | return AllocObjectFromCode(type_idx, referrer, thread, false); |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 209 | } |
| 210 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 211 | Object* art_alloc_object_from_code_with_access_check(uint32_t type_idx, |
| 212 | Method* referrer, |
| 213 | Thread* thread) { |
| 214 | return AllocObjectFromCode(type_idx, referrer, thread, true); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 215 | } |
| 216 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 217 | Object* art_alloc_array_from_code(uint32_t type_idx, |
| 218 | Method* referrer, |
| 219 | uint32_t length, |
| 220 | Thread* thread) { |
| 221 | return AllocArrayFromCode(type_idx, referrer, length, thread, false); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | Object* art_alloc_array_from_code_with_access_check(uint32_t type_idx, |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 225 | Method* referrer, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 226 | uint32_t length, |
| 227 | Thread* thread) { |
| 228 | return AllocArrayFromCode(type_idx, referrer, length, thread, true); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | Object* art_check_and_alloc_array_from_code(uint32_t type_idx, |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 232 | Method* referrer, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 233 | uint32_t length, |
| 234 | Thread* thread) { |
| 235 | return CheckAndAllocArrayFromCode(type_idx, referrer, length, thread, false); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | Object* art_check_and_alloc_array_from_code_with_access_check(uint32_t type_idx, |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 239 | Method* referrer, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 240 | uint32_t length, |
| 241 | Thread* thread) { |
| 242 | return CheckAndAllocArrayFromCode(type_idx, referrer, length, thread, true); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 243 | } |
| 244 | |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 245 | static Method* FindMethodHelper(uint32_t method_idx, Object* this_object, Method* caller_method, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 246 | bool access_check, InvokeType type, Thread* thread) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 247 | Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type); |
| 248 | if (UNLIKELY(method == NULL)) { |
| 249 | method = FindMethodFromCode(method_idx, this_object, caller_method, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 250 | thread, access_check, type); |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 251 | if (UNLIKELY(method == NULL)) { |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 252 | CHECK(thread->IsExceptionPending()); |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 253 | return 0; // failure |
| 254 | } |
| 255 | } |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 256 | DCHECK(!thread->IsExceptionPending()); |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 257 | return method; |
| 258 | } |
| 259 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 260 | Object* art_find_static_method_from_code_with_access_check(uint32_t method_idx, |
| 261 | Object* this_object, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 262 | Method* referrer, |
| 263 | Thread* thread) { |
| 264 | return FindMethodHelper(method_idx, this_object, referrer, true, kStatic, thread); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 267 | Object* art_find_direct_method_from_code_with_access_check(uint32_t method_idx, |
| 268 | Object* this_object, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 269 | Method* referrer, |
| 270 | Thread* thread) { |
| 271 | return FindMethodHelper(method_idx, this_object, referrer, true, kDirect, thread); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | Object* art_find_virtual_method_from_code_with_access_check(uint32_t method_idx, |
| 275 | Object* this_object, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 276 | Method* referrer, |
| 277 | Thread* thread) { |
| 278 | return FindMethodHelper(method_idx, this_object, referrer, true, kVirtual, thread); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 281 | Object* art_find_super_method_from_code_with_access_check(uint32_t method_idx, |
| 282 | Object* this_object, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 283 | Method* referrer, |
| 284 | Thread* thread) { |
| 285 | return FindMethodHelper(method_idx, this_object, referrer, true, kSuper, thread); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 286 | } |
| 287 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 288 | Object* |
| 289 | art_find_interface_method_from_code_with_access_check(uint32_t method_idx, |
| 290 | Object* this_object, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 291 | Method* referrer, |
| 292 | Thread* thread) { |
| 293 | return FindMethodHelper(method_idx, this_object, referrer, true, kInterface, thread); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | Object* art_find_interface_method_from_code(uint32_t method_idx, |
| 297 | Object* this_object, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 298 | Method* referrer, |
| 299 | Thread* thread) { |
| 300 | return FindMethodHelper(method_idx, this_object, referrer, false, kInterface, thread); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 301 | } |
| 302 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 303 | Object* art_initialize_static_storage_from_code(uint32_t type_idx, |
| 304 | Method* referrer, |
| 305 | Thread* thread) { |
| 306 | return ResolveVerifyAndClinit(type_idx, referrer, thread, true, false); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 307 | } |
| 308 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 309 | Object* art_initialize_type_from_code(uint32_t type_idx, |
| 310 | Method* referrer, |
| 311 | Thread* thread) { |
| 312 | return ResolveVerifyAndClinit(type_idx, referrer, thread, false, false); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 313 | } |
| 314 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 315 | Object* art_initialize_type_and_verify_access_from_code(uint32_t type_idx, |
| 316 | Method* referrer, |
| 317 | Thread* thread) { |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 318 | // Called when caller isn't guaranteed to have access to a type and the dex cache may be |
| 319 | // unpopulated |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 320 | return ResolveVerifyAndClinit(type_idx, referrer, thread, false, true); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 321 | } |
| 322 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 323 | Object* art_resolve_string_from_code(Method* referrer, uint32_t string_idx) { |
| 324 | return ResolveStringFromCode(referrer, string_idx); |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 325 | } |
| 326 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 327 | int32_t art_set32_static_from_code(uint32_t field_idx, Method* referrer, int32_t new_value) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 328 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(uint32_t)); |
| 329 | if (LIKELY(field != NULL)) { |
| 330 | field->Set32(NULL, new_value); |
| 331 | return 0; |
| 332 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 333 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 334 | true, true, true, sizeof(uint32_t)); |
| 335 | if (LIKELY(field != NULL)) { |
| 336 | field->Set32(NULL, new_value); |
| 337 | return 0; |
| 338 | } |
| 339 | return -1; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 340 | } |
| 341 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 342 | int32_t art_set64_static_from_code(uint32_t field_idx, Method* referrer, int64_t new_value) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 343 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(uint64_t)); |
| 344 | if (LIKELY(field != NULL)) { |
| 345 | field->Set64(NULL, new_value); |
| 346 | return 0; |
| 347 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 348 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 349 | true, true, true, sizeof(uint64_t)); |
| 350 | if (LIKELY(field != NULL)) { |
| 351 | field->Set64(NULL, new_value); |
| 352 | return 0; |
| 353 | } |
| 354 | return -1; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 355 | } |
| 356 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 357 | int32_t art_set_obj_static_from_code(uint32_t field_idx, Method* referrer, Object* new_value) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 358 | Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*)); |
| 359 | if (LIKELY(field != NULL)) { |
| 360 | field->SetObj(NULL, new_value); |
| 361 | return 0; |
| 362 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 363 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 364 | true, false, true, sizeof(Object*)); |
| 365 | if (LIKELY(field != NULL)) { |
| 366 | field->SetObj(NULL, new_value); |
| 367 | return 0; |
| 368 | } |
| 369 | return -1; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 370 | } |
| 371 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 372 | int32_t art_get32_static_from_code(uint32_t field_idx, Method* referrer) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 373 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(uint32_t)); |
| 374 | if (LIKELY(field != NULL)) { |
| 375 | return field->Get32(NULL); |
| 376 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 377 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 378 | true, true, false, sizeof(uint32_t)); |
| 379 | if (LIKELY(field != NULL)) { |
| 380 | return field->Get32(NULL); |
| 381 | } |
| 382 | return 0; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 383 | } |
| 384 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 385 | int64_t art_get64_static_from_code(uint32_t field_idx, Method* referrer) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 386 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(uint64_t)); |
| 387 | if (LIKELY(field != NULL)) { |
| 388 | return field->Get64(NULL); |
| 389 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 390 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 391 | true, true, false, sizeof(uint64_t)); |
| 392 | if (LIKELY(field != NULL)) { |
| 393 | return field->Get64(NULL); |
| 394 | } |
| 395 | return 0; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 396 | } |
| 397 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 398 | Object* art_get_obj_static_from_code(uint32_t field_idx, Method* referrer) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 399 | Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*)); |
| 400 | if (LIKELY(field != NULL)) { |
| 401 | return field->GetObj(NULL); |
| 402 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 403 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 404 | true, false, false, sizeof(Object*)); |
| 405 | if (LIKELY(field != NULL)) { |
| 406 | return field->GetObj(NULL); |
| 407 | } |
| 408 | return 0; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 409 | } |
| 410 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 411 | int32_t art_set32_instance_from_code(uint32_t field_idx, Method* referrer, |
| 412 | Object* obj, uint32_t new_value) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 413 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(uint32_t)); |
| 414 | if (LIKELY(field != NULL)) { |
| 415 | field->Set32(obj, new_value); |
| 416 | return 0; |
| 417 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 418 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 419 | false, true, true, sizeof(uint32_t)); |
| 420 | if (LIKELY(field != NULL)) { |
| 421 | field->Set32(obj, new_value); |
| 422 | return 0; |
| 423 | } |
| 424 | return -1; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 425 | } |
| 426 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 427 | int32_t art_set64_instance_from_code(uint32_t field_idx, Method* referrer, |
| 428 | Object* obj, int64_t new_value) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 429 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(uint64_t)); |
| 430 | if (LIKELY(field != NULL)) { |
| 431 | field->Set64(obj, new_value); |
| 432 | return 0; |
| 433 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 434 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 435 | false, true, true, sizeof(uint64_t)); |
| 436 | if (LIKELY(field != NULL)) { |
| 437 | field->Set64(obj, new_value); |
| 438 | return 0; |
| 439 | } |
| 440 | return -1; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 441 | } |
| 442 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 443 | int32_t art_set_obj_instance_from_code(uint32_t field_idx, Method* referrer, |
| 444 | Object* obj, Object* new_value) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 445 | Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*)); |
| 446 | if (LIKELY(field != NULL)) { |
| 447 | field->SetObj(obj, new_value); |
| 448 | return 0; |
| 449 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 450 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 451 | false, false, true, sizeof(Object*)); |
| 452 | if (LIKELY(field != NULL)) { |
| 453 | field->SetObj(obj, new_value); |
| 454 | return 0; |
| 455 | } |
| 456 | return -1; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 457 | } |
| 458 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 459 | int32_t art_get32_instance_from_code(uint32_t field_idx, Method* referrer, Object* obj) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 460 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(uint32_t)); |
| 461 | if (LIKELY(field != NULL)) { |
| 462 | return field->Get32(obj); |
| 463 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 464 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 465 | false, true, false, sizeof(uint32_t)); |
| 466 | if (LIKELY(field != NULL)) { |
| 467 | return field->Get32(obj); |
| 468 | } |
| 469 | return 0; |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | int64_t art_get64_instance_from_code(uint32_t field_idx, Method* referrer, Object* obj) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 473 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(uint64_t)); |
| 474 | if (LIKELY(field != NULL)) { |
| 475 | return field->Get64(obj); |
| 476 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 477 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 478 | false, true, false, sizeof(uint64_t)); |
| 479 | if (LIKELY(field != NULL)) { |
| 480 | return field->Get64(obj); |
| 481 | } |
| 482 | return 0; |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | Object* art_get_obj_instance_from_code(uint32_t field_idx, Method* referrer, Object* obj) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 486 | Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*)); |
| 487 | if (LIKELY(field != NULL)) { |
| 488 | return field->GetObj(obj); |
| 489 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 490 | field = FindFieldFromCode(field_idx, referrer, art_get_current_thread_from_code(), |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 491 | false, false, false, sizeof(Object*)); |
| 492 | if (LIKELY(field != NULL)) { |
| 493 | return field->GetObj(obj); |
| 494 | } |
| 495 | return 0; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 496 | } |
| 497 | |
Ian Rogers | c553b95 | 2012-06-18 23:39:11 -0700 | [diff] [blame] | 498 | Object* art_decode_jobject_in_thread(Thread* self, jobject java_object) { |
| 499 | if (self->IsExceptionPending()) { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
Ian Rogers | c553b95 | 2012-06-18 23:39:11 -0700 | [diff] [blame] | 502 | Object* o = self->DecodeJObject(java_object); |
| 503 | if (o == NULL || !self->GetJniEnv()->check_jni) { |
| 504 | return o; |
| 505 | } |
| 506 | |
| 507 | if (o == kInvalidIndirectRefObject) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 508 | JniAbortF(NULL, "invalid reference returned from %s", |
| 509 | PrettyMethod(self->GetCurrentMethod()).c_str()); |
Ian Rogers | c553b95 | 2012-06-18 23:39:11 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // Make sure that the result is an instance of the type this |
| 513 | // method was expected to return. |
| 514 | Method* m = self->GetCurrentMethod(); |
| 515 | MethodHelper mh(m); |
| 516 | Class* return_type = mh.GetReturnType(); |
| 517 | |
| 518 | if (!o->InstanceOf(return_type)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 519 | JniAbortF(NULL, "attempt to return an instance of %s from %s", |
| 520 | PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str()); |
Ian Rogers | c553b95 | 2012-06-18 23:39:11 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return o; |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 524 | } |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 525 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 526 | void art_fill_array_data_from_code(Method* method, uint32_t dex_pc, |
| 527 | Array* array, uint32_t payload_offset) { |
| 528 | // Test: Is array equal to null? (Guard NullPointerException) |
| 529 | if (UNLIKELY(array == NULL)) { |
| 530 | art_throw_null_pointer_exception_from_code(dex_pc); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | // Find the payload from the CodeItem |
| 535 | MethodHelper mh(method); |
| 536 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
| 537 | |
| 538 | DCHECK_GT(code_item->insns_size_in_code_units_, payload_offset); |
| 539 | |
| 540 | const Instruction::ArrayDataPayload* payload = |
| 541 | reinterpret_cast<const Instruction::ArrayDataPayload*>( |
| 542 | code_item->insns_ + payload_offset); |
| 543 | |
| 544 | DCHECK_EQ(payload->ident, |
| 545 | static_cast<uint16_t>(Instruction::kArrayDataSignature)); |
| 546 | |
| 547 | // Test: Is array big enough? |
| 548 | uint32_t array_len = static_cast<uint32_t>(array->GetLength()); |
| 549 | if (UNLIKELY(array_len < payload->element_count)) { |
| 550 | int32_t last_index = payload->element_count - 1; |
| 551 | art_throw_array_bounds_from_code(array_len, last_index); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | // Copy the data |
| 556 | size_t size = payload->element_width * payload->element_count; |
| 557 | memcpy(array->GetRawData(payload->element_width), payload->data, size); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 562 | //---------------------------------------------------------------------------- |
Shih-wei Liao | 9e0e54d | 2012-04-02 19:22:28 -0700 | [diff] [blame] | 563 | // Type checking, in the nature of casting |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 564 | //---------------------------------------------------------------------------- |
| 565 | |
TDYa127 | 3de5ba3 | 2012-04-02 07:04:40 -0700 | [diff] [blame] | 566 | int32_t art_is_assignable_from_code(const Class* dest_type, const Class* src_type) { |
| 567 | DCHECK(dest_type != NULL); |
| 568 | DCHECK(src_type != NULL); |
| 569 | return dest_type->IsAssignableFrom(src_type) ? 1 : 0; |
Logan Chien | fc5bc67 | 2012-03-06 16:54:30 +0800 | [diff] [blame] | 570 | } |
| 571 | |
TDYa127 | 3de5ba3 | 2012-04-02 07:04:40 -0700 | [diff] [blame] | 572 | void art_check_cast_from_code(const Class* dest_type, const Class* src_type) { |
TDYa127 | 1b86d07 | 2012-04-05 17:38:56 -0700 | [diff] [blame] | 573 | DCHECK(dest_type->IsClass()) << PrettyClass(dest_type); |
TDYa127 | 3de5ba3 | 2012-04-02 07:04:40 -0700 | [diff] [blame] | 574 | DCHECK(src_type->IsClass()) << PrettyClass(src_type); |
| 575 | if (UNLIKELY(!dest_type->IsAssignableFrom(src_type))) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 576 | Thread* thread = art_get_current_thread_from_code(); |
| 577 | thread->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
| 578 | "%s cannot be cast to %s", |
TDYa127 | 8e9b449 | 2012-04-24 15:50:27 -0700 | [diff] [blame] | 579 | PrettyDescriptor(src_type).c_str(), |
| 580 | PrettyDescriptor(dest_type).c_str()); |
TDYa127 | 3de5ba3 | 2012-04-02 07:04:40 -0700 | [diff] [blame] | 581 | } |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 582 | } |
| 583 | |
TDYa127 | 1b86d07 | 2012-04-05 17:38:56 -0700 | [diff] [blame] | 584 | void art_check_put_array_element_from_code(const Object* element, const Object* array) { |
| 585 | if (element == NULL) { |
| 586 | return; |
| 587 | } |
| 588 | DCHECK(array != NULL); |
| 589 | Class* array_class = array->GetClass(); |
| 590 | DCHECK(array_class != NULL); |
| 591 | Class* component_type = array_class->GetComponentType(); |
| 592 | Class* element_class = element->GetClass(); |
| 593 | if (UNLIKELY(!component_type->IsAssignableFrom(element_class))) { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 594 | Thread* thread = art_get_current_thread_from_code(); |
| 595 | thread->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
| 596 | "%s cannot be stored in an array of type %s", |
| 597 | PrettyDescriptor(element_class).c_str(), |
| 598 | PrettyDescriptor(array_class).c_str()); |
TDYa127 | 1b86d07 | 2012-04-05 17:38:56 -0700 | [diff] [blame] | 599 | } |
| 600 | return; |
| 601 | } |
| 602 | |
Logan Chien | 2771fb1 | 2012-03-06 16:28:35 +0800 | [diff] [blame] | 603 | //---------------------------------------------------------------------------- |
| 604 | // Runtime Support Function Lookup Callback |
| 605 | //---------------------------------------------------------------------------- |
| 606 | |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 607 | #define EXTERNAL_LINKAGE(NAME) \ |
| 608 | extern "C" void NAME(...); |
Logan Chien | 95dfa6a | 2012-06-01 16:08:02 +0800 | [diff] [blame] | 609 | COMPILER_RUNTIME_FUNC_LIST_NATIVE(EXTERNAL_LINKAGE) |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 610 | #undef EXTERNAL_LINKAGE |
| 611 | |
| 612 | static void* art_find_compiler_runtime_func(char const* name) { |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 613 | // TODO: If target support some math func, use the target's version. (e.g. art_d2i -> __aeabi_d2iz) |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 614 | static const char* const names[] = { |
| 615 | #define DEFINE_ENTRY(NAME) #NAME , |
Logan Chien | 95dfa6a | 2012-06-01 16:08:02 +0800 | [diff] [blame] | 616 | COMPILER_RUNTIME_FUNC_LIST_NATIVE(DEFINE_ENTRY) |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 617 | #undef DEFINE_ENTRY |
| 618 | }; |
| 619 | |
| 620 | static void* const funcs[] = { |
| 621 | #define DEFINE_ENTRY(NAME) reinterpret_cast<void*>(NAME) , |
Logan Chien | 95dfa6a | 2012-06-01 16:08:02 +0800 | [diff] [blame] | 622 | COMPILER_RUNTIME_FUNC_LIST_NATIVE(DEFINE_ENTRY) |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 623 | #undef DEFINE_ENTRY |
| 624 | }; |
| 625 | |
| 626 | static const size_t num_entries = sizeof(names) / sizeof(const char* const); |
| 627 | |
Logan Chien | 64f884d | 2012-04-03 18:30:47 +0800 | [diff] [blame] | 628 | const char* const* const names_begin = names; |
| 629 | const char* const* const names_end = names + num_entries; |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 630 | |
Logan Chien | 64f884d | 2012-04-03 18:30:47 +0800 | [diff] [blame] | 631 | const char* const* name_lbound_ptr = |
Logan Chien | a1beb1f | 2012-06-01 16:03:27 +0800 | [diff] [blame] | 632 | std::lower_bound(names_begin, names_end, name, |
| 633 | CStringLessThanComparator()); |
Logan Chien | 64f884d | 2012-04-03 18:30:47 +0800 | [diff] [blame] | 634 | |
| 635 | if (name_lbound_ptr < names_end && strcmp(*name_lbound_ptr, name) == 0) { |
| 636 | return funcs[name_lbound_ptr - names_begin]; |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 637 | } else { |
Logan Chien | 64f884d | 2012-04-03 18:30:47 +0800 | [diff] [blame] | 638 | return NULL; |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
TDYa127 | 05fe3b6 | 2012-04-21 00:28:54 -0700 | [diff] [blame] | 642 | // TODO: This runtime support function is the temporary solution for the link issue. |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 643 | // It calls to this function before invoking any function, and this function will check: |
TDYa127 | 05fe3b6 | 2012-04-21 00:28:54 -0700 | [diff] [blame] | 644 | // 1. The code address is 0. -> Link the code by ELFLoader |
| 645 | // That will solved by in-place linking at image loading. |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 646 | const void* art_fix_stub_from_code(Method* called) { |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 647 | const void* code = called->GetCode(); |
TDYa127 | 05fe3b6 | 2012-04-21 00:28:54 -0700 | [diff] [blame] | 648 | // 1. The code address is 0. -> Link the code by ELFLoader |
| 649 | if (UNLIKELY(code == NULL)) { |
| 650 | Runtime::Current()->GetClassLinker()->LinkOatCodeFor(called); |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 651 | return called->GetCode(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 652 | } |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 653 | |
| 654 | return code; |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 655 | } |
| 656 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 657 | // Handler for invocation on proxy methods. We create a boxed argument array. And we invoke |
| 658 | // the invocation handler which is a field within the proxy object receiver. |
| 659 | void art_proxy_invoke_handler_from_code(Method* proxy_method, ...) { |
| 660 | va_list ap; |
| 661 | va_start(ap, proxy_method); |
| 662 | |
| 663 | Object* receiver = va_arg(ap, Object*); |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 664 | Thread* thread = va_arg(ap, Thread*); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 665 | MethodHelper proxy_mh(proxy_method); |
| 666 | const size_t num_params = proxy_mh.NumArgs(); |
| 667 | |
| 668 | // Start new JNI local reference state |
| 669 | JNIEnvExt* env = thread->GetJniEnv(); |
| 670 | ScopedJniEnvLocalRefState env_state(env); |
| 671 | |
TDYa127 | 5482503 | 2012-04-11 10:45:23 -0700 | [diff] [blame] | 672 | // Create local ref. copies of the receiver |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 673 | jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver); |
| 674 | |
| 675 | // Convert proxy method into expected interface method |
| 676 | Method* interface_method = proxy_method->FindOverriddenMethod(); |
| 677 | DCHECK(interface_method != NULL); |
| 678 | DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method); |
| 679 | |
| 680 | // Set up arguments array and place in local IRT during boxing (which may allocate/GC) |
| 681 | jvalue args_jobj[3]; |
| 682 | args_jobj[0].l = rcvr_jobj; |
| 683 | args_jobj[1].l = AddLocalReference<jobject>(env, interface_method); |
| 684 | // Args array, if no arguments then NULL (don't include receiver in argument count) |
| 685 | args_jobj[2].l = NULL; |
| 686 | ObjectArray<Object>* args = NULL; |
| 687 | if ((num_params - 1) > 0) { |
| 688 | args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1); |
| 689 | if (args == NULL) { |
| 690 | CHECK(thread->IsExceptionPending()); |
| 691 | return; |
| 692 | } |
| 693 | args_jobj[2].l = AddLocalReference<jobjectArray>(env, args); |
| 694 | } |
| 695 | |
| 696 | // Get parameter types. |
| 697 | const char* shorty = proxy_mh.GetShorty(); |
| 698 | ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes(); |
| 699 | if (param_types == NULL) { |
| 700 | CHECK(thread->IsExceptionPending()); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | // Box arguments. |
| 705 | for (size_t i = 0; i < (num_params - 1);++i) { |
| 706 | JValue val; |
| 707 | switch (shorty[i+1]) { |
| 708 | case 'Z': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 709 | val.SetZ(va_arg(ap, jint)); |
| 710 | break; |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 711 | case 'B': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 712 | val.SetB(va_arg(ap, jint)); |
| 713 | break; |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 714 | case 'C': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 715 | val.SetC(va_arg(ap, jint)); |
| 716 | break; |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 717 | case 'S': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 718 | val.SetS(va_arg(ap, jint)); |
| 719 | break; |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 720 | case 'I': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 721 | val.SetI(va_arg(ap, jint)); |
| 722 | break; |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 723 | case 'F': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 724 | val.SetI(va_arg(ap, jint)); // TODO: is this right? |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 725 | break; |
| 726 | case 'L': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 727 | val.SetL(va_arg(ap, Object*)); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 728 | break; |
| 729 | case 'D': |
| 730 | case 'J': |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 731 | val.SetJ(va_arg(ap, jlong)); // TODO: is this right for double? |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | Class* param_type = param_types->Get(i); |
| 735 | if (param_type->IsPrimitive()) { |
| 736 | BoxPrimitive(param_type->GetPrimitiveType(), val); |
| 737 | if (thread->IsExceptionPending()) { |
| 738 | return; |
| 739 | } |
| 740 | } |
Shih-wei Liao | 2272f7b | 2012-04-12 14:23:48 -0700 | [diff] [blame] | 741 | args->Set(i, val.GetL()); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 744 | DCHECK(env->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy)); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 745 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 746 | jobject inv_hand = env->GetObjectField(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy_h); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 747 | // Call InvocationHandler.invoke |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 748 | jobject result = env->CallObjectMethodA(inv_hand, WellKnownClasses::java_lang_reflect_InvocationHandler_invoke, args_jobj); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 749 | |
| 750 | // Place result in stack args |
| 751 | if (!thread->IsExceptionPending()) { |
| 752 | if (shorty[0] == 'V') { |
| 753 | return; |
| 754 | } |
| 755 | Object* result_ref = thread->DecodeJObject(result); |
| 756 | JValue* result_unboxed = va_arg(ap, JValue*); |
| 757 | if (result_ref == NULL) { |
Shih-wei Liao | 2272f7b | 2012-04-12 14:23:48 -0700 | [diff] [blame] | 758 | result_unboxed->SetL(NULL); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 759 | } else { |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 760 | bool unboxed_okay = UnboxPrimitiveForResult(result_ref, proxy_mh.GetReturnType(), *result_unboxed); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 761 | if (!unboxed_okay) { |
| 762 | thread->ClearException(); |
| 763 | thread->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
| 764 | "Couldn't convert result of type %s to %s", |
| 765 | PrettyTypeOf(result_ref).c_str(), |
| 766 | PrettyDescriptor(proxy_mh.GetReturnType()).c_str()); |
| 767 | return; |
| 768 | } |
| 769 | } |
| 770 | } else { |
| 771 | // In the case of checked exceptions that aren't declared, the exception must be wrapped by |
| 772 | // a UndeclaredThrowableException. |
| 773 | Throwable* exception = thread->GetException(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 774 | if (exception->IsCheckedException()) { |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 775 | SynthesizedProxyClass* proxy_class = |
| 776 | down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass()); |
| 777 | int throws_index = -1; |
| 778 | size_t num_virt_methods = proxy_class->NumVirtualMethods(); |
| 779 | for (size_t i = 0; i < num_virt_methods; i++) { |
| 780 | if (proxy_class->GetVirtualMethod(i) == proxy_method) { |
| 781 | throws_index = i; |
| 782 | break; |
| 783 | } |
| 784 | } |
| 785 | CHECK_NE(throws_index, -1); |
| 786 | ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index); |
| 787 | Class* exception_class = exception->GetClass(); |
| 788 | bool declares_exception = false; |
| 789 | for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { |
| 790 | Class* declared_exception = declared_exceptions->Get(i); |
| 791 | declares_exception = declared_exception->IsAssignableFrom(exception_class); |
| 792 | } |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 793 | if (!declares_exception) { |
Shih-wei Liao | 4b644ec | 2012-05-29 17:16:31 -0700 | [diff] [blame] | 794 | thread->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", NULL); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | va_end(ap); |
| 800 | } |
| 801 | |
Logan Chien | 2771fb1 | 2012-03-06 16:28:35 +0800 | [diff] [blame] | 802 | void* art_find_runtime_support_func(void* context, char const* name) { |
| 803 | struct func_entry_t { |
| 804 | char const* name; |
| 805 | size_t name_len; |
| 806 | void* addr; |
| 807 | }; |
| 808 | |
| 809 | static struct func_entry_t const tab[] = { |
Logan Chien | bc615a1 | 2012-03-06 17:03:48 +0800 | [diff] [blame] | 810 | #define DEFINE_ENTRY(ID, NAME) \ |
| 811 | { #NAME, sizeof(#NAME) - 1, reinterpret_cast<void*>(NAME) }, |
Logan Chien | bc615a1 | 2012-03-06 17:03:48 +0800 | [diff] [blame] | 812 | RUNTIME_SUPPORT_FUNC_LIST(DEFINE_ENTRY) |
Logan Chien | bc615a1 | 2012-03-06 17:03:48 +0800 | [diff] [blame] | 813 | #undef DEFINE_ENTRY |
Logan Chien | 2771fb1 | 2012-03-06 16:28:35 +0800 | [diff] [blame] | 814 | }; |
| 815 | |
| 816 | static size_t const tab_size = sizeof(tab) / sizeof(struct func_entry_t); |
| 817 | |
Logan Chien | d23c5ad | 2012-03-30 17:46:04 +0800 | [diff] [blame] | 818 | // Search the compiler runtime (such as __divdi3) |
| 819 | void* result = art_find_compiler_runtime_func(name); |
| 820 | if (result != NULL) { |
| 821 | return result; |
| 822 | } |
| 823 | |
Logan Chien | 2771fb1 | 2012-03-06 16:28:35 +0800 | [diff] [blame] | 824 | // Note: Since our table is small, we are using trivial O(n) searching |
| 825 | // function. For bigger table, it will be better to use binary |
| 826 | // search or hash function. |
| 827 | size_t i; |
| 828 | size_t name_len = strlen(name); |
| 829 | for (i = 0; i < tab_size; ++i) { |
| 830 | if (name_len == tab[i].name_len && strcmp(name, tab[i].name) == 0) { |
| 831 | return tab[i].addr; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | LOG(FATAL) << "Error: Can't find symbol " << name; |
| 836 | return 0; |
| 837 | } |
| 838 | |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 839 | } // namespace art |