blob: 876de055adde8b84b935d8345eecfa3ec7e1f70d [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"
Elliott Hughese222ee02012-12-13 14:41:43 -080029#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070030#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070033#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070034#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070035#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070036#include "mirror/art_field-inl.h"
37#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class-inl.h"
39#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
42#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070044#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070045#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070051#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070052
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053namespace art {
54
Brian Carlstrom7934ac22013-07-26 10:54:15 -070055static const size_t kMonitorsInitial = 32; // Arbitrary.
56static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070057
Brian Carlstrom7934ac22013-07-26 10:54:15 -070058static const size_t kLocalsInitial = 64; // Arbitrary.
59static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070060
Brian Carlstrom7934ac22013-07-26 10:54:15 -070061static const size_t kPinTableInitial = 16; // Arbitrary.
62static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070063
Brian Carlstrom7934ac22013-07-26 10:54:15 -070064static size_t gGlobalsInitial = 512; // Arbitrary.
65static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070066
Brian Carlstrom7934ac22013-07-26 10:54:15 -070067static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
68static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070069
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080070static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070072 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070073}
74
Jeff Hao19c5d372013-03-15 14:33:43 -070075static bool IsBadJniVersion(int version) {
76 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
77 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
78}
79
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080080static void CheckMethodArguments(mirror::ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070081 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080082 const DexFile::TypeList* params = MethodHelper(m).GetParameterTypeList();
83 if (params == nullptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -070084 return; // No arguments so nothing to check.
85 }
Jeff Hao5d917302013-02-27 17:57:33 -080086 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070087 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070088 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -080089 if (!m->IsStatic()) {
90 offset = 1;
91 }
Ian Rogers50b35e22012-10-04 10:09:15 -070092 for (uint32_t i = 0; i < num_params; i++) {
93 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080094 mirror::Class* param_type = MethodHelper(m).GetClassFromTypeIdx(type_idx);
95 if (param_type == nullptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -070096 Thread* self = Thread::Current();
97 CHECK(self->IsExceptionPending());
98 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080099 << MethodHelper(m).GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
100 << self->GetException(nullptr)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 self->ClearException();
102 ++error_count;
103 } else if (!param_type->IsPrimitive()) {
104 // TODO: check primitives are in range.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800105 mirror::Object* argument = reinterpret_cast<mirror::Object*>(args[i + offset]);
106 if (argument != nullptr && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700107 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
108 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
109 ++error_count;
110 }
Jeff Hao5d917302013-02-27 17:57:33 -0800111 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
112 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 }
114 }
115 if (error_count > 0) {
116 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
117 // with an argument.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800118 JniAbortF(nullptr, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700119 }
120}
Elliott Hughesb264f082012-04-06 17:10:10 -0700121
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800122void InvokeWithArgArray(const ScopedObjectAccess& soa, mirror::ArtMethod* method,
Ian Rogers0177e532014-02-11 16:30:46 -0800123 ArgArray* arg_array, JValue* result, const char* shorty)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700125 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700127 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700128 }
Ian Rogers0177e532014-02-11 16:30:46 -0800129 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700130}
131
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
133 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800135 mirror::ArtMethod* method = soa.DecodeMethod(mid);
136 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700137 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800138 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700139 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800140 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800141 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700142 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700143}
144
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver, mirror::ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700147 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700148}
149
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
151 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800153 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
154 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700155 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800156 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700157 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800158 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800159 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700160 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700161}
162
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
164 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800166 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
167 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700168 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800169 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700170 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800171 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800172 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700173 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700174}
175
Elliott Hughes6b436852011-08-12 10:16:44 -0700176// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
177// separated with slashes but aren't wrapped with "L;" like regular descriptors
178// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
179// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
180// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700181static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700182 std::string result;
183 // Add the missing "L;" if necessary.
184 if (name[0] == '[') {
185 result = name;
186 } else {
187 result += 'L';
188 result += name;
189 result += ';';
190 }
191 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700192 if (result.find('.') != std::string::npos) {
193 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
194 << "\"" << name << "\"";
195 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700196 }
197 return result;
198}
199
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800200static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
204 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
205 "no %s method \"%s.%s%s\"",
206 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700207}
208
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800209static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
211 if (LIKELY(klass->IsInitialized())) {
212 return klass;
213 }
214 SirtRef<mirror::Class> sirt_klass(self, klass);
215 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
216 return nullptr;
217 }
218 return sirt_klass.get();
219}
220
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
222 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800224 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800225 if (c == nullptr) {
226 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700227 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800228 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700229 if (is_static) {
230 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700231 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700232 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800233 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 // No virtual method matching the signature. Search declared
235 // private methods and constructors.
236 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700237 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700238 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800239 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800241 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700242 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700244}
245
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800246static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800248 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700249 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
250 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800251 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700252 }
Brian Carlstromce888532013-10-10 00:32:58 -0700253 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800254 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700255 return method->GetDeclaringClass()->GetClassLoader();
256 }
257 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800258 mirror::ClassLoader* class_loader =
259 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
260 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700261 return class_loader;
262 }
263 // See if the override ClassLoader is set for gtests.
264 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800265 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700266 // If so, CommonTest should have set UseCompileTimeClassPath.
267 CHECK(Runtime::Current()->UseCompileTimeClassPath());
268 return class_loader;
269 }
270 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800271 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700272}
273
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
275 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 SirtRef<mirror::Class> c(soa.Self(), EnsureInitialized(soa.Self(),
278 soa.Decode<mirror::Class*>(jni_class)));
279 if (c.get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800280 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700281 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800282 mirror::ArtField* field = nullptr;
283 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700284 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
285 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800286 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287 field_type = class_linker->FindClass(sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700288 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700290 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800291 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800294 ThrowLocation throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800295 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800297 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
298 "no type \"%s\" found and so no field \"%s\" could be found in class "
299 "\"%s\" or its superclasses", sig, name,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800300 ClassHelper(c.get()).GetDescriptor());
301 soa.Self()->GetException(nullptr)->SetCause(cause.get());
302 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 }
304 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700308 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800309 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
311 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
312 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800313 sig, name, ClassHelper(c.get()).GetDescriptor());
314 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700315 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700317}
318
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800319static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700322 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700323 vm->pin_table.Add(array);
324}
325
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800326static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700328 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700329 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700330 vm->pin_table.Remove(array);
331}
332
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800333static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700336 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
338 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
339 "%s offset=%d length=%d %s.length=%d",
340 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700341}
Ian Rogers0571d352011-11-03 19:51:38 -0700342
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
344 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
347 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
348 "offset=%d length=%d string.length()=%d", start, length,
349 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700350}
Elliott Hughes814e4032011-08-23 12:07:56 -0700351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700353 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 // Turn the const char* into a java.lang.String.
355 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800356 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700358 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700359
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 // Choose an appropriate constructor and set up the arguments.
361 jvalue args[2];
362 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800363 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800365 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 signature = "(Ljava/lang/String;)V";
367 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800368 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 signature = "(Ljava/lang/Throwable;)V";
370 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700371 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
373 args[0].l = s.get();
374 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700375 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800377 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800378 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800380 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 return JNI_ERR;
382 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700383
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800384 ScopedLocalRef<jthrowable> exception(
385 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
386 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 return JNI_ERR;
388 }
Ian Rogersef28b142012-11-30 14:22:18 -0800389 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800391 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700393}
394
Elliott Hughes462c9442012-03-23 18:47:50 -0700395static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800396 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700397 return JNI_ERR;
398 }
399
Elliott Hughes462c9442012-03-23 18:47:50 -0700400 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700401 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800402 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700403 *p_env = self->GetJniEnv();
404 return JNI_OK;
405 }
406
407 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
408
409 // No threads allowed in zygote mode.
410 if (runtime->IsZygote()) {
411 LOG(ERROR) << "Attempt to attach a thread in the zygote";
412 return JNI_ERR;
413 }
414
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800416 const char* thread_name = nullptr;
417 jobject thread_group = nullptr;
418 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700419 if (IsBadJniVersion(args->version)) {
420 LOG(ERROR) << "Bad JNI version passed to "
421 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
422 << args->version;
423 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700424 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700425 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700426 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700427 }
Elliott Hughes75770752011-08-24 17:52:38 -0700428
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800429 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800430 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700431 return JNI_ERR;
432 } else {
433 *p_env = Thread::Current()->GetJniEnv();
434 return JNI_OK;
435 }
Elliott Hughes75770752011-08-24 17:52:38 -0700436}
437
Elliott Hughes79082e32011-08-25 12:07:32 -0700438class SharedLibrary {
439 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800440 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700441 : path_(path),
442 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700443 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700444 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700445 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700446 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700448 }
449
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800450 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700451 return class_loader_;
452 }
453
454 std::string GetPath() {
455 return path_;
456 }
457
458 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700459 * Check the result of an earlier call to JNI_OnLoad on this library.
460 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700461 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 bool CheckOnLoadResult()
463 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700465 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700466 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
467 bool okay;
468 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700469 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700470
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700471 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
473 // caller can continue.
474 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
475 okay = true;
476 } else {
477 while (jni_on_load_result_ == kPending) {
478 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700479 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700480 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700481
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700482 okay = (jni_on_load_result_ == kOkay);
483 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
484 << (okay ? "succeeded" : "failed") << "]";
485 }
486 }
487 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700488 return okay;
489 }
490
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700492 Thread* self = Thread::Current();
493 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700494
Elliott Hughes79082e32011-08-25 12:07:32 -0700495 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700496 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700497
498 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700499 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 }
501
502 void* FindSymbol(const std::string& symbol_name) {
503 return dlsym(handle_, symbol_name.c_str());
504 }
505
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800506 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800507 if (class_loader_ != nullptr) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800508 class_loader_ = visitor(class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800509 }
510 }
511
Elliott Hughes79082e32011-08-25 12:07:32 -0700512 private:
513 enum JNI_OnLoadState {
514 kPending,
515 kFailed,
516 kOkay,
517 };
518
519 // Path to library "/system/lib/libjni.so".
520 std::string path_;
521
522 // The void* returned by dlopen(3).
523 void* handle_;
524
525 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800526 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700527
528 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700531 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700533 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700535 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700536};
537
Elliott Hughes79082e32011-08-25 12:07:32 -0700538// This exists mainly to keep implementation details out of the header file.
539class Libraries {
540 public:
541 Libraries() {
542 }
543
544 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700545 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 }
547
Elliott Hughesae80b492012-04-24 10:43:17 -0700548 void Dump(std::ostream& os) const {
549 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700550 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700551 if (!first) {
552 os << ' ';
553 }
554 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700555 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700556 }
557 }
558
559 size_t size() const {
560 return libraries_.size();
561 }
562
Elliott Hughes79082e32011-08-25 12:07:32 -0700563 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700564 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800565 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 }
567
568 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700569 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700570 }
571
572 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800573 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 std::string jni_short_name(JniShortName(m));
576 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800577 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700578 for (const auto& lib : libraries_) {
579 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 if (library->GetClassLoader() != declaring_class_loader) {
581 // We only search libraries loaded by the appropriate ClassLoader.
582 continue;
583 }
584 // Try the short name then the long name...
585 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800586 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700587 fn = library->FindSymbol(jni_long_name);
588 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800590 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
591 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 return fn;
593 }
594 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700595 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700596 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700597 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700598 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800599 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 }
601
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800602 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800603 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800604 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800605 }
606 }
607
Elliott Hughes79082e32011-08-25 12:07:32 -0700608 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700609 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700610};
611
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
613 jvalue* args) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800614 mirror::ArtMethod* method = soa.DecodeMethod(mid);
615 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700616 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800617 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700618 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800619 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800620 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700621 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800622}
623
Ian Rogersbc939662013-08-15 10:26:54 -0700624#define CHECK_NON_NULL_ARGUMENT(fn, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800625 if (UNLIKELY(value == nullptr)) { \
Ian Rogersbc939662013-08-15 10:26:54 -0700626 JniAbortF(#fn, #value " == null"); \
627 }
628
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700629#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800630 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700631 JniAbortF(#fn, #value " == null"); \
632 }
633
Elliott Hughescdf53122011-08-19 15:46:09 -0700634class JNI {
635 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700636 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 return JNI_VERSION_1_6;
638 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700639
Ian Rogers25e8b912012-09-07 11:31:36 -0700640 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700643 }
644
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700646 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700647 Runtime* runtime = Runtime::Current();
648 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700650 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800651 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700652 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700653 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
654 c = class_linker->FindClass(descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700655 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800656 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700657 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700662 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800664 jobject art_method = env->GetObjectField(
665 java_method, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
666 mirror::ArtMethod* method = soa.Decode<mirror::ArtMethod*>(art_method);
667 DCHECK(method != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700669 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700670
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700672 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700674 jobject art_field = env->GetObjectField(java_field,
675 WellKnownClasses::java_lang_reflect_Field_artField);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800676 mirror::ArtField* field = soa.Decode<mirror::ArtField*>(art_field);
677 DCHECK(field != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700680
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700682 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800684 mirror::ArtMethod* m = soa.DecodeMethod(mid);
685 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700686 jobject art_method = soa.AddLocalReference<jobject>(m);
687 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
688 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800689 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700690 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800691 SetObjectField(env, reflect_method,
692 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700693 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700697 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800699 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700700 jobject art_field = soa.AddLocalReference<jobject>(f);
701 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
702 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800703 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700704 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800705 SetObjectField(env, reflect_field,
706 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700707 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700711 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800713 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 }
716
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700717 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700718 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800720 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700723
Elliott Hughes37f7a402011-08-22 18:56:01 -0700724 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700725 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
726 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800728 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
729 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughese84278b2012-03-22 10:06:53 -0700733 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700734 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800735 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700736 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 return JNI_TRUE;
738 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700739 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800740 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
741 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700742 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700745
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800748 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
749 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 return JNI_ERR;
751 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
753 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 return JNI_OK;
755 }
756
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700757 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700758 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800759 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 }
761
762 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700763 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700764 }
765
766 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700767 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700768 }
769
770 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700772
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800773 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
774 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
775 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800776 uint32_t old_throw_dex_pc;
777 {
778 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800779 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 old_throw_this_object.reset(old_throw_location.GetThis());
781 old_throw_method.reset(old_throw_location.GetMethod());
782 old_exception.reset(old_exception_obj);
783 old_throw_dex_pc = old_throw_location.GetDexPc();
784 soa.Self()->ClearException();
785 }
786 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700787 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
788 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800789 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 } else {
793 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800794 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800795 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700796 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800797 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 }
799 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800800 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
801 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700802
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700805
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800808 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 }
811
Ian Rogers25e8b912012-09-07 11:31:36 -0700812 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 LOG(FATAL) << "JNI FatalError called: " << msg;
814 }
815
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700816 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800817 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700818 return JNI_ERR;
819 }
Ian Rogersef28b142012-11-30 14:22:18 -0800820 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 return JNI_OK;
822 }
823
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700824 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800826 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 soa.Env()->PopFrame();
828 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700831 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800832 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 }
834
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700836 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800837 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800838 // Check for null after decoding the object to handle cleared weak globals.
839 if (decoded_obj == nullptr) {
840 return nullptr;
841 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700844 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 return reinterpret_cast<jobject>(ref);
847 }
848
849 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800850 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 return;
852 }
Ian Rogersef28b142012-11-30 14:22:18 -0800853 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800855 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700856 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700857
858 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
859 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
860 << "failed to find entry";
861 }
862 }
863
864 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800866 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 }
868
869 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700870 if (obj != nullptr) {
871 ScopedObjectAccess soa(env);
872 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874 }
875
876 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700877 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800878 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800879 // Check for null after decoding the object to handle cleared weak globals.
880 if (decoded_obj == nullptr) {
881 return nullptr;
882 }
883 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885
886 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800887 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 return;
889 }
Ian Rogersef28b142012-11-30 14:22:18 -0800890 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700891
Ian Rogersef28b142012-11-30 14:22:18 -0800892 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 if (!locals.Remove(cookie, obj)) {
894 // Attempting to delete a local reference that is not in the
895 // topmost local reference frame is a no-op. DeleteLocalRef returns
896 // void and doesn't throw any exceptions, but we should probably
897 // complain about it so the user will notice that things aren't
898 // going quite the way they expect.
899 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
900 << "failed to find entry";
901 }
902 }
903
904 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700905 if (obj1 == obj2) {
906 return JNI_TRUE;
907 } else {
908 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800909 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700910 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 }
912
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700913 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700914 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800916 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800917 if (c == nullptr) {
918 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700919 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700920 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Ian Rogersbc939662013-08-15 10:26:54 -0700923 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700926 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
927 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
928 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 va_end(args);
930 return result;
931 }
932
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700934 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
935 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800937 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800938 if (c == nullptr) {
939 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700940 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800941 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800942 if (result == nullptr) {
943 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700944 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800947 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800948 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700949 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800950 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700954 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
955 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800957 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800958 if (c == nullptr) {
959 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700960 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800961 mirror::Object* result = c->AllocObject(soa.Self());
962 if (result == nullptr) {
963 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700964 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800967 if (soa.Self()->IsExceptionPending()) {
968 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700969 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800970 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Ian Rogersbc939662013-08-15 10:26:54 -0700973 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
974 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
975 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
976 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700978 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Ian Rogersbc939662013-08-15 10:26:54 -0700981 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
982 const char* sig) {
983 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
984 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
985 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700987 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700993 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
994 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700995 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001002 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1003 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1006 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001010 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1011 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 ScopedObjectAccess soa(env);
1013 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1014 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_list ap;
1019 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001020 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1021 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001022 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001025 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001029 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1030 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 ScopedObjectAccess soa(env);
1032 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001036 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1037 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(env);
1039 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_list ap;
1044 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001045 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1046 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1047 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001050 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001054 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1055 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 ScopedObjectAccess soa(env);
1057 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001061 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1062 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
1064 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001070 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1071 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001072 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001075 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001079 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1080 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001086 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1087 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 ScopedObjectAccess soa(env);
1089 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_list ap;
1094 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001095 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1096 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001097 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001100 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001104 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1105 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001111 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1112 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
1114 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_list ap;
1119 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001120 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1121 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1122 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001125 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001129 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1130 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131 ScopedObjectAccess soa(env);
1132 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001136 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1137 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
1139 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_list ap;
1144 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001145 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1146 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001147 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001154 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1155 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001161 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1162 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
1164 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_list ap;
1169 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001170 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1171 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001172 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001175 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001179 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1180 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 ScopedObjectAccess soa(env);
1182 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001186 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1187 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
1189 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 va_list ap;
1194 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001195 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1196 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001197 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001204 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1205 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
1207 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001211 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1212 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213 ScopedObjectAccess soa(env);
1214 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_list ap;
1219 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001220 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1221 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001222 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001223 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001228 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1229 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 ScopedObjectAccess soa(env);
1231 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001235 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1236 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
1238 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001241 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001244 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1245 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001246 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1248 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 va_end(ap);
1250 return local_result;
1251 }
1252
Ian Rogersbc939662013-08-15 10:26:54 -07001253 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1254 va_list args) {
1255 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1256 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 ScopedObjectAccess soa(env);
1258 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1259 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Ian Rogersbc939662013-08-15 10:26:54 -07001262 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1263 jvalue* args) {
1264 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1265 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
1267 JValue result(InvokeWithJValues(soa, obj, mid, args));
1268 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Ian Rogersbc939662013-08-15 10:26:54 -07001271 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1272 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001275 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1276 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001277 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001280 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Ian Rogersbc939662013-08-15 10:26:54 -07001283 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1284 va_list args) {
1285 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1286 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env);
1288 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Ian Rogersbc939662013-08-15 10:26:54 -07001291 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1292 jvalue* args) {
1293 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1294 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 ScopedObjectAccess soa(env);
1296 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001299 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001302 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1303 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001304 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001307 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Ian Rogersbc939662013-08-15 10:26:54 -07001310 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1311 va_list args) {
1312 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1313 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001314 ScopedObjectAccess soa(env);
1315 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Ian Rogersbc939662013-08-15 10:26:54 -07001318 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1319 jvalue* args) {
1320 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1321 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 ScopedObjectAccess soa(env);
1323 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001329 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1330 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1331 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001334 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Ian Rogersbc939662013-08-15 10:26:54 -07001337 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1338 va_list args) {
1339 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1340 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 ScopedObjectAccess soa(env);
1342 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
Ian Rogersbc939662013-08-15 10:26:54 -07001345 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1346 jvalue* args) {
1347 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1348 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001349 ScopedObjectAccess soa(env);
1350 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001353 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001356 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1357 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001358 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001359 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001361 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Ian Rogersbc939662013-08-15 10:26:54 -07001364 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1365 va_list args) {
1366 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1367 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001368 ScopedObjectAccess soa(env);
1369 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Ian Rogersbc939662013-08-15 10:26:54 -07001372 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1373 jvalue* args) {
1374 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1375 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376 ScopedObjectAccess soa(env);
1377 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001380 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001383 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1384 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001385 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001386 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001388 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Ian Rogersbc939662013-08-15 10:26:54 -07001391 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1392 va_list args) {
1393 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1394 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001395 ScopedObjectAccess soa(env);
1396 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Ian Rogersbc939662013-08-15 10:26:54 -07001399 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1400 jvalue* args) {
1401 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1402 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001403 ScopedObjectAccess soa(env);
1404 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001407 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001409 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001410 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1411 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001412 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001415 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Ian Rogersbc939662013-08-15 10:26:54 -07001418 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1419 va_list args) {
1420 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1421 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001422 ScopedObjectAccess soa(env);
1423 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Ian Rogersbc939662013-08-15 10:26:54 -07001426 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1427 jvalue* args) {
1428 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1429 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env);
1431 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001434 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001436 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001437 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1438 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001439 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001440 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001442 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Ian Rogersbc939662013-08-15 10:26:54 -07001445 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1446 va_list args) {
1447 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1448 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001449 ScopedObjectAccess soa(env);
1450 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Ian Rogersbc939662013-08-15 10:26:54 -07001453 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1454 jvalue* args) {
1455 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1456 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 ScopedObjectAccess soa(env);
1458 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001461 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001463 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001464 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1465 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001466 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001469 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Ian Rogersbc939662013-08-15 10:26:54 -07001472 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1473 va_list args) {
1474 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1475 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001476 ScopedObjectAccess soa(env);
1477 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Ian Rogersbc939662013-08-15 10:26:54 -07001480 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1481 jvalue* args) {
1482 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1483 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001484 ScopedObjectAccess soa(env);
1485 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001488 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001490 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001491 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1492 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001493 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001494 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 va_end(ap);
1496 }
1497
Brian Carlstromea46f952013-07-30 01:26:50 -07001498 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1499 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001500 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1501 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001502 ScopedObjectAccess soa(env);
1503 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Ian Rogersbc939662013-08-15 10:26:54 -07001506 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1507 jvalue* args) {
1508 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1509 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001510 ScopedObjectAccess soa(env);
1511 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Ian Rogersbc939662013-08-15 10:26:54 -07001514 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1515 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1516 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1517 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001519 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001521
Ian Rogersbc939662013-08-15 10:26:54 -07001522 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1523 const char* sig) {
1524 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1525 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1526 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001527 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001528 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001532 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1533 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001534 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001535 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1536 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001542 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001543 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001544 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001548 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1549 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001550 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001551 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1552 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1553 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001554 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001558 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001559 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001560 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1561 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001562 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001566 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1567 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001568 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001569 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1570 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001571 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001573#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001574 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001575 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001576 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001577 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001578
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001579#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001580 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1581 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001582 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001583 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1584 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001585 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001587#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001588 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001589 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001590 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001591 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001592
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001594 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001598 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001602 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001606 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001610 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001613 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001614 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001617 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001618 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001621 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001622 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001626 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001630 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 }
1632
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001633 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001634 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001638 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001642 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001646 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001650 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001654 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001655 }
1656
1657 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001659 }
1660
1661 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001662 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001663 }
1664
1665 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001666 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001667 }
1668
1669 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001670 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001671 }
1672
1673 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001674 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001675 }
1676
1677 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001678 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001679 }
1680
1681 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001682 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001683 }
1684
1685 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001686 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001687 }
1688
1689 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001690 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001691 }
1692
1693 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001694 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001695 }
1696
1697 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001698 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001699 }
1700
1701 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001702 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001703 }
1704
1705 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001706 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001707 }
1708
1709 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001710 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001711 }
1712
1713 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001714 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001715 }
1716
1717 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001718 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001721 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001724 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001725 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001726 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 va_end(ap);
1729 return local_result;
1730 }
1731
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001732 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001733 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001734 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001735 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001740 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001742 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001746 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001749 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001750 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001751 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001753 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001756 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001757 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001759 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001763 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001765 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001768 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001770 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001771 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001772 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001773 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001775 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001778 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001779 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001781 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001784 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001785 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001787 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001792 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001793 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001794 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001795 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001797 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001801 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001803 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001807 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001809 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001812 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001814 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001815 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001816 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001817 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001819 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001822 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001823 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001825 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001828 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001829 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001830 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001831 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001836 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001837 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001838 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001839 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001841 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001844 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001845 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001846 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001847 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001850 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001851 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001853 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001856 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001858 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001859 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001860 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001861 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001863 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001866 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001867 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001869 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001872 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001873 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001875 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001878 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001880 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001881 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001882 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001883 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001885 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001888 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001889 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001891 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001894 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001895 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001897 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001900 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001902 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001903 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001904 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001905 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001907 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001910 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001911 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001913 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001916 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001917 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001919 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 }
1921
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001922 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001924 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001925 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001926 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001927 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 va_end(ap);
1929 }
1930
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001931 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001932 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001934 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001937 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001938 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001940 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes814e4032011-08-23 12:07:56 -07001943 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001944 if (UNLIKELY(char_count < 0)) {
1945 JniAbortF("NewString", "char_count < 0: %d", char_count);
1946 return nullptr;
1947 }
1948 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1949 JniAbortF("NewString", "chars == null && char_count > 0");
1950 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001951 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001953 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001954 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
1957 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001958 if (utf == nullptr) {
1959 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001962 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001963 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes814e4032011-08-23 12:07:56 -07001966 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001967 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001968 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001969 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 }
1971
1972 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001973 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001975 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 }
1977
Ian Rogersbc939662013-08-15 10:26:54 -07001978 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1979 jchar* buf) {
1980 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001982 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001983 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001984 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001985 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001986 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001987 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1988 memcpy(buf, chars + start, length * sizeof(jchar));
1989 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 }
1991
Ian Rogersbc939662013-08-15 10:26:54 -07001992 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1993 char* buf) {
1994 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001996 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001997 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001999 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002000 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002001 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2002 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
2003 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002007 CHECK_NON_NULL_ARGUMENT(GetStringChars, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002008 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002009 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2010 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002012 if (is_copy != nullptr) {
2013 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07002014 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002015 int32_t char_count = s->GetLength();
2016 int32_t offset = s->GetOffset();
2017 jchar* bytes = new jchar[char_count + 1];
2018 for (int32_t i = 0; i < char_count; i++) {
2019 bytes[i] = chars->Get(i + offset);
2020 }
2021 bytes[char_count] = '\0';
2022 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 }
2024
Mathieu Chartier590fee92013-09-13 13:46:47 -07002025 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002026 CHECK_NON_NULL_ARGUMENT(ReleaseStringChars, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002027 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002028 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002029 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002033 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002037 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002041 if (java_string == nullptr) {
2042 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002043 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002044 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002045 *is_copy = JNI_TRUE;
2046 }
Ian Rogersef28b142012-11-30 14:22:18 -08002047 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002048 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002049 size_t byte_count = s->GetUtfLength();
2050 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002051 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002052 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2053 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2054 bytes[byte_count] = '\0';
2055 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002059 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002060 }
2061
Elliott Hughesbd935992011-08-22 11:59:34 -07002062 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002063 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002064 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002065 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002066 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002067 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2068 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002069 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002070 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002074 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002076 mirror::ObjectArray<mirror::Object>* array =
2077 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002078 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Ian Rogersbc939662013-08-15 10:26:54 -07002081 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2082 jobject java_value) {
2083 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002085 mirror::ObjectArray<mirror::Object>* array =
2086 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2087 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002088 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
2091 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002093 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
2096 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002097 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002098 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
2101 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002103 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
2106 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002107 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002108 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
2111 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002112 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002113 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
2116 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002117 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002118 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
2121 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002123 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Ian Rogers1d99e452014-01-02 17:36:41 -08002126 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2127 jobject initial_element) {
2128 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002129 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002130 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002131 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002132
2133 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002134 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002135 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002136 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002137 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002138 if (UNLIKELY(element_class->IsPrimitive())) {
2139 JniAbortF("NewObjectArray", "not an object type: %s",
2140 PrettyDescriptor(element_class).c_str());
2141 return nullptr;
2142 }
2143 std::string descriptor("[");
2144 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002145
Ian Rogers1d99e452014-01-02 17:36:41 -08002146 // Find the class.
2147 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2148 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), element_class->GetClassLoader());
2149 array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
2150 if (UNLIKELY(array_class == nullptr)) {
2151 return nullptr;
2152 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughes75770752011-08-24 17:52:38 -07002155 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002156 mirror::ObjectArray<mirror::Object>* result =
2157 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002158 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002159 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002160 if (initial_object != nullptr) {
2161 mirror::Class* element_class = result->GetClass()->GetComponentType();
2162 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2163 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2164 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2165 PrettyDescriptor(element_class).c_str());
2166
2167 } else {
2168 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002169 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002170 }
2171 }
Elliott Hughes75770752011-08-24 17:52:38 -07002172 }
2173 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002174 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
2177 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002178 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002179 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Ian Rogersa15e67d2012-02-28 13:51:55 -08002182 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002183 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002185 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002186 gc::Heap* heap = Runtime::Current()->GetHeap();
2187 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002188 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002189 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002190 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002191 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002192 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002193 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002194 *is_copy = JNI_FALSE;
2195 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002196 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002197 }
2198
Mathieu Chartier590fee92013-09-13 13:46:47 -07002199 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002200 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002201 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002202 }
2203
Elliott Hughes75770752011-08-24 17:52:38 -07002204 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002205 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002207 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
Elliott Hughes75770752011-08-24 17:52:38 -07002210 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002211 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002212 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002213 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughes75770752011-08-24 17:52:38 -07002216 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002217 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002218 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002219 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughes75770752011-08-24 17:52:38 -07002222 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002223 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002224 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002225 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughes75770752011-08-24 17:52:38 -07002228 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002229 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002230 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002231 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Elliott Hughes75770752011-08-24 17:52:38 -07002234 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002235 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002237 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Elliott Hughes75770752011-08-24 17:52:38 -07002240 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002241 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002243 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Elliott Hughes75770752011-08-24 17:52:38 -07002246 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002247 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002248 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002249 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Mathieu Chartier590fee92013-09-13 13:46:47 -07002252 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2253 jint mode) {
2254 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 }
2256
Mathieu Chartier590fee92013-09-13 13:46:47 -07002257 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2258 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Mathieu Chartier590fee92013-09-13 13:46:47 -07002261 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2262 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Mathieu Chartier590fee92013-09-13 13:46:47 -07002265 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2266 jint mode) {
2267 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Mathieu Chartier590fee92013-09-13 13:46:47 -07002270 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2271 jint mode) {
2272 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 }
2274
Mathieu Chartier590fee92013-09-13 13:46:47 -07002275 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2276 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Mathieu Chartier590fee92013-09-13 13:46:47 -07002279 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2280 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Mathieu Chartier590fee92013-09-13 13:46:47 -07002283 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2284 jint mode) {
2285 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 }
2287
Ian Rogersbc939662013-08-15 10:26:54 -07002288 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2289 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002290 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002291 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2292 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 }
2294
Ian Rogersbc939662013-08-15 10:26:54 -07002295 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2296 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002297 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002298 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 }
2300
Ian Rogersbc939662013-08-15 10:26:54 -07002301 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2302 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002303 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002304 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 }
2306
Ian Rogersbc939662013-08-15 10:26:54 -07002307 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2308 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002309 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002310 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2311 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313
Ian Rogersbc939662013-08-15 10:26:54 -07002314 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2315 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002316 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002317 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2318 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320
Ian Rogersbc939662013-08-15 10:26:54 -07002321 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2322 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002323 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002324 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 }
2326
Ian Rogersbc939662013-08-15 10:26:54 -07002327 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2328 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002329 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002330 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 }
2332
Ian Rogersbc939662013-08-15 10:26:54 -07002333 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2334 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002335 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002336 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2337 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 }
2339
Ian Rogersbc939662013-08-15 10:26:54 -07002340 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2341 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002343 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 }
2345
Ian Rogersbc939662013-08-15 10:26:54 -07002346 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2347 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002349 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2353 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002355 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Ian Rogersbc939662013-08-15 10:26:54 -07002358 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2359 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002361 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363
Ian Rogersbc939662013-08-15 10:26:54 -07002364 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2365 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002366 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002367 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 }
2369
Ian Rogersbc939662013-08-15 10:26:54 -07002370 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2371 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002373 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002374 }
2375
Ian Rogersbc939662013-08-15 10:26:54 -07002376 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2377 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002379 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002380 }
2381
Ian Rogersbc939662013-08-15 10:26:54 -07002382 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2383 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002385 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2386 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002387 }
2388
Ian Rogersbc939662013-08-15 10:26:54 -07002389 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2390 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002391 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2392 }
2393
Ian Rogersbc939662013-08-15 10:26:54 -07002394 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2395 jint method_count, bool return_errors) {
2396 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002397 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002398 return JNI_ERR; // Not reached.
2399 }
2400 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002401 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002402 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002403 if (UNLIKELY(method_count == 0)) {
2404 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2405 << PrettyDescriptor(c);
2406 return JNI_OK;
2407 }
2408 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2409 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002410 const char* name = methods[i].name;
2411 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002412 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002413 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002414 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002415 ++sig;
2416 }
2417
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002418 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2419 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002420 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002421 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002422 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002423 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002424 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002425 << PrettyDescriptor(c) << "." << name << sig << " in "
2426 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002427 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002428 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002429 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002430 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002431 << PrettyDescriptor(c) << "." << name << sig
2432 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002433 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002434 return JNI_ERR;
2435 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002436
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002437 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002438
Ian Rogers1eb512d2013-10-18 15:42:20 -07002439 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002440 }
2441 return JNI_OK;
2442 }
2443
Elliott Hughes5174fe62011-08-23 15:12:35 -07002444 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002445 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002446 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002447 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002448
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002449 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002450
2451 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002452 mirror::ArtMethod* m = c->GetDirectMethod(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 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002458 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002459 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002460 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002461 }
2462 }
2463
2464 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002465 }
2466
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002467 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2468 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002469 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002470 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002471 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2472 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002473 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002474 return JNI_ERR;
2475 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002476 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002477 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002478 }
2479
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002480 static jint MonitorExit(JNIEnv* env, jobject java_object)
2481 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002482 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002484 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002485 o->MonitorExit(soa.Self());
2486 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002487 return JNI_ERR;
2488 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002489 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002490 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002491 }
2492
2493 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002494 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002495 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002496 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002497 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002498 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002499 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002500 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002501 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002502 }
2503
Elliott Hughescdf53122011-08-19 15:46:09 -07002504 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002505 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002506 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002507 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002508 if (address == nullptr && capacity != 0) {
2509 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002510 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002511
Elliott Hughes96a98872012-12-19 14:21:15 -08002512 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002513 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2514 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002515 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002516 jint capacity_arg = static_cast<jint>(capacity);
2517
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002518 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2519 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002520 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002521 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002522 }
2523
Elliott Hughesb465ab02011-08-24 11:21:21 -07002524 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002525 return reinterpret_cast<void*>(env->GetLongField(
2526 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002527 }
2528
Elliott Hughesb465ab02011-08-24 11:21:21 -07002529 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002530 return static_cast<jlong>(env->GetIntField(
2531 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002532 }
2533
Elliott Hughesb465ab02011-08-24 11:21:21 -07002534 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002535 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002536
2537 // Do we definitely know what kind of reference this is?
2538 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2539 IndirectRefKind kind = GetIndirectRefKind(ref);
2540 switch (kind) {
2541 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002542 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002543 return JNILocalRefType;
2544 }
2545 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002546 case kGlobal:
2547 return JNIGlobalRefType;
2548 case kWeakGlobal:
2549 return JNIWeakGlobalRefType;
2550 case kSirtOrInvalid:
2551 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002552 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002553 return JNILocalRefType;
2554 }
2555
Ian Rogersef28b142012-11-30 14:22:18 -08002556 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002557 return JNIInvalidRefType;
2558 }
2559
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002560 // If we're handing out direct pointers, check whether it's a direct pointer to a local
2561 // reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002562 {
2563 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002564 if (soa.Decode<mirror::Object*>(java_object) ==
2565 reinterpret_cast<mirror::Object*>(java_object)) {
2566 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<mirror::Object*>(java_object))) {
Ian Rogersef28b142012-11-30 14:22:18 -08002567 return JNILocalRefType;
2568 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002569 }
2570 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002571 return JNIInvalidRefType;
2572 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002573 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2574 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002575 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576
2577 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002578 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2579 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002580 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002581 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002582 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2583 return JNI_ERR;
2584 }
2585 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002586 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002587 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2588 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002589 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002590 soa.Self()->ThrowOutOfMemoryError(caller);
2591 }
2592 return okay ? JNI_OK : JNI_ERR;
2593 }
2594
2595 template<typename JniT, typename ArtT>
2596 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002597 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002598 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002599 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002600 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002601 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002602 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002603 return soa.AddLocalReference<JniT>(result);
2604 }
2605
2606 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2607 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2608 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002609 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002610 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2611 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002612 // Only make a copy if necessary.
2613 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2614 if (is_copy != nullptr) {
2615 *is_copy = JNI_TRUE;
2616 }
2617 static const size_t component_size = array->GetClass()->GetComponentSize();
2618 size_t size = array->GetLength() * component_size;
2619 void* data = new uint64_t[RoundUp(size, 8) / 8];
2620 memcpy(data, array->GetData(), size);
2621 return reinterpret_cast<CArrayT>(data);
2622 } else {
2623 if (is_copy != nullptr) {
2624 *is_copy = JNI_FALSE;
2625 }
2626 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002627 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002628 }
2629
Mathieu Chartier590fee92013-09-13 13:46:47 -07002630 template <typename ArrayT, typename ElementT>
2631 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2632 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002633 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002634 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002635 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002636 gc::Heap* heap = Runtime::Current()->GetHeap();
2637 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2638 size_t bytes = array->GetLength() * component_size;
2639 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2640 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002641 if (is_copy) {
2642 // Sanity check: If elements is not the same as the java array's data, it better not be a
2643 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2644 // copies we make?
2645 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2646 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2647 reinterpret_cast<void*>(elements), array_data);
2648 return;
2649 }
2650 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002651 // Don't need to copy if we had a direct pointer.
2652 if (mode != JNI_ABORT && is_copy) {
2653 memcpy(array_data, elements, bytes);
2654 }
2655 if (mode != JNI_COMMIT) {
2656 if (is_copy) {
2657 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002658 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002659 // Non copy to a movable object must means that we had disabled the moving GC.
2660 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002661 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002662 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002663 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002664 }
2665
2666 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2667 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2668 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002669 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002670 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002671 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2672 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2673 ThrowAIOOBE(soa, array, start, length, "src");
2674 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002675 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002676 JavaT* data = array->GetData();
2677 memcpy(buf, data + start, length * sizeof(JavaT));
2678 }
2679 }
2680
2681 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2682 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2683 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002684 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002685 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002686 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2687 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2688 ThrowAIOOBE(soa, array, start, length, "dst");
2689 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002690 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002691 JavaT* data = array->GetData();
2692 memcpy(data + start, buf, length * sizeof(JavaT));
2693 }
2694 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002695};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002696
Elliott Hughes88c5c352012-03-15 18:49:48 -07002697const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002698 nullptr, // reserved0.
2699 nullptr, // reserved1.
2700 nullptr, // reserved2.
2701 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002702 JNI::GetVersion,
2703 JNI::DefineClass,
2704 JNI::FindClass,
2705 JNI::FromReflectedMethod,
2706 JNI::FromReflectedField,
2707 JNI::ToReflectedMethod,
2708 JNI::GetSuperclass,
2709 JNI::IsAssignableFrom,
2710 JNI::ToReflectedField,
2711 JNI::Throw,
2712 JNI::ThrowNew,
2713 JNI::ExceptionOccurred,
2714 JNI::ExceptionDescribe,
2715 JNI::ExceptionClear,
2716 JNI::FatalError,
2717 JNI::PushLocalFrame,
2718 JNI::PopLocalFrame,
2719 JNI::NewGlobalRef,
2720 JNI::DeleteGlobalRef,
2721 JNI::DeleteLocalRef,
2722 JNI::IsSameObject,
2723 JNI::NewLocalRef,
2724 JNI::EnsureLocalCapacity,
2725 JNI::AllocObject,
2726 JNI::NewObject,
2727 JNI::NewObjectV,
2728 JNI::NewObjectA,
2729 JNI::GetObjectClass,
2730 JNI::IsInstanceOf,
2731 JNI::GetMethodID,
2732 JNI::CallObjectMethod,
2733 JNI::CallObjectMethodV,
2734 JNI::CallObjectMethodA,
2735 JNI::CallBooleanMethod,
2736 JNI::CallBooleanMethodV,
2737 JNI::CallBooleanMethodA,
2738 JNI::CallByteMethod,
2739 JNI::CallByteMethodV,
2740 JNI::CallByteMethodA,
2741 JNI::CallCharMethod,
2742 JNI::CallCharMethodV,
2743 JNI::CallCharMethodA,
2744 JNI::CallShortMethod,
2745 JNI::CallShortMethodV,
2746 JNI::CallShortMethodA,
2747 JNI::CallIntMethod,
2748 JNI::CallIntMethodV,
2749 JNI::CallIntMethodA,
2750 JNI::CallLongMethod,
2751 JNI::CallLongMethodV,
2752 JNI::CallLongMethodA,
2753 JNI::CallFloatMethod,
2754 JNI::CallFloatMethodV,
2755 JNI::CallFloatMethodA,
2756 JNI::CallDoubleMethod,
2757 JNI::CallDoubleMethodV,
2758 JNI::CallDoubleMethodA,
2759 JNI::CallVoidMethod,
2760 JNI::CallVoidMethodV,
2761 JNI::CallVoidMethodA,
2762 JNI::CallNonvirtualObjectMethod,
2763 JNI::CallNonvirtualObjectMethodV,
2764 JNI::CallNonvirtualObjectMethodA,
2765 JNI::CallNonvirtualBooleanMethod,
2766 JNI::CallNonvirtualBooleanMethodV,
2767 JNI::CallNonvirtualBooleanMethodA,
2768 JNI::CallNonvirtualByteMethod,
2769 JNI::CallNonvirtualByteMethodV,
2770 JNI::CallNonvirtualByteMethodA,
2771 JNI::CallNonvirtualCharMethod,
2772 JNI::CallNonvirtualCharMethodV,
2773 JNI::CallNonvirtualCharMethodA,
2774 JNI::CallNonvirtualShortMethod,
2775 JNI::CallNonvirtualShortMethodV,
2776 JNI::CallNonvirtualShortMethodA,
2777 JNI::CallNonvirtualIntMethod,
2778 JNI::CallNonvirtualIntMethodV,
2779 JNI::CallNonvirtualIntMethodA,
2780 JNI::CallNonvirtualLongMethod,
2781 JNI::CallNonvirtualLongMethodV,
2782 JNI::CallNonvirtualLongMethodA,
2783 JNI::CallNonvirtualFloatMethod,
2784 JNI::CallNonvirtualFloatMethodV,
2785 JNI::CallNonvirtualFloatMethodA,
2786 JNI::CallNonvirtualDoubleMethod,
2787 JNI::CallNonvirtualDoubleMethodV,
2788 JNI::CallNonvirtualDoubleMethodA,
2789 JNI::CallNonvirtualVoidMethod,
2790 JNI::CallNonvirtualVoidMethodV,
2791 JNI::CallNonvirtualVoidMethodA,
2792 JNI::GetFieldID,
2793 JNI::GetObjectField,
2794 JNI::GetBooleanField,
2795 JNI::GetByteField,
2796 JNI::GetCharField,
2797 JNI::GetShortField,
2798 JNI::GetIntField,
2799 JNI::GetLongField,
2800 JNI::GetFloatField,
2801 JNI::GetDoubleField,
2802 JNI::SetObjectField,
2803 JNI::SetBooleanField,
2804 JNI::SetByteField,
2805 JNI::SetCharField,
2806 JNI::SetShortField,
2807 JNI::SetIntField,
2808 JNI::SetLongField,
2809 JNI::SetFloatField,
2810 JNI::SetDoubleField,
2811 JNI::GetStaticMethodID,
2812 JNI::CallStaticObjectMethod,
2813 JNI::CallStaticObjectMethodV,
2814 JNI::CallStaticObjectMethodA,
2815 JNI::CallStaticBooleanMethod,
2816 JNI::CallStaticBooleanMethodV,
2817 JNI::CallStaticBooleanMethodA,
2818 JNI::CallStaticByteMethod,
2819 JNI::CallStaticByteMethodV,
2820 JNI::CallStaticByteMethodA,
2821 JNI::CallStaticCharMethod,
2822 JNI::CallStaticCharMethodV,
2823 JNI::CallStaticCharMethodA,
2824 JNI::CallStaticShortMethod,
2825 JNI::CallStaticShortMethodV,
2826 JNI::CallStaticShortMethodA,
2827 JNI::CallStaticIntMethod,
2828 JNI::CallStaticIntMethodV,
2829 JNI::CallStaticIntMethodA,
2830 JNI::CallStaticLongMethod,
2831 JNI::CallStaticLongMethodV,
2832 JNI::CallStaticLongMethodA,
2833 JNI::CallStaticFloatMethod,
2834 JNI::CallStaticFloatMethodV,
2835 JNI::CallStaticFloatMethodA,
2836 JNI::CallStaticDoubleMethod,
2837 JNI::CallStaticDoubleMethodV,
2838 JNI::CallStaticDoubleMethodA,
2839 JNI::CallStaticVoidMethod,
2840 JNI::CallStaticVoidMethodV,
2841 JNI::CallStaticVoidMethodA,
2842 JNI::GetStaticFieldID,
2843 JNI::GetStaticObjectField,
2844 JNI::GetStaticBooleanField,
2845 JNI::GetStaticByteField,
2846 JNI::GetStaticCharField,
2847 JNI::GetStaticShortField,
2848 JNI::GetStaticIntField,
2849 JNI::GetStaticLongField,
2850 JNI::GetStaticFloatField,
2851 JNI::GetStaticDoubleField,
2852 JNI::SetStaticObjectField,
2853 JNI::SetStaticBooleanField,
2854 JNI::SetStaticByteField,
2855 JNI::SetStaticCharField,
2856 JNI::SetStaticShortField,
2857 JNI::SetStaticIntField,
2858 JNI::SetStaticLongField,
2859 JNI::SetStaticFloatField,
2860 JNI::SetStaticDoubleField,
2861 JNI::NewString,
2862 JNI::GetStringLength,
2863 JNI::GetStringChars,
2864 JNI::ReleaseStringChars,
2865 JNI::NewStringUTF,
2866 JNI::GetStringUTFLength,
2867 JNI::GetStringUTFChars,
2868 JNI::ReleaseStringUTFChars,
2869 JNI::GetArrayLength,
2870 JNI::NewObjectArray,
2871 JNI::GetObjectArrayElement,
2872 JNI::SetObjectArrayElement,
2873 JNI::NewBooleanArray,
2874 JNI::NewByteArray,
2875 JNI::NewCharArray,
2876 JNI::NewShortArray,
2877 JNI::NewIntArray,
2878 JNI::NewLongArray,
2879 JNI::NewFloatArray,
2880 JNI::NewDoubleArray,
2881 JNI::GetBooleanArrayElements,
2882 JNI::GetByteArrayElements,
2883 JNI::GetCharArrayElements,
2884 JNI::GetShortArrayElements,
2885 JNI::GetIntArrayElements,
2886 JNI::GetLongArrayElements,
2887 JNI::GetFloatArrayElements,
2888 JNI::GetDoubleArrayElements,
2889 JNI::ReleaseBooleanArrayElements,
2890 JNI::ReleaseByteArrayElements,
2891 JNI::ReleaseCharArrayElements,
2892 JNI::ReleaseShortArrayElements,
2893 JNI::ReleaseIntArrayElements,
2894 JNI::ReleaseLongArrayElements,
2895 JNI::ReleaseFloatArrayElements,
2896 JNI::ReleaseDoubleArrayElements,
2897 JNI::GetBooleanArrayRegion,
2898 JNI::GetByteArrayRegion,
2899 JNI::GetCharArrayRegion,
2900 JNI::GetShortArrayRegion,
2901 JNI::GetIntArrayRegion,
2902 JNI::GetLongArrayRegion,
2903 JNI::GetFloatArrayRegion,
2904 JNI::GetDoubleArrayRegion,
2905 JNI::SetBooleanArrayRegion,
2906 JNI::SetByteArrayRegion,
2907 JNI::SetCharArrayRegion,
2908 JNI::SetShortArrayRegion,
2909 JNI::SetIntArrayRegion,
2910 JNI::SetLongArrayRegion,
2911 JNI::SetFloatArrayRegion,
2912 JNI::SetDoubleArrayRegion,
2913 JNI::RegisterNatives,
2914 JNI::UnregisterNatives,
2915 JNI::MonitorEnter,
2916 JNI::MonitorExit,
2917 JNI::GetJavaVM,
2918 JNI::GetStringRegion,
2919 JNI::GetStringUTFRegion,
2920 JNI::GetPrimitiveArrayCritical,
2921 JNI::ReleasePrimitiveArrayCritical,
2922 JNI::GetStringCritical,
2923 JNI::ReleaseStringCritical,
2924 JNI::NewWeakGlobalRef,
2925 JNI::DeleteWeakGlobalRef,
2926 JNI::ExceptionCheck,
2927 JNI::NewDirectByteBuffer,
2928 JNI::GetDirectBufferAddress,
2929 JNI::GetDirectBufferCapacity,
2930 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002931};
2932
Elliott Hughes75770752011-08-24 17:52:38 -07002933JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002934 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002935 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002936 local_ref_cookie(IRT_FIRST_SEGMENT),
2937 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002938 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002939 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002940 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002941 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002942 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002943 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002944 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002945}
2946
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002947JNIEnvExt::~JNIEnvExt() {
2948}
2949
Mathieu Chartier590fee92013-09-13 13:46:47 -07002950jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2951 if (obj == nullptr) {
2952 return nullptr;
2953 }
2954 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2955}
2956
2957void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2958 if (obj != nullptr) {
2959 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2960 }
2961}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002962void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2963 check_jni = enabled;
2964 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002965}
2966
Elliott Hughes73e66f72012-05-09 09:34:45 -07002967void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2968 locals.Dump(os);
2969 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002970}
2971
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002972void JNIEnvExt::PushFrame(int /*capacity*/) {
2973 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002974 stacked_local_ref_cookies.push_back(local_ref_cookie);
2975 local_ref_cookie = locals.GetSegmentState();
2976}
2977
2978void JNIEnvExt::PopFrame() {
2979 locals.SetSegmentState(local_ref_cookie);
2980 local_ref_cookie = stacked_local_ref_cookies.back();
2981 stacked_local_ref_cookies.pop_back();
2982}
2983
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002984Offset JNIEnvExt::SegmentStateOffset() {
2985 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2986 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2987}
2988
Carl Shapiroea4dca82011-08-01 13:45:38 -07002989// JNI Invocation interface.
2990
Brian Carlstrombddf9762013-05-14 11:35:37 -07002991extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002992 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002993 if (IsBadJniVersion(args->version)) {
2994 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002995 return JNI_EVERSION;
2996 }
2997 Runtime::Options options;
2998 for (int i = 0; i < args->nOptions; ++i) {
2999 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003000 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003001 }
3002 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003003 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003004 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003005 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003006 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003007 bool started = runtime->Start();
3008 if (!started) {
3009 delete Thread::Current()->GetJniEnv();
3010 delete runtime->GetJavaVM();
3011 LOG(WARNING) << "CreateJavaVM failed";
3012 return JNI_ERR;
3013 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003014 *p_env = Thread::Current()->GetJniEnv();
3015 *p_vm = runtime->GetJavaVM();
3016 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003017}
3018
Elliott Hughesf2682d52011-08-15 16:37:04 -07003019extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003020 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003021 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003022 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003023 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003024 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003025 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003026 }
3027 return JNI_OK;
3028}
3029
3030// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003031extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003032 return JNI_ERR;
3033}
3034
Elliott Hughescdf53122011-08-19 15:46:09 -07003035class JII {
3036 public:
3037 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003038 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003039 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003040 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003041 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3042 delete raw_vm->runtime;
3043 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003044 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003045
Elliott Hughescdf53122011-08-19 15:46:09 -07003046 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003047 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003048 }
3049
3050 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003051 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003052 }
3053
3054 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003055 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003056 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003057 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003058 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3059 Runtime* runtime = raw_vm->runtime;
3060 runtime->DetachCurrentThread();
3061 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003062 }
3063
3064 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003065 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3066 // and unlike other calls that take a JNI version doesn't care if you supply
3067 // JNI_VERSION_1_1, which we don't otherwise support.
3068 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003069 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003070 return JNI_EVERSION;
3071 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003072 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003073 return JNI_ERR;
3074 }
3075 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003076 if (thread == nullptr) {
3077 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003078 return JNI_EDETACHED;
3079 }
3080 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003081 return JNI_OK;
3082 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003083};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003084
Elliott Hughes88c5c352012-03-15 18:49:48 -07003085const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003086 nullptr, // reserved0
3087 nullptr, // reserved1
3088 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003089 JII::DestroyJavaVM,
3090 JII::AttachCurrentThread,
3091 JII::DetachCurrentThread,
3092 JII::GetEnv,
3093 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003094};
3095
Elliott Hughesa0957642011-09-02 14:27:33 -07003096JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003097 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003098 check_jni_abort_hook(nullptr),
3099 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003100 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003101 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003102 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003103 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003104 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003105 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003106 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003107 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003108 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003109 libraries(new Libraries),
3110 weak_globals_lock_("JNI weak global reference table lock"),
3111 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3112 allow_new_weak_globals_(true),
3113 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003114 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003115 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003116 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003117 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003118}
3119
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003120JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003121 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003122}
3123
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003124jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3125 if (obj == nullptr) {
3126 return nullptr;
3127 }
3128 MutexLock mu(self, weak_globals_lock_);
3129 while (UNLIKELY(!allow_new_weak_globals_)) {
3130 weak_globals_add_condition_.WaitHoldingLocks(self);
3131 }
3132 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3133 return reinterpret_cast<jweak>(ref);
3134}
3135
3136void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3137 MutexLock mu(self, weak_globals_lock_);
3138 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3139 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3140 << "failed to find entry";
3141 }
3142}
3143
Elliott Hughes88c5c352012-03-15 18:49:48 -07003144void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3145 check_jni = enabled;
3146 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003147}
3148
Elliott Hughesae80b492012-04-24 10:43:17 -07003149void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3150 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3151 if (force_copy) {
3152 os << " (with forcecopy)";
3153 }
3154 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003155 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003156 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003157 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003158 os << "; pins=" << pin_table.Size();
3159 }
3160 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003161 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003162 os << "; globals=" << globals.Capacity();
3163 }
3164 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003165 MutexLock mu(self, weak_globals_lock_);
3166 if (weak_globals_.Capacity() > 0) {
3167 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003168 }
3169 }
3170 os << '\n';
3171
3172 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003173 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003174 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3175 }
3176}
3177
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003178void JavaVMExt::DisallowNewWeakGlobals() {
3179 MutexLock mu(Thread::Current(), weak_globals_lock_);
3180 allow_new_weak_globals_ = false;
3181}
3182
3183void JavaVMExt::AllowNewWeakGlobals() {
3184 Thread* self = Thread::Current();
3185 MutexLock mu(self, weak_globals_lock_);
3186 allow_new_weak_globals_ = true;
3187 weak_globals_add_condition_.Broadcast(self);
3188}
3189
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003190mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3191 MutexLock mu(self, weak_globals_lock_);
3192 while (UNLIKELY(!allow_new_weak_globals_)) {
3193 weak_globals_add_condition_.WaitHoldingLocks(self);
3194 }
3195 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3196}
3197
Elliott Hughes73e66f72012-05-09 09:34:45 -07003198void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003199 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003200 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003201 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003202 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003203 }
3204 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003205 MutexLock mu(self, weak_globals_lock_);
3206 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003207 }
3208 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003209 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003210 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003211 }
3212}
3213
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003214bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003215 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003216 std::string* detail) {
3217 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003218
3219 // See if we've already loaded this library. If we have, and the class loader
3220 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003221 // TODO: for better results we should canonicalize the pathname (or even compare
3222 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003223 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003224 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003225 {
3226 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003227 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003228 library = libraries->Get(path);
3229 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003230 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003231 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003232 // The library will be associated with class_loader. The JNI
3233 // spec says we can't load the same library into more than one
3234 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003235 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003236 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003237 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003238 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003239 return false;
3240 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003241 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003242 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003243 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003244 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003245 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003246 return false;
3247 }
3248 return true;
3249 }
3250
3251 // Open the shared library. Because we're using a full path, the system
3252 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3253 // resolve this library's dependencies though.)
3254
3255 // Failures here are expected when java.library.path has several entries
3256 // and we have to hunt for the lib.
3257
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003258 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3259 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3260 // dlopen) becomes zero from dlclose.
3261
Elliott Hughescdf53122011-08-19 15:46:09 -07003262 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003263 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003264 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003265 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003266 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003267
Elliott Hughes84b2f142012-09-27 09:16:28 -07003268 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003269
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003270 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003271 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003272 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003273 return false;
3274 }
3275
3276 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003277 // TODO: move the locking (and more of this logic) into Libraries.
3278 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003279 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003280 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003281 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003282 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003283 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003284 libraries->Put(path, library);
3285 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003286 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003287 }
3288 if (!created_library) {
3289 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003290 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003291 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003292 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003293
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003294 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3295 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003296
Elliott Hughes79353722013-08-02 16:52:18 -07003297 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003298 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003299 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003300 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003301 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003302 } else {
3303 // Call JNI_OnLoad. We have to override the current class
3304 // loader, which will always be "null" since the stuff at the
3305 // top of the stack is around Runtime.loadLibrary(). (See
3306 // the comments in the JNI FindClass function.)
3307 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3308 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003309 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003310 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003311
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003312 int version = 0;
3313 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003314 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003315 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003316 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003317 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003318
Mathieu Chartier590fee92013-09-13 13:46:47 -07003319 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003320
Elliott Hughes79353722013-08-02 16:52:18 -07003321 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003322 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003323 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003324 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003325 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003326 // It's unwise to call dlclose() here, but we can mark it
3327 // as bad and ensure that future load attempts will fail.
3328 // We don't know how far JNI_OnLoad got, so there could
3329 // be some partially-initialized stuff accessible through
3330 // newly-registered native method calls. We could try to
3331 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003332 } else {
3333 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003334 }
Elliott Hughes79353722013-08-02 16:52:18 -07003335 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003336 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003337 }
3338
Elliott Hughes79353722013-08-02 16:52:18 -07003339 library->SetResult(was_successful);
3340 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003341}
3342
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003343void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003344 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003345 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003346 // If this is a static method, it could be called before the class
3347 // has been initialized.
3348 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003349 c = EnsureInitialized(Thread::Current(), c);
3350 if (c == nullptr) {
3351 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003352 }
3353 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003354 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003355 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003356 std::string detail;
3357 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003358 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003359 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003360 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003361 native_method = libraries->FindNativeMethod(m, detail);
3362 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003363 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003364 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003365 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3366 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003367 }
3368 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003369}
3370
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003371void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003372 MutexLock mu(Thread::Current(), weak_globals_lock_);
3373 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003374 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003375 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003376 if (new_obj == nullptr) {
3377 new_obj = kClearedJniWeakGlobal;
3378 }
3379 *entry = new_obj;
3380 }
3381}
3382
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003383void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003384 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003385 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003386 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003387 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003388 }
3389 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003390 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003391 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003392 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003393 {
3394 MutexLock mu(self, libraries_lock);
3395 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003396 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003397 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003398 // The weak_globals table is visited by the GC itself (because it mutates the table).
3399}
3400
Elliott Hughesc8fece32013-01-02 11:27:23 -08003401void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003402 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003403 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003404 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003405 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3406 }
3407 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3408}
3409
Ian Rogersdf20fe02011-07-20 20:34:16 -07003410} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003411
3412std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3413 switch (rhs) {
3414 case JNIInvalidRefType:
3415 os << "JNIInvalidRefType";
3416 return os;
3417 case JNILocalRefType:
3418 os << "JNILocalRefType";
3419 return os;
3420 case JNIGlobalRefType:
3421 os << "JNIGlobalRefType";
3422 return os;
3423 case JNIWeakGlobalRefType:
3424 os << "JNIWeakGlobalRefType";
3425 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003426 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003427 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003428 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003429 }
3430}