blob: a2209e3eab5ad8697bd96df491d3ddcd18127f06 [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
2 * Copyright (C) 2011 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 "reflection.h"
18
19#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080020#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070022#include "jni_internal.h"
Ian Rogerse5877a12014-07-16 12:06:35 -070023#include "method_helper-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_field-inl.h"
25#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070027#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object_array-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070029#include "mirror/object_array.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070030#include "nth_caller_visitor.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070032#include "stack.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070034
Elliott Hughes418d20f2011-09-22 14:00:39 -070035namespace art {
36
Ian Rogers53b8b092014-03-13 23:45:53 -070037class ArgArray {
38 public:
39 explicit ArgArray(const char* shorty, uint32_t shorty_len)
40 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
41 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
42 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
43 // We can trivially use the small arg array.
44 arg_array_ = small_arg_array_;
45 } else {
46 // Analyze shorty to see if we need the large arg array.
47 for (size_t i = 1; i < shorty_len; ++i) {
48 char c = shorty[i];
49 if (c == 'J' || c == 'D') {
50 num_slots++;
51 }
52 }
53 if (num_slots <= kSmallArgArraySize) {
54 arg_array_ = small_arg_array_;
55 } else {
56 large_arg_array_.reset(new uint32_t[num_slots]);
57 arg_array_ = large_arg_array_.get();
58 }
59 }
60 }
61
62 uint32_t* GetArray() {
63 return arg_array_;
64 }
65
66 uint32_t GetNumBytes() {
67 return num_bytes_;
68 }
69
70 void Append(uint32_t value) {
71 arg_array_[num_bytes_ / 4] = value;
72 num_bytes_ += 4;
73 }
74
75 void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
76 Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue());
77 }
78
79 void AppendWide(uint64_t value) {
80 // For ARM and MIPS portable, align wide values to 8 bytes (ArgArray starts at offset of 4).
81#if defined(ART_USE_PORTABLE_COMPILER) && (defined(__arm__) || defined(__mips__))
82 if (num_bytes_ % 8 == 0) {
83 num_bytes_ += 4;
84 }
85#endif
86 arg_array_[num_bytes_ / 4] = value;
87 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
88 num_bytes_ += 8;
89 }
90
91 void AppendFloat(float value) {
92 jvalue jv;
93 jv.f = value;
94 Append(jv.i);
95 }
96
97 void AppendDouble(double value) {
98 jvalue jv;
99 jv.d = value;
100 AppendWide(jv.j);
101 }
102
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700103 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
104 mirror::Object* receiver, va_list ap)
Ian Rogers53b8b092014-03-13 23:45:53 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
106 // Set receiver if non-null (method is not static)
107 if (receiver != nullptr) {
108 Append(receiver);
109 }
110 for (size_t i = 1; i < shorty_len_; ++i) {
111 switch (shorty_[i]) {
112 case 'Z':
113 case 'B':
114 case 'C':
115 case 'S':
116 case 'I':
117 Append(va_arg(ap, jint));
118 break;
119 case 'F':
120 AppendFloat(va_arg(ap, jdouble));
121 break;
122 case 'L':
123 Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
124 break;
125 case 'D':
126 AppendDouble(va_arg(ap, jdouble));
127 break;
128 case 'J':
129 AppendWide(va_arg(ap, jlong));
130 break;
131#ifndef NDEBUG
132 default:
133 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
134#endif
135 }
136 }
137 }
138
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700139 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
140 mirror::Object* receiver, jvalue* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
142 // Set receiver if non-null (method is not static)
143 if (receiver != nullptr) {
144 Append(receiver);
145 }
146 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
147 switch (shorty_[i]) {
148 case 'Z':
149 Append(args[args_offset].z);
150 break;
151 case 'B':
152 Append(args[args_offset].b);
153 break;
154 case 'C':
155 Append(args[args_offset].c);
156 break;
157 case 'S':
158 Append(args[args_offset].s);
159 break;
160 case 'I':
161 case 'F':
162 Append(args[args_offset].i);
163 break;
164 case 'L':
165 Append(soa.Decode<mirror::Object*>(args[args_offset].l));
166 break;
167 case 'D':
168 case 'J':
169 AppendWide(args[args_offset].j);
170 break;
171#ifndef NDEBUG
172 default:
173 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
174#endif
175 }
176 }
177 }
178
179 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
181 // Set receiver if non-null (method is not static)
182 size_t cur_arg = arg_offset;
183 if (!shadow_frame->GetMethod()->IsStatic()) {
184 Append(shadow_frame->GetVReg(cur_arg));
185 cur_arg++;
186 }
187 for (size_t i = 1; i < shorty_len_; ++i) {
188 switch (shorty_[i]) {
189 case 'Z':
190 case 'B':
191 case 'C':
192 case 'S':
193 case 'I':
194 case 'F':
195 case 'L':
196 Append(shadow_frame->GetVReg(cur_arg));
197 cur_arg++;
198 break;
199 case 'D':
200 case 'J':
201 AppendWide(shadow_frame->GetVRegLong(cur_arg));
202 cur_arg++;
203 cur_arg++;
204 break;
205#ifndef NDEBUG
206 default:
207 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
208#endif
209 }
210 }
211 }
212
213 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
214 const StringPiece& found_descriptor)
215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
216 ThrowIllegalArgumentException(nullptr,
217 StringPrintf("Invalid primitive conversion from %s to %s", expected,
218 PrettyDescriptor(found_descriptor.as_string()).c_str()).c_str());
219 }
220
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700221 bool BuildArgArrayFromObjectArray(const ScopedObjectAccessAlreadyRunnable& soa,
222 mirror::Object* receiver,
Ian Rogerse18fdd22014-03-14 13:29:43 -0700223 mirror::ObjectArray<mirror::Object>* args, MethodHelper& mh)
Ian Rogers53b8b092014-03-13 23:45:53 -0700224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700225 const DexFile::TypeList* classes = mh.GetMethod()->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700226 // Set receiver if non-null (method is not static)
227 if (receiver != nullptr) {
228 Append(receiver);
229 }
230 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
231 mirror::Object* arg = args->Get(args_offset);
232 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
233 mirror::Class* dst_class =
234 mh.GetClassFromTypeIdx(classes->GetTypeItem(args_offset).type_idx_);
235 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
236 ThrowIllegalArgumentException(nullptr,
Ian Rogers11e4c032014-03-14 12:00:39 -0700237 StringPrintf("method %s argument %zd has type %s, got %s",
Ian Rogers53b8b092014-03-13 23:45:53 -0700238 PrettyMethod(mh.GetMethod(), false).c_str(),
239 args_offset + 1, // Humans don't count from 0.
240 PrettyDescriptor(dst_class).c_str(),
241 PrettyTypeOf(arg).c_str()).c_str());
242 return false;
243 }
244 }
245
246#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700247 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Ian Rogers53b8b092014-03-13 23:45:53 -0700248 mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
249 append(primitive_field-> get_fn(arg));
250
251#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700252 } else if (LIKELY(arg != nullptr && \
253 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Ian Rogers53b8b092014-03-13 23:45:53 -0700254 mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
255 append(primitive_field-> get_fn(arg));
256
257#define DO_FAIL(expected) \
258 } else { \
259 if (arg->GetClass<>()->IsPrimitive()) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700260 ThrowIllegalPrimitiveArgumentException(expected, \
261 arg->GetClass<>()->GetDescriptor().c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700262 } else { \
263 ThrowIllegalArgumentException(nullptr, \
Ian Rogers11e4c032014-03-14 12:00:39 -0700264 StringPrintf("method %s argument %zd has type %s, got %s", \
Ian Rogers53b8b092014-03-13 23:45:53 -0700265 PrettyMethod(mh.GetMethod(), false).c_str(), \
266 args_offset + 1, \
267 expected, \
268 PrettyTypeOf(arg).c_str()).c_str()); \
269 } \
270 return false; \
271 } }
272
273 switch (shorty_[i]) {
274 case 'L':
275 Append(arg);
276 break;
277 case 'Z':
278 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
279 DO_FAIL("boolean")
280 break;
281 case 'B':
282 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
283 DO_FAIL("byte")
284 break;
285 case 'C':
286 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
287 DO_FAIL("char")
288 break;
289 case 'S':
290 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
291 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
292 DO_FAIL("short")
293 break;
294 case 'I':
295 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
296 DO_ARG("Ljava/lang/Character;", GetChar, Append)
297 DO_ARG("Ljava/lang/Short;", GetShort, Append)
298 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
299 DO_FAIL("int")
300 break;
301 case 'J':
302 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
303 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
304 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
305 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
306 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
307 DO_FAIL("long")
308 break;
309 case 'F':
310 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
311 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
312 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
313 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
314 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
315 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
316 DO_FAIL("float")
317 break;
318 case 'D':
319 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
320 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
321 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
322 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
323 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
324 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
325 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
326 DO_FAIL("double")
327 break;
328#ifndef NDEBUG
329 default:
330 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
331#endif
332 }
333#undef DO_FIRST_ARG
334#undef DO_ARG
335#undef DO_FAIL
336 }
337 return true;
338 }
339
340 private:
341 enum { kSmallArgArraySize = 16 };
342 const char* const shorty_;
343 const uint32_t shorty_len_;
344 uint32_t num_bytes_;
345 uint32_t* arg_array_;
346 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700347 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700348};
349
Ian Rogers68d8b422014-07-17 11:09:10 -0700350static void CheckMethodArguments(JavaVMExt* vm, mirror::ArtMethod* m, uint32_t* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700352 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700353 if (params == nullptr) {
354 return; // No arguments so nothing to check.
355 }
356 uint32_t offset = 0;
357 uint32_t num_params = params->Size();
358 size_t error_count = 0;
359 if (!m->IsStatic()) {
360 offset = 1;
361 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700362 // TODO: If args contain object references, it may cause problems
363 Thread* self = Thread::Current();
364 StackHandleScope<1> hs(self);
365 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
366 MethodHelper mh(h_m);
Ian Rogers53b8b092014-03-13 23:45:53 -0700367 for (uint32_t i = 0; i < num_params; i++) {
368 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700369 mirror::Class* param_type = mh.GetClassFromTypeIdx(type_idx);
Ian Rogers53b8b092014-03-13 23:45:53 -0700370 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700371 CHECK(self->IsExceptionPending());
372 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700373 << h_m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers53b8b092014-03-13 23:45:53 -0700374 << self->GetException(nullptr)->Dump();
375 self->ClearException();
376 ++error_count;
377 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700378 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
379 // this is a hard to fix problem since the args can contain Object*, we need to save and
380 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers68d8b422014-07-17 11:09:10 -0700381 mirror::Object* argument =
382 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700383 if (argument != nullptr && !argument->InstanceOf(param_type)) {
384 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
385 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700386 << " to " << PrettyMethod(h_m.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700387 ++error_count;
388 }
389 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
390 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700391 } else {
392 int32_t arg = static_cast<int32_t>(args[i + offset]);
393 if (param_type->IsPrimitiveBoolean()) {
394 if (arg != JNI_TRUE && arg != JNI_FALSE) {
395 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
396 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
397 ++error_count;
398 }
399 } else if (param_type->IsPrimitiveByte()) {
400 if (arg < -128 || arg > 127) {
401 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
402 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
403 ++error_count;
404 }
405 } else if (param_type->IsPrimitiveChar()) {
406 if (args[i + offset] > 0xFFFF) {
407 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
408 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
409 ++error_count;
410 }
411 } else if (param_type->IsPrimitiveShort()) {
412 if (arg < -32768 || arg > 0x7FFF) {
413 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
414 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
415 ++error_count;
416 }
417 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700418 }
419 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700420 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700421 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
422 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700423 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
424 PrettyMethod(h_m.Get()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700425 }
426}
427
428static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver,
429 mirror::ArtMethod* method)
430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
431 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
432}
433
434
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700435static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
436 mirror::ArtMethod* method, ArgArray* arg_array, JValue* result,
437 const char* shorty)
Ian Rogers53b8b092014-03-13 23:45:53 -0700438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
439 uint32_t* args = arg_array->GetArray();
440 if (UNLIKELY(soa.Env()->check_jni)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700441 CheckMethodArguments(soa.Vm(), method, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700442 }
443 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
444}
445
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700446JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
447 va_list args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700448 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700449 // We want to make sure that the stack is not within a small distance from the
450 // protected region in case we are calling into a leaf function whose stack
451 // check has been elided.
452 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
453 ThrowStackOverflowError(soa.Self());
454 return JValue();
455 }
456
Ian Rogers53b8b092014-03-13 23:45:53 -0700457 mirror::ArtMethod* method = soa.DecodeMethod(mid);
458 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700459 uint32_t shorty_len = 0;
460 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700461 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700462 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700463 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700464 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700465 return result;
466}
467
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700468JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, mirror::Object* receiver,
Ian Rogers53b8b092014-03-13 23:45:53 -0700469 jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700470 // We want to make sure that the stack is not within a small distance from the
471 // protected region in case we are calling into a leaf function whose stack
472 // check has been elided.
473 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
474 ThrowStackOverflowError(soa.Self());
475 return JValue();
476 }
477
Ian Rogers53b8b092014-03-13 23:45:53 -0700478 mirror::ArtMethod* method = soa.DecodeMethod(mid);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479 uint32_t shorty_len = 0;
480 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700481 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700482 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700483 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700484 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700485 return result;
486}
487
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700488JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700489 mirror::Object* receiver, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700490 // We want to make sure that the stack is not within a small distance from the
491 // protected region in case we are calling into a leaf function whose stack
492 // check has been elided.
493 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
494 ThrowStackOverflowError(soa.Self());
495 return JValue();
496 }
497
Ian Rogers53b8b092014-03-13 23:45:53 -0700498 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499 uint32_t shorty_len = 0;
500 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700501 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700502 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700503 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700504 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700505 return result;
506}
507
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700508JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700509 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700510 // We want to make sure that the stack is not within a small distance from the
511 // protected region in case we are calling into a leaf function whose stack
512 // check has been elided.
513 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
514 ThrowStackOverflowError(soa.Self());
515 return JValue();
516 }
517
Ian Rogers53b8b092014-03-13 23:45:53 -0700518 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
519 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700520 uint32_t shorty_len = 0;
521 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700522 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700523 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700524 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700525 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700526 return result;
527}
528
529void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset,
530 MethodHelper& mh, JValue* result) {
Dave Allison648d7112014-07-25 16:15:27 -0700531 // We want to make sure that the stack is not within a small distance from the
532 // protected region in case we are calling into a leaf function whose stack
533 // check has been elided.
534 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
535 ThrowStackOverflowError(self);
536 return;
537 }
538
Ian Rogers53b8b092014-03-13 23:45:53 -0700539 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
540 arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);
541 shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result,
542 mh.GetShorty());
543}
544
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700545jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700546 jobject javaReceiver, jobject javaArgs, bool accessible) {
Dave Allison648d7112014-07-25 16:15:27 -0700547 // We want to make sure that the stack is not within a small distance from the
548 // protected region in case we are calling into a leaf function whose stack
549 // check has been elided.
550 if (UNLIKELY(__builtin_frame_address(0) <
551 soa.Self()->GetStackEndForInterpreter(true))) {
552 ThrowStackOverflowError(soa.Self());
553 return nullptr;
554 }
555
Ian Rogers62f05122014-03-21 11:21:29 -0700556 mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700557
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800559 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700560 StackHandleScope<1> hs(soa.Self());
561 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
562 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800563 return nullptr;
564 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700565 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700566 }
567
Ian Rogers53b8b092014-03-13 23:45:53 -0700568 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700569 if (!m->IsStatic()) {
570 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Ian Rogers53b8b092014-03-13 23:45:53 -0700572 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700573 return NULL;
574 }
575
576 // Find the actual implementation of the virtual method.
577 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
578 }
579
580 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581 mirror::ObjectArray<mirror::Object>* objects =
582 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700583 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700584 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
585 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800586 if (arg_count != classes_size) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800587 ThrowIllegalArgumentException(NULL,
588 StringPrintf("Wrong number of arguments; expected %d, got %d",
589 classes_size, arg_count).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700590 return NULL;
591 }
592
Jeff Haocb4581a2014-03-28 15:43:37 -0700593 // If method is not set to be accessible, verify it can be accessed by the caller.
594 if (!accessible && !VerifyAccess(receiver, declaring_class, m->GetAccessFlags())) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700595 ThrowIllegalAccessException(nullptr, StringPrintf("Cannot access method: %s",
596 PrettyMethod(m).c_str()).c_str());
597 return nullptr;
598 }
599
Ian Rogers53b8b092014-03-13 23:45:53 -0700600 // Invoke the method.
601 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700602 uint32_t shorty_len = 0;
603 const char* shorty = m->GetShorty(&shorty_len);
604 ArgArray arg_array(shorty, shorty_len);
605 StackHandleScope<1> hs(soa.Self());
606 MethodHelper mh(hs.NewHandle(m));
Ian Rogerse18fdd22014-03-14 13:29:43 -0700607 if (!arg_array.BuildArgArrayFromObjectArray(soa, receiver, objects, mh)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700608 CHECK(soa.Self()->IsExceptionPending());
609 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700610 }
611
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700612 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700613
614 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 if (soa.Self()->IsExceptionPending()) {
616 jthrowable th = soa.Env()->ExceptionOccurred();
617 soa.Env()->ExceptionClear();
618 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
619 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
620 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
621 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700622 return NULL;
623 }
624
625 // Box if necessary and return.
Ian Rogers53b8b092014-03-13 23:45:53 -0700626 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(),
627 result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700628}
629
Ian Rogers53b8b092014-03-13 23:45:53 -0700630bool VerifyObjectIsClass(mirror::Object* o, mirror::Class* c) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700631 if (o == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800632 ThrowNullPointerException(NULL, "null receiver");
633 return false;
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700634 } else if (!o->InstanceOf(c)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700635 std::string expected_class_name(PrettyDescriptor(c));
636 std::string actual_class_name(PrettyTypeOf(o));
Ian Rogers62d6c772013-02-27 08:32:07 -0800637 ThrowIllegalArgumentException(NULL,
638 StringPrintf("Expected receiver of type %s, but got %s",
639 expected_class_name.c_str(),
640 actual_class_name.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700641 return false;
642 }
643 return true;
644}
645
Ian Rogers62d6c772013-02-27 08:32:07 -0800646bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result,
647 Primitive::Type srcType, Primitive::Type dstType,
Ian Rogers84956ff2014-03-26 23:52:41 -0700648 const JValue& src, JValue* dst) {
649 DCHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
650 if (LIKELY(srcType == dstType)) {
651 dst->SetJ(src.GetJ());
652 return true;
653 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700654 switch (dstType) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700655 case Primitive::kPrimBoolean: // Fall-through.
656 case Primitive::kPrimChar: // Fall-through.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700657 case Primitive::kPrimByte:
Ian Rogers84956ff2014-03-26 23:52:41 -0700658 // Only expect assignment with source and destination of identical type.
Elliott Hughes418d20f2011-09-22 14:00:39 -0700659 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700660 case Primitive::kPrimShort:
Ian Rogers84956ff2014-03-26 23:52:41 -0700661 if (srcType == Primitive::kPrimByte) {
662 dst->SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700663 return true;
664 }
665 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700666 case Primitive::kPrimInt:
667 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
Ian Rogers84956ff2014-03-26 23:52:41 -0700668 srcType == Primitive::kPrimShort) {
669 dst->SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700670 return true;
671 }
672 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700673 case Primitive::kPrimLong:
674 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
675 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700676 dst->SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700677 return true;
678 }
679 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700680 case Primitive::kPrimFloat:
681 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
682 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700683 dst->SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700684 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700685 } else if (srcType == Primitive::kPrimLong) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700686 dst->SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700687 return true;
688 }
689 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700690 case Primitive::kPrimDouble:
691 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
692 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700693 dst->SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700694 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700695 } else if (srcType == Primitive::kPrimLong) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700696 dst->SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700697 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700698 } else if (srcType == Primitive::kPrimFloat) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700699 dst->SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700700 return true;
701 }
702 break;
703 default:
704 break;
705 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800706 if (!unbox_for_result) {
707 ThrowIllegalArgumentException(throw_location,
708 StringPrintf("Invalid primitive conversion from %s to %s",
709 PrettyDescriptor(srcType).c_str(),
710 PrettyDescriptor(dstType).c_str()).c_str());
711 } else {
712 ThrowClassCastException(throw_location,
713 StringPrintf("Couldn't convert result of type %s to %s",
714 PrettyDescriptor(srcType).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700715 PrettyDescriptor(dstType).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700717 return false;
718}
719
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800720mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700721 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800722 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700723 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700724 if (src_class == Primitive::kPrimVoid) {
725 // There's no such thing as a void field, and void methods invoked via reflection return null.
726 return nullptr;
727 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700728
Ian Rogers84956ff2014-03-26 23:52:41 -0700729 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800730 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700731 switch (src_class) {
732 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700733 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800734 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700735 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700736 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800738 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700739 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700740 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800742 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700743 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700744 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800746 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700747 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700748 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700749 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800750 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700751 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700752 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800754 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700755 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700756 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800758 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700759 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700760 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700761 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800762 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700763 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700764 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700765 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800766 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700767 }
768
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700769 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700770 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800771
Ian Rogers53b8b092014-03-13 23:45:53 -0700772 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800773 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800774 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
775 arg_array.AppendWide(value.GetJ());
776 } else {
777 arg_array.Append(value.GetI());
778 }
779
780 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800781 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800782 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700783}
784
Ian Rogers84956ff2014-03-26 23:52:41 -0700785static std::string UnboxingFailureKind(mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700786 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700787 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700788 return "field " + PrettyField(f, false);
789 }
790 return "result";
791}
792
Ian Rogers62d6c772013-02-27 08:32:07 -0800793static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700794 mirror::Class* dst_class, mirror::ArtField* f,
795 JValue* unboxed_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700796 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700797 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700798 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700799 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800800 if (!unbox_for_result) {
801 ThrowIllegalArgumentException(throw_location,
802 StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700803 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800804 PrettyDescriptor(dst_class).c_str(),
805 PrettyTypeOf(o).c_str()).c_str());
806 } else {
807 ThrowClassCastException(throw_location,
808 StringPrintf("Couldn't convert result of type %s to %s",
809 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700810 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800811 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700812 return false;
813 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700814 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700815 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800816 }
817 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
818 ThrowIllegalArgumentException(throw_location,
819 StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700820 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700821 return false;
822 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700823 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800824 if (!unbox_for_result) {
825 ThrowIllegalArgumentException(throw_location,
826 StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700827 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800828 PrettyDescriptor(dst_class).c_str()).c_str());
829 } else {
830 ThrowNullPointerException(throw_location,
831 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
832 PrettyDescriptor(dst_class).c_str()).c_str());
833 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700834 return false;
835 }
836
Elliott Hughes1d878f32012-04-11 15:17:54 -0700837 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700838 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700839 mirror::Class* src_class = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700840 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700841 mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700842 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700843 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700844 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700845 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700846 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700847 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700848 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700849 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700850 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700851 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700852 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700853 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700854 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700855 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700856 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700857 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700858 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700859 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700860 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700861 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700862 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700863 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700864 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700865 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700866 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800867 ThrowIllegalArgumentException(throw_location,
868 StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700869 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800870 PrettyDescriptor(dst_class).c_str(),
Mathieu Chartierf8322842014-05-16 10:59:25 -0700871 PrettyDescriptor(o->GetClass()->GetDescriptor()).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700872 return false;
873 }
874
Ian Rogers62d6c772013-02-27 08:32:07 -0800875 return ConvertPrimitiveValue(throw_location, unbox_for_result,
876 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700877 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700878}
879
Ian Rogers84956ff2014-03-26 23:52:41 -0700880bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, mirror::ArtField* f,
881 JValue* unboxed_value) {
882 DCHECK(f != nullptr);
883 return UnboxPrimitive(nullptr, o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700884}
885
Ian Rogers62d6c772013-02-27 08:32:07 -0800886bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700887 mirror::Class* dst_class, JValue* unboxed_value) {
888 return UnboxPrimitive(&throw_location, o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700889}
890
Jeff Haocb4581a2014-03-28 15:43:37 -0700891bool VerifyAccess(mirror::Object* obj, mirror::Class* declaring_class, uint32_t access_flags) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700892 NthCallerVisitor visitor(Thread::Current(), 2);
893 visitor.WalkStack();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100894 if (UNLIKELY(visitor.caller == nullptr)) {
895 // The caller is an attached native thread.
896 return (access_flags & kAccPublic) != 0;
897 }
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700898 mirror::Class* caller_class = visitor.caller->GetDeclaringClass();
899
Jeff Hao925b6872014-04-01 11:21:30 -0700900 if (((access_flags & kAccPublic) != 0) || (caller_class == declaring_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700901 return true;
902 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700903 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700904 return false;
905 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700906 if ((access_flags & kAccProtected) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700907 if (obj != nullptr && !obj->InstanceOf(caller_class) &&
908 !declaring_class->IsInSamePackage(caller_class)) {
909 return false;
910 } else if (declaring_class->IsAssignableFrom(caller_class)) {
911 return true;
912 }
913 }
914 if (!declaring_class->IsInSamePackage(caller_class)) {
915 return false;
916 }
917 return true;
918}
919
Elliott Hughes418d20f2011-09-22 14:00:39 -0700920} // namespace art