blob: f8c70815b2e16a0af88a5cc1e589abe32f046218 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070020#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/entrypoint_utils.h"
Jeff Hao39b6c242015-05-19 20:30:23 -070024#include "indirect_reference_table-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070025#include "jni_internal.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070026#include "mirror/abstract_method.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object_array-inl.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))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700242 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700243 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))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700248 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700249 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);
Jeff Hao39b6c242015-05-19 20:30:23 -0700453 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
454 if (is_string_init) {
455 // Replace calls to String.<init> with equivalent StringFactory call.
456 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
457 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700458 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);
Jeff Hao39b6c242015-05-19 20:30:23 -0700465 if (is_string_init) {
466 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700467 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700468 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700469 return result;
470}
471
Jeff Hao39b6c242015-05-19 20:30:23 -0700472JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
473 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700474 // We want to make sure that the stack is not within a small distance from the
475 // protected region in case we are calling into a leaf function whose stack
476 // check has been elided.
477 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
478 ThrowStackOverflowError(soa.Self());
479 return JValue();
480 }
481
Ian Rogers53b8b092014-03-13 23:45:53 -0700482 mirror::ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700483 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
484 if (is_string_init) {
485 // Replace calls to String.<init> with equivalent StringFactory call.
486 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
487 }
488 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700489 uint32_t shorty_len = 0;
490 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700491 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700492 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700493 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700494 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700495 if (is_string_init) {
496 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700497 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700498 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700499 return result;
500}
501
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700502JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700503 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700504 // We want to make sure that the stack is not within a small distance from the
505 // protected region in case we are calling into a leaf function whose stack
506 // check has been elided.
507 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
508 ThrowStackOverflowError(soa.Self());
509 return JValue();
510 }
511
Jeff Hao39b6c242015-05-19 20:30:23 -0700512 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
Ian Rogers53b8b092014-03-13 23:45:53 -0700513 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700514 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
515 if (is_string_init) {
516 // Replace calls to String.<init> with equivalent StringFactory call.
517 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
518 receiver = nullptr;
519 }
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.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700525 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700526 if (is_string_init) {
527 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700528 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700529 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700530 return result;
531}
532
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700533JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700534 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700535 // We want to make sure that the stack is not within a small distance from the
536 // protected region in case we are calling into a leaf function whose stack
537 // check has been elided.
538 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
539 ThrowStackOverflowError(soa.Self());
540 return JValue();
541 }
542
Ian Rogers53b8b092014-03-13 23:45:53 -0700543 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
544 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700545 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
546 if (is_string_init) {
547 // Replace calls to String.<init> with equivalent StringFactory call.
548 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
549 receiver = nullptr;
550 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700551 uint32_t shorty_len = 0;
552 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700553 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700554 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700555 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700556 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700557 if (is_string_init) {
558 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700559 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700560 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700561 return result;
562}
563
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700564jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700565 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700566 // We want to make sure that the stack is not within a small distance from the
567 // protected region in case we are calling into a leaf function whose stack
568 // check has been elided.
569 if (UNLIKELY(__builtin_frame_address(0) <
570 soa.Self()->GetStackEndForInterpreter(true))) {
571 ThrowStackOverflowError(soa.Self());
572 return nullptr;
573 }
574
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700575 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(javaMethod);
576 const bool accessible = abstract_method->IsAccessible();
577 mirror::ArtMethod* m = abstract_method->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700578
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800580 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700581 StackHandleScope<1> hs(soa.Self());
582 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700583 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800584 return nullptr;
585 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700586 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700587 }
588
Ian Rogers53b8b092014-03-13 23:45:53 -0700589 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700590 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800591 // Replace calls to String.<init> with equivalent StringFactory call.
592 if (declaring_class->IsStringClass() && m->IsConstructor()) {
593 jmethodID mid = soa.EncodeMethod(m);
594 m = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
595 CHECK(javaReceiver == nullptr);
596 } else {
597 // Check that the receiver is non-null and an instance of the field's declaring class.
598 receiver = soa.Decode<mirror::Object*>(javaReceiver);
599 if (!VerifyObjectIsClass(receiver, declaring_class)) {
600 return nullptr;
601 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700602
Jeff Hao848f70a2014-01-15 13:49:50 -0800603 // Find the actual implementation of the virtual method.
604 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
605 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700606 }
607
608 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700609 auto* objects = soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700610 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700611 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
612 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800613 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000614 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800615 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800616 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700617 }
618
Jeff Haocb4581a2014-03-28 15:43:37 -0700619 // If method is not set to be accessible, verify it can be accessed by the caller.
Andreas Gampec0d82292014-09-23 10:38:30 -0700620 mirror::Class* calling_class = nullptr;
621 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700622 &calling_class, num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000623 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700624 StringPrintf("Class %s cannot access %s method %s of class %s",
625 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
626 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
627 PrettyMethod(m).c_str(),
628 m->GetDeclaringClass() == nullptr ? "null" :
629 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700630 return nullptr;
631 }
632
Ian Rogers53b8b092014-03-13 23:45:53 -0700633 // Invoke the method.
634 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700635 uint32_t shorty_len = 0;
636 const char* shorty = m->GetShorty(&shorty_len);
637 ArgArray arg_array(shorty, shorty_len);
638 StackHandleScope<1> hs(soa.Self());
Ian Rogersa0485602014-12-02 15:48:04 -0800639 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
640 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, h_m)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700641 CHECK(soa.Self()->IsExceptionPending());
642 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700643 }
644
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700645 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700646
647 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700649 // If we get another exception when we are trying to wrap, then just use that instead.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 jthrowable th = soa.Env()->ExceptionOccurred();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700651 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700653 if (exception_class == nullptr) {
654 soa.Self()->AssertPendingOOMException();
655 return nullptr;
656 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700658 CHECK(mid != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700660 if (exception_instance == nullptr) {
661 soa.Self()->AssertPendingOOMException();
662 return nullptr;
663 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800665 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700666 }
667
668 // Box if necessary and return.
Ian Rogersa0485602014-12-02 15:48:04 -0800669 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700670}
671
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800672mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700673 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800674 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700675 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700676 if (src_class == Primitive::kPrimVoid) {
677 // There's no such thing as a void field, and void methods invoked via reflection return null.
678 return nullptr;
679 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700680
Ian Rogers84956ff2014-03-26 23:52:41 -0700681 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800682 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700683 switch (src_class) {
684 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800686 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700687 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700688 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700689 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800690 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700691 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700692 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800694 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700695 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700696 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800698 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700699 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700700 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800702 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700703 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700704 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700705 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800706 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700707 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700708 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700709 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800710 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700711 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700712 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800714 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700715 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700716 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700717 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800718 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700719 }
720
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700722 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800723
Ian Rogers53b8b092014-03-13 23:45:53 -0700724 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800725 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800726 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
727 arg_array.AppendWide(value.GetJ());
728 } else {
729 arg_array.Append(value.GetI());
730 }
731
732 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800733 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800734 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700735}
736
Mathieu Chartierc7853442015-03-27 14:35:38 -0700737static std::string UnboxingFailureKind(ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700739 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700740 return "field " + PrettyField(f, false);
741 }
742 return "result";
743}
744
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000745static bool UnboxPrimitive(mirror::Object* o,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700746 mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700747 JValue* unboxed_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700748 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700749 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700750 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700751 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000753 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700754 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800755 PrettyDescriptor(dst_class).c_str(),
756 PrettyTypeOf(o).c_str()).c_str());
757 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000758 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800759 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700760 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800761 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700762 return false;
763 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700764 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700765 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800766 }
767 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000768 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700769 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700770 return false;
771 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700772 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800773 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000774 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700775 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800776 PrettyDescriptor(dst_class).c_str()).c_str());
777 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000778 ThrowNullPointerException(StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
Ian Rogers62d6c772013-02-27 08:32:07 -0800779 PrettyDescriptor(dst_class).c_str()).c_str());
780 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700781 return false;
782 }
783
Elliott Hughes1d878f32012-04-11 15:17:54 -0700784 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700785 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700786 mirror::Class* src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700787 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
788 ArtField* primitive_field = &klass->GetIFields()[0];
Mathieu Chartierf8322842014-05-16 10:59:25 -0700789 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700790 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700791 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700792 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700793 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700794 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700795 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700796 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700797 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700798 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700799 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700800 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700801 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700802 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700803 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700804 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700805 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700806 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700807 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700808 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700809 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700810 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700811 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700812 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700813 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700814 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000815 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700816 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
817 PrettyDescriptor(dst_class).c_str(),
818 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700819 return false;
820 }
821
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000822 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800823 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700824 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700825}
826
Mathieu Chartierc7853442015-03-27 14:35:38 -0700827bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700828 JValue* unboxed_value) {
829 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000830 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700831}
832
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700833bool UnboxPrimitiveForResult(mirror::Object* o, mirror::Class* dst_class, JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000834 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700835}
836
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700837mirror::Class* GetCallingClass(Thread* self, size_t num_frames) {
838 NthCallerVisitor visitor(self, num_frames);
839 visitor.WalkStack();
840 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
841}
842
Andreas Gampec0d82292014-09-23 10:38:30 -0700843bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
Mathieu Chartierca239af2015-03-29 18:27:50 -0700844 uint32_t access_flags, mirror::Class** calling_class, size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700845 if ((access_flags & kAccPublic) != 0) {
846 return true;
847 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700848 auto* klass = GetCallingClass(self, num_frames);
849 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100850 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700851 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100852 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700853 *calling_class = klass;
854 return VerifyAccess(self, obj, declaring_class, access_flags, klass);
855}
856
857bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
858 uint32_t access_flags, mirror::Class* calling_class) {
859 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700860 return true;
861 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700862 ScopedAssertNoThreadSuspension sants(self, "verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700863 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700864 return false;
865 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700866 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700867 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
868 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700869 return false;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700870 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700871 return true;
872 }
873 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700874 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700875}
876
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700877void InvalidReceiverError(mirror::Object* o, mirror::Class* c) {
878 std::string expected_class_name(PrettyDescriptor(c));
879 std::string actual_class_name(PrettyTypeOf(o));
880 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
881 expected_class_name.c_str(),
882 actual_class_name.c_str()).c_str());
883}
884
Jeff Hao83c81952015-05-27 19:29:29 -0700885// This only works if there's one reference which points to the object in obj.
886// Will need to be fixed if there's cases where it's not.
887void UpdateReference(Thread* self, jobject obj, mirror::Object* result) {
888 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
889 IndirectRefKind kind = GetIndirectRefKind(ref);
890 if (kind == kLocal) {
891 self->GetJniEnv()->locals.Update(obj, result);
892 } else if (kind == kHandleScopeOrInvalid) {
893 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
894 } else if (kind == kGlobal) {
895 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result);
896 } else {
897 DCHECK_EQ(kind, kWeakGlobal);
898 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result);
899 }
900}
901
Elliott Hughes418d20f2011-09-22 14:00:39 -0700902} // namespace art