blob: 28c27cd9713be470cf5a36a127c4bf4e7d99d0f8 [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070021#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080022#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "dex_file-inl.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"
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:
Roland Levillain3887c462015-08-12 18:15:42 +010038 ArgArray(const char* shorty, uint32_t shorty_len)
Ian Rogers53b8b092014-03-13 23:45:53 -070039 : 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
Mathieu Chartier90443472015-07-16 20:32:27 -070074 void Append(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -070075 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)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -070099 // 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700135 // 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700173 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700174 // 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700208 SHARED_REQUIRES(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,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 mirror::ObjectArray<mirror::Object>* args, ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -0700216 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700218 // Set receiver if non-null (method is not static)
219 if (receiver != nullptr) {
220 Append(receiver);
221 }
222 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
223 mirror::Object* arg = args->Get(args_offset);
224 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100225 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers53b8b092014-03-13 23:45:53 -0700226 mirror::Class* dst_class =
Vladimir Marko05792b92015-08-03 11:56:49 +0100227 m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_,
228 true /* resolve */,
229 pointer_size);
Ian Rogers53b8b092014-03-13 23:45:53 -0700230 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000231 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700232 StringPrintf("method %s argument %zd has type %s, got %s",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 PrettyMethod(m, false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700234 args_offset + 1, // Humans don't count from 0.
235 PrettyDescriptor(dst_class).c_str(),
236 PrettyTypeOf(arg).c_str()).c_str());
237 return false;
238 }
239 }
240
241#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700242 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700243 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700244 append(primitive_field-> get_fn(arg));
245
246#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700247 } else if (LIKELY(arg != nullptr && \
248 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700249 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700250 append(primitive_field-> get_fn(arg));
251
252#define DO_FAIL(expected) \
253 } else { \
254 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700255 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700256 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700257 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700258 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000259 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700260 StringPrintf("method %s argument %zd has type %s, got %s", \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700262 args_offset + 1, \
263 expected, \
264 PrettyTypeOf(arg).c_str()).c_str()); \
265 } \
266 return false; \
267 } }
268
269 switch (shorty_[i]) {
270 case 'L':
271 Append(arg);
272 break;
273 case 'Z':
274 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
275 DO_FAIL("boolean")
276 break;
277 case 'B':
278 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
279 DO_FAIL("byte")
280 break;
281 case 'C':
282 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
283 DO_FAIL("char")
284 break;
285 case 'S':
286 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
287 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
288 DO_FAIL("short")
289 break;
290 case 'I':
291 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
292 DO_ARG("Ljava/lang/Character;", GetChar, Append)
293 DO_ARG("Ljava/lang/Short;", GetShort, Append)
294 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
295 DO_FAIL("int")
296 break;
297 case 'J':
298 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
299 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
300 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
301 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
302 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
303 DO_FAIL("long")
304 break;
305 case 'F':
306 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
307 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
308 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
309 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
310 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
311 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
312 DO_FAIL("float")
313 break;
314 case 'D':
315 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
316 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
317 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
318 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
319 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
320 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
321 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
322 DO_FAIL("double")
323 break;
324#ifndef NDEBUG
325 default:
326 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800327 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700328#endif
329 }
330#undef DO_FIRST_ARG
331#undef DO_ARG
332#undef DO_FAIL
333 }
334 return true;
335 }
336
337 private:
338 enum { kSmallArgArraySize = 16 };
339 const char* const shorty_;
340 const uint32_t shorty_len_;
341 uint32_t num_bytes_;
342 uint32_t* arg_array_;
343 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700344 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700345};
346
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Mathieu Chartier90443472015-07-16 20:32:27 -0700348 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700349 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700350 if (params == nullptr) {
351 return; // No arguments so nothing to check.
352 }
353 uint32_t offset = 0;
354 uint32_t num_params = params->Size();
355 size_t error_count = 0;
356 if (!m->IsStatic()) {
357 offset = 1;
358 }
Ian Rogersa0485602014-12-02 15:48:04 -0800359 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 Thread* const self = Thread::Current();
Vladimir Marko05792b92015-08-03 11:56:49 +0100361 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
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_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100364 mirror::Class* param_type = m->GetClassFromTypeIndex(type_idx,
365 true /* resolve*/,
366 pointer_size);
Ian Rogers53b8b092014-03-13 23:45:53 -0700367 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700368 CHECK(self->IsExceptionPending());
369 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700370 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000371 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700372 self->ClearException();
373 ++error_count;
374 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700375 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
376 // this is a hard to fix problem since the args can contain Object*, we need to save and
377 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers68d8b422014-07-17 11:09:10 -0700378 mirror::Object* argument =
379 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700380 if (argument != nullptr && !argument->InstanceOf(param_type)) {
381 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
382 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 << " to " << PrettyMethod(m);
Ian Rogers53b8b092014-03-13 23:45:53 -0700384 ++error_count;
385 }
386 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
387 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700388 } else {
389 int32_t arg = static_cast<int32_t>(args[i + offset]);
390 if (param_type->IsPrimitiveBoolean()) {
391 if (arg != JNI_TRUE && arg != JNI_FALSE) {
392 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700394 ++error_count;
395 }
396 } else if (param_type->IsPrimitiveByte()) {
397 if (arg < -128 || arg > 127) {
398 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700400 ++error_count;
401 }
402 } else if (param_type->IsPrimitiveChar()) {
403 if (args[i + offset] > 0xFFFF) {
404 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700406 ++error_count;
407 }
408 } else if (param_type->IsPrimitiveShort()) {
409 if (arg < -32768 || arg > 0x7FFF) {
410 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700412 ++error_count;
413 }
414 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700415 }
416 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700417 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700418 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
419 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700420 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 PrettyMethod(m).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700422 }
423}
424
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425static ArtMethod* FindVirtualMethod(mirror::Object* receiver, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700426 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, sizeof(void*));
Ian Rogers53b8b092014-03-13 23:45:53 -0700428}
429
430
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700431static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700433 const char* shorty)
Mathieu Chartier90443472015-07-16 20:32:27 -0700434 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700435 uint32_t* args = arg_array->GetArray();
436 if (UNLIKELY(soa.Env()->check_jni)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(sizeof(void*)), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700438 }
439 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
440}
441
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700442JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
443 va_list args)
Mathieu Chartier90443472015-07-16 20:32:27 -0700444 SHARED_REQUIRES(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700445 // We want to make sure that the stack is not within a small distance from the
446 // protected region in case we are calling into a leaf function whose stack
447 // check has been elided.
448 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
449 ThrowStackOverflowError(soa.Self());
450 return JValue();
451 }
452
Mathieu Chartiere401d142015-04-22 13:56:20 -0700453 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700454 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
455 if (is_string_init) {
456 // Replace calls to String.<init> with equivalent StringFactory call.
457 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
458 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700459 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700460 uint32_t shorty_len = 0;
Andreas Gampee8067322015-09-08 17:42:59 -0700461 const char* shorty = method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700462 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700463 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700464 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700465 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700466 if (is_string_init) {
467 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700468 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700469 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700470 return result;
471}
472
Jeff Hao39b6c242015-05-19 20:30:23 -0700473JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
474 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700475 // We want to make sure that the stack is not within a small distance from the
476 // protected region in case we are calling into a leaf function whose stack
477 // check has been elided.
478 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
479 ThrowStackOverflowError(soa.Self());
480 return JValue();
481 }
482
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700484 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
485 if (is_string_init) {
486 // Replace calls to String.<init> with equivalent StringFactory call.
487 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
488 }
489 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700490 uint32_t shorty_len = 0;
Andreas Gampee8067322015-09-08 17:42:59 -0700491 const char* shorty = method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700492 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700493 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700494 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700495 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700496 if (is_string_init) {
497 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700498 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700499 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700500 return result;
501}
502
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700503JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700504 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700505 // We want to make sure that the stack is not within a small distance from the
506 // protected region in case we are calling into a leaf function whose stack
507 // check has been elided.
508 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
509 ThrowStackOverflowError(soa.Self());
510 return JValue();
511 }
512
Jeff Hao39b6c242015-05-19 20:30:23 -0700513 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700515 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
516 if (is_string_init) {
517 // Replace calls to String.<init> with equivalent StringFactory call.
518 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
519 receiver = nullptr;
520 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700521 uint32_t shorty_len = 0;
Andreas Gampee8067322015-09-08 17:42:59 -0700522 const char* shorty = method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700523 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700524 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700525 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700526 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700527 if (is_string_init) {
528 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700529 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700530 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700531 return result;
532}
533
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700534JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700535 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700536 // We want to make sure that the stack is not within a small distance from the
537 // protected region in case we are calling into a leaf function whose stack
538 // check has been elided.
539 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
540 ThrowStackOverflowError(soa.Self());
541 return JValue();
542 }
543
Ian Rogers53b8b092014-03-13 23:45:53 -0700544 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700546 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
547 if (is_string_init) {
548 // Replace calls to String.<init> with equivalent StringFactory call.
549 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
550 receiver = nullptr;
551 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700552 uint32_t shorty_len = 0;
Andreas Gampee8067322015-09-08 17:42:59 -0700553 const char* shorty = method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700554 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700555 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700556 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700557 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700558 if (is_string_init) {
559 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700560 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700561 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700562 return result;
563}
564
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700565jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700566 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700567 // We want to make sure that the stack is not within a small distance from the
568 // protected region in case we are calling into a leaf function whose stack
569 // check has been elided.
570 if (UNLIKELY(__builtin_frame_address(0) <
571 soa.Self()->GetStackEndForInterpreter(true))) {
572 ThrowStackOverflowError(soa.Self());
573 return nullptr;
574 }
575
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700576 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(javaMethod);
577 const bool accessible = abstract_method->IsAccessible();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700578 ArtMethod* m = abstract_method->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700579
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800580 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800581 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700582 StackHandleScope<1> hs(soa.Self());
583 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700584 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800585 return nullptr;
586 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700587 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700588 }
589
Ian Rogers53b8b092014-03-13 23:45:53 -0700590 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700591 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800592 // Replace calls to String.<init> with equivalent StringFactory call.
593 if (declaring_class->IsStringClass() && m->IsConstructor()) {
594 jmethodID mid = soa.EncodeMethod(m);
595 m = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
596 CHECK(javaReceiver == nullptr);
597 } else {
598 // Check that the receiver is non-null and an instance of the field's declaring class.
599 receiver = soa.Decode<mirror::Object*>(javaReceiver);
600 if (!VerifyObjectIsClass(receiver, declaring_class)) {
601 return nullptr;
602 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700603
Jeff Hao848f70a2014-01-15 13:49:50 -0800604 // Find the actual implementation of the virtual method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700605 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, sizeof(void*));
Jeff Hao848f70a2014-01-15 13:49:50 -0800606 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700607 }
608
609 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700610 auto* objects = soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611 auto* np_method = m->GetInterfaceMethodIfProxy(sizeof(void*));
612 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700613 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
614 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800615 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000616 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800617 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800618 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700619 }
620
Jeff Haocb4581a2014-03-28 15:43:37 -0700621 // If method is not set to be accessible, verify it can be accessed by the caller.
Andreas Gampec0d82292014-09-23 10:38:30 -0700622 mirror::Class* calling_class = nullptr;
623 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700624 &calling_class, num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000625 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700626 StringPrintf("Class %s cannot access %s method %s of class %s",
627 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
628 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
629 PrettyMethod(m).c_str(),
630 m->GetDeclaringClass() == nullptr ? "null" :
631 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700632 return nullptr;
633 }
634
Ian Rogers53b8b092014-03-13 23:45:53 -0700635 // Invoke the method.
636 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700637 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700639 ArgArray arg_array(shorty, shorty_len);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method)) {
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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700738 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700748 SHARED_REQUIRES(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();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700788 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(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