blob: 18fcee43816643f882d84db856ea6ac1b24d2912 [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 Chartier9ee0f772014-09-26 14:32:37 -070017#include "reflection-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070018
19#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080020#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070022#include "jni_internal.h"
Ian Rogerse5877a12014-07-16 12:06:35 -070023#include "method_helper-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_field-inl.h"
25#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070027#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object_array-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070029#include "mirror/object_array.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070030#include "nth_caller_visitor.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070032#include "stack.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070034
Elliott Hughes418d20f2011-09-22 14:00:39 -070035namespace art {
36
Ian Rogers53b8b092014-03-13 23:45:53 -070037class ArgArray {
38 public:
39 explicit ArgArray(const char* shorty, uint32_t shorty_len)
40 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
41 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
42 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
43 // We can trivially use the small arg array.
44 arg_array_ = small_arg_array_;
45 } else {
46 // Analyze shorty to see if we need the large arg array.
47 for (size_t i = 1; i < shorty_len; ++i) {
48 char c = shorty[i];
49 if (c == 'J' || c == 'D') {
50 num_slots++;
51 }
52 }
53 if (num_slots <= kSmallArgArraySize) {
54 arg_array_ = small_arg_array_;
55 } else {
56 large_arg_array_.reset(new uint32_t[num_slots]);
57 arg_array_ = large_arg_array_.get();
58 }
59 }
60 }
61
62 uint32_t* GetArray() {
63 return arg_array_;
64 }
65
66 uint32_t GetNumBytes() {
67 return num_bytes_;
68 }
69
70 void Append(uint32_t value) {
71 arg_array_[num_bytes_ / 4] = value;
72 num_bytes_ += 4;
73 }
74
75 void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
76 Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue());
77 }
78
79 void AppendWide(uint64_t value) {
80 // For ARM and MIPS portable, align wide values to 8 bytes (ArgArray starts at offset of 4).
81#if defined(ART_USE_PORTABLE_COMPILER) && (defined(__arm__) || defined(__mips__))
82 if (num_bytes_ % 8 == 0) {
83 num_bytes_ += 4;
84 }
85#endif
86 arg_array_[num_bytes_ / 4] = value;
87 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
88 num_bytes_ += 8;
89 }
90
91 void AppendFloat(float value) {
92 jvalue jv;
93 jv.f = value;
94 Append(jv.i);
95 }
96
97 void AppendDouble(double value) {
98 jvalue jv;
99 jv.d = value;
100 AppendWide(jv.j);
101 }
102
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700103 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
104 mirror::Object* receiver, va_list ap)
Ian Rogers53b8b092014-03-13 23:45:53 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
106 // Set receiver if non-null (method is not static)
107 if (receiver != nullptr) {
108 Append(receiver);
109 }
110 for (size_t i = 1; i < shorty_len_; ++i) {
111 switch (shorty_[i]) {
112 case 'Z':
113 case 'B':
114 case 'C':
115 case 'S':
116 case 'I':
117 Append(va_arg(ap, jint));
118 break;
119 case 'F':
120 AppendFloat(va_arg(ap, jdouble));
121 break;
122 case 'L':
123 Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
124 break;
125 case 'D':
126 AppendDouble(va_arg(ap, jdouble));
127 break;
128 case 'J':
129 AppendWide(va_arg(ap, jlong));
130 break;
131#ifndef NDEBUG
132 default:
133 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
134#endif
135 }
136 }
137 }
138
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700139 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
140 mirror::Object* receiver, jvalue* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
142 // Set receiver if non-null (method is not static)
143 if (receiver != nullptr) {
144 Append(receiver);
145 }
146 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
147 switch (shorty_[i]) {
148 case 'Z':
149 Append(args[args_offset].z);
150 break;
151 case 'B':
152 Append(args[args_offset].b);
153 break;
154 case 'C':
155 Append(args[args_offset].c);
156 break;
157 case 'S':
158 Append(args[args_offset].s);
159 break;
160 case 'I':
161 case 'F':
162 Append(args[args_offset].i);
163 break;
164 case 'L':
165 Append(soa.Decode<mirror::Object*>(args[args_offset].l));
166 break;
167 case 'D':
168 case 'J':
169 AppendWide(args[args_offset].j);
170 break;
171#ifndef NDEBUG
172 default:
173 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
174#endif
175 }
176 }
177 }
178
179 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
181 // Set receiver if non-null (method is not static)
182 size_t cur_arg = arg_offset;
183 if (!shadow_frame->GetMethod()->IsStatic()) {
184 Append(shadow_frame->GetVReg(cur_arg));
185 cur_arg++;
186 }
187 for (size_t i = 1; i < shorty_len_; ++i) {
188 switch (shorty_[i]) {
189 case 'Z':
190 case 'B':
191 case 'C':
192 case 'S':
193 case 'I':
194 case 'F':
195 case 'L':
196 Append(shadow_frame->GetVReg(cur_arg));
197 cur_arg++;
198 break;
199 case 'D':
200 case 'J':
201 AppendWide(shadow_frame->GetVRegLong(cur_arg));
202 cur_arg++;
203 cur_arg++;
204 break;
205#ifndef NDEBUG
206 default:
207 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
208#endif
209 }
210 }
211 }
212
213 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700214 const char* found_descriptor)
Ian Rogers53b8b092014-03-13 23:45:53 -0700215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
216 ThrowIllegalArgumentException(nullptr,
217 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700218 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700219 }
220
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700221 bool BuildArgArrayFromObjectArray(const ScopedObjectAccessAlreadyRunnable& soa,
222 mirror::Object* receiver,
Ian Rogerse18fdd22014-03-14 13:29:43 -0700223 mirror::ObjectArray<mirror::Object>* args, MethodHelper& mh)
Ian Rogers53b8b092014-03-13 23:45:53 -0700224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700225 const DexFile::TypeList* classes = mh.GetMethod()->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700226 // Set receiver if non-null (method is not static)
227 if (receiver != nullptr) {
228 Append(receiver);
229 }
230 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
231 mirror::Object* arg = args->Get(args_offset);
232 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
233 mirror::Class* dst_class =
234 mh.GetClassFromTypeIdx(classes->GetTypeItem(args_offset).type_idx_);
235 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
236 ThrowIllegalArgumentException(nullptr,
Ian Rogers11e4c032014-03-14 12:00:39 -0700237 StringPrintf("method %s argument %zd has type %s, got %s",
Ian Rogers53b8b092014-03-13 23:45:53 -0700238 PrettyMethod(mh.GetMethod(), false).c_str(),
239 args_offset + 1, // Humans don't count from 0.
240 PrettyDescriptor(dst_class).c_str(),
241 PrettyTypeOf(arg).c_str()).c_str());
242 return false;
243 }
244 }
245
246#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700247 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Ian Rogers53b8b092014-03-13 23:45:53 -0700248 mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
249 append(primitive_field-> get_fn(arg));
250
251#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700252 } else if (LIKELY(arg != nullptr && \
253 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Ian Rogers53b8b092014-03-13 23:45:53 -0700254 mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
255 append(primitive_field-> get_fn(arg));
256
257#define DO_FAIL(expected) \
258 } else { \
259 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700260 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700261 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700262 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700263 } else { \
264 ThrowIllegalArgumentException(nullptr, \
Ian Rogers11e4c032014-03-14 12:00:39 -0700265 StringPrintf("method %s argument %zd has type %s, got %s", \
Ian Rogers53b8b092014-03-13 23:45:53 -0700266 PrettyMethod(mh.GetMethod(), false).c_str(), \
267 args_offset + 1, \
268 expected, \
269 PrettyTypeOf(arg).c_str()).c_str()); \
270 } \
271 return false; \
272 } }
273
274 switch (shorty_[i]) {
275 case 'L':
276 Append(arg);
277 break;
278 case 'Z':
279 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
280 DO_FAIL("boolean")
281 break;
282 case 'B':
283 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
284 DO_FAIL("byte")
285 break;
286 case 'C':
287 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
288 DO_FAIL("char")
289 break;
290 case 'S':
291 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
292 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
293 DO_FAIL("short")
294 break;
295 case 'I':
296 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
297 DO_ARG("Ljava/lang/Character;", GetChar, Append)
298 DO_ARG("Ljava/lang/Short;", GetShort, Append)
299 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
300 DO_FAIL("int")
301 break;
302 case 'J':
303 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
304 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
305 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
306 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
307 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
308 DO_FAIL("long")
309 break;
310 case 'F':
311 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
312 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
313 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
314 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
315 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
316 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
317 DO_FAIL("float")
318 break;
319 case 'D':
320 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
321 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
322 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
323 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
324 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
325 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
326 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
327 DO_FAIL("double")
328 break;
329#ifndef NDEBUG
330 default:
331 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
332#endif
333 }
334#undef DO_FIRST_ARG
335#undef DO_ARG
336#undef DO_FAIL
337 }
338 return true;
339 }
340
341 private:
342 enum { kSmallArgArraySize = 16 };
343 const char* const shorty_;
344 const uint32_t shorty_len_;
345 uint32_t num_bytes_;
346 uint32_t* arg_array_;
347 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700348 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700349};
350
351static void CheckMethodArguments(mirror::ArtMethod* m, uint32_t* args)
352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700353 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700354 if (params == nullptr) {
355 return; // No arguments so nothing to check.
356 }
357 uint32_t offset = 0;
358 uint32_t num_params = params->Size();
359 size_t error_count = 0;
360 if (!m->IsStatic()) {
361 offset = 1;
362 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700363 // TODO: If args contain object references, it may cause problems
364 Thread* self = Thread::Current();
365 StackHandleScope<1> hs(self);
366 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
367 MethodHelper mh(h_m);
Ian Rogers53b8b092014-03-13 23:45:53 -0700368 for (uint32_t i = 0; i < num_params; i++) {
369 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700370 mirror::Class* param_type = mh.GetClassFromTypeIdx(type_idx);
Ian Rogers53b8b092014-03-13 23:45:53 -0700371 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700372 CHECK(self->IsExceptionPending());
373 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700374 << h_m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers53b8b092014-03-13 23:45:53 -0700375 << self->GetException(nullptr)->Dump();
376 self->ClearException();
377 ++error_count;
378 } else if (!param_type->IsPrimitive()) {
379 // TODO: check primitives are in range.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700380 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
381 // this is a hard to fix problem since the args can contain Object*, we need to save and
382 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers53b8b092014-03-13 23:45:53 -0700383 mirror::Object* argument = reinterpret_cast<mirror::Object*>(args[i + offset]);
384 if (argument != nullptr && !argument->InstanceOf(param_type)) {
385 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
386 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700387 << " to " << PrettyMethod(h_m.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700388 ++error_count;
389 }
390 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
391 offset++;
392 }
393 }
394 if (error_count > 0) {
395 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
396 // with an argument.
397 JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700398 PrettyMethod(h_m.Get()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700399 }
400}
401
402static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver,
403 mirror::ArtMethod* method)
404 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
405 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
406}
407
408
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700409static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
410 mirror::ArtMethod* method, ArgArray* arg_array, JValue* result,
411 const char* shorty)
Ian Rogers53b8b092014-03-13 23:45:53 -0700412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
413 uint32_t* args = arg_array->GetArray();
414 if (UNLIKELY(soa.Env()->check_jni)) {
415 CheckMethodArguments(method, args);
416 }
417 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
418}
419
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700420JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
421 va_list args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Dave Allison03c97852014-08-14 17:02:48 +0000423 // We want to make sure that the stack is not within a small distance from the
424 // protected region in case we are calling into a leaf function whose stack
425 // check has been elided.
426 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
427 ThrowStackOverflowError(soa.Self());
428 return JValue();
429 }
430
Ian Rogers53b8b092014-03-13 23:45:53 -0700431 mirror::ArtMethod* method = soa.DecodeMethod(mid);
432 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700433 uint32_t shorty_len = 0;
434 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700435 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700436 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700437 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700438 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700439 return result;
440}
441
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700442JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, mirror::Object* receiver,
Ian Rogers53b8b092014-03-13 23:45:53 -0700443 jmethodID mid, jvalue* args) {
Dave Allison03c97852014-08-14 17:02:48 +0000444 // We want to make sure that the stack is not within a small distance from the
445 // protected region in case we are calling into a leaf function whose stack
446 // check has been elided.
447 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
448 ThrowStackOverflowError(soa.Self());
449 return JValue();
450 }
451
Ian Rogers53b8b092014-03-13 23:45:53 -0700452 mirror::ArtMethod* method = soa.DecodeMethod(mid);
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.BuildArgArrayFromJValues(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 InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700463 mirror::Object* receiver, jmethodID mid, jvalue* args) {
Dave Allison03c97852014-08-14 17:02:48 +0000464 // 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 = FindVirtualMethod(receiver, 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 InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700483 jobject obj, jmethodID mid, va_list args) {
Dave Allison03c97852014-08-14 17:02:48 +0000484 // 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::Object* receiver = soa.Decode<mirror::Object*>(obj);
493 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700494 uint32_t shorty_len = 0;
495 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700496 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700497 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700498 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700500 return result;
501}
502
503void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset,
504 MethodHelper& mh, JValue* result) {
Dave Allison03c97852014-08-14 17:02:48 +0000505 // 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) < self->GetStackEnd())) {
509 ThrowStackOverflowError(self);
510 return;
511 }
512
Ian Rogers53b8b092014-03-13 23:45:53 -0700513 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
514 arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);
515 shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result,
516 mh.GetShorty());
517}
518
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700519jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700520 jobject javaReceiver, jobject javaArgs, bool accessible) {
Dave Allison03c97852014-08-14 17:02:48 +0000521 // We want to make sure that the stack is not within a small distance from the
522 // protected region in case we are calling into a leaf function whose stack
523 // check has been elided.
524 if (UNLIKELY(__builtin_frame_address(0) <
525 soa.Self()->GetStackEndForInterpreter(true))) {
526 ThrowStackOverflowError(soa.Self());
527 return nullptr;
528 }
529
Ian Rogers62f05122014-03-21 11:21:29 -0700530 mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700531
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800532 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800533 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700534 StackHandleScope<1> hs(soa.Self());
535 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
536 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800537 return nullptr;
538 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700539 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700540 }
541
Ian Rogers53b8b092014-03-13 23:45:53 -0700542 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700543 if (!m->IsStatic()) {
544 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800545 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Ian Rogers53b8b092014-03-13 23:45:53 -0700546 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700547 return NULL;
548 }
549
550 // Find the actual implementation of the virtual method.
551 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
552 }
553
554 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 mirror::ObjectArray<mirror::Object>* objects =
556 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700557 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700558 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
559 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800560 if (arg_count != classes_size) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 ThrowIllegalArgumentException(NULL,
562 StringPrintf("Wrong number of arguments; expected %d, got %d",
563 classes_size, arg_count).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700564 return NULL;
565 }
566
Jeff Haocb4581a2014-03-28 15:43:37 -0700567 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartier9ee0f772014-09-26 14:32:37 -0700568 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags())) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700569 ThrowIllegalAccessException(nullptr, StringPrintf("Cannot access method: %s",
570 PrettyMethod(m).c_str()).c_str());
571 return nullptr;
572 }
573
Ian Rogers53b8b092014-03-13 23:45:53 -0700574 // Invoke the method.
575 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700576 uint32_t shorty_len = 0;
577 const char* shorty = m->GetShorty(&shorty_len);
578 ArgArray arg_array(shorty, shorty_len);
579 StackHandleScope<1> hs(soa.Self());
580 MethodHelper mh(hs.NewHandle(m));
Ian Rogerse18fdd22014-03-14 13:29:43 -0700581 if (!arg_array.BuildArgArrayFromObjectArray(soa, receiver, objects, mh)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700582 CHECK(soa.Self()->IsExceptionPending());
583 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700584 }
585
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700586 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700587
588 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 if (soa.Self()->IsExceptionPending()) {
590 jthrowable th = soa.Env()->ExceptionOccurred();
591 soa.Env()->ExceptionClear();
592 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
593 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
594 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
595 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700596 return NULL;
597 }
598
599 // Box if necessary and return.
Ian Rogers53b8b092014-03-13 23:45:53 -0700600 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(),
601 result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700602}
603
Ian Rogers53b8b092014-03-13 23:45:53 -0700604bool VerifyObjectIsClass(mirror::Object* o, mirror::Class* c) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700605 if (o == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800606 ThrowNullPointerException(NULL, "null receiver");
607 return false;
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700608 } else if (!o->InstanceOf(c)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700609 std::string expected_class_name(PrettyDescriptor(c));
610 std::string actual_class_name(PrettyTypeOf(o));
Ian Rogers62d6c772013-02-27 08:32:07 -0800611 ThrowIllegalArgumentException(NULL,
612 StringPrintf("Expected receiver of type %s, but got %s",
613 expected_class_name.c_str(),
614 actual_class_name.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700615 return false;
616 }
617 return true;
618}
619
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700621 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800622 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700623 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700624 if (src_class == Primitive::kPrimVoid) {
625 // There's no such thing as a void field, and void methods invoked via reflection return null.
626 return nullptr;
627 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700628
Ian Rogers84956ff2014-03-26 23:52:41 -0700629 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800630 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700631 switch (src_class) {
632 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700633 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800634 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700635 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700636 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800638 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700639 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700640 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800642 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700643 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700644 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700645 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800646 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700647 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700648 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800650 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700651 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700652 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700653 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800654 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700655 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700656 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800658 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700659 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700660 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800662 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700663 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700664 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700665 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800666 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700667 }
668
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700670 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800671
Ian Rogers53b8b092014-03-13 23:45:53 -0700672 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800673 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800674 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
675 arg_array.AppendWide(value.GetJ());
676 } else {
677 arg_array.Append(value.GetI());
678 }
679
680 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800681 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800682 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700683}
684
Ian Rogers84956ff2014-03-26 23:52:41 -0700685static std::string UnboxingFailureKind(mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700686 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700687 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700688 return "field " + PrettyField(f, false);
689 }
690 return "result";
691}
692
Ian Rogers62d6c772013-02-27 08:32:07 -0800693static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700694 mirror::Class* dst_class, mirror::ArtField* f,
695 JValue* unboxed_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700696 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700697 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700698 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700699 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800700 if (!unbox_for_result) {
701 ThrowIllegalArgumentException(throw_location,
702 StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700703 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800704 PrettyDescriptor(dst_class).c_str(),
705 PrettyTypeOf(o).c_str()).c_str());
706 } else {
707 ThrowClassCastException(throw_location,
708 StringPrintf("Couldn't convert result of type %s to %s",
709 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700710 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800711 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 return false;
713 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700714 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700715 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 }
717 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
718 ThrowIllegalArgumentException(throw_location,
719 StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700720 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700721 return false;
722 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700723 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800724 if (!unbox_for_result) {
725 ThrowIllegalArgumentException(throw_location,
726 StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700727 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800728 PrettyDescriptor(dst_class).c_str()).c_str());
729 } else {
730 ThrowNullPointerException(throw_location,
731 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
732 PrettyDescriptor(dst_class).c_str()).c_str());
733 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700734 return false;
735 }
736
Elliott Hughes1d878f32012-04-11 15:17:54 -0700737 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700738 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700739 mirror::Class* src_class = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700740 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700741 mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700742 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700743 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700744 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700745 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700746 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700747 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700748 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700749 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700750 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700751 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700752 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700753 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700754 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700755 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700756 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700757 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700758 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700759 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700760 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700761 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700762 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700763 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700764 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700765 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700766 } else {
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700767 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800768 ThrowIllegalArgumentException(throw_location,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700769 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
770 PrettyDescriptor(dst_class).c_str(),
771 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700772 return false;
773 }
774
Ian Rogers62d6c772013-02-27 08:32:07 -0800775 return ConvertPrimitiveValue(throw_location, unbox_for_result,
776 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700777 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700778}
779
Ian Rogers84956ff2014-03-26 23:52:41 -0700780bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, mirror::ArtField* f,
781 JValue* unboxed_value) {
782 DCHECK(f != nullptr);
783 return UnboxPrimitive(nullptr, o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700784}
785
Ian Rogers62d6c772013-02-27 08:32:07 -0800786bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700787 mirror::Class* dst_class, JValue* unboxed_value) {
788 return UnboxPrimitive(&throw_location, o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700789}
790
Mathieu Chartier9ee0f772014-09-26 14:32:37 -0700791bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class, uint32_t access_flags) {
792 if ((access_flags & kAccPublic) != 0) {
793 return true;
794 }
795 NthCallerVisitor visitor(self, 2);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700796 visitor.WalkStack();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100797 if (UNLIKELY(visitor.caller == nullptr)) {
798 // The caller is an attached native thread.
Mathieu Chartier9ee0f772014-09-26 14:32:37 -0700799 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100800 }
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700801 mirror::Class* caller_class = visitor.caller->GetDeclaringClass();
Mathieu Chartier9ee0f772014-09-26 14:32:37 -0700802 if (caller_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700803 return true;
804 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700805 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700806 return false;
807 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700808 if ((access_flags & kAccProtected) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700809 if (obj != nullptr && !obj->InstanceOf(caller_class) &&
810 !declaring_class->IsInSamePackage(caller_class)) {
811 return false;
812 } else if (declaring_class->IsAssignableFrom(caller_class)) {
813 return true;
814 }
815 }
Mathieu Chartier9ee0f772014-09-26 14:32:37 -0700816 return declaring_class->IsInSamePackage(caller_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700817}
818
Elliott Hughes418d20f2011-09-22 14:00:39 -0700819} // namespace art