blob: 329ceb561da67e2a42d0344af207e93c888f09aa [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"
Elliott Hughes418d20f2011-09-22 14:00:39 -070024#include "jni_internal.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070025#include "mirror/abstract_method.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object_array-inl.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070029#include "nth_caller_visitor.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "scoped_thread_state_change.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070031#include "stack.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070033
Elliott Hughes418d20f2011-09-22 14:00:39 -070034namespace art {
35
Ian Rogers53b8b092014-03-13 23:45:53 -070036class ArgArray {
37 public:
38 explicit ArgArray(const char* shorty, uint32_t shorty_len)
39 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
40 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
41 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
42 // We can trivially use the small arg array.
43 arg_array_ = small_arg_array_;
44 } else {
45 // Analyze shorty to see if we need the large arg array.
46 for (size_t i = 1; i < shorty_len; ++i) {
47 char c = shorty[i];
48 if (c == 'J' || c == 'D') {
49 num_slots++;
50 }
51 }
52 if (num_slots <= kSmallArgArraySize) {
53 arg_array_ = small_arg_array_;
54 } else {
55 large_arg_array_.reset(new uint32_t[num_slots]);
56 arg_array_ = large_arg_array_.get();
57 }
58 }
59 }
60
61 uint32_t* GetArray() {
62 return arg_array_;
63 }
64
65 uint32_t GetNumBytes() {
66 return num_bytes_;
67 }
68
69 void Append(uint32_t value) {
70 arg_array_[num_bytes_ / 4] = value;
71 num_bytes_ += 4;
72 }
73
74 void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
75 Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue());
76 }
77
78 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070079 arg_array_[num_bytes_ / 4] = value;
80 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
81 num_bytes_ += 8;
82 }
83
84 void AppendFloat(float value) {
85 jvalue jv;
86 jv.f = value;
87 Append(jv.i);
88 }
89
90 void AppendDouble(double value) {
91 jvalue jv;
92 jv.d = value;
93 AppendWide(jv.j);
94 }
95
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070096 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
97 mirror::Object* receiver, va_list ap)
Ian Rogers53b8b092014-03-13 23:45:53 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99 // Set receiver if non-null (method is not static)
100 if (receiver != nullptr) {
101 Append(receiver);
102 }
103 for (size_t i = 1; i < shorty_len_; ++i) {
104 switch (shorty_[i]) {
105 case 'Z':
106 case 'B':
107 case 'C':
108 case 'S':
109 case 'I':
110 Append(va_arg(ap, jint));
111 break;
112 case 'F':
113 AppendFloat(va_arg(ap, jdouble));
114 break;
115 case 'L':
116 Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
117 break;
118 case 'D':
119 AppendDouble(va_arg(ap, jdouble));
120 break;
121 case 'J':
122 AppendWide(va_arg(ap, jlong));
123 break;
124#ifndef NDEBUG
125 default:
126 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
127#endif
128 }
129 }
130 }
131
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700132 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
133 mirror::Object* receiver, jvalue* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
135 // Set receiver if non-null (method is not static)
136 if (receiver != nullptr) {
137 Append(receiver);
138 }
139 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
140 switch (shorty_[i]) {
141 case 'Z':
142 Append(args[args_offset].z);
143 break;
144 case 'B':
145 Append(args[args_offset].b);
146 break;
147 case 'C':
148 Append(args[args_offset].c);
149 break;
150 case 'S':
151 Append(args[args_offset].s);
152 break;
153 case 'I':
154 case 'F':
155 Append(args[args_offset].i);
156 break;
157 case 'L':
158 Append(soa.Decode<mirror::Object*>(args[args_offset].l));
159 break;
160 case 'D':
161 case 'J':
162 AppendWide(args[args_offset].j);
163 break;
164#ifndef NDEBUG
165 default:
166 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
167#endif
168 }
169 }
170 }
171
172 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
174 // Set receiver if non-null (method is not static)
175 size_t cur_arg = arg_offset;
176 if (!shadow_frame->GetMethod()->IsStatic()) {
177 Append(shadow_frame->GetVReg(cur_arg));
178 cur_arg++;
179 }
180 for (size_t i = 1; i < shorty_len_; ++i) {
181 switch (shorty_[i]) {
182 case 'Z':
183 case 'B':
184 case 'C':
185 case 'S':
186 case 'I':
187 case 'F':
188 case 'L':
189 Append(shadow_frame->GetVReg(cur_arg));
190 cur_arg++;
191 break;
192 case 'D':
193 case 'J':
194 AppendWide(shadow_frame->GetVRegLong(cur_arg));
195 cur_arg++;
196 cur_arg++;
197 break;
198#ifndef NDEBUG
199 default:
200 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
201#endif
202 }
203 }
204 }
205
206 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700207 const char* found_descriptor)
Ian Rogers53b8b092014-03-13 23:45:53 -0700208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000209 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700210 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700212 }
213
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700214 bool BuildArgArrayFromObjectArray(mirror::Object* receiver,
Ian Rogersa0485602014-12-02 15:48:04 -0800215 mirror::ObjectArray<mirror::Object>* args,
216 Handle<mirror::ArtMethod> h_m)
Ian Rogers53b8b092014-03-13 23:45:53 -0700217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa0485602014-12-02 15:48:04 -0800218 const DexFile::TypeList* classes = h_m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700219 // Set receiver if non-null (method is not static)
220 if (receiver != nullptr) {
221 Append(receiver);
222 }
223 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
224 mirror::Object* arg = args->Get(args_offset);
225 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
226 mirror::Class* dst_class =
Ian Rogersa0485602014-12-02 15:48:04 -0800227 h_m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700228 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000229 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700230 StringPrintf("method %s argument %zd has type %s, got %s",
Ian Rogersa0485602014-12-02 15:48:04 -0800231 PrettyMethod(h_m.Get(), false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700232 args_offset + 1, // Humans don't count from 0.
233 PrettyDescriptor(dst_class).c_str(),
234 PrettyTypeOf(arg).c_str()).c_str());
235 return false;
236 }
237 }
238
239#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700240 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700241 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700242 append(primitive_field-> get_fn(arg));
243
244#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700245 } else if (LIKELY(arg != nullptr && \
246 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700248 append(primitive_field-> get_fn(arg));
249
250#define DO_FAIL(expected) \
251 } else { \
252 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700253 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700254 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700255 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700256 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000257 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700258 StringPrintf("method %s argument %zd has type %s, got %s", \
Ian Rogersa0485602014-12-02 15:48:04 -0800259 PrettyMethod(h_m.Get(), false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700260 args_offset + 1, \
261 expected, \
262 PrettyTypeOf(arg).c_str()).c_str()); \
263 } \
264 return false; \
265 } }
266
267 switch (shorty_[i]) {
268 case 'L':
269 Append(arg);
270 break;
271 case 'Z':
272 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
273 DO_FAIL("boolean")
274 break;
275 case 'B':
276 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
277 DO_FAIL("byte")
278 break;
279 case 'C':
280 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
281 DO_FAIL("char")
282 break;
283 case 'S':
284 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
285 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
286 DO_FAIL("short")
287 break;
288 case 'I':
289 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
290 DO_ARG("Ljava/lang/Character;", GetChar, Append)
291 DO_ARG("Ljava/lang/Short;", GetShort, Append)
292 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
293 DO_FAIL("int")
294 break;
295 case 'J':
296 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
297 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
298 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
299 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
300 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
301 DO_FAIL("long")
302 break;
303 case 'F':
304 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
305 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
306 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
307 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
308 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
309 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
310 DO_FAIL("float")
311 break;
312 case 'D':
313 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
314 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
315 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
316 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
317 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
318 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
319 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
320 DO_FAIL("double")
321 break;
322#ifndef NDEBUG
323 default:
324 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800325 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700326#endif
327 }
328#undef DO_FIRST_ARG
329#undef DO_ARG
330#undef DO_FAIL
331 }
332 return true;
333 }
334
335 private:
336 enum { kSmallArgArraySize = 16 };
337 const char* const shorty_;
338 const uint32_t shorty_len_;
339 uint32_t num_bytes_;
340 uint32_t* arg_array_;
341 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700342 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700343};
344
Ian Rogers68d8b422014-07-17 11:09:10 -0700345static void CheckMethodArguments(JavaVMExt* vm, mirror::ArtMethod* m, uint32_t* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700347 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700348 if (params == nullptr) {
349 return; // No arguments so nothing to check.
350 }
351 uint32_t offset = 0;
352 uint32_t num_params = params->Size();
353 size_t error_count = 0;
354 if (!m->IsStatic()) {
355 offset = 1;
356 }
Ian Rogersa0485602014-12-02 15:48:04 -0800357 // TODO: If args contain object references, it may cause problems.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700358 Thread* self = Thread::Current();
359 StackHandleScope<1> hs(self);
360 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
Ian Rogers53b8b092014-03-13 23:45:53 -0700361 for (uint32_t i = 0; i < num_params; i++) {
362 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Ian Rogersa0485602014-12-02 15:48:04 -0800363 mirror::Class* param_type = h_m->GetClassFromTypeIndex(type_idx, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700364 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700365 CHECK(self->IsExceptionPending());
366 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700367 << h_m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000368 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700369 self->ClearException();
370 ++error_count;
371 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700372 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
373 // this is a hard to fix problem since the args can contain Object*, we need to save and
374 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers68d8b422014-07-17 11:09:10 -0700375 mirror::Object* argument =
376 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700377 if (argument != nullptr && !argument->InstanceOf(param_type)) {
378 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
379 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700380 << " to " << PrettyMethod(h_m.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700381 ++error_count;
382 }
383 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
384 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700385 } else {
386 int32_t arg = static_cast<int32_t>(args[i + offset]);
387 if (param_type->IsPrimitiveBoolean()) {
388 if (arg != JNI_TRUE && arg != JNI_FALSE) {
389 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
390 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
391 ++error_count;
392 }
393 } else if (param_type->IsPrimitiveByte()) {
394 if (arg < -128 || arg > 127) {
395 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
396 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
397 ++error_count;
398 }
399 } else if (param_type->IsPrimitiveChar()) {
400 if (args[i + offset] > 0xFFFF) {
401 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
402 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
403 ++error_count;
404 }
405 } else if (param_type->IsPrimitiveShort()) {
406 if (arg < -32768 || arg > 0x7FFF) {
407 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
408 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
409 ++error_count;
410 }
411 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700412 }
413 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700414 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700415 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
416 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700417 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
418 PrettyMethod(h_m.Get()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700419 }
420}
421
422static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver,
423 mirror::ArtMethod* method)
424 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
425 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
426}
427
428
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700429static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
430 mirror::ArtMethod* method, ArgArray* arg_array, JValue* result,
431 const char* shorty)
Ian Rogers53b8b092014-03-13 23:45:53 -0700432 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
433 uint32_t* args = arg_array->GetArray();
434 if (UNLIKELY(soa.Env()->check_jni)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700435 CheckMethodArguments(soa.Vm(), method, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700436 }
437 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
438}
439
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700440JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
441 va_list args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700443 // We want to make sure that the stack is not within a small distance from the
444 // protected region in case we are calling into a leaf function whose stack
445 // check has been elided.
446 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
447 ThrowStackOverflowError(soa.Self());
448 return JValue();
449 }
450
Ian Rogers53b8b092014-03-13 23:45:53 -0700451 mirror::ArtMethod* method = soa.DecodeMethod(mid);
452 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700453 uint32_t shorty_len = 0;
454 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700455 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700456 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700457 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700458 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700459 return result;
460}
461
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700462JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, mirror::Object* receiver,
Ian Rogers53b8b092014-03-13 23:45:53 -0700463 jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700464 // We want to make sure that the stack is not within a small distance from the
465 // protected region in case we are calling into a leaf function whose stack
466 // check has been elided.
467 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
468 ThrowStackOverflowError(soa.Self());
469 return JValue();
470 }
471
Ian Rogers53b8b092014-03-13 23:45:53 -0700472 mirror::ArtMethod* method = soa.DecodeMethod(mid);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700473 uint32_t shorty_len = 0;
474 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700475 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700476 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700477 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700478 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700479 return result;
480}
481
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700482JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700483 mirror::Object* receiver, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700484 // We want to make sure that the stack is not within a small distance from the
485 // protected region in case we are calling into a leaf function whose stack
486 // check has been elided.
487 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
488 ThrowStackOverflowError(soa.Self());
489 return JValue();
490 }
491
Ian Rogers53b8b092014-03-13 23:45:53 -0700492 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700493 uint32_t shorty_len = 0;
494 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700495 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700496 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700497 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700498 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700499 return result;
500}
501
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700502JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700503 jobject obj, jmethodID mid, va_list 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
Ian Rogers53b8b092014-03-13 23:45:53 -0700512 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
513 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700514 uint32_t shorty_len = 0;
515 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700516 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700517 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700518 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700519 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700520 return result;
521}
522
523void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset,
Ian Rogerse94652f2014-12-02 11:13:19 -0800524 JValue* result) {
Dave Allison648d7112014-07-25 16:15:27 -0700525 // We want to make sure that the stack is not within a small distance from the
526 // protected region in case we are calling into a leaf function whose stack
527 // check has been elided.
528 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
529 ThrowStackOverflowError(self);
530 return;
531 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800532 uint32_t shorty_len;
533 const char* shorty = shadow_frame->GetMethod()->GetShorty(&shorty_len);
534 ArgArray arg_array(shorty, shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700535 arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);
536 shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result,
Ian Rogerse94652f2014-12-02 11:13:19 -0800537 shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700538}
539
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700540jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700541 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700542 // We want to make sure that the stack is not within a small distance from the
543 // protected region in case we are calling into a leaf function whose stack
544 // check has been elided.
545 if (UNLIKELY(__builtin_frame_address(0) <
546 soa.Self()->GetStackEndForInterpreter(true))) {
547 ThrowStackOverflowError(soa.Self());
548 return nullptr;
549 }
550
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700551 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(javaMethod);
552 const bool accessible = abstract_method->IsAccessible();
553 mirror::ArtMethod* m = abstract_method->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700554
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800556 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700557 StackHandleScope<1> hs(soa.Self());
558 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700559 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800560 return nullptr;
561 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700562 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700563 }
564
Ian Rogers53b8b092014-03-13 23:45:53 -0700565 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700566 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800567 // Replace calls to String.<init> with equivalent StringFactory call.
568 if (declaring_class->IsStringClass() && m->IsConstructor()) {
569 jmethodID mid = soa.EncodeMethod(m);
570 m = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
571 CHECK(javaReceiver == nullptr);
572 } else {
573 // Check that the receiver is non-null and an instance of the field's declaring class.
574 receiver = soa.Decode<mirror::Object*>(javaReceiver);
575 if (!VerifyObjectIsClass(receiver, declaring_class)) {
576 return nullptr;
577 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700578
Jeff Hao848f70a2014-01-15 13:49:50 -0800579 // Find the actual implementation of the virtual method.
580 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
581 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700582 }
583
584 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700585 auto* objects = soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700586 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700587 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
588 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800589 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000590 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800591 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800592 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700593 }
594
Jeff Haocb4581a2014-03-28 15:43:37 -0700595 // If method is not set to be accessible, verify it can be accessed by the caller.
Andreas Gampec0d82292014-09-23 10:38:30 -0700596 mirror::Class* calling_class = nullptr;
597 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700598 &calling_class, num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000599 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700600 StringPrintf("Class %s cannot access %s method %s of class %s",
601 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
602 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
603 PrettyMethod(m).c_str(),
604 m->GetDeclaringClass() == nullptr ? "null" :
605 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700606 return nullptr;
607 }
608
Ian Rogers53b8b092014-03-13 23:45:53 -0700609 // Invoke the method.
610 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700611 uint32_t shorty_len = 0;
612 const char* shorty = m->GetShorty(&shorty_len);
613 ArgArray arg_array(shorty, shorty_len);
614 StackHandleScope<1> hs(soa.Self());
Ian Rogersa0485602014-12-02 15:48:04 -0800615 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
616 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, h_m)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700617 CHECK(soa.Self()->IsExceptionPending());
618 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700619 }
620
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700621 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700622
623 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700625 // If we get another exception when we are trying to wrap, then just use that instead.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 jthrowable th = soa.Env()->ExceptionOccurred();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700627 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700629 if (exception_class == nullptr) {
630 soa.Self()->AssertPendingOOMException();
631 return nullptr;
632 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700633 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700634 CHECK(mid != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700636 if (exception_instance == nullptr) {
637 soa.Self()->AssertPendingOOMException();
638 return nullptr;
639 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800641 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700642 }
643
644 // Box if necessary and return.
Ian Rogersa0485602014-12-02 15:48:04 -0800645 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700646}
647
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800648mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700649 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800650 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700651 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700652 if (src_class == Primitive::kPrimVoid) {
653 // There's no such thing as a void field, and void methods invoked via reflection return null.
654 return nullptr;
655 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700656
Ian Rogers84956ff2014-03-26 23:52:41 -0700657 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800658 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700659 switch (src_class) {
660 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800662 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700663 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700664 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700665 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800666 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700667 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700668 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800670 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700671 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700672 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800674 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700675 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700676 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800678 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700679 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700680 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800682 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700683 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700684 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800686 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700687 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700688 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700689 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800690 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700691 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700692 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700693 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800694 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700695 }
696
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700698 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800699
Ian Rogers53b8b092014-03-13 23:45:53 -0700700 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800701 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800702 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
703 arg_array.AppendWide(value.GetJ());
704 } else {
705 arg_array.Append(value.GetI());
706 }
707
708 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800709 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800710 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700711}
712
Mathieu Chartierc7853442015-03-27 14:35:38 -0700713static std::string UnboxingFailureKind(ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700714 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700715 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700716 return "field " + PrettyField(f, false);
717 }
718 return "result";
719}
720
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000721static bool UnboxPrimitive(mirror::Object* o,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700722 mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700723 JValue* unboxed_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700724 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700725 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700726 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700727 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800728 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000729 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700730 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800731 PrettyDescriptor(dst_class).c_str(),
732 PrettyTypeOf(o).c_str()).c_str());
733 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000734 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800735 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700736 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800737 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700738 return false;
739 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700740 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700741 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800742 }
743 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000744 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700745 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700746 return false;
747 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700748 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800749 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000750 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700751 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 PrettyDescriptor(dst_class).c_str()).c_str());
753 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000754 ThrowNullPointerException(StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
Ian Rogers62d6c772013-02-27 08:32:07 -0800755 PrettyDescriptor(dst_class).c_str()).c_str());
756 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700757 return false;
758 }
759
Elliott Hughes1d878f32012-04-11 15:17:54 -0700760 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700761 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700762 mirror::Class* src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700763 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
764 ArtField* primitive_field = &klass->GetIFields()[0];
Mathieu Chartierf8322842014-05-16 10:59:25 -0700765 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700766 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700767 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700768 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700769 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700770 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700771 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700772 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700773 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700774 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700775 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700776 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700777 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700778 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700779 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700780 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700781 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700782 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700783 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700784 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700785 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700786 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700787 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700788 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700789 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700790 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000791 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700792 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
793 PrettyDescriptor(dst_class).c_str(),
794 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700795 return false;
796 }
797
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000798 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800799 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700800 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700801}
802
Mathieu Chartierc7853442015-03-27 14:35:38 -0700803bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700804 JValue* unboxed_value) {
805 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000806 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700807}
808
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700809bool UnboxPrimitiveForResult(mirror::Object* o, mirror::Class* dst_class, JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000810 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700811}
812
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700813mirror::Class* GetCallingClass(Thread* self, size_t num_frames) {
814 NthCallerVisitor visitor(self, num_frames);
815 visitor.WalkStack();
816 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
817}
818
Andreas Gampec0d82292014-09-23 10:38:30 -0700819bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
Mathieu Chartierca239af2015-03-29 18:27:50 -0700820 uint32_t access_flags, mirror::Class** calling_class, size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700821 if ((access_flags & kAccPublic) != 0) {
822 return true;
823 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700824 auto* klass = GetCallingClass(self, num_frames);
825 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100826 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700827 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100828 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700829 *calling_class = klass;
830 return VerifyAccess(self, obj, declaring_class, access_flags, klass);
831}
832
833bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
834 uint32_t access_flags, mirror::Class* calling_class) {
835 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700836 return true;
837 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700838 ScopedAssertNoThreadSuspension sants(self, "verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700839 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700840 return false;
841 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700842 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700843 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
844 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700845 return false;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700846 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700847 return true;
848 }
849 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700850 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700851}
852
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700853void InvalidReceiverError(mirror::Object* o, mirror::Class* c) {
854 std::string expected_class_name(PrettyDescriptor(c));
855 std::string actual_class_name(PrettyTypeOf(o));
856 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
857 expected_class_name.c_str(),
858 actual_class_name.c_str()).c_str());
859}
860
Elliott Hughes418d20f2011-09-22 14:00:39 -0700861} // namespace art