blob: 36b250ca425a38af2f1f4de145345d0fe0c78f41 [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"
18
19namespace art {
20namespace interpreter {
21
22template<InvokeType type, bool is_range, bool do_access_check>
23bool DoInvoke(Thread* self, ShadowFrame& shadow_frame,
24 const Instruction* inst, JValue* result) {
Jeff Haoa3faaf42013-09-03 19:07:00 -070025 bool do_assignability_check = do_access_check;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020026 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
27 uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
28 Object* receiver = (type == kStatic) ? NULL : shadow_frame.GetVRegReference(vregC);
29 ArtMethod* method = FindMethodFromCode(method_idx, receiver, shadow_frame.GetMethod(), self,
30 do_access_check, type);
31 if (UNLIKELY(method == NULL)) {
32 CHECK(self->IsExceptionPending());
33 result->SetJ(0);
34 return false;
35 } else if (UNLIKELY(method->IsAbstract())) {
36 ThrowAbstractMethodError(method);
37 result->SetJ(0);
38 return false;
39 }
40
41 MethodHelper mh(method);
42 const DexFile::CodeItem* code_item = mh.GetCodeItem();
43 uint16_t num_regs;
44 uint16_t num_ins;
45 if (LIKELY(code_item != NULL)) {
46 num_regs = code_item->registers_size_;
47 num_ins = code_item->ins_size_;
48 } else {
49 DCHECK(method->IsNative() || method->IsProxyMethod());
50 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
51 if (!method->IsStatic()) {
52 num_regs++;
53 num_ins++;
54 }
55 }
56
57 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
58 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory));
59 size_t cur_reg = num_regs - num_ins;
60 if (receiver != NULL) {
61 new_shadow_frame->SetVRegReference(cur_reg, receiver);
62 ++cur_reg;
63 }
64
Jeff Haoa3faaf42013-09-03 19:07:00 -070065 const DexFile::TypeList* params;
66 if (do_assignability_check) {
67 params = mh.GetParameterTypeList();
68 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +020069 size_t arg_offset = (receiver == NULL) ? 0 : 1;
70 const char* shorty = mh.GetShorty();
71 uint32_t arg[5];
72 if (!is_range) {
73 inst->GetArgs(arg);
74 }
75 for (size_t shorty_pos = 0; cur_reg < num_regs; ++shorty_pos, cur_reg++, arg_offset++) {
76 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
77 size_t arg_pos = is_range ? vregC + arg_offset : arg[arg_offset];
78 switch (shorty[shorty_pos + 1]) {
79 case 'L': {
80 Object* o = shadow_frame.GetVRegReference(arg_pos);
Jeff Haoa3faaf42013-09-03 19:07:00 -070081 if (do_assignability_check && o != NULL) {
82 Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_);
83 if (arg_type == NULL) {
84 CHECK(self->IsExceptionPending());
85 return false;
86 }
87 if (!o->VerifierInstanceOf(arg_type)) {
88 // This should never happen.
89 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
90 "Ljava/lang/VirtualMachineError;",
91 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
92 mh.GetName(), shorty_pos,
93 ClassHelper(o->GetClass()).GetDescriptor(),
94 ClassHelper(arg_type).GetDescriptor());
95 return false;
96 }
97 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +020098 new_shadow_frame->SetVRegReference(cur_reg, o);
99 break;
100 }
101 case 'J': case 'D': {
102 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(arg_pos + 1)) << 32) |
103 static_cast<uint32_t>(shadow_frame.GetVReg(arg_pos));
104 new_shadow_frame->SetVRegLong(cur_reg, wide_value);
105 cur_reg++;
106 arg_offset++;
107 break;
108 }
109 default:
110 new_shadow_frame->SetVReg(cur_reg, shadow_frame.GetVReg(arg_pos));
111 break;
112 }
113 }
114
115 if (LIKELY(Runtime::Current()->IsStarted())) {
116 (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result);
117 } else {
118 UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, num_regs - num_ins);
119 }
120 return !self->IsExceptionPending();
121}
122
123template<bool is_range>
124bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
125 const Instruction* inst, JValue* result) {
126 uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
127 Object* receiver = shadow_frame.GetVRegReference(vregC);
128 if (UNLIKELY(receiver == NULL)) {
129 // We lost the reference to the method index so we cannot get a more
130 // precised exception message.
131 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
132 return false;
133 }
134 uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
135 // TODO: use ObjectArray<T>::GetWithoutChecks ?
136 ArtMethod* method = receiver->GetClass()->GetVTable()->Get(vtable_idx);
137 if (UNLIKELY(method == NULL)) {
138 CHECK(self->IsExceptionPending());
139 result->SetJ(0);
140 return false;
141 } else if (UNLIKELY(method->IsAbstract())) {
142 ThrowAbstractMethodError(method);
143 result->SetJ(0);
144 return false;
145 }
146
147 MethodHelper mh(method);
148 const DexFile::CodeItem* code_item = mh.GetCodeItem();
149 uint16_t num_regs;
150 uint16_t num_ins;
151 if (code_item != NULL) {
152 num_regs = code_item->registers_size_;
153 num_ins = code_item->ins_size_;
154 } else {
155 DCHECK(method->IsNative() || method->IsProxyMethod());
156 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
157 if (!method->IsStatic()) {
158 num_regs++;
159 num_ins++;
160 }
161 }
162
163 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
164 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame,
165 method, 0, memory));
166 size_t cur_reg = num_regs - num_ins;
167 if (receiver != NULL) {
168 new_shadow_frame->SetVRegReference(cur_reg, receiver);
169 ++cur_reg;
170 }
171
172 size_t arg_offset = (receiver == NULL) ? 0 : 1;
173 const char* shorty = mh.GetShorty();
174 uint32_t arg[5];
175 if (!is_range) {
176 inst->GetArgs(arg);
177 }
178 for (size_t shorty_pos = 0; cur_reg < num_regs; ++shorty_pos, cur_reg++, arg_offset++) {
179 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
180 size_t arg_pos = is_range ? vregC + arg_offset : arg[arg_offset];
181 switch (shorty[shorty_pos + 1]) {
182 case 'L': {
183 Object* o = shadow_frame.GetVRegReference(arg_pos);
184 new_shadow_frame->SetVRegReference(cur_reg, o);
185 break;
186 }
187 case 'J': case 'D': {
188 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(arg_pos + 1)) << 32) |
189 static_cast<uint32_t>(shadow_frame.GetVReg(arg_pos));
190 new_shadow_frame->SetVRegLong(cur_reg, wide_value);
191 cur_reg++;
192 arg_offset++;
193 break;
194 }
195 default:
196 new_shadow_frame->SetVReg(cur_reg, shadow_frame.GetVReg(arg_pos));
197 break;
198 }
199 }
200
201 if (LIKELY(Runtime::Current()->IsStarted())) {
202 (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result);
203 } else {
204 UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, num_regs - num_ins);
205 }
206 return !self->IsExceptionPending();
207}
208
209template <bool is_range, bool do_access_check>
210bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
211 Thread* self, JValue* result) {
212 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
213 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
214 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
215 if (!is_range) {
216 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
217 CHECK_LE(length, 5);
218 }
219 if (UNLIKELY(length < 0)) {
220 ThrowNegativeArraySizeException(length);
221 return false;
222 }
223 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
224 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
225 self, false, do_access_check);
226 if (UNLIKELY(arrayClass == NULL)) {
227 DCHECK(self->IsExceptionPending());
228 return false;
229 }
230 CHECK(arrayClass->IsArrayClass());
231 Class* componentClass = arrayClass->GetComponentType();
232 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
233 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
234 ThrowRuntimeException("Bad filled array request for type %s",
235 PrettyDescriptor(componentClass).c_str());
236 } else {
237 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
238 "Ljava/lang/InternalError;",
239 "Found type %s; filled-new-array not implemented for anything but \'int\'",
240 PrettyDescriptor(componentClass).c_str());
241 }
242 return false;
243 }
244 Object* newArray = Array::Alloc(self, arrayClass, length);
245 if (UNLIKELY(newArray == NULL)) {
246 DCHECK(self->IsExceptionPending());
247 return false;
248 }
249 if (is_range) {
250 uint32_t vregC = inst->VRegC_3rc();
251 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
252 for (int32_t i = 0; i < length; ++i) {
253 if (is_primitive_int_component) {
254 newArray->AsIntArray()->Set(i, shadow_frame.GetVReg(vregC + i));
255 } else {
256 newArray->AsObjectArray<Object>()->Set(i, shadow_frame.GetVRegReference(vregC + i));
257 }
258 }
259 } else {
260 uint32_t arg[5];
261 inst->GetArgs(arg);
262 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
263 for (int32_t i = 0; i < length; ++i) {
264 if (is_primitive_int_component) {
265 newArray->AsIntArray()->Set(i, shadow_frame.GetVReg(arg[i]));
266 } else {
267 newArray->AsObjectArray<Object>()->Set(i, shadow_frame.GetVRegReference(arg[i]));
268 }
269 }
270 }
271
272 result->SetL(newArray);
273 return true;
274}
275
276void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh,
277 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
278 JValue* result, size_t arg_offset) {
279 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
280 // problems in core libraries.
281 std::string name(PrettyMethod(shadow_frame->GetMethod()));
282 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
283 std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset)->AsString()->ToModifiedUtf8().c_str()));
284 ClassLoader* class_loader = NULL; // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader();
285 Class* found = Runtime::Current()->GetClassLinker()->FindClass(descriptor.c_str(),
286 class_loader);
287 CHECK(found != NULL) << "Class.forName failed in un-started runtime for class: "
288 << PrettyDescriptor(descriptor);
289 result->SetL(found);
290 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
291 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
292 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
293 CHECK(c != NULL);
294 SirtRef<Object> obj(self, klass->AllocObject(self));
295 CHECK(obj.get() != NULL);
296 EnterInterpreterFromInvoke(self, c, obj.get(), NULL, NULL);
297 result->SetL(obj.get());
298 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
299 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
300 // going the reflective Dex way.
301 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
302 String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
303 ArtField* found = NULL;
304 FieldHelper fh;
305 ObjectArray<ArtField>* fields = klass->GetIFields();
306 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
307 ArtField* f = fields->Get(i);
308 fh.ChangeField(f);
309 if (name->Equals(fh.GetName())) {
310 found = f;
311 }
312 }
313 if (found == NULL) {
314 fields = klass->GetSFields();
315 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
316 ArtField* f = fields->Get(i);
317 fh.ChangeField(f);
318 if (name->Equals(fh.GetName())) {
319 found = f;
320 }
321 }
322 }
323 CHECK(found != NULL)
324 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
325 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
326 // TODO: getDeclaredField calls GetType once the field is found to ensure a
327 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
328 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
329 SirtRef<Object> field(self, jlr_Field->AllocObject(self));
330 CHECK(field.get() != NULL);
331 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
332 uint32_t args[1];
333 args[0] = reinterpret_cast<uint32_t>(found);
334 EnterInterpreterFromInvoke(self, c, field.get(), args, NULL);
335 result->SetL(field.get());
336 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
337 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
338 // Special case array copying without initializing System.
339 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
340 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
341 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
342 jint length = shadow_frame->GetVReg(arg_offset + 4);
343 if (!ctype->IsPrimitive()) {
344 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
345 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
346 for (jint i = 0; i < length; ++i) {
347 dst->Set(dstPos + i, src->Get(srcPos + i));
348 }
349 } else if (ctype->IsPrimitiveChar()) {
350 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
351 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
352 for (jint i = 0; i < length; ++i) {
353 dst->Set(dstPos + i, src->Get(srcPos + i));
354 }
355 } else if (ctype->IsPrimitiveInt()) {
356 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
357 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
358 for (jint i = 0; i < length; ++i) {
359 dst->Set(dstPos + i, src->Get(srcPos + i));
360 }
361 } else {
362 UNIMPLEMENTED(FATAL) << "System.arraycopy of unexpected type: " << PrettyDescriptor(ctype);
363 }
364 } else {
365 // Not special, continue with regular interpreter execution.
366 artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result);
367 }
368}
369
370// Explicit DoInvoke template function declarations.
371#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range_, _check) \
372 template bool DoInvoke<_type, _is_range_, _check>(Thread* self, ShadowFrame& shadow_frame, \
373 const Instruction* inst, JValue* result)
374
375#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL_VARIANTS(_type) \
376 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
377 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
378 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
379 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true)
380
381EXPLICIT_DO_INVOKE_TEMPLATE_DECL_VARIANTS(kStatic);
382EXPLICIT_DO_INVOKE_TEMPLATE_DECL_VARIANTS(kDirect);
383EXPLICIT_DO_INVOKE_TEMPLATE_DECL_VARIANTS(kVirtual);
384EXPLICIT_DO_INVOKE_TEMPLATE_DECL_VARIANTS(kSuper);
385EXPLICIT_DO_INVOKE_TEMPLATE_DECL_VARIANTS(kInterface);
386
387#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL_VARIANTS
388#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
389
390// Explicit DoInvokeVirtualQuick template function declarations.
391#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
392template bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
393 const Instruction* inst, JValue* result)
394
395EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false);
396EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true);
397#undef EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL
398
399// Explicit DoFilledNewArray template function declarations.
400#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check) \
401 template bool DoFilledNewArray<_is_range_, _check>(const Instruction* inst, \
402 const ShadowFrame& shadow_frame, \
403 Thread* self, JValue* result)
404EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false);
405EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true);
406EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false);
407EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true);
408#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
409
410} // namespace interpreter
411} // namespace art