blob: c6e05ae02023ecae4d6de48b6a58239a50223dd4 [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
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 */
16
17#include "interpreter_common.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010018#include "mirror/array-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020019
20namespace art {
21namespace interpreter {
22
Sebastien Hertzc61124b2013-09-10 11:44:19 +020023static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh,
24 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
25 JValue* result, size_t arg_offset)
26 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020027
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020028// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -080029static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
30 size_t dest_reg, size_t src_reg)
31 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020032 // If both register locations contains the same value, the register probably holds a reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -070033 // Uint required, so that sign extension does not make this wrong on 64b systems
34 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -080035 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Andreas Gampe7104cbf2014-03-21 11:44:43 -070036 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080037 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020038 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080039 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020040 }
41}
42
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -070043void AbortTransaction(Thread* self, const char* fmt, ...) {
44 CHECK(Runtime::Current()->IsActiveTransaction());
45 // Throw an exception so we can abort the transaction and undo every change.
46 va_list args;
47 va_start(args, fmt);
48 self->ThrowNewExceptionV(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", fmt,
49 args);
50 va_end(args);
51}
52
Sebastien Hertzc61124b2013-09-10 11:44:19 +020053template<bool is_range, bool do_assignability_check>
Sebastien Hertz9119c5f2013-12-16 11:31:45 +010054bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +020055 const Instruction* inst, uint16_t inst_data, JValue* result) {
56 // Compute method information.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070057 const DexFile::CodeItem* code_item = method->GetCodeItem();
Sebastien Hertzc61124b2013-09-10 11:44:19 +020058 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020059 uint16_t num_regs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020060 if (LIKELY(code_item != NULL)) {
61 num_regs = code_item->registers_size_;
Sebastien Hertzc61124b2013-09-10 11:44:19 +020062 DCHECK_EQ(num_ins, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020063 } else {
64 DCHECK(method->IsNative() || method->IsProxyMethod());
Sebastien Hertzc61124b2013-09-10 11:44:19 +020065 num_regs = num_ins;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020066 }
67
Sebastien Hertzc61124b2013-09-10 11:44:19 +020068 // Allocate shadow frame on the stack.
Mathieu Chartiere861ebd2013-10-09 15:01:21 -070069 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
Sebastien Hertz8ece0502013-08-07 11:26:41 +020070 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
71 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +020072
73 // Initialize new shadow frame.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020074 const size_t first_dest_reg = num_regs - num_ins;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070075 StackHandleScope<1> hs(self);
76 MethodHelper mh(hs.NewHandle(method));
Jeff Haoa3faaf42013-09-03 19:07:00 -070077 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -070078 // Slow path.
79 // We might need to do class loading, which incurs a thread state change to kNative. So
80 // register the shadow frame as under construction and allow suspension again.
81 self->SetShadowFrameUnderConstruction(new_shadow_frame);
82 self->EndAssertNoThreadSuspension(old_cause);
83
84 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020085 // to get the exact type of each reference argument.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070086 const DexFile::TypeList* params = method->GetParameterTypeList();
87 uint32_t shorty_len = 0;
88 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020089
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020090 // TODO: find a cleaner way to separate non-range and range information without duplicating code.
91 uint32_t arg[5]; // only used in invoke-XXX.
92 uint32_t vregC; // only used in invoke-XXX-range.
93 if (is_range) {
94 vregC = inst->VRegC_3rc();
95 } else {
Ian Rogers29a26482014-05-02 15:27:29 -070096 inst->GetVarArgs(arg, inst_data);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +020097 }
Sebastien Hertz9119c5f2013-12-16 11:31:45 +010098
99 // Handle receiver apart since it's not part of the shorty.
100 size_t dest_reg = first_dest_reg;
101 size_t arg_offset = 0;
102 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700103 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100104 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
105 ++dest_reg;
106 ++arg_offset;
107 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700109 DCHECK_LT(shorty_pos + 1, shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200110 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
111 switch (shorty[shorty_pos + 1]) {
112 case 'L': {
113 Object* o = shadow_frame.GetVRegReference(src_reg);
114 if (do_assignability_check && o != NULL) {
115 Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_);
116 if (arg_type == NULL) {
117 CHECK(self->IsExceptionPending());
118 return false;
119 }
120 if (!o->VerifierInstanceOf(arg_type)) {
121 // This should never happen.
122 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
123 "Ljava/lang/VirtualMachineError;",
124 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700125 method->GetName(), shorty_pos,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700126 o->GetClass()->GetDescriptor().c_str(),
127 arg_type->GetDescriptor().c_str());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200128 return false;
129 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700130 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200131 new_shadow_frame->SetVRegReference(dest_reg, o);
132 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700133 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200134 case 'J': case 'D': {
135 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
136 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
137 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
138 ++dest_reg;
139 ++arg_offset;
140 break;
141 }
142 default:
143 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
144 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200145 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200146 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700147 // We're done with the construction.
148 self->ClearShadowFrameUnderConstruction();
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200149 } else {
150 // Fast path: no extra checks.
151 if (is_range) {
152 const uint16_t first_src_reg = inst->VRegC_3rc();
153 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
154 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200156 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200157 } else {
158 DCHECK_LE(num_ins, 5U);
159 uint16_t regList = inst->Fetch16(2);
160 uint16_t count = num_ins;
161 if (count == 5) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800162 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, (inst_data >> 8) & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200163 --count;
164 }
165 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200167 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200168 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700169 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200170 }
171
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200172 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200173 if (LIKELY(Runtime::Current()->IsStarted())) {
Ian Rogers1d99e452014-01-02 17:36:41 -0800174 if (kIsDebugBuild && method->GetEntryPointFromInterpreter() == nullptr) {
175 LOG(FATAL) << "Attempt to invoke non-executable method: " << PrettyMethod(method);
176 }
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800177 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
178 !method->IsNative() && !method->IsProxyMethod() &&
179 method->GetEntryPointFromInterpreter() == artInterpreterToCompiledCodeBridge) {
180 LOG(FATAL) << "Attempt to call compiled code when -Xint: " << PrettyMethod(method);
181 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200182 (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result);
183 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200184 UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200185 }
186 return !self->IsExceptionPending();
187}
188
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100189template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200190bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
191 Thread* self, JValue* result) {
192 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
193 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
194 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
195 if (!is_range) {
196 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
197 CHECK_LE(length, 5);
198 }
199 if (UNLIKELY(length < 0)) {
200 ThrowNegativeArraySizeException(length);
201 return false;
202 }
203 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
204 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
205 self, false, do_access_check);
206 if (UNLIKELY(arrayClass == NULL)) {
207 DCHECK(self->IsExceptionPending());
208 return false;
209 }
210 CHECK(arrayClass->IsArrayClass());
211 Class* componentClass = arrayClass->GetComponentType();
212 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
213 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
214 ThrowRuntimeException("Bad filled array request for type %s",
215 PrettyDescriptor(componentClass).c_str());
216 } else {
217 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
218 "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800219 "Found type %s; filled-new-array not implemented for anything but 'int'",
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200220 PrettyDescriptor(componentClass).c_str());
221 }
222 return false;
223 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800224 Object* newArray = Array::Alloc<true>(self, arrayClass, length, arrayClass->GetComponentSize(),
225 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200226 if (UNLIKELY(newArray == NULL)) {
227 DCHECK(self->IsExceptionPending());
228 return false;
229 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100230 uint32_t arg[5]; // only used in filled-new-array.
231 uint32_t vregC; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200232 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100233 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200234 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700235 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100236 }
237 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
238 for (int32_t i = 0; i < length; ++i) {
239 size_t src_reg = is_range ? vregC + i : arg[i];
240 if (is_primitive_int_component) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100241 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100242 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100243 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200244 }
245 }
246
247 result->SetL(newArray);
248 return true;
249}
250
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100251// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
252template<typename T>
253static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
254 NO_THREAD_SAFETY_ANALYSIS {
255 Runtime* runtime = Runtime::Current();
256 for (int32_t i = 0; i < count; ++i) {
257 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
258 }
259}
260
261void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
263 DCHECK(Runtime::Current()->IsActiveTransaction());
264 DCHECK(array != nullptr);
265 DCHECK_LE(count, array->GetLength());
266 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
267 switch (primitive_component_type) {
268 case Primitive::kPrimBoolean:
269 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
270 break;
271 case Primitive::kPrimByte:
272 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
273 break;
274 case Primitive::kPrimChar:
275 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
276 break;
277 case Primitive::kPrimShort:
278 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
279 break;
280 case Primitive::kPrimInt:
281 case Primitive::kPrimFloat:
282 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
283 break;
284 case Primitive::kPrimLong:
285 case Primitive::kPrimDouble:
286 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
287 break;
288 default:
289 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
290 << " in fill-array-data";
291 break;
292 }
293}
294
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200295static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh,
296 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
297 JValue* result, size_t arg_offset) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200298 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
299 // problems in core libraries.
300 std::string name(PrettyMethod(shadow_frame->GetMethod()));
Kenny Rootfa31b3c2013-12-09 13:51:32 -0800301 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)"
302 || name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") {
303 // TODO Class#forName should actually call Class::EnsureInitialized always. Support for the
304 // other variants that take more arguments should also be added.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200305 std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset)->AsString()->ToModifiedUtf8().c_str()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700306
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700307 // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700308 Class* found = Runtime::Current()->GetClassLinker()->FindClass(
309 self, descriptor.c_str(), NullHandle<mirror::ClassLoader>());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200310 CHECK(found != NULL) << "Class.forName failed in un-started runtime for class: "
311 << PrettyDescriptor(descriptor);
312 result->SetL(found);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700313 } else if (name == "java.lang.Class java.lang.Void.lookupType()") {
314 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
Kenny Rootfa31b3c2013-12-09 13:51:32 -0800315 } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700316 StackHandleScope<1> hs(self);
317 Handle<ClassLoader> class_loader(
318 hs.NewHandle(down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset))));
Kenny Rootfa31b3c2013-12-09 13:51:32 -0800319 std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset + 1)->AsString()->ToModifiedUtf8().c_str()));
320
Ian Rogers98379392014-02-24 16:53:16 -0800321 Class* found = Runtime::Current()->GetClassLinker()->FindClass(self, descriptor.c_str(),
Kenny Rootfa31b3c2013-12-09 13:51:32 -0800322 class_loader);
323 result->SetL(found);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200324 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
325 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
326 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
327 CHECK(c != NULL);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700328 StackHandleScope<1> hs(self);
329 Handle<Object> obj(hs.NewHandle(klass->AllocObject(self)));
330 CHECK(obj.Get() != NULL);
331 EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL);
332 result->SetL(obj.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200333 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
334 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
335 // going the reflective Dex way.
336 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
337 String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
338 ArtField* found = NULL;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200339 ObjectArray<ArtField>* fields = klass->GetIFields();
340 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
341 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700342 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200343 found = f;
344 }
345 }
346 if (found == NULL) {
347 fields = klass->GetSFields();
348 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
349 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700350 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200351 found = f;
352 }
353 }
354 }
355 CHECK(found != NULL)
356 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
357 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
358 // TODO: getDeclaredField calls GetType once the field is found to ensure a
359 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
360 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700361 StackHandleScope<1> hs(self);
362 Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self)));
363 CHECK(field.Get() != NULL);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200364 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
365 uint32_t args[1];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800366 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700367 EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL);
368 result->SetL(field.Get());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700369 } else if (name == "int java.lang.Object.hashCode()") {
370 Object* obj = shadow_frame->GetVRegReference(arg_offset);
371 result->SetI(obj->IdentityHashCode());
372 } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700373 StackHandleScope<1> hs(self);
374 MethodHelper mh(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsArtMethod()));
375 result->SetL(mh.GetNameAsString(self));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200376 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
377 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
378 // Special case array copying without initializing System.
379 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
380 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
381 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
382 jint length = shadow_frame->GetVReg(arg_offset + 4);
383 if (!ctype->IsPrimitive()) {
384 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
385 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
386 for (jint i = 0; i < length; ++i) {
387 dst->Set(dstPos + i, src->Get(srcPos + i));
388 }
389 } else if (ctype->IsPrimitiveChar()) {
390 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
391 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
392 for (jint i = 0; i < length; ++i) {
393 dst->Set(dstPos + i, src->Get(srcPos + i));
394 }
395 } else if (ctype->IsPrimitiveInt()) {
396 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
397 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
398 for (jint i = 0; i < length; ++i) {
399 dst->Set(dstPos + i, src->Get(srcPos + i));
400 }
401 } else {
Ian Rogersc45b8b52014-05-03 01:39:59 -0700402 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
403 "Unimplemented System.arraycopy for type '%s'",
404 PrettyDescriptor(ctype).c_str());
405 }
406 } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") {
407 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
408 if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") {
409 // Allocate non-threadlocal buffer.
410 result->SetL(mirror::CharArray::Alloc(self, 11));
411 } else {
412 self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
413 "Unimplemented ThreadLocal.get");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200414 }
415 } else {
416 // Not special, continue with regular interpreter execution.
417 artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result);
418 }
419}
420
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200421// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200422#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
423 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100424 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
425 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200426 const Instruction* inst, uint16_t inst_data, \
427 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200428EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
429EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
430EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
431EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
432#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200433
434// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100435#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
436 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
437 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
438 const ShadowFrame& shadow_frame, \
439 Thread* self, JValue* result)
440#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
441 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
442 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
443 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
444 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
445EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
446EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
447#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200448#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
449
450} // namespace interpreter
451} // namespace art