blob: a54a39d5d6b3578db8f465815fc5caac18e2ed5e [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
Mathieu Chartier76433272014-09-26 14:32:37 -070017#include "reflection-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070018
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"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include "entrypoints/entrypoint_utils.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070023#include "jni_internal.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) {
Ian Rogers53b8b092014-03-13 23:45:53 -070080 arg_array_[num_bytes_ / 4] = value;
81 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
82 num_bytes_ += 8;
83 }
84
85 void AppendFloat(float value) {
86 jvalue jv;
87 jv.f = value;
88 Append(jv.i);
89 }
90
91 void AppendDouble(double value) {
92 jvalue jv;
93 jv.d = value;
94 AppendWide(jv.j);
95 }
96
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070097 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
98 mirror::Object* receiver, va_list ap)
Ian Rogers53b8b092014-03-13 23:45:53 -070099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100 // Set receiver if non-null (method is not static)
101 if (receiver != nullptr) {
102 Append(receiver);
103 }
104 for (size_t i = 1; i < shorty_len_; ++i) {
105 switch (shorty_[i]) {
106 case 'Z':
107 case 'B':
108 case 'C':
109 case 'S':
110 case 'I':
111 Append(va_arg(ap, jint));
112 break;
113 case 'F':
114 AppendFloat(va_arg(ap, jdouble));
115 break;
116 case 'L':
117 Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
118 break;
119 case 'D':
120 AppendDouble(va_arg(ap, jdouble));
121 break;
122 case 'J':
123 AppendWide(va_arg(ap, jlong));
124 break;
125#ifndef NDEBUG
126 default:
127 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
128#endif
129 }
130 }
131 }
132
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700133 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
134 mirror::Object* receiver, jvalue* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136 // Set receiver if non-null (method is not static)
137 if (receiver != nullptr) {
138 Append(receiver);
139 }
140 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
141 switch (shorty_[i]) {
142 case 'Z':
143 Append(args[args_offset].z);
144 break;
145 case 'B':
146 Append(args[args_offset].b);
147 break;
148 case 'C':
149 Append(args[args_offset].c);
150 break;
151 case 'S':
152 Append(args[args_offset].s);
153 break;
154 case 'I':
155 case 'F':
156 Append(args[args_offset].i);
157 break;
158 case 'L':
159 Append(soa.Decode<mirror::Object*>(args[args_offset].l));
160 break;
161 case 'D':
162 case 'J':
163 AppendWide(args[args_offset].j);
164 break;
165#ifndef NDEBUG
166 default:
167 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
168#endif
169 }
170 }
171 }
172
173 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
175 // Set receiver if non-null (method is not static)
176 size_t cur_arg = arg_offset;
177 if (!shadow_frame->GetMethod()->IsStatic()) {
178 Append(shadow_frame->GetVReg(cur_arg));
179 cur_arg++;
180 }
181 for (size_t i = 1; i < shorty_len_; ++i) {
182 switch (shorty_[i]) {
183 case 'Z':
184 case 'B':
185 case 'C':
186 case 'S':
187 case 'I':
188 case 'F':
189 case 'L':
190 Append(shadow_frame->GetVReg(cur_arg));
191 cur_arg++;
192 break;
193 case 'D':
194 case 'J':
195 AppendWide(shadow_frame->GetVRegLong(cur_arg));
196 cur_arg++;
197 cur_arg++;
198 break;
199#ifndef NDEBUG
200 default:
201 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
202#endif
203 }
204 }
205 }
206
207 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700208 const char* found_descriptor)
Ian Rogers53b8b092014-03-13 23:45:53 -0700209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000210 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700211 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700213 }
214
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700215 bool BuildArgArrayFromObjectArray(mirror::Object* receiver,
Ian Rogersa0485602014-12-02 15:48:04 -0800216 mirror::ObjectArray<mirror::Object>* args,
217 Handle<mirror::ArtMethod> h_m)
Ian Rogers53b8b092014-03-13 23:45:53 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa0485602014-12-02 15:48:04 -0800219 const DexFile::TypeList* classes = h_m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700220 // Set receiver if non-null (method is not static)
221 if (receiver != nullptr) {
222 Append(receiver);
223 }
224 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
225 mirror::Object* arg = args->Get(args_offset);
226 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
227 mirror::Class* dst_class =
Ian Rogersa0485602014-12-02 15:48:04 -0800228 h_m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700229 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000230 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700231 StringPrintf("method %s argument %zd has type %s, got %s",
Ian Rogersa0485602014-12-02 15:48:04 -0800232 PrettyMethod(h_m.Get(), false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700233 args_offset + 1, // Humans don't count from 0.
234 PrettyDescriptor(dst_class).c_str(),
235 PrettyTypeOf(arg).c_str()).c_str());
236 return false;
237 }
238 }
239
240#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700241 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Ian Rogers53b8b092014-03-13 23:45:53 -0700242 mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
243 append(primitive_field-> get_fn(arg));
244
245#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700246 } else if (LIKELY(arg != nullptr && \
247 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_FAIL(expected) \
252 } else { \
253 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700254 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700255 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700256 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700257 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000258 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700259 StringPrintf("method %s argument %zd has type %s, got %s", \
Ian Rogersa0485602014-12-02 15:48:04 -0800260 PrettyMethod(h_m.Get(), false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700261 args_offset + 1, \
262 expected, \
263 PrettyTypeOf(arg).c_str()).c_str()); \
264 } \
265 return false; \
266 } }
267
268 switch (shorty_[i]) {
269 case 'L':
270 Append(arg);
271 break;
272 case 'Z':
273 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
274 DO_FAIL("boolean")
275 break;
276 case 'B':
277 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
278 DO_FAIL("byte")
279 break;
280 case 'C':
281 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
282 DO_FAIL("char")
283 break;
284 case 'S':
285 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
286 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
287 DO_FAIL("short")
288 break;
289 case 'I':
290 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
291 DO_ARG("Ljava/lang/Character;", GetChar, Append)
292 DO_ARG("Ljava/lang/Short;", GetShort, Append)
293 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
294 DO_FAIL("int")
295 break;
296 case 'J':
297 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
298 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
299 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
300 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
301 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
302 DO_FAIL("long")
303 break;
304 case 'F':
305 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
306 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
307 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
308 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
309 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
310 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
311 DO_FAIL("float")
312 break;
313 case 'D':
314 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
315 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
316 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
317 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
318 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
319 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
320 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
321 DO_FAIL("double")
322 break;
323#ifndef NDEBUG
324 default:
325 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800326 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700327#endif
328 }
329#undef DO_FIRST_ARG
330#undef DO_ARG
331#undef DO_FAIL
332 }
333 return true;
334 }
335
336 private:
337 enum { kSmallArgArraySize = 16 };
338 const char* const shorty_;
339 const uint32_t shorty_len_;
340 uint32_t num_bytes_;
341 uint32_t* arg_array_;
342 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700343 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700344};
345
Ian Rogers68d8b422014-07-17 11:09:10 -0700346static void CheckMethodArguments(JavaVMExt* vm, mirror::ArtMethod* m, uint32_t* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700347 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700348 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700349 if (params == nullptr) {
350 return; // No arguments so nothing to check.
351 }
352 uint32_t offset = 0;
353 uint32_t num_params = params->Size();
354 size_t error_count = 0;
355 if (!m->IsStatic()) {
356 offset = 1;
357 }
Ian Rogersa0485602014-12-02 15:48:04 -0800358 // TODO: If args contain object references, it may cause problems.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700359 Thread* self = Thread::Current();
360 StackHandleScope<1> hs(self);
361 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
Ian Rogers53b8b092014-03-13 23:45:53 -0700362 for (uint32_t i = 0; i < num_params; i++) {
363 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Ian Rogersa0485602014-12-02 15:48:04 -0800364 mirror::Class* param_type = h_m->GetClassFromTypeIndex(type_idx, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700365 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700366 CHECK(self->IsExceptionPending());
367 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700368 << h_m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000369 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700370 self->ClearException();
371 ++error_count;
372 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700373 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
374 // this is a hard to fix problem since the args can contain Object*, we need to save and
375 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers68d8b422014-07-17 11:09:10 -0700376 mirror::Object* argument =
377 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700378 if (argument != nullptr && !argument->InstanceOf(param_type)) {
379 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
380 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700381 << " to " << PrettyMethod(h_m.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700382 ++error_count;
383 }
384 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
385 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700386 } else {
387 int32_t arg = static_cast<int32_t>(args[i + offset]);
388 if (param_type->IsPrimitiveBoolean()) {
389 if (arg != JNI_TRUE && arg != JNI_FALSE) {
390 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
391 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
392 ++error_count;
393 }
394 } else if (param_type->IsPrimitiveByte()) {
395 if (arg < -128 || arg > 127) {
396 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
397 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
398 ++error_count;
399 }
400 } else if (param_type->IsPrimitiveChar()) {
401 if (args[i + offset] > 0xFFFF) {
402 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
403 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
404 ++error_count;
405 }
406 } else if (param_type->IsPrimitiveShort()) {
407 if (arg < -32768 || arg > 0x7FFF) {
408 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
409 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
410 ++error_count;
411 }
412 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700413 }
414 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700415 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700416 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
417 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700418 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
419 PrettyMethod(h_m.Get()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700420 }
421}
422
423static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver,
424 mirror::ArtMethod* method)
425 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
426 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
427}
428
429
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700430static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
431 mirror::ArtMethod* method, ArgArray* arg_array, JValue* result,
432 const char* shorty)
Ian Rogers53b8b092014-03-13 23:45:53 -0700433 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
434 uint32_t* args = arg_array->GetArray();
435 if (UNLIKELY(soa.Env()->check_jni)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700436 CheckMethodArguments(soa.Vm(), method, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700437 }
438 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
439}
440
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700441JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
442 va_list args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700443 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700444 // We want to make sure that the stack is not within a small distance from the
445 // protected region in case we are calling into a leaf function whose stack
446 // check has been elided.
447 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
448 ThrowStackOverflowError(soa.Self());
449 return JValue();
450 }
451
Ian Rogers53b8b092014-03-13 23:45:53 -0700452 mirror::ArtMethod* method = soa.DecodeMethod(mid);
453 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700454 uint32_t shorty_len = 0;
455 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700456 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700457 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700458 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700459 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700460 return result;
461}
462
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700463JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, mirror::Object* receiver,
Ian Rogers53b8b092014-03-13 23:45:53 -0700464 jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700465 // We want to make sure that the stack is not within a small distance from the
466 // protected region in case we are calling into a leaf function whose stack
467 // check has been elided.
468 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
469 ThrowStackOverflowError(soa.Self());
470 return JValue();
471 }
472
Ian Rogers53b8b092014-03-13 23:45:53 -0700473 mirror::ArtMethod* method = soa.DecodeMethod(mid);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700474 uint32_t shorty_len = 0;
475 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700476 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700477 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700478 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700480 return result;
481}
482
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700483JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700484 mirror::Object* receiver, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700485 // We want to make sure that the stack is not within a small distance from the
486 // protected region in case we are calling into a leaf function whose stack
487 // check has been elided.
488 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
489 ThrowStackOverflowError(soa.Self());
490 return JValue();
491 }
492
Ian Rogers53b8b092014-03-13 23:45:53 -0700493 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700494 uint32_t shorty_len = 0;
495 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700496 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700497 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700498 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700500 return result;
501}
502
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700503JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700504 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700505 // We want to make sure that the stack is not within a small distance from the
506 // protected region in case we are calling into a leaf function whose stack
507 // check has been elided.
508 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
509 ThrowStackOverflowError(soa.Self());
510 return JValue();
511 }
512
Ian Rogers53b8b092014-03-13 23:45:53 -0700513 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
514 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700515 uint32_t shorty_len = 0;
516 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700517 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700518 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700519 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700520 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700521 return result;
522}
523
524void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset,
Ian Rogerse94652f2014-12-02 11:13:19 -0800525 JValue* result) {
Dave Allison648d7112014-07-25 16:15:27 -0700526 // We want to make sure that the stack is not within a small distance from the
527 // protected region in case we are calling into a leaf function whose stack
528 // check has been elided.
529 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
530 ThrowStackOverflowError(self);
531 return;
532 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800533 uint32_t shorty_len;
534 const char* shorty = shadow_frame->GetMethod()->GetShorty(&shorty_len);
535 ArgArray arg_array(shorty, shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700536 arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);
537 shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result,
Ian Rogerse94652f2014-12-02 11:13:19 -0800538 shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700539}
540
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700541jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700542 jobject javaReceiver, jobject javaArgs, bool accessible) {
Dave Allison648d7112014-07-25 16:15:27 -0700543 // We want to make sure that the stack is not within a small distance from the
544 // protected region in case we are calling into a leaf function whose stack
545 // check has been elided.
546 if (UNLIKELY(__builtin_frame_address(0) <
547 soa.Self()->GetStackEndForInterpreter(true))) {
548 ThrowStackOverflowError(soa.Self());
549 return nullptr;
550 }
551
Ian Rogers62f05122014-03-21 11:21:29 -0700552 mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700553
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800555 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700556 StackHandleScope<1> hs(soa.Self());
557 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700558 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800559 return nullptr;
560 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700561 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700562 }
563
Ian Rogers53b8b092014-03-13 23:45:53 -0700564 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700565 if (!m->IsStatic()) {
566 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Ian Rogers53b8b092014-03-13 23:45:53 -0700568 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Ian Rogersa0485602014-12-02 15:48:04 -0800569 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700570 }
571
572 // Find the actual implementation of the virtual method.
573 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
574 }
575
576 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 mirror::ObjectArray<mirror::Object>* objects =
578 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700579 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700580 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
581 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800582 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000583 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800584 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800585 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700586 }
587
Jeff Haocb4581a2014-03-28 15:43:37 -0700588 // If method is not set to be accessible, verify it can be accessed by the caller.
Andreas Gampec0d82292014-09-23 10:38:30 -0700589 mirror::Class* calling_class = nullptr;
590 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
591 &calling_class)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000592 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700593 StringPrintf("Class %s cannot access %s method %s of class %s",
594 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
595 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
596 PrettyMethod(m).c_str(),
597 m->GetDeclaringClass() == nullptr ? "null" :
598 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700599 return nullptr;
600 }
601
Ian Rogers53b8b092014-03-13 23:45:53 -0700602 // Invoke the method.
603 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700604 uint32_t shorty_len = 0;
605 const char* shorty = m->GetShorty(&shorty_len);
606 ArgArray arg_array(shorty, shorty_len);
607 StackHandleScope<1> hs(soa.Self());
Ian Rogersa0485602014-12-02 15:48:04 -0800608 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
609 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, h_m)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700610 CHECK(soa.Self()->IsExceptionPending());
611 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700612 }
613
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700614 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700615
616 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 if (soa.Self()->IsExceptionPending()) {
618 jthrowable th = soa.Env()->ExceptionOccurred();
619 soa.Env()->ExceptionClear();
620 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
621 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
622 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
623 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800624 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700625 }
626
627 // Box if necessary and return.
Ian Rogersa0485602014-12-02 15:48:04 -0800628 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700629}
630
Ian Rogers53b8b092014-03-13 23:45:53 -0700631bool VerifyObjectIsClass(mirror::Object* o, mirror::Class* c) {
Ian Rogersa0485602014-12-02 15:48:04 -0800632 if (o == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000633 ThrowNullPointerException("null receiver");
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 return false;
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700635 } else if (!o->InstanceOf(c)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700636 std::string expected_class_name(PrettyDescriptor(c));
637 std::string actual_class_name(PrettyTypeOf(o));
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000638 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800646mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700647 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800648 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700649 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700650 if (src_class == Primitive::kPrimVoid) {
651 // There's no such thing as a void field, and void methods invoked via reflection return null.
652 return nullptr;
653 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700654
Ian Rogers84956ff2014-03-26 23:52:41 -0700655 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800656 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700657 switch (src_class) {
658 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800660 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700661 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700662 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800664 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700665 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700666 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700667 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800668 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700669 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700670 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800672 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700673 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700674 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800676 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700677 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700678 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800680 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700681 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700682 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800684 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700685 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700686 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800688 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700689 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700690 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700691 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800692 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700693 }
694
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700696 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800697
Ian Rogers53b8b092014-03-13 23:45:53 -0700698 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800699 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800700 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
701 arg_array.AppendWide(value.GetJ());
702 } else {
703 arg_array.Append(value.GetI());
704 }
705
706 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800707 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800708 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700709}
710
Ian Rogers84956ff2014-03-26 23:52:41 -0700711static std::string UnboxingFailureKind(mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700712 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700713 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700714 return "field " + PrettyField(f, false);
715 }
716 return "result";
717}
718
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000719static bool UnboxPrimitive(mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700720 mirror::Class* dst_class, mirror::ArtField* f,
721 JValue* unboxed_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700722 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700723 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700724 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700725 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800726 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000727 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700728 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800729 PrettyDescriptor(dst_class).c_str(),
730 PrettyTypeOf(o).c_str()).c_str());
731 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000732 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800733 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700734 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800735 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700736 return false;
737 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700738 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700739 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800740 }
741 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000742 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700743 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700744 return false;
745 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700746 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800747 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000748 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700749 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 PrettyDescriptor(dst_class).c_str()).c_str());
751 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000752 ThrowNullPointerException(StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
Ian Rogers62d6c772013-02-27 08:32:07 -0800753 PrettyDescriptor(dst_class).c_str()).c_str());
754 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700755 return false;
756 }
757
Elliott Hughes1d878f32012-04-11 15:17:54 -0700758 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700759 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700760 mirror::Class* src_class = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700761 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700762 mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700763 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700764 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700765 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700766 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700767 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700768 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700769 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700770 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700771 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700772 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700773 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700774 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700775 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700776 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700777 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700778 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700779 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700780 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700781 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700782 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700783 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700784 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700785 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700786 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700787 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700788 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000789 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700790 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
791 PrettyDescriptor(dst_class).c_str(),
792 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700793 return false;
794 }
795
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000796 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800797 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700798 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700799}
800
Ian Rogers84956ff2014-03-26 23:52:41 -0700801bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, mirror::ArtField* f,
802 JValue* unboxed_value) {
803 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000804 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700805}
806
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000807bool UnboxPrimitiveForResult(mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700808 mirror::Class* dst_class, JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000809 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700810}
811
Andreas Gampec0d82292014-09-23 10:38:30 -0700812bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
813 uint32_t access_flags, mirror::Class** calling_class) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700814 if ((access_flags & kAccPublic) != 0) {
815 return true;
816 }
817 NthCallerVisitor visitor(self, 2);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700818 visitor.WalkStack();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100819 if (UNLIKELY(visitor.caller == nullptr)) {
820 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700821 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100822 }
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700823 mirror::Class* caller_class = visitor.caller->GetDeclaringClass();
Mathieu Chartier76433272014-09-26 14:32:37 -0700824 if (caller_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700825 return true;
826 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700827 ScopedAssertNoThreadSuspension sants(self, "verify-access");
828 *calling_class = caller_class;
Jeff Haocb4581a2014-03-28 15:43:37 -0700829 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700830 return false;
831 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700832 if ((access_flags & kAccProtected) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700833 if (obj != nullptr && !obj->InstanceOf(caller_class) &&
834 !declaring_class->IsInSamePackage(caller_class)) {
835 return false;
836 } else if (declaring_class->IsAssignableFrom(caller_class)) {
837 return true;
838 }
839 }
Mathieu Chartier76433272014-09-26 14:32:37 -0700840 return declaring_class->IsInSamePackage(caller_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700841}
842
Elliott Hughes418d20f2011-09-22 14:00:39 -0700843} // namespace art