blob: 2db0f5ffc9e26c8a35d69a54d4ae1df3944ff070 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Ian Rogersef7d42f2014-01-06 12:55:46 -080025#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080029#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070033#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_field-inl.h"
36#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
38#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
41#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070043#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052namespace art {
53
Brian Carlstrom7934ac22013-07-26 10:54:15 -070054static const size_t kMonitorsInitial = 32; // Arbitrary.
55static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070056
Brian Carlstrom7934ac22013-07-26 10:54:15 -070057static const size_t kLocalsInitial = 64; // Arbitrary.
58static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070059
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060static const size_t kPinTableInitial = 16; // Arbitrary.
61static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070062
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063static size_t gGlobalsInitial = 512; // Arbitrary.
64static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070065
Brian Carlstrom7934ac22013-07-26 10:54:15 -070066static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
67static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070068
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080069static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070070 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070071 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070072}
73
Jeff Hao19c5d372013-03-15 14:33:43 -070074static bool IsBadJniVersion(int version) {
75 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
76 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
77}
78
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080079static void CheckMethodArguments(mirror::ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080081 const DexFile::TypeList* params = MethodHelper(m).GetParameterTypeList();
82 if (params == nullptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -070083 return; // No arguments so nothing to check.
84 }
Jeff Hao5d917302013-02-27 17:57:33 -080085 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070086 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070087 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -080088 if (!m->IsStatic()) {
89 offset = 1;
90 }
Ian Rogers50b35e22012-10-04 10:09:15 -070091 for (uint32_t i = 0; i < num_params; i++) {
92 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080093 mirror::Class* param_type = MethodHelper(m).GetClassFromTypeIdx(type_idx);
94 if (param_type == nullptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -070095 Thread* self = Thread::Current();
96 CHECK(self->IsExceptionPending());
97 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080098 << MethodHelper(m).GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
99 << self->GetException(nullptr)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700100 self->ClearException();
101 ++error_count;
102 } else if (!param_type->IsPrimitive()) {
103 // TODO: check primitives are in range.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800104 mirror::Object* argument = reinterpret_cast<mirror::Object*>(args[i + offset]);
105 if (argument != nullptr && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
107 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
108 ++error_count;
109 }
Jeff Hao5d917302013-02-27 17:57:33 -0800110 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
111 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700112 }
113 }
114 if (error_count > 0) {
115 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
116 // with an argument.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800117 JniAbortF(nullptr, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700118 }
119}
Elliott Hughesb264f082012-04-06 17:10:10 -0700120
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800121void InvokeWithArgArray(const ScopedObjectAccess& soa, mirror::ArtMethod* method,
Ian Rogers0177e532014-02-11 16:30:46 -0800122 ArgArray* arg_array, JValue* result, const char* shorty)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700124 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700126 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700127 }
Ian Rogers0177e532014-02-11 16:30:46 -0800128 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700129}
130
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
132 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800134 mirror::ArtMethod* method = soa.DecodeMethod(mid);
135 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700136 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800137 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700138 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800139 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800140 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700141 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700142}
143
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800144static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver, mirror::ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700146 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700147}
148
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
150 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800152 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
153 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700154 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800155 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700156 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800157 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800158 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700159 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700160}
161
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
163 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800165 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
166 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700167 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800168 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700169 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800170 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800171 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700172 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700173}
174
Elliott Hughes6b436852011-08-12 10:16:44 -0700175// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
176// separated with slashes but aren't wrapped with "L;" like regular descriptors
177// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
178// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
179// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700180static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700181 std::string result;
182 // Add the missing "L;" if necessary.
183 if (name[0] == '[') {
184 result = name;
185 } else {
186 result += 'L';
187 result += name;
188 result += ';';
189 }
190 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700191 if (result.find('.') != std::string::npos) {
192 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
193 << "\"" << name << "\"";
194 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700195 }
196 return result;
197}
198
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800199static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
203 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
204 "no %s method \"%s.%s%s\"",
205 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700206}
207
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800208static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
210 if (LIKELY(klass->IsInitialized())) {
211 return klass;
212 }
213 SirtRef<mirror::Class> sirt_klass(self, klass);
214 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
215 return nullptr;
216 }
217 return sirt_klass.get();
218}
219
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
221 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800223 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800224 if (c == nullptr) {
225 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700226 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800227 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700228 if (is_static) {
229 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700230 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700231 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800232 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700233 // No virtual method matching the signature. Search declared
234 // private methods and constructors.
235 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700236 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700237 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800238 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800240 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700241 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700243}
244
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800245static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800247 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700248 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
249 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800250 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700251 }
Brian Carlstromce888532013-10-10 00:32:58 -0700252 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800253 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700254 return method->GetDeclaringClass()->GetClassLoader();
255 }
256 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800257 mirror::ClassLoader* class_loader =
258 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
259 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700260 return class_loader;
261 }
262 // See if the override ClassLoader is set for gtests.
263 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800264 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800265 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700266 CHECK(Runtime::Current()->UseCompileTimeClassPath());
267 return class_loader;
268 }
269 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800270 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700271}
272
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
274 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800276 SirtRef<mirror::Class> c(soa.Self(), EnsureInitialized(soa.Self(),
277 soa.Decode<mirror::Class*>(jni_class)));
278 if (c.get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800279 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700280 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800281 mirror::ArtField* field = nullptr;
282 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
284 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800285 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Ian Rogers98379392014-02-24 16:53:16 -0800286 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700287 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700288 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700289 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800290 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800293 ThrowLocation throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800294 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
297 "no type \"%s\" found and so no field \"%s\" could be found in class "
298 "\"%s\" or its superclasses", sig, name,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800299 ClassHelper(c.get()).GetDescriptor());
300 soa.Self()->GetException(nullptr)->SetCause(cause.get());
301 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 }
303 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800306 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700307 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800308 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
310 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
311 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800312 sig, name, ClassHelper(c.get()).GetDescriptor());
313 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700314 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700316}
317
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800318static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700321 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700322 vm->pin_table.Add(array);
323}
324
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800325static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700326 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700328 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700329 vm->pin_table.Remove(array);
330}
331
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800332static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700335 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
337 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
338 "%s offset=%d length=%d %s.length=%d",
339 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700340}
Ian Rogers0571d352011-11-03 19:51:38 -0700341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
343 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
346 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
347 "offset=%d length=%d string.length()=%d", start, length,
348 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700349}
Elliott Hughes814e4032011-08-23 12:07:56 -0700350
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700352 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 // Turn the const char* into a java.lang.String.
354 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800355 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700357 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700358
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 // Choose an appropriate constructor and set up the arguments.
360 jvalue args[2];
361 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800362 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800364 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365 signature = "(Ljava/lang/String;)V";
366 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800367 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 signature = "(Ljava/lang/Throwable;)V";
369 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700370 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
372 args[0].l = s.get();
373 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700374 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800376 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800377 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800379 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 return JNI_ERR;
381 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700382
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800383 ScopedLocalRef<jthrowable> exception(
384 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
385 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 return JNI_ERR;
387 }
Ian Rogersef28b142012-11-30 14:22:18 -0800388 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800390 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700392}
393
Elliott Hughes462c9442012-03-23 18:47:50 -0700394static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800395 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700396 return JNI_ERR;
397 }
398
Elliott Hughes462c9442012-03-23 18:47:50 -0700399 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700400 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800401 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700402 *p_env = self->GetJniEnv();
403 return JNI_OK;
404 }
405
406 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
407
408 // No threads allowed in zygote mode.
409 if (runtime->IsZygote()) {
410 LOG(ERROR) << "Attempt to attach a thread in the zygote";
411 return JNI_ERR;
412 }
413
Elliott Hughes462c9442012-03-23 18:47:50 -0700414 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800415 const char* thread_name = nullptr;
416 jobject thread_group = nullptr;
417 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700418 if (IsBadJniVersion(args->version)) {
419 LOG(ERROR) << "Bad JNI version passed to "
420 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
421 << args->version;
422 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700423 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700424 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700425 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700426 }
Elliott Hughes75770752011-08-24 17:52:38 -0700427
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800428 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800429 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700430 return JNI_ERR;
431 } else {
432 *p_env = Thread::Current()->GetJniEnv();
433 return JNI_OK;
434 }
Elliott Hughes75770752011-08-24 17:52:38 -0700435}
436
Elliott Hughes79082e32011-08-25 12:07:32 -0700437class SharedLibrary {
438 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800439 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700440 : path_(path),
441 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700442 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700443 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700444 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700445 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700446 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 }
448
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800449 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700450 return class_loader_;
451 }
452
453 std::string GetPath() {
454 return path_;
455 }
456
457 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700458 * Check the result of an earlier call to JNI_OnLoad on this library.
459 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700460 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 bool CheckOnLoadResult()
462 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700463 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700464 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700465 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
466 bool okay;
467 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700468 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700469
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700470 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700471 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
472 // caller can continue.
473 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
474 okay = true;
475 } else {
476 while (jni_on_load_result_ == kPending) {
477 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700478 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700480
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 okay = (jni_on_load_result_ == kOkay);
482 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
483 << (okay ? "succeeded" : "failed") << "]";
484 }
485 }
486 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 return okay;
488 }
489
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700491 Thread* self = Thread::Current();
492 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700493
Elliott Hughes79082e32011-08-25 12:07:32 -0700494 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700495 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700496
497 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700498 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 }
500
501 void* FindSymbol(const std::string& symbol_name) {
502 return dlsym(handle_, symbol_name.c_str());
503 }
504
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800505 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800506 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800507 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800508 }
509 }
510
Elliott Hughes79082e32011-08-25 12:07:32 -0700511 private:
512 enum JNI_OnLoadState {
513 kPending,
514 kFailed,
515 kOkay,
516 };
517
518 // Path to library "/system/lib/libjni.so".
519 std::string path_;
520
521 // The void* returned by dlopen(3).
522 void* handle_;
523
524 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800525 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700526
527 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700528 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700529 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700530 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700532 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700533 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700534 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700535};
536
Elliott Hughes79082e32011-08-25 12:07:32 -0700537// This exists mainly to keep implementation details out of the header file.
538class Libraries {
539 public:
540 Libraries() {
541 }
542
543 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700544 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 }
546
Elliott Hughesae80b492012-04-24 10:43:17 -0700547 void Dump(std::ostream& os) const {
548 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700549 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700550 if (!first) {
551 os << ' ';
552 }
553 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700554 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700555 }
556 }
557
558 size_t size() const {
559 return libraries_.size();
560 }
561
Elliott Hughes79082e32011-08-25 12:07:32 -0700562 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700563 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800564 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700565 }
566
567 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700568 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 }
570
571 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800572 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700573 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 std::string jni_short_name(JniShortName(m));
575 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800576 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700577 for (const auto& lib : libraries_) {
578 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 if (library->GetClassLoader() != declaring_class_loader) {
580 // We only search libraries loaded by the appropriate ClassLoader.
581 continue;
582 }
583 // Try the short name then the long name...
584 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800585 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 fn = library->FindSymbol(jni_long_name);
587 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800588 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800589 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
590 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 return fn;
592 }
593 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700595 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700596 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800598 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 }
600
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800601 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800602 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800603 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800604 }
605 }
606
Elliott Hughes79082e32011-08-25 12:07:32 -0700607 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700608 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700609};
610
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
612 jvalue* args) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800613 mirror::ArtMethod* method = soa.DecodeMethod(mid);
614 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700615 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800616 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700617 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800618 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800619 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700620 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800621}
622
Ian Rogersbc939662013-08-15 10:26:54 -0700623#define CHECK_NON_NULL_ARGUMENT(fn, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800624 if (UNLIKELY(value == nullptr)) { \
Ian Rogersbc939662013-08-15 10:26:54 -0700625 JniAbortF(#fn, #value " == null"); \
626 }
627
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700628#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800629 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700630 JniAbortF(#fn, #value " == null"); \
631 }
632
Elliott Hughescdf53122011-08-19 15:46:09 -0700633class JNI {
634 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700635 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 return JNI_VERSION_1_6;
637 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700638
Ian Rogers25e8b912012-09-07 11:31:36 -0700639 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800641 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700642 }
643
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700645 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700646 Runtime* runtime = Runtime::Current();
647 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800650 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700651 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700652 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800653 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700654 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800655 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700656 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700661 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 jobject art_method = env->GetObjectField(
664 java_method, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
665 mirror::ArtMethod* method = soa.Decode<mirror::ArtMethod*>(art_method);
666 DCHECK(method != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700667 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700668 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700669
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700671 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700672 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700673 jobject art_field = env->GetObjectField(java_field,
674 WellKnownClasses::java_lang_reflect_Field_artField);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800675 mirror::ArtField* field = soa.Decode<mirror::ArtField*>(art_field);
676 DCHECK(field != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700679
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700681 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700682 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800683 mirror::ArtMethod* m = soa.DecodeMethod(mid);
684 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700685 jobject art_method = soa.AddLocalReference<jobject>(m);
686 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
687 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800688 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700689 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800690 SetObjectField(env, reflect_method,
691 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700692 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700696 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800698 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700699 jobject art_field = soa.AddLocalReference<jobject>(f);
700 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
701 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800702 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700703 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800704 SetObjectField(env, reflect_field,
705 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700706 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700710 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800712 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 }
715
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700716 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700717 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800719 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700722
Elliott Hughes37f7a402011-08-22 18:56:01 -0700723 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700724 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
725 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800727 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
728 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700729 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughese84278b2012-03-22 10:06:53 -0700732 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700733 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800734 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700735 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 return JNI_TRUE;
737 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700738 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800739 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
740 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700741 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700744
Elliott Hughes37f7a402011-08-22 18:56:01 -0700745 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800747 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
748 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 return JNI_ERR;
750 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800751 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
752 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700753 return JNI_OK;
754 }
755
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700756 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700757 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800758 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700759 }
760
761 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700762 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 }
764
765 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700766 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 }
768
769 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700771
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800772 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
773 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
774 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800775 uint32_t old_throw_dex_pc;
776 {
777 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800778 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800779 old_throw_this_object.reset(old_throw_location.GetThis());
780 old_throw_method.reset(old_throw_location.GetMethod());
781 old_exception.reset(old_exception_obj);
782 old_throw_dex_pc = old_throw_location.GetDexPc();
783 soa.Self()->ClearException();
784 }
785 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
787 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800788 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800790 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700791 } else {
792 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800793 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800794 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800796 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700797 }
798 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800799 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
800 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700801
Ian Rogers62d6c772013-02-27 08:32:07 -0800802 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700804
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800807 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700808 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 }
810
Ian Rogers25e8b912012-09-07 11:31:36 -0700811 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 LOG(FATAL) << "JNI FatalError called: " << msg;
813 }
814
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700815 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800816 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700817 return JNI_ERR;
818 }
Ian Rogersef28b142012-11-30 14:22:18 -0800819 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 return JNI_OK;
821 }
822
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700823 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700824 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800825 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 soa.Env()->PopFrame();
827 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 }
829
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700830 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800831 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 }
833
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700835 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800836 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800837 // Check for null after decoding the object to handle cleared weak globals.
838 if (decoded_obj == nullptr) {
839 return nullptr;
840 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700843 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 return reinterpret_cast<jobject>(ref);
846 }
847
848 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800849 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 return;
851 }
Ian Rogersef28b142012-11-30 14:22:18 -0800852 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800854 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700855 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700856
857 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
858 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
859 << "failed to find entry";
860 }
861 }
862
863 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800865 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 }
867
868 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700869 if (obj != nullptr) {
870 ScopedObjectAccess soa(env);
871 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873 }
874
875 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700876 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800877 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800878 // Check for null after decoding the object to handle cleared weak globals.
879 if (decoded_obj == nullptr) {
880 return nullptr;
881 }
882 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 }
884
885 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800886 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 return;
888 }
Ian Rogersef28b142012-11-30 14:22:18 -0800889 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700890
Ian Rogersef28b142012-11-30 14:22:18 -0800891 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 if (!locals.Remove(cookie, obj)) {
893 // Attempting to delete a local reference that is not in the
894 // topmost local reference frame is a no-op. DeleteLocalRef returns
895 // void and doesn't throw any exceptions, but we should probably
896 // complain about it so the user will notice that things aren't
897 // going quite the way they expect.
898 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
899 << "failed to find entry";
900 }
901 }
902
903 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700904 if (obj1 == obj2) {
905 return JNI_TRUE;
906 } else {
907 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800908 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700909 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700912 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700913 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800915 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800916 if (c == nullptr) {
917 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700918 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700919 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Ian Rogersbc939662013-08-15 10:26:54 -0700922 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700925 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
926 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
927 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 va_end(args);
929 return result;
930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700933 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
934 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800936 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800937 if (c == nullptr) {
938 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700939 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800940 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800941 if (result == nullptr) {
942 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700943 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800946 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800947 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700948 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800949 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700953 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
954 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800956 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800957 if (c == nullptr) {
958 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700959 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800960 mirror::Object* result = c->AllocObject(soa.Self());
961 if (result == nullptr) {
962 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700963 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800966 if (soa.Self()->IsExceptionPending()) {
967 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700968 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800969 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Ian Rogersbc939662013-08-15 10:26:54 -0700972 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
973 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
974 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
975 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700977 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Ian Rogersbc939662013-08-15 10:26:54 -0700980 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
981 const char* sig) {
982 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
983 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
984 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700986 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700992 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
993 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700994 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001001 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1002 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
1004 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1005 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001009 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1010 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
1012 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1013 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_list ap;
1018 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001019 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1020 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001021 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001024 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001028 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1029 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 ScopedObjectAccess soa(env);
1031 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001035 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1036 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_list ap;
1043 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001044 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1045 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1046 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001049 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001053 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1054 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
1056 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001060 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1061 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_list ap;
1068 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001069 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1070 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001071 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001074 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001078 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1079 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 ScopedObjectAccess soa(env);
1081 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001085 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1086 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(env);
1088 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_list ap;
1093 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001094 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1095 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001096 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001099 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001103 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1104 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
1106 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001110 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1111 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112 ScopedObjectAccess soa(env);
1113 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001119 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1120 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1121 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001124 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001128 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1129 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001135 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1136 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 ScopedObjectAccess soa(env);
1138 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_list ap;
1143 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001144 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1145 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001146 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001149 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001153 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1154 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001155 ScopedObjectAccess soa(env);
1156 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001160 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1161 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_list ap;
1168 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001169 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1170 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001171 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001174 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001178 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1179 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001185 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1186 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 ScopedObjectAccess soa(env);
1188 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_list ap;
1193 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001194 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1195 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001196 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001197 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001199 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001203 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1204 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 ScopedObjectAccess soa(env);
1206 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001210 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1211 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(env);
1213 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 va_list ap;
1218 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001219 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1220 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001221 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001222 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001227 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1228 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 ScopedObjectAccess soa(env);
1230 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001234 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1235 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001236 ScopedObjectAccess soa(env);
1237 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001240 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001243 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1244 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001245 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1247 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 va_end(ap);
1249 return local_result;
1250 }
1251
Ian Rogersbc939662013-08-15 10:26:54 -07001252 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1253 va_list args) {
1254 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1255 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 ScopedObjectAccess soa(env);
1257 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1258 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
Ian Rogersbc939662013-08-15 10:26:54 -07001261 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1262 jvalue* args) {
1263 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1264 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 ScopedObjectAccess soa(env);
1266 JValue result(InvokeWithJValues(soa, obj, mid, args));
1267 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Ian Rogersbc939662013-08-15 10:26:54 -07001270 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1271 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001274 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1275 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001276 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001277 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001279 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
Ian Rogersbc939662013-08-15 10:26:54 -07001282 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1283 va_list args) {
1284 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1285 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 ScopedObjectAccess soa(env);
1287 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Ian Rogersbc939662013-08-15 10:26:54 -07001290 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1291 jvalue* args) {
1292 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1293 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001294 ScopedObjectAccess soa(env);
1295 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001298 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001301 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1302 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001303 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001306 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Ian Rogersbc939662013-08-15 10:26:54 -07001309 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1310 va_list args) {
1311 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1312 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 ScopedObjectAccess soa(env);
1314 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Ian Rogersbc939662013-08-15 10:26:54 -07001317 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1318 jvalue* args) {
1319 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1320 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
1322 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001328 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1329 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1330 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001333 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Ian Rogersbc939662013-08-15 10:26:54 -07001336 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1337 va_list args) {
1338 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1339 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001340 ScopedObjectAccess soa(env);
1341 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
Ian Rogersbc939662013-08-15 10:26:54 -07001344 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1345 jvalue* args) {
1346 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1347 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001348 ScopedObjectAccess soa(env);
1349 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001355 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1356 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001357 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001358 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001360 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Ian Rogersbc939662013-08-15 10:26:54 -07001363 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1364 va_list args) {
1365 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1366 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001367 ScopedObjectAccess soa(env);
1368 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Ian Rogersbc939662013-08-15 10:26:54 -07001371 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1372 jvalue* args) {
1373 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1374 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001375 ScopedObjectAccess soa(env);
1376 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001382 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1383 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001384 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001385 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001387 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Ian Rogersbc939662013-08-15 10:26:54 -07001390 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1391 va_list args) {
1392 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1393 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001394 ScopedObjectAccess soa(env);
1395 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Ian Rogersbc939662013-08-15 10:26:54 -07001398 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1399 jvalue* args) {
1400 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1401 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001402 ScopedObjectAccess soa(env);
1403 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001406 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001408 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001409 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1410 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001411 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001412 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001414 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Ian Rogersbc939662013-08-15 10:26:54 -07001417 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1418 va_list args) {
1419 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1420 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ScopedObjectAccess soa(env);
1422 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Ian Rogersbc939662013-08-15 10:26:54 -07001425 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1426 jvalue* args) {
1427 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1428 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
1430 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001433 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001435 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001436 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1437 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001438 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001441 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Ian Rogersbc939662013-08-15 10:26:54 -07001444 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1445 va_list args) {
1446 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1447 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(env);
1449 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Ian Rogersbc939662013-08-15 10:26:54 -07001452 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1453 jvalue* args) {
1454 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1455 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(env);
1457 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001460 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001462 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001463 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1464 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001465 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001466 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001468 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Ian Rogersbc939662013-08-15 10:26:54 -07001471 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1472 va_list args) {
1473 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1474 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001475 ScopedObjectAccess soa(env);
1476 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Ian Rogersbc939662013-08-15 10:26:54 -07001479 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1480 jvalue* args) {
1481 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1482 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 ScopedObjectAccess soa(env);
1484 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001489 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001490 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1491 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001492 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001493 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 va_end(ap);
1495 }
1496
Brian Carlstromea46f952013-07-30 01:26:50 -07001497 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1498 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001499 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1500 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 ScopedObjectAccess soa(env);
1502 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Ian Rogersbc939662013-08-15 10:26:54 -07001505 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1506 jvalue* args) {
1507 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1508 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 ScopedObjectAccess soa(env);
1510 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 }
1512
Ian Rogersbc939662013-08-15 10:26:54 -07001513 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1514 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1515 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1516 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001518 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001520
Ian Rogersbc939662013-08-15 10:26:54 -07001521 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1522 const char* sig) {
1523 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1524 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1525 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001526 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001527 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001531 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1532 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001534 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1535 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001536 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001538
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001540 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001542 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001543 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001547 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1548 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001550 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1551 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1552 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001553 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001559 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1560 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001561 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001565 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1566 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001567 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001568 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1569 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001570 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001572#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001573 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001574 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001575 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001576 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001577
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001579 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1580 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001582 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1583 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001584 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001586#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001587 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001588 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001589 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001590 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001591
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001620 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001628 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001640 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001649 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001650 }
1651
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001652 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001653 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 }
1655
1656 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001657 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001658 }
1659
1660 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001661 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001662 }
1663
1664 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001665 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001666 }
1667
1668 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001669 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001670 }
1671
1672 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001673 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001674 }
1675
1676 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001677 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001678 }
1679
1680 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001681 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001682 }
1683
1684 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001685 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001686 }
1687
1688 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001689 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001690 }
1691
1692 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001693 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001694 }
1695
1696 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001697 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001698 }
1699
1700 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001701 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001702 }
1703
1704 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001705 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001706 }
1707
1708 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001709 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001710 }
1711
1712 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001713 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001714 }
1715
1716 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001717 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001723 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001724 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001725 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 va_end(ap);
1728 return local_result;
1729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001732 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001733 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001734 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001738 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001739 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001741 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001742 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001748 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001749 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001750 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001752 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001756 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001762 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001764 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001769 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001770 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001771 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001772 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001774 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001778 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001780 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001784 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001786 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001792 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001793 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001794 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001796 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001800 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001802 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001806 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001813 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001814 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001815 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001816 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001818 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001822 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001824 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001828 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001830 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001835 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001836 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001837 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001838 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001840 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001844 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001845 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001846 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001849 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001850 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001852 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001855 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001857 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001858 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001859 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001860 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001862 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001865 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001866 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001868 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001871 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001872 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001874 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001877 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001879 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001880 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001881 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001882 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001884 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001887 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001888 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001890 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001893 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001894 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001896 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 }
1898
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001899 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001901 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001902 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001903 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001904 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001906 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001909 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001910 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001912 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001915 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001916 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001917 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001918 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001921 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001923 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001924 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001925 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001926 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 va_end(ap);
1928 }
1929
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001930 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001931 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001932 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001933 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001936 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001937 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001939 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes814e4032011-08-23 12:07:56 -07001942 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001943 if (UNLIKELY(char_count < 0)) {
1944 JniAbortF("NewString", "char_count < 0: %d", char_count);
1945 return nullptr;
1946 }
1947 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1948 JniAbortF("NewString", "chars == null && char_count > 0");
1949 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001950 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001952 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
1956 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001957 if (utf == nullptr) {
1958 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001960 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001961 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes814e4032011-08-23 12:07:56 -07001965 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001966 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001968 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001969 }
1970
1971 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001972 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001974 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001975 }
1976
Ian Rogersbc939662013-08-15 10:26:54 -07001977 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1978 jchar* buf) {
1979 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001980 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001981 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001982 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001984 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001985 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001986 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1987 memcpy(buf, chars + start, length * sizeof(jchar));
1988 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001989 }
1990
Ian Rogersbc939662013-08-15 10:26:54 -07001991 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1992 char* buf) {
1993 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001995 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001996 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001998 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001999 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002000 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2001 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
2002 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002006 CHECK_NON_NULL_ARGUMENT(GetStringChars, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002008 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2009 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002010 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002011 if (is_copy != nullptr) {
2012 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07002013 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002014 int32_t char_count = s->GetLength();
2015 int32_t offset = s->GetOffset();
2016 jchar* bytes = new jchar[char_count + 1];
2017 for (int32_t i = 0; i < char_count; i++) {
2018 bytes[i] = chars->Get(i + offset);
2019 }
2020 bytes[char_count] = '\0';
2021 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07002022 }
2023
Mathieu Chartier590fee92013-09-13 13:46:47 -07002024 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002025 CHECK_NON_NULL_ARGUMENT(ReleaseStringChars, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002026 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002027 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002028 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002032 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002036 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 if (java_string == nullptr) {
2041 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002042 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002043 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002044 *is_copy = JNI_TRUE;
2045 }
Ian Rogersef28b142012-11-30 14:22:18 -08002046 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002047 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002048 size_t byte_count = s->GetUtfLength();
2049 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002050 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002051 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2052 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2053 bytes[byte_count] = '\0';
2054 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002055 }
2056
Elliott Hughes75770752011-08-24 17:52:38 -07002057 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002058 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002059 }
2060
Elliott Hughesbd935992011-08-22 11:59:34 -07002061 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002062 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002063 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002064 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002065 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002066 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2067 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002068 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002069 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002073 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002074 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002075 mirror::ObjectArray<mirror::Object>* array =
2076 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Ian Rogersbc939662013-08-15 10:26:54 -07002080 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2081 jobject java_value) {
2082 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002083 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002084 mirror::ObjectArray<mirror::Object>* array =
2085 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2086 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002087 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
2090 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002091 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002092 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
2095 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002097 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
2100 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002101 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002102 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
2105 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002107 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
2110 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002111 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002112 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
2115 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002117 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
2120 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002121 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002122 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Ian Rogers1d99e452014-01-02 17:36:41 -08002125 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2126 jobject initial_element) {
2127 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002128 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002129 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002130 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002131
2132 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002133 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002134 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002135 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002136 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002137 if (UNLIKELY(element_class->IsPrimitive())) {
2138 JniAbortF("NewObjectArray", "not an object type: %s",
2139 PrettyDescriptor(element_class).c_str());
2140 return nullptr;
2141 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002142 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002143 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002144 if (UNLIKELY(array_class == nullptr)) {
2145 return nullptr;
2146 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes75770752011-08-24 17:52:38 -07002149 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002150 mirror::ObjectArray<mirror::Object>* result =
2151 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002152 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002153 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002154 if (initial_object != nullptr) {
2155 mirror::Class* element_class = result->GetClass()->GetComponentType();
2156 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2157 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2158 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2159 PrettyDescriptor(element_class).c_str());
2160
2161 } else {
2162 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002163 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002164 }
2165 }
Elliott Hughes75770752011-08-24 17:52:38 -07002166 }
2167 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002168 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
2171 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002172 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002173 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Ian Rogersa15e67d2012-02-28 13:51:55 -08002176 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002177 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002178 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002179 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002180 gc::Heap* heap = Runtime::Current()->GetHeap();
2181 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002182 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002183 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002184 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002185 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002186 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002187 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002188 *is_copy = JNI_FALSE;
2189 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002190 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002191 }
2192
Mathieu Chartier590fee92013-09-13 13:46:47 -07002193 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002194 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002195 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002196 }
2197
Elliott Hughes75770752011-08-24 17:52:38 -07002198 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002199 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002201 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Elliott Hughes75770752011-08-24 17:52:38 -07002204 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002205 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002207 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
Elliott Hughes75770752011-08-24 17:52:38 -07002210 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002211 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002212 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002213 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughes75770752011-08-24 17:52:38 -07002216 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002217 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002218 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002219 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughes75770752011-08-24 17:52:38 -07002222 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002223 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002224 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002225 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughes75770752011-08-24 17:52:38 -07002228 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002229 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002230 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002231 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Elliott Hughes75770752011-08-24 17:52:38 -07002234 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002235 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002237 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Elliott Hughes75770752011-08-24 17:52:38 -07002240 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002241 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002243 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Mathieu Chartier590fee92013-09-13 13:46:47 -07002246 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2247 jint mode) {
2248 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 }
2250
Mathieu Chartier590fee92013-09-13 13:46:47 -07002251 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2252 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Mathieu Chartier590fee92013-09-13 13:46:47 -07002255 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2256 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Mathieu Chartier590fee92013-09-13 13:46:47 -07002259 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2260 jint mode) {
2261 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Mathieu Chartier590fee92013-09-13 13:46:47 -07002264 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2265 jint mode) {
2266 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Mathieu Chartier590fee92013-09-13 13:46:47 -07002269 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2270 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272
Mathieu Chartier590fee92013-09-13 13:46:47 -07002273 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2274 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Mathieu Chartier590fee92013-09-13 13:46:47 -07002277 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2278 jint mode) {
2279 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2283 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002284 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002285 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2286 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 }
2288
Ian Rogersbc939662013-08-15 10:26:54 -07002289 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2290 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002291 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002292 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 }
2294
Ian Rogersbc939662013-08-15 10:26:54 -07002295 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2296 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002297 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002298 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 }
2300
Ian Rogersbc939662013-08-15 10:26:54 -07002301 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2302 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002303 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002304 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2305 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Ian Rogersbc939662013-08-15 10:26:54 -07002308 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2309 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002310 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002311 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2312 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Ian Rogersbc939662013-08-15 10:26:54 -07002315 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2316 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002317 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002318 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320
Ian Rogersbc939662013-08-15 10:26:54 -07002321 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2322 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002323 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002324 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 }
2326
Ian Rogersbc939662013-08-15 10:26:54 -07002327 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2328 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002329 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002330 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2331 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002332 }
2333
Ian Rogersbc939662013-08-15 10:26:54 -07002334 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2335 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002336 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002337 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 }
2339
Ian Rogersbc939662013-08-15 10:26:54 -07002340 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2341 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002343 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 }
2345
Ian Rogersbc939662013-08-15 10:26:54 -07002346 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2347 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002349 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2353 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002355 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Ian Rogersbc939662013-08-15 10:26:54 -07002358 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2359 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002361 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363
Ian Rogersbc939662013-08-15 10:26:54 -07002364 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2365 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002366 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002367 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 }
2369
Ian Rogersbc939662013-08-15 10:26:54 -07002370 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2371 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002373 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002374 }
2375
Ian Rogersbc939662013-08-15 10:26:54 -07002376 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2377 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002379 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2380 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002381 }
2382
Ian Rogersbc939662013-08-15 10:26:54 -07002383 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2384 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002385 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2386 }
2387
Ian Rogersbc939662013-08-15 10:26:54 -07002388 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2389 jint method_count, bool return_errors) {
2390 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002391 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002392 return JNI_ERR; // Not reached.
2393 }
2394 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002395 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002396 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002397 if (UNLIKELY(method_count == 0)) {
2398 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2399 << PrettyDescriptor(c);
2400 return JNI_OK;
2401 }
2402 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2403 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002404 const char* name = methods[i].name;
2405 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002406 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002407 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002408 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002409 ++sig;
2410 }
2411
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002412 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2413 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002414 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002415 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002416 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002417 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002418 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002419 << PrettyDescriptor(c) << "." << name << sig << " in "
2420 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002421 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002423 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002424 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002425 << PrettyDescriptor(c) << "." << name << sig
2426 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002427 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002428 return JNI_ERR;
2429 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002430
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002431 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002432
Ian Rogers1eb512d2013-10-18 15:42:20 -07002433 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002434 }
2435 return JNI_OK;
2436 }
2437
Elliott Hughes5174fe62011-08-23 15:12:35 -07002438 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002439 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002440 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002441 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002442
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002443 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002444
2445 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002446 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002447 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002449 }
2450 }
2451 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002452 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002453 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002454 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002455 }
2456 }
2457
2458 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002459 }
2460
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002461 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2462 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002463 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002464 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002465 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2466 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002467 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002468 return JNI_ERR;
2469 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002470 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002471 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002472 }
2473
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002474 static jint MonitorExit(JNIEnv* env, jobject java_object)
2475 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002476 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002477 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002478 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002479 o->MonitorExit(soa.Self());
2480 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002481 return JNI_ERR;
2482 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002484 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002485 }
2486
2487 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002488 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002489 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002490 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002491 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002492 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002493 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002494 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002495 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002496 }
2497
Elliott Hughescdf53122011-08-19 15:46:09 -07002498 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002499 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002500 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002501 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002502 if (address == nullptr && capacity != 0) {
2503 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002504 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002505
Ian Rogers936b37f2014-02-14 00:52:24 -08002506 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002507 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002508 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002509 jint capacity_arg = static_cast<jint>(capacity);
2510
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002511 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2512 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002513 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002514 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002515 }
2516
Elliott Hughesb465ab02011-08-24 11:21:21 -07002517 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002518 return reinterpret_cast<void*>(env->GetLongField(
2519 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002520 }
2521
Elliott Hughesb465ab02011-08-24 11:21:21 -07002522 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002523 return static_cast<jlong>(env->GetIntField(
2524 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002525 }
2526
Elliott Hughesb465ab02011-08-24 11:21:21 -07002527 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002528 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002529
2530 // Do we definitely know what kind of reference this is?
2531 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2532 IndirectRefKind kind = GetIndirectRefKind(ref);
2533 switch (kind) {
2534 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002535 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002536 return JNILocalRefType;
2537 }
2538 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002539 case kGlobal:
2540 return JNIGlobalRefType;
2541 case kWeakGlobal:
2542 return JNIWeakGlobalRefType;
2543 case kSirtOrInvalid:
2544 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002545 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002546 return JNILocalRefType;
2547 }
2548
Ian Rogersef28b142012-11-30 14:22:18 -08002549 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002550 return JNIInvalidRefType;
2551 }
2552
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002553 // If we're handing out direct pointers, check whether it's a direct pointer to a local
2554 // reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002555 {
2556 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002557 if (soa.Decode<mirror::Object*>(java_object) ==
2558 reinterpret_cast<mirror::Object*>(java_object)) {
2559 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<mirror::Object*>(java_object))) {
Ian Rogersef28b142012-11-30 14:22:18 -08002560 return JNILocalRefType;
2561 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002562 }
2563 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002564 return JNIInvalidRefType;
2565 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002566 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2567 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002568 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002569
2570 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002571 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2572 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002573 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002574 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002575 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2576 return JNI_ERR;
2577 }
2578 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002579 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002580 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2581 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002582 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002583 soa.Self()->ThrowOutOfMemoryError(caller);
2584 }
2585 return okay ? JNI_OK : JNI_ERR;
2586 }
2587
2588 template<typename JniT, typename ArtT>
2589 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002591 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002592 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002593 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002594 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002595 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002596 return soa.AddLocalReference<JniT>(result);
2597 }
2598
2599 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2600 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2601 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002602 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002603 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2604 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002605 // Only make a copy if necessary.
2606 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2607 if (is_copy != nullptr) {
2608 *is_copy = JNI_TRUE;
2609 }
2610 static const size_t component_size = array->GetClass()->GetComponentSize();
2611 size_t size = array->GetLength() * component_size;
2612 void* data = new uint64_t[RoundUp(size, 8) / 8];
2613 memcpy(data, array->GetData(), size);
2614 return reinterpret_cast<CArrayT>(data);
2615 } else {
2616 if (is_copy != nullptr) {
2617 *is_copy = JNI_FALSE;
2618 }
2619 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002620 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002621 }
2622
Mathieu Chartier590fee92013-09-13 13:46:47 -07002623 template <typename ArrayT, typename ElementT>
2624 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2625 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002626 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002627 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002628 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002629 gc::Heap* heap = Runtime::Current()->GetHeap();
2630 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2631 size_t bytes = array->GetLength() * component_size;
2632 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2633 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002634 if (is_copy) {
2635 // Sanity check: If elements is not the same as the java array's data, it better not be a
2636 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2637 // copies we make?
2638 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2639 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2640 reinterpret_cast<void*>(elements), array_data);
2641 return;
2642 }
2643 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002644 // Don't need to copy if we had a direct pointer.
2645 if (mode != JNI_ABORT && is_copy) {
2646 memcpy(array_data, elements, bytes);
2647 }
2648 if (mode != JNI_COMMIT) {
2649 if (is_copy) {
2650 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002651 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002652 // Non copy to a movable object must means that we had disabled the moving GC.
2653 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002654 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002655 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002656 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002657 }
2658
2659 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2660 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2661 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002663 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002664 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2665 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2666 ThrowAIOOBE(soa, array, start, length, "src");
2667 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002668 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002669 JavaT* data = array->GetData();
2670 memcpy(buf, data + start, length * sizeof(JavaT));
2671 }
2672 }
2673
2674 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2675 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2676 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002677 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002678 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002679 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2680 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2681 ThrowAIOOBE(soa, array, start, length, "dst");
2682 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002683 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002684 JavaT* data = array->GetData();
2685 memcpy(data + start, buf, length * sizeof(JavaT));
2686 }
2687 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002688};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002689
Elliott Hughes88c5c352012-03-15 18:49:48 -07002690const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002691 nullptr, // reserved0.
2692 nullptr, // reserved1.
2693 nullptr, // reserved2.
2694 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002695 JNI::GetVersion,
2696 JNI::DefineClass,
2697 JNI::FindClass,
2698 JNI::FromReflectedMethod,
2699 JNI::FromReflectedField,
2700 JNI::ToReflectedMethod,
2701 JNI::GetSuperclass,
2702 JNI::IsAssignableFrom,
2703 JNI::ToReflectedField,
2704 JNI::Throw,
2705 JNI::ThrowNew,
2706 JNI::ExceptionOccurred,
2707 JNI::ExceptionDescribe,
2708 JNI::ExceptionClear,
2709 JNI::FatalError,
2710 JNI::PushLocalFrame,
2711 JNI::PopLocalFrame,
2712 JNI::NewGlobalRef,
2713 JNI::DeleteGlobalRef,
2714 JNI::DeleteLocalRef,
2715 JNI::IsSameObject,
2716 JNI::NewLocalRef,
2717 JNI::EnsureLocalCapacity,
2718 JNI::AllocObject,
2719 JNI::NewObject,
2720 JNI::NewObjectV,
2721 JNI::NewObjectA,
2722 JNI::GetObjectClass,
2723 JNI::IsInstanceOf,
2724 JNI::GetMethodID,
2725 JNI::CallObjectMethod,
2726 JNI::CallObjectMethodV,
2727 JNI::CallObjectMethodA,
2728 JNI::CallBooleanMethod,
2729 JNI::CallBooleanMethodV,
2730 JNI::CallBooleanMethodA,
2731 JNI::CallByteMethod,
2732 JNI::CallByteMethodV,
2733 JNI::CallByteMethodA,
2734 JNI::CallCharMethod,
2735 JNI::CallCharMethodV,
2736 JNI::CallCharMethodA,
2737 JNI::CallShortMethod,
2738 JNI::CallShortMethodV,
2739 JNI::CallShortMethodA,
2740 JNI::CallIntMethod,
2741 JNI::CallIntMethodV,
2742 JNI::CallIntMethodA,
2743 JNI::CallLongMethod,
2744 JNI::CallLongMethodV,
2745 JNI::CallLongMethodA,
2746 JNI::CallFloatMethod,
2747 JNI::CallFloatMethodV,
2748 JNI::CallFloatMethodA,
2749 JNI::CallDoubleMethod,
2750 JNI::CallDoubleMethodV,
2751 JNI::CallDoubleMethodA,
2752 JNI::CallVoidMethod,
2753 JNI::CallVoidMethodV,
2754 JNI::CallVoidMethodA,
2755 JNI::CallNonvirtualObjectMethod,
2756 JNI::CallNonvirtualObjectMethodV,
2757 JNI::CallNonvirtualObjectMethodA,
2758 JNI::CallNonvirtualBooleanMethod,
2759 JNI::CallNonvirtualBooleanMethodV,
2760 JNI::CallNonvirtualBooleanMethodA,
2761 JNI::CallNonvirtualByteMethod,
2762 JNI::CallNonvirtualByteMethodV,
2763 JNI::CallNonvirtualByteMethodA,
2764 JNI::CallNonvirtualCharMethod,
2765 JNI::CallNonvirtualCharMethodV,
2766 JNI::CallNonvirtualCharMethodA,
2767 JNI::CallNonvirtualShortMethod,
2768 JNI::CallNonvirtualShortMethodV,
2769 JNI::CallNonvirtualShortMethodA,
2770 JNI::CallNonvirtualIntMethod,
2771 JNI::CallNonvirtualIntMethodV,
2772 JNI::CallNonvirtualIntMethodA,
2773 JNI::CallNonvirtualLongMethod,
2774 JNI::CallNonvirtualLongMethodV,
2775 JNI::CallNonvirtualLongMethodA,
2776 JNI::CallNonvirtualFloatMethod,
2777 JNI::CallNonvirtualFloatMethodV,
2778 JNI::CallNonvirtualFloatMethodA,
2779 JNI::CallNonvirtualDoubleMethod,
2780 JNI::CallNonvirtualDoubleMethodV,
2781 JNI::CallNonvirtualDoubleMethodA,
2782 JNI::CallNonvirtualVoidMethod,
2783 JNI::CallNonvirtualVoidMethodV,
2784 JNI::CallNonvirtualVoidMethodA,
2785 JNI::GetFieldID,
2786 JNI::GetObjectField,
2787 JNI::GetBooleanField,
2788 JNI::GetByteField,
2789 JNI::GetCharField,
2790 JNI::GetShortField,
2791 JNI::GetIntField,
2792 JNI::GetLongField,
2793 JNI::GetFloatField,
2794 JNI::GetDoubleField,
2795 JNI::SetObjectField,
2796 JNI::SetBooleanField,
2797 JNI::SetByteField,
2798 JNI::SetCharField,
2799 JNI::SetShortField,
2800 JNI::SetIntField,
2801 JNI::SetLongField,
2802 JNI::SetFloatField,
2803 JNI::SetDoubleField,
2804 JNI::GetStaticMethodID,
2805 JNI::CallStaticObjectMethod,
2806 JNI::CallStaticObjectMethodV,
2807 JNI::CallStaticObjectMethodA,
2808 JNI::CallStaticBooleanMethod,
2809 JNI::CallStaticBooleanMethodV,
2810 JNI::CallStaticBooleanMethodA,
2811 JNI::CallStaticByteMethod,
2812 JNI::CallStaticByteMethodV,
2813 JNI::CallStaticByteMethodA,
2814 JNI::CallStaticCharMethod,
2815 JNI::CallStaticCharMethodV,
2816 JNI::CallStaticCharMethodA,
2817 JNI::CallStaticShortMethod,
2818 JNI::CallStaticShortMethodV,
2819 JNI::CallStaticShortMethodA,
2820 JNI::CallStaticIntMethod,
2821 JNI::CallStaticIntMethodV,
2822 JNI::CallStaticIntMethodA,
2823 JNI::CallStaticLongMethod,
2824 JNI::CallStaticLongMethodV,
2825 JNI::CallStaticLongMethodA,
2826 JNI::CallStaticFloatMethod,
2827 JNI::CallStaticFloatMethodV,
2828 JNI::CallStaticFloatMethodA,
2829 JNI::CallStaticDoubleMethod,
2830 JNI::CallStaticDoubleMethodV,
2831 JNI::CallStaticDoubleMethodA,
2832 JNI::CallStaticVoidMethod,
2833 JNI::CallStaticVoidMethodV,
2834 JNI::CallStaticVoidMethodA,
2835 JNI::GetStaticFieldID,
2836 JNI::GetStaticObjectField,
2837 JNI::GetStaticBooleanField,
2838 JNI::GetStaticByteField,
2839 JNI::GetStaticCharField,
2840 JNI::GetStaticShortField,
2841 JNI::GetStaticIntField,
2842 JNI::GetStaticLongField,
2843 JNI::GetStaticFloatField,
2844 JNI::GetStaticDoubleField,
2845 JNI::SetStaticObjectField,
2846 JNI::SetStaticBooleanField,
2847 JNI::SetStaticByteField,
2848 JNI::SetStaticCharField,
2849 JNI::SetStaticShortField,
2850 JNI::SetStaticIntField,
2851 JNI::SetStaticLongField,
2852 JNI::SetStaticFloatField,
2853 JNI::SetStaticDoubleField,
2854 JNI::NewString,
2855 JNI::GetStringLength,
2856 JNI::GetStringChars,
2857 JNI::ReleaseStringChars,
2858 JNI::NewStringUTF,
2859 JNI::GetStringUTFLength,
2860 JNI::GetStringUTFChars,
2861 JNI::ReleaseStringUTFChars,
2862 JNI::GetArrayLength,
2863 JNI::NewObjectArray,
2864 JNI::GetObjectArrayElement,
2865 JNI::SetObjectArrayElement,
2866 JNI::NewBooleanArray,
2867 JNI::NewByteArray,
2868 JNI::NewCharArray,
2869 JNI::NewShortArray,
2870 JNI::NewIntArray,
2871 JNI::NewLongArray,
2872 JNI::NewFloatArray,
2873 JNI::NewDoubleArray,
2874 JNI::GetBooleanArrayElements,
2875 JNI::GetByteArrayElements,
2876 JNI::GetCharArrayElements,
2877 JNI::GetShortArrayElements,
2878 JNI::GetIntArrayElements,
2879 JNI::GetLongArrayElements,
2880 JNI::GetFloatArrayElements,
2881 JNI::GetDoubleArrayElements,
2882 JNI::ReleaseBooleanArrayElements,
2883 JNI::ReleaseByteArrayElements,
2884 JNI::ReleaseCharArrayElements,
2885 JNI::ReleaseShortArrayElements,
2886 JNI::ReleaseIntArrayElements,
2887 JNI::ReleaseLongArrayElements,
2888 JNI::ReleaseFloatArrayElements,
2889 JNI::ReleaseDoubleArrayElements,
2890 JNI::GetBooleanArrayRegion,
2891 JNI::GetByteArrayRegion,
2892 JNI::GetCharArrayRegion,
2893 JNI::GetShortArrayRegion,
2894 JNI::GetIntArrayRegion,
2895 JNI::GetLongArrayRegion,
2896 JNI::GetFloatArrayRegion,
2897 JNI::GetDoubleArrayRegion,
2898 JNI::SetBooleanArrayRegion,
2899 JNI::SetByteArrayRegion,
2900 JNI::SetCharArrayRegion,
2901 JNI::SetShortArrayRegion,
2902 JNI::SetIntArrayRegion,
2903 JNI::SetLongArrayRegion,
2904 JNI::SetFloatArrayRegion,
2905 JNI::SetDoubleArrayRegion,
2906 JNI::RegisterNatives,
2907 JNI::UnregisterNatives,
2908 JNI::MonitorEnter,
2909 JNI::MonitorExit,
2910 JNI::GetJavaVM,
2911 JNI::GetStringRegion,
2912 JNI::GetStringUTFRegion,
2913 JNI::GetPrimitiveArrayCritical,
2914 JNI::ReleasePrimitiveArrayCritical,
2915 JNI::GetStringCritical,
2916 JNI::ReleaseStringCritical,
2917 JNI::NewWeakGlobalRef,
2918 JNI::DeleteWeakGlobalRef,
2919 JNI::ExceptionCheck,
2920 JNI::NewDirectByteBuffer,
2921 JNI::GetDirectBufferAddress,
2922 JNI::GetDirectBufferCapacity,
2923 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002924};
2925
Elliott Hughes75770752011-08-24 17:52:38 -07002926JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002927 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002928 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002929 local_ref_cookie(IRT_FIRST_SEGMENT),
2930 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002931 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002932 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002933 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002934 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002935 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002936 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002937 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002938}
2939
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002940JNIEnvExt::~JNIEnvExt() {
2941}
2942
Mathieu Chartier590fee92013-09-13 13:46:47 -07002943jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2944 if (obj == nullptr) {
2945 return nullptr;
2946 }
2947 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2948}
2949
2950void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2951 if (obj != nullptr) {
2952 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2953 }
2954}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002955void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2956 check_jni = enabled;
2957 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002958}
2959
Elliott Hughes73e66f72012-05-09 09:34:45 -07002960void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2961 locals.Dump(os);
2962 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002963}
2964
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002965void JNIEnvExt::PushFrame(int /*capacity*/) {
2966 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002967 stacked_local_ref_cookies.push_back(local_ref_cookie);
2968 local_ref_cookie = locals.GetSegmentState();
2969}
2970
2971void JNIEnvExt::PopFrame() {
2972 locals.SetSegmentState(local_ref_cookie);
2973 local_ref_cookie = stacked_local_ref_cookies.back();
2974 stacked_local_ref_cookies.pop_back();
2975}
2976
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002977Offset JNIEnvExt::SegmentStateOffset() {
2978 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2979 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2980}
2981
Carl Shapiroea4dca82011-08-01 13:45:38 -07002982// JNI Invocation interface.
2983
Brian Carlstrombddf9762013-05-14 11:35:37 -07002984extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002985 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002986 if (IsBadJniVersion(args->version)) {
2987 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002988 return JNI_EVERSION;
2989 }
2990 Runtime::Options options;
2991 for (int i = 0; i < args->nOptions; ++i) {
2992 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002993 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002994 }
2995 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002996 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002997 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002998 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002999 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003000 bool started = runtime->Start();
3001 if (!started) {
3002 delete Thread::Current()->GetJniEnv();
3003 delete runtime->GetJavaVM();
3004 LOG(WARNING) << "CreateJavaVM failed";
3005 return JNI_ERR;
3006 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003007 *p_env = Thread::Current()->GetJniEnv();
3008 *p_vm = runtime->GetJavaVM();
3009 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003010}
3011
Elliott Hughesf2682d52011-08-15 16:37:04 -07003012extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003013 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003014 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003015 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003016 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003017 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003018 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003019 }
3020 return JNI_OK;
3021}
3022
3023// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003024extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003025 return JNI_ERR;
3026}
3027
Elliott Hughescdf53122011-08-19 15:46:09 -07003028class JII {
3029 public:
3030 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003031 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003032 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003033 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003034 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3035 delete raw_vm->runtime;
3036 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003037 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003038
Elliott Hughescdf53122011-08-19 15:46:09 -07003039 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003040 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003041 }
3042
3043 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003044 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003045 }
3046
3047 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003048 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003049 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003050 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003051 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3052 Runtime* runtime = raw_vm->runtime;
3053 runtime->DetachCurrentThread();
3054 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003055 }
3056
3057 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003058 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3059 // and unlike other calls that take a JNI version doesn't care if you supply
3060 // JNI_VERSION_1_1, which we don't otherwise support.
3061 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003062 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003063 return JNI_EVERSION;
3064 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003065 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003066 return JNI_ERR;
3067 }
3068 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003069 if (thread == nullptr) {
3070 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003071 return JNI_EDETACHED;
3072 }
3073 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003074 return JNI_OK;
3075 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003076};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003077
Elliott Hughes88c5c352012-03-15 18:49:48 -07003078const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003079 nullptr, // reserved0
3080 nullptr, // reserved1
3081 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003082 JII::DestroyJavaVM,
3083 JII::AttachCurrentThread,
3084 JII::DetachCurrentThread,
3085 JII::GetEnv,
3086 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003087};
3088
Elliott Hughesa0957642011-09-02 14:27:33 -07003089JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003090 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003091 check_jni_abort_hook(nullptr),
3092 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003093 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003094 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003095 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003096 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003097 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003098 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003099 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003100 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003101 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003102 libraries(new Libraries),
3103 weak_globals_lock_("JNI weak global reference table lock"),
3104 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3105 allow_new_weak_globals_(true),
3106 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003107 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003108 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003109 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003110 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003111}
3112
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003113JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003114 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003115}
3116
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003117jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3118 if (obj == nullptr) {
3119 return nullptr;
3120 }
3121 MutexLock mu(self, weak_globals_lock_);
3122 while (UNLIKELY(!allow_new_weak_globals_)) {
3123 weak_globals_add_condition_.WaitHoldingLocks(self);
3124 }
3125 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3126 return reinterpret_cast<jweak>(ref);
3127}
3128
3129void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3130 MutexLock mu(self, weak_globals_lock_);
3131 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3132 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3133 << "failed to find entry";
3134 }
3135}
3136
Elliott Hughes88c5c352012-03-15 18:49:48 -07003137void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3138 check_jni = enabled;
3139 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003140}
3141
Elliott Hughesae80b492012-04-24 10:43:17 -07003142void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3143 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3144 if (force_copy) {
3145 os << " (with forcecopy)";
3146 }
3147 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003148 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003149 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003150 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003151 os << "; pins=" << pin_table.Size();
3152 }
3153 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003154 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003155 os << "; globals=" << globals.Capacity();
3156 }
3157 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003158 MutexLock mu(self, weak_globals_lock_);
3159 if (weak_globals_.Capacity() > 0) {
3160 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003161 }
3162 }
3163 os << '\n';
3164
3165 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003166 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003167 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3168 }
3169}
3170
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003171void JavaVMExt::DisallowNewWeakGlobals() {
3172 MutexLock mu(Thread::Current(), weak_globals_lock_);
3173 allow_new_weak_globals_ = false;
3174}
3175
3176void JavaVMExt::AllowNewWeakGlobals() {
3177 Thread* self = Thread::Current();
3178 MutexLock mu(self, weak_globals_lock_);
3179 allow_new_weak_globals_ = true;
3180 weak_globals_add_condition_.Broadcast(self);
3181}
3182
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003183mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3184 MutexLock mu(self, weak_globals_lock_);
3185 while (UNLIKELY(!allow_new_weak_globals_)) {
3186 weak_globals_add_condition_.WaitHoldingLocks(self);
3187 }
3188 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3189}
3190
Elliott Hughes73e66f72012-05-09 09:34:45 -07003191void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003192 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003193 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003194 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003195 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003196 }
3197 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003198 MutexLock mu(self, weak_globals_lock_);
3199 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003200 }
3201 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003202 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003203 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003204 }
3205}
3206
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003207bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003208 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003209 std::string* detail) {
3210 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003211
3212 // See if we've already loaded this library. If we have, and the class loader
3213 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003214 // TODO: for better results we should canonicalize the pathname (or even compare
3215 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003216 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003217 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003218 {
3219 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003220 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003221 library = libraries->Get(path);
3222 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003223 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003224 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003225 // The library will be associated with class_loader. The JNI
3226 // spec says we can't load the same library into more than one
3227 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003228 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003229 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003230 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003231 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003232 return false;
3233 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003234 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003235 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003236 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003237 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003238 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003239 return false;
3240 }
3241 return true;
3242 }
3243
3244 // Open the shared library. Because we're using a full path, the system
3245 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3246 // resolve this library's dependencies though.)
3247
3248 // Failures here are expected when java.library.path has several entries
3249 // and we have to hunt for the lib.
3250
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003251 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3252 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3253 // dlopen) becomes zero from dlclose.
3254
Elliott Hughescdf53122011-08-19 15:46:09 -07003255 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003256 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003257 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003258 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003259 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003260
Elliott Hughes84b2f142012-09-27 09:16:28 -07003261 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003262
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003263 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003264 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003265 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003266 return false;
3267 }
3268
3269 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003270 // TODO: move the locking (and more of this logic) into Libraries.
3271 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003272 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003273 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003274 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003275 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003276 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003277 libraries->Put(path, library);
3278 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003279 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003280 }
3281 if (!created_library) {
3282 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003283 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003284 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003285 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003286
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003287 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3288 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003289
Elliott Hughes79353722013-08-02 16:52:18 -07003290 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003291 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003292 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003293 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003294 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003295 } else {
3296 // Call JNI_OnLoad. We have to override the current class
3297 // loader, which will always be "null" since the stuff at the
3298 // top of the stack is around Runtime.loadLibrary(). (See
3299 // the comments in the JNI FindClass function.)
3300 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3301 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003302 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003303 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003304
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003305 int version = 0;
3306 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003307 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003308 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003309 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003310 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003311
Mathieu Chartier590fee92013-09-13 13:46:47 -07003312 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003313
Elliott Hughes79353722013-08-02 16:52:18 -07003314 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003315 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003316 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003317 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003318 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003319 // It's unwise to call dlclose() here, but we can mark it
3320 // as bad and ensure that future load attempts will fail.
3321 // We don't know how far JNI_OnLoad got, so there could
3322 // be some partially-initialized stuff accessible through
3323 // newly-registered native method calls. We could try to
3324 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003325 } else {
3326 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003327 }
Elliott Hughes79353722013-08-02 16:52:18 -07003328 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003329 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003330 }
3331
Elliott Hughes79353722013-08-02 16:52:18 -07003332 library->SetResult(was_successful);
3333 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003334}
3335
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003336void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003337 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003338 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003339 // If this is a static method, it could be called before the class
3340 // has been initialized.
3341 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003342 c = EnsureInitialized(Thread::Current(), c);
3343 if (c == nullptr) {
3344 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003345 }
3346 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003347 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003348 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003349 std::string detail;
3350 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003351 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003352 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003353 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003354 native_method = libraries->FindNativeMethod(m, detail);
3355 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003356 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003357 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003358 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3359 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003360 }
3361 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003362}
3363
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003364void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003365 MutexLock mu(Thread::Current(), weak_globals_lock_);
3366 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003367 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003368 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003369 if (new_obj == nullptr) {
3370 new_obj = kClearedJniWeakGlobal;
3371 }
3372 *entry = new_obj;
3373 }
3374}
3375
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003376void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003377 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003378 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003379 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003380 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003381 }
3382 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003383 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003384 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003385 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003386 {
3387 MutexLock mu(self, libraries_lock);
3388 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003389 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003390 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003391 // The weak_globals table is visited by the GC itself (because it mutates the table).
3392}
3393
Elliott Hughesc8fece32013-01-02 11:27:23 -08003394void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003395 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003396 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003397 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003398 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3399 }
3400 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3401}
3402
Ian Rogersdf20fe02011-07-20 20:34:16 -07003403} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003404
3405std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3406 switch (rhs) {
3407 case JNIInvalidRefType:
3408 os << "JNIInvalidRefType";
3409 return os;
3410 case JNILocalRefType:
3411 os << "JNILocalRefType";
3412 return os;
3413 case JNIGlobalRefType:
3414 os << "JNIGlobalRefType";
3415 return os;
3416 case JNIWeakGlobalRefType:
3417 os << "JNIWeakGlobalRefType";
3418 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003419 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003420 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003421 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003422 }
3423}