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