blob: 5186399151467f254f9238495cff944b79922989 [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
Mathieu Chartier590fee92013-09-13 13:46:47 -070025#include "atomic_integer.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
Brian Carlstromea46f952013-07-30 01:26:50 -070053using ::art::mirror::ArtField;
54using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070055using ::art::mirror::Array;
56using ::art::mirror::BooleanArray;
57using ::art::mirror::ByteArray;
58using ::art::mirror::CharArray;
59using ::art::mirror::Class;
60using ::art::mirror::ClassLoader;
61using ::art::mirror::DoubleArray;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070062using ::art::mirror::FloatArray;
63using ::art::mirror::IntArray;
64using ::art::mirror::LongArray;
65using ::art::mirror::Object;
66using ::art::mirror::ObjectArray;
67using ::art::mirror::ShortArray;
68using ::art::mirror::String;
69using ::art::mirror::Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070071namespace art {
72
Brian Carlstrom7934ac22013-07-26 10:54:15 -070073static const size_t kMonitorsInitial = 32; // Arbitrary.
74static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070075
Brian Carlstrom7934ac22013-07-26 10:54:15 -070076static const size_t kLocalsInitial = 64; // Arbitrary.
77static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070078
Brian Carlstrom7934ac22013-07-26 10:54:15 -070079static const size_t kPinTableInitial = 16; // Arbitrary.
80static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070081
Brian Carlstrom7934ac22013-07-26 10:54:15 -070082static size_t gGlobalsInitial = 512; // Arbitrary.
83static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070084
Brian Carlstrom7934ac22013-07-26 10:54:15 -070085static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
86static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070087
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070090 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070091}
92
Jeff Hao19c5d372013-03-15 14:33:43 -070093static bool IsBadJniVersion(int version) {
94 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
95 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
96}
97
Brian Carlstromea46f952013-07-30 01:26:50 -070098static void CheckMethodArguments(ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700100 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 const DexFile::TypeList* params = mh.GetParameterTypeList();
102 if (params == NULL) {
103 return; // No arguments so nothing to check.
104 }
Jeff Hao5d917302013-02-27 17:57:33 -0800105 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700106 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700107 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800108 if (!m->IsStatic()) {
109 offset = 1;
110 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700111 for (uint32_t i = 0; i < num_params; i++) {
112 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
113 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
114 if (param_type == NULL) {
115 Thread* self = Thread::Current();
116 CHECK(self->IsExceptionPending());
117 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
118 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800119 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700120 self->ClearException();
121 ++error_count;
122 } else if (!param_type->IsPrimitive()) {
123 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800124 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700125 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700126 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
127 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
128 ++error_count;
129 }
Jeff Hao5d917302013-02-27 17:57:33 -0800130 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
131 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700132 }
133 }
134 if (error_count > 0) {
135 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
136 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700137 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700138 }
139}
Elliott Hughesb264f082012-04-06 17:10:10 -0700140
Brian Carlstromea46f952013-07-30 01:26:50 -0700141void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700142 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700144 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700146 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700147 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200148 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700149}
150
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
152 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700155 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700156 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800157 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700158 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800159 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700160 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
161 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700162}
163
Brian Carlstromea46f952013-07-30 01:26:50 -0700164static ArtMethod* FindVirtualMethod(Object* receiver, ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700166 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700167}
168
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
170 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700173 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700174 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800175 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700176 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800177 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700178 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
179 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700180}
181
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
183 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700186 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700187 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800188 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700189 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800190 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700191 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
192 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700193}
194
Elliott Hughes6b436852011-08-12 10:16:44 -0700195// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
196// separated with slashes but aren't wrapped with "L;" like regular descriptors
197// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
198// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
199// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700200static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700201 std::string result;
202 // Add the missing "L;" if necessary.
203 if (name[0] == '[') {
204 result = name;
205 } else {
206 result += 'L';
207 result += name;
208 result += ';';
209 }
210 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700211 if (result.find('.') != std::string::npos) {
212 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
213 << "\"" << name << "\"";
214 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700215 }
216 return result;
217}
218
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
220 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800222 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
223 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
224 "no %s method \"%s.%s%s\"",
225 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700226}
227
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800228static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
230 if (LIKELY(klass->IsInitialized())) {
231 return klass;
232 }
233 SirtRef<mirror::Class> sirt_klass(self, klass);
234 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
235 return nullptr;
236 }
237 return sirt_klass.get();
238}
239
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
241 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800243 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(jni_class));
244 if (c == nullptr) {
245 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700246 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700247
Brian Carlstromea46f952013-07-30 01:26:50 -0700248 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700249 if (is_static) {
250 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700251 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700252 method = c->FindVirtualMethod(name, sig);
253 if (method == NULL) {
254 // No virtual method matching the signature. Search declared
255 // private methods and constructors.
256 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700257 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700258 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700259
Elliott Hughescdf53122011-08-19 15:46:09 -0700260 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700262 return NULL;
263 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700266}
267
Ian Rogersef28b142012-11-30 14:22:18 -0800268static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700269 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700270 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Brian Carlstromce888532013-10-10 00:32:58 -0700271 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
272 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800273 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700274 }
Brian Carlstromce888532013-10-10 00:32:58 -0700275 // If we have a method, use its ClassLoader for context.
276 if (method != NULL) {
277 return method->GetDeclaringClass()->GetClassLoader();
278 }
279 // We don't have a method, so try to use the system ClassLoader.
280 ClassLoader* class_loader = soa.Decode<ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
281 if (class_loader != NULL) {
282 return class_loader;
283 }
284 // See if the override ClassLoader is set for gtests.
285 class_loader = soa.Self()->GetClassLoaderOverride();
286 if (class_loader != NULL) {
287 // If so, CommonTest should have set UseCompileTimeClassPath.
288 CHECK(Runtime::Current()->UseCompileTimeClassPath());
289 return class_loader;
290 }
291 // Use the BOOTCLASSPATH.
292 return NULL;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700293}
294
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
296 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800298 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(jni_class));
299 if (c == nullptr) {
300 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700301 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700302
Brian Carlstromea46f952013-07-30 01:26:50 -0700303 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 Class* field_type;
305 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
306 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800307 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700308 field_type = class_linker->FindClass(sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700309 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700310 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700311 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 if (field_type == NULL) {
313 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700316 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
319 "no type \"%s\" found and so no field \"%s\" could be found in class "
320 "\"%s\" or its superclasses", sig, name,
321 ClassHelper(c).GetDescriptor());
322 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 return NULL;
324 }
325 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800326 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800328 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700330 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
332 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
333 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
334 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700335 return NULL;
336 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700338}
339
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700340static void PinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700343 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700344 vm->pin_table.Add(array);
345}
346
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700347static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700350 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700351 vm->pin_table.Remove(array);
352}
353
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
355 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700356 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700357 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
359 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
360 "%s offset=%d length=%d %s.length=%d",
361 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700362}
Ian Rogers0571d352011-11-03 19:51:38 -0700363
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
365 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
368 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
369 "offset=%d length=%d string.length()=%d", start, length,
370 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700371}
Elliott Hughes814e4032011-08-23 12:07:56 -0700372
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700373int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700374 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 // Turn the const char* into a java.lang.String.
376 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
377 if (msg != NULL && s.get() == NULL) {
378 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700379 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700380
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 // Choose an appropriate constructor and set up the arguments.
382 jvalue args[2];
383 const char* signature;
384 if (msg == NULL && cause == NULL) {
385 signature = "()V";
386 } else if (msg != NULL && cause == NULL) {
387 signature = "(Ljava/lang/String;)V";
388 args[0].l = s.get();
389 } else if (msg == NULL && cause != NULL) {
390 signature = "(Ljava/lang/Throwable;)V";
391 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700392 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
394 args[0].l = s.get();
395 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700396 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
398 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800399 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 LOG(ERROR) << "No <init>" << signature << " in "
401 << PrettyClass(soa.Decode<Class*>(exception_class));
402 return JNI_ERR;
403 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700404
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
406 if (exception.get() == NULL) {
407 return JNI_ERR;
408 }
Ian Rogersef28b142012-11-30 14:22:18 -0800409 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
411 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700412 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700413}
414
Elliott Hughes462c9442012-03-23 18:47:50 -0700415static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700416 if (vm == NULL || p_env == NULL) {
417 return JNI_ERR;
418 }
419
Elliott Hughes462c9442012-03-23 18:47:50 -0700420 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700421 Thread* self = Thread::Current();
422 if (self != NULL) {
423 *p_env = self->GetJniEnv();
424 return JNI_OK;
425 }
426
427 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
428
429 // No threads allowed in zygote mode.
430 if (runtime->IsZygote()) {
431 LOG(ERROR) << "Attempt to attach a thread in the zygote";
432 return JNI_ERR;
433 }
434
Elliott Hughes462c9442012-03-23 18:47:50 -0700435 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
436 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700437 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700438 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700439 if (IsBadJniVersion(args->version)) {
440 LOG(ERROR) << "Bad JNI version passed to "
441 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
442 << args->version;
443 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700444 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700445 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700446 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700447 }
Elliott Hughes75770752011-08-24 17:52:38 -0700448
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800449 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700450 *p_env = NULL;
451 return JNI_ERR;
452 } else {
453 *p_env = Thread::Current()->GetJniEnv();
454 return JNI_OK;
455 }
Elliott Hughes75770752011-08-24 17:52:38 -0700456}
457
Elliott Hughes79082e32011-08-25 12:07:32 -0700458class SharedLibrary {
459 public:
460 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
461 : path_(path),
462 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700463 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700464 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700465 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700466 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700467 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 }
469
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 Object* GetClassLoader() {
471 return class_loader_;
472 }
473
474 std::string GetPath() {
475 return path_;
476 }
477
478 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700479 * Check the result of an earlier call to JNI_OnLoad on this library.
480 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700482 bool CheckOnLoadResult()
483 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700486 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
487 bool okay;
488 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700489 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700490
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700491 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700492 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
493 // caller can continue.
494 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
495 okay = true;
496 } else {
497 while (jni_on_load_result_ == kPending) {
498 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700499 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700501
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502 okay = (jni_on_load_result_ == kOkay);
503 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
504 << (okay ? "succeeded" : "failed") << "]";
505 }
506 }
507 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700508 return okay;
509 }
510
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700512 Thread* self = Thread::Current();
513 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700514
Elliott Hughes79082e32011-08-25 12:07:32 -0700515 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700516 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700517
518 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700519 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700520 }
521
522 void* FindSymbol(const std::string& symbol_name) {
523 return dlsym(handle_, symbol_name.c_str());
524 }
525
526 private:
527 enum JNI_OnLoadState {
528 kPending,
529 kFailed,
530 kOkay,
531 };
532
533 // Path to library "/system/lib/libjni.so".
534 std::string path_;
535
536 // The void* returned by dlopen(3).
537 void* handle_;
538
539 // The ClassLoader this library is associated with.
540 Object* class_loader_;
541
542 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700545 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700547 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700549 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700550};
551
Elliott Hughes79082e32011-08-25 12:07:32 -0700552// This exists mainly to keep implementation details out of the header file.
553class Libraries {
554 public:
555 Libraries() {
556 }
557
558 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700559 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 }
561
Elliott Hughesae80b492012-04-24 10:43:17 -0700562 void Dump(std::ostream& os) const {
563 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700564 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700565 if (!first) {
566 os << ' ';
567 }
568 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700569 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700570 }
571 }
572
573 size_t size() const {
574 return libraries_.size();
575 }
576
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700578 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700579 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 }
581
582 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700583 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700584 }
585
586 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700587 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 std::string jni_short_name(JniShortName(m));
590 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700591 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700592 for (const auto& lib : libraries_) {
593 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 if (library->GetClassLoader() != declaring_class_loader) {
595 // We only search libraries loaded by the appropriate ClassLoader.
596 continue;
597 }
598 // Try the short name then the long name...
599 void* fn = library->FindSymbol(jni_short_name);
600 if (fn == NULL) {
601 fn = library->FindSymbol(jni_long_name);
602 }
603 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800604 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
605 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700606 return fn;
607 }
608 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700609 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700610 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700611 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700612 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 return NULL;
614 }
615
616 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700617 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700618};
619
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
621 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700622 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700623 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700624 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800625 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700626 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800627 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700628 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
629 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800630}
631
Ian Rogersbc939662013-08-15 10:26:54 -0700632#define CHECK_NON_NULL_ARGUMENT(fn, value) \
633 if (UNLIKELY(value == NULL)) { \
634 JniAbortF(#fn, #value " == null"); \
635 }
636
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700637#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
638 if (UNLIKELY(length != 0 && value == NULL)) { \
639 JniAbortF(#fn, #value " == null"); \
640 }
641
Elliott Hughescdf53122011-08-19 15:46:09 -0700642class JNI {
643 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700644 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 return JNI_VERSION_1_6;
646 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Ian Rogers25e8b912012-09-07 11:31:36 -0700648 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700650 return NULL;
651 }
652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700654 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700655 Runtime* runtime = Runtime::Current();
656 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700658 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700659 Class* c = NULL;
660 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700661 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
662 c = class_linker->FindClass(descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700663 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800664 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700665 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700666 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700667 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700668
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700670 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 jobject art_method = env->GetObjectField(java_method,
673 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
674 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
675 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700677 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700678
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700680 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700682 jobject art_field = env->GetObjectField(java_field,
683 WellKnownClasses::java_lang_reflect_Field_artField);
684 ArtField* field = soa.Decode<ArtField*>(art_field);
685 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700686 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700690 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700691 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700692 ArtMethod* m = soa.DecodeMethod(mid);
693 jobject art_method = soa.AddLocalReference<jobject>(m);
694 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
695 if (env->ExceptionCheck()) {
696 return NULL;
697 }
698 SetObjectField(env,
699 reflect_method,
700 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
701 art_method);
702 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700704
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700706 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700708 ArtField* f = soa.DecodeField(fid);
709 jobject art_field = soa.AddLocalReference<jobject>(f);
710 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
711 if (env->ExceptionCheck()) {
712 return NULL;
713 }
714 SetObjectField(env,
715 reflect_field,
716 WellKnownClasses::java_lang_reflect_Field_artField,
717 art_field);
718 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700720
Elliott Hughes37f7a402011-08-22 18:56:01 -0700721 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700722 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 ScopedObjectAccess soa(env);
724 Object* o = soa.Decode<Object*>(java_object);
725 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700726 }
727
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700728 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700729 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 ScopedObjectAccess soa(env);
731 Class* c = soa.Decode<Class*>(java_class);
732 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700734
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700736 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
737 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738 ScopedObjectAccess soa(env);
739 Class* c1 = soa.Decode<Class*>(java_class1);
740 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700743
Elliott Hughese84278b2012-03-22 10:06:53 -0700744 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700745 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700747 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 return JNI_TRUE;
749 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700750 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700751 Object* obj = soa.Decode<Object*>(jobj);
752 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700753 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700756
Elliott Hughes37f7a402011-08-22 18:56:01 -0700757 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758 ScopedObjectAccess soa(env);
759 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 if (exception == NULL) {
761 return JNI_ERR;
762 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800763 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
764 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700765 return JNI_OK;
766 }
767
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700768 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700769 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700770 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700771 }
772
773 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700774 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700775 }
776
777 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700778 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700779 }
780
781 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700783
Brian Carlstromea46f952013-07-30 01:26:50 -0700784 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
785 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
786 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800787 uint32_t old_throw_dex_pc;
788 {
789 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700790 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 old_throw_this_object.reset(old_throw_location.GetThis());
792 old_throw_method.reset(old_throw_location.GetMethod());
793 old_exception.reset(old_exception_obj);
794 old_throw_dex_pc = old_throw_location.GetDexPc();
795 soa.Self()->ClearException();
796 }
797 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
799 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
800 if (mid == NULL) {
801 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800802 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700803 } else {
804 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800805 if (soa.Self()->IsExceptionPending()) {
806 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800808 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700809 }
810 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800811 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
812 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700813
Ian Rogers62d6c772013-02-27 08:32:07 -0800814 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700816
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800819 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 }
822
Ian Rogers25e8b912012-09-07 11:31:36 -0700823 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 LOG(FATAL) << "JNI FatalError called: " << msg;
825 }
826
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700827 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800828 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700829 return JNI_ERR;
830 }
Ian Rogersef28b142012-11-30 14:22:18 -0800831 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 return JNI_OK;
833 }
834
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700835 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 ScopedObjectAccess soa(env);
837 Object* survivor = soa.Decode<Object*>(java_survivor);
838 soa.Env()->PopFrame();
839 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 }
841
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700842 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800843 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 }
845
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700847 ScopedObjectAccess soa(env);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800848 Object* decoded_obj = soa.Decode<Object*>(obj);
849 // Check for null after decoding the object to handle cleared weak globals.
850 if (decoded_obj == nullptr) {
851 return nullptr;
852 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700853 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700855 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 return reinterpret_cast<jobject>(ref);
858 }
859
860 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 if (obj == NULL) {
862 return;
863 }
Ian Rogersef28b142012-11-30 14:22:18 -0800864 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800866 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700867 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700868
869 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
870 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
871 << "failed to find entry";
872 }
873 }
874
875 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700876 ScopedObjectAccess soa(env);
877 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 }
879
880 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700881 if (obj != nullptr) {
882 ScopedObjectAccess soa(env);
883 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885 }
886
887 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700888 ScopedObjectAccess soa(env);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800889 mirror::Object* decoded_obj = soa.Decode<Object*>(obj);
890 // Check for null after decoding the object to handle cleared weak globals.
891 if (decoded_obj == nullptr) {
892 return nullptr;
893 }
894 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
897 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 if (obj == NULL) {
899 return;
900 }
Ian Rogersef28b142012-11-30 14:22:18 -0800901 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700902
Ian Rogersef28b142012-11-30 14:22:18 -0800903 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 if (!locals.Remove(cookie, obj)) {
905 // Attempting to delete a local reference that is not in the
906 // topmost local reference frame is a no-op. DeleteLocalRef returns
907 // void and doesn't throw any exceptions, but we should probably
908 // complain about it so the user will notice that things aren't
909 // going quite the way they expect.
910 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
911 << "failed to find entry";
912 }
913 }
914
915 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700916 if (obj1 == obj2) {
917 return JNI_TRUE;
918 } else {
919 ScopedObjectAccess soa(env);
920 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
921 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700924 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700925 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 ScopedObjectAccess soa(env);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800927 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(java_class));
928 if (c == nullptr) {
929 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700930 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700931 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 }
933
Ian Rogersbc939662013-08-15 10:26:54 -0700934 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700937 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
938 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
939 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 va_end(args);
941 return result;
942 }
943
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700945 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
946 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800948 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(java_class));
949 if (c == nullptr) {
950 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700951 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700952 Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800953 if (result == nullptr) {
954 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700955 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700959 return local_result;
960 } else {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800961 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700962 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700966 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
967 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 ScopedObjectAccess soa(env);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800969 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(java_class));
970 if (c == nullptr) {
971 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700972 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700973 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700974 if (result == NULL) {
975 return NULL;
976 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700980 return local_result;
981 } else {
982 return NULL;
983 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Ian Rogersbc939662013-08-15 10:26:54 -0700986 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
987 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
988 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
989 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700991 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Ian Rogersbc939662013-08-15 10:26:54 -0700994 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
995 const char* sig) {
996 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
997 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
998 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001000 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 va_list ap;
1005 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001006 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
1007 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001008 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001009 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001015 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1016 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001017 ScopedObjectAccess soa(env);
1018 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1019 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001023 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1024 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
1026 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1027 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_list ap;
1032 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001033 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1034 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001035 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001038 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001042 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1043 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 ScopedObjectAccess soa(env);
1045 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001049 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1050 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
1052 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_list ap;
1057 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001058 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1059 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1060 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001063 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001067 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1068 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 ScopedObjectAccess soa(env);
1070 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001074 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1075 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_list ap;
1082 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001083 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1084 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001085 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001088 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001092 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1093 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 ScopedObjectAccess soa(env);
1095 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001099 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1100 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001101 ScopedObjectAccess soa(env);
1102 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001108 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1109 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001110 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001113 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001117 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1118 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001124 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1125 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 ScopedObjectAccess soa(env);
1127 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_list ap;
1132 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001133 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1134 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1135 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001138 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001142 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1143 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 ScopedObjectAccess soa(env);
1145 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001149 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1150 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
1152 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_list ap;
1157 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001158 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1159 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001160 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001163 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001167 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1168 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(env);
1170 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001174 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1175 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 ScopedObjectAccess soa(env);
1177 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 va_list ap;
1182 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001183 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1184 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001185 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001188 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001192 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1193 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194 ScopedObjectAccess soa(env);
1195 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001199 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1200 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 ScopedObjectAccess soa(env);
1202 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_list ap;
1207 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001208 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1209 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001210 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001213 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001217 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1218 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 ScopedObjectAccess soa(env);
1220 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001224 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1225 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 ScopedObjectAccess soa(env);
1227 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_list ap;
1232 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001233 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1234 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001235 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001236 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001241 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1242 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 ScopedObjectAccess soa(env);
1244 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001248 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1249 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001250 ScopedObjectAccess soa(env);
1251 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001254 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001257 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1258 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001259 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001260 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1261 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
1263 return local_result;
1264 }
1265
Ian Rogersbc939662013-08-15 10:26:54 -07001266 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1267 va_list args) {
1268 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1269 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 ScopedObjectAccess soa(env);
1271 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1272 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
Ian Rogersbc939662013-08-15 10:26:54 -07001275 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1276 jvalue* args) {
1277 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1278 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001279 ScopedObjectAccess soa(env);
1280 JValue result(InvokeWithJValues(soa, obj, mid, args));
1281 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
Ian Rogersbc939662013-08-15 10:26:54 -07001284 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1285 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001288 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1289 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001290 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001293 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Ian Rogersbc939662013-08-15 10:26:54 -07001296 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1297 va_list args) {
1298 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1299 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 ScopedObjectAccess soa(env);
1301 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Ian Rogersbc939662013-08-15 10:26:54 -07001304 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1305 jvalue* args) {
1306 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1307 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308 ScopedObjectAccess soa(env);
1309 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001315 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1316 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001317 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001320 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Ian Rogersbc939662013-08-15 10:26:54 -07001323 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1324 va_list args) {
1325 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1326 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327 ScopedObjectAccess soa(env);
1328 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Ian Rogersbc939662013-08-15 10:26:54 -07001331 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1332 jvalue* args) {
1333 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1334 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001335 ScopedObjectAccess soa(env);
1336 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001339 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001342 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1343 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1344 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001345 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001347 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Ian Rogersbc939662013-08-15 10:26:54 -07001350 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1351 va_list args) {
1352 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1353 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 ScopedObjectAccess soa(env);
1355 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Ian Rogersbc939662013-08-15 10:26:54 -07001358 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1359 jvalue* args) {
1360 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1361 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001362 ScopedObjectAccess soa(env);
1363 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001366 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001369 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1370 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001371 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001374 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Ian Rogersbc939662013-08-15 10:26:54 -07001377 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1378 va_list args) {
1379 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1380 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001381 ScopedObjectAccess soa(env);
1382 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Ian Rogersbc939662013-08-15 10:26:54 -07001385 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1386 jvalue* args) {
1387 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1388 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001389 ScopedObjectAccess soa(env);
1390 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001393 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001395 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001396 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1397 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001398 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001401 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Ian Rogersbc939662013-08-15 10:26:54 -07001404 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1405 va_list args) {
1406 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1407 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(env);
1409 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Ian Rogersbc939662013-08-15 10:26:54 -07001412 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1413 jvalue* args) {
1414 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1415 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416 ScopedObjectAccess soa(env);
1417 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001420 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001422 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001423 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1424 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001425 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001426 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001428 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Ian Rogersbc939662013-08-15 10:26:54 -07001431 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1432 va_list args) {
1433 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1434 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 ScopedObjectAccess soa(env);
1436 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Ian Rogersbc939662013-08-15 10:26:54 -07001439 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1440 jvalue* args) {
1441 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1442 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001443 ScopedObjectAccess soa(env);
1444 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001447 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001449 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001450 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1451 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001452 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001455 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Ian Rogersbc939662013-08-15 10:26:54 -07001458 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1459 va_list args) {
1460 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1461 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001462 ScopedObjectAccess soa(env);
1463 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Ian Rogersbc939662013-08-15 10:26:54 -07001466 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1467 jvalue* args) {
1468 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1469 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001470 ScopedObjectAccess soa(env);
1471 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001474 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001476 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001477 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1478 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001479 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001480 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001482 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 }
1484
Ian Rogersbc939662013-08-15 10:26:54 -07001485 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1486 va_list args) {
1487 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1488 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 ScopedObjectAccess soa(env);
1490 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
Ian Rogersbc939662013-08-15 10:26:54 -07001493 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1494 jvalue* args) {
1495 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1496 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001497 ScopedObjectAccess soa(env);
1498 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001501 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001503 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001504 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1505 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001506 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001507 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 va_end(ap);
1509 }
1510
Brian Carlstromea46f952013-07-30 01:26:50 -07001511 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1512 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001513 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1514 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001515 ScopedObjectAccess soa(env);
1516 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Ian Rogersbc939662013-08-15 10:26:54 -07001519 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1520 jvalue* args) {
1521 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1522 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001523 ScopedObjectAccess soa(env);
1524 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Ian Rogersbc939662013-08-15 10:26:54 -07001527 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1528 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1529 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1530 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001532 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001534
Ian Rogersbc939662013-08-15 10:26:54 -07001535 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1536 const char* sig) {
1537 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1538 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1539 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001541 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1546 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001547 ScopedObjectAccess soa(env);
1548 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001549 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001550 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001552
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001554 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001555 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001556 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001557 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1562 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001563 ScopedObjectAccess soa(env);
1564 Object* o = soa.Decode<Object*>(java_object);
1565 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001566 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001571 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001572 ScopedObjectAccess soa(env);
1573 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001574 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001575 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001579 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1580 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581 ScopedObjectAccess soa(env); \
1582 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001583 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001584 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001586#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001587 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001588 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001589 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001590 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001591
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001593 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1594 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001595 ScopedObjectAccess soa(env); \
1596 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001597 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001598 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001599
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001600#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001601 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001602 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001603 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001604 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001605
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001607 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001611 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 }
1613
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001615 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001619 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001623 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001626 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001627 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001630 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001631 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001635 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001638 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001639 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001643 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001646 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001647 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001651 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001654 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001655 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001658 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001659 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001660 }
1661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001662 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001663 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001667 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001668 }
1669
1670 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001671 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001672 }
1673
1674 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001675 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001676 }
1677
1678 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001679 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001680 }
1681
1682 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001683 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001684 }
1685
1686 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001687 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001688 }
1689
1690 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001691 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001692 }
1693
1694 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001695 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001696 }
1697
1698 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001699 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001700 }
1701
1702 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001703 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001704 }
1705
1706 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001707 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001708 }
1709
1710 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001711 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001712 }
1713
1714 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001715 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001716 }
1717
1718 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001719 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001720 }
1721
1722 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001723 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001724 }
1725
1726 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001727 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001728 }
1729
1730 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001731 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001737 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001738 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001739 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1740 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_end(ap);
1742 return local_result;
1743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001746 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 ScopedObjectAccess soa(env);
1748 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1749 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001753 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 ScopedObjectAccess soa(env);
1755 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1756 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001759 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001761 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001762 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001763 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001766 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001769 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001770 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 ScopedObjectAccess soa(env);
1772 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001775 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001776 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001777 ScopedObjectAccess soa(env);
1778 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001783 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001784 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001785 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001788 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001791 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001792 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 ScopedObjectAccess soa(env);
1794 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001797 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001798 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001799 ScopedObjectAccess soa(env);
1800 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001803 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001805 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001806 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001807 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001810 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001813 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001814 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001815 ScopedObjectAccess soa(env);
1816 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001820 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 ScopedObjectAccess soa(env);
1822 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001827 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001828 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001829 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001830 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001832 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001835 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001836 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 ScopedObjectAccess soa(env);
1838 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001841 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001842 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001843 ScopedObjectAccess soa(env);
1844 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001847 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001849 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001850 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001851 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001854 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 }
1856
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001857 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001858 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 ScopedObjectAccess soa(env);
1860 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001863 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001864 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001865 ScopedObjectAccess soa(env);
1866 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001869 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001871 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001872 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001873 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001876 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001879 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001880 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 ScopedObjectAccess soa(env);
1882 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001885 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001886 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ScopedObjectAccess soa(env);
1888 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001891 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001893 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001894 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001895 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001898 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 }
1900
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001901 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001902 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ScopedObjectAccess soa(env);
1904 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001907 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001908 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001909 ScopedObjectAccess soa(env);
1910 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001913 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001915 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001916 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001917 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001920 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 }
1922
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001923 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001924 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 ScopedObjectAccess soa(env);
1926 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001929 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001930 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001931 ScopedObjectAccess soa(env);
1932 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001935 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001937 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001938 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001939 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001940 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 va_end(ap);
1942 }
1943
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001944 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001945 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
1947 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001950 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001951 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
1953 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes814e4032011-08-23 12:07:56 -07001956 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001957 if (UNLIKELY(char_count < 0)) {
1958 JniAbortF("NewString", "char_count < 0: %d", char_count);
1959 return nullptr;
1960 }
1961 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1962 JniAbortF("NewString", "chars == null && char_count > 0");
1963 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001964 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001965 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001966 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
1970 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 if (utf == NULL) {
1972 return NULL;
1973 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001975 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001976 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes814e4032011-08-23 12:07:56 -07001979 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001980 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
1982 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001983 }
1984
1985 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001986 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
1988 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001989 }
1990
Ian Rogersbc939662013-08-15 10:26:54 -07001991 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1992 jchar* buf) {
1993 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 ScopedObjectAccess soa(env);
1995 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001996 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001998 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001999 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002000 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2001 memcpy(buf, chars + start, length * sizeof(jchar));
2002 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002003 }
2004
Ian Rogersbc939662013-08-15 10:26:54 -07002005 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
2006 char* buf) {
2007 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002008 ScopedObjectAccess soa(env);
2009 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002010 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002012 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002013 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002014 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2015 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
2016 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002020 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 ScopedObjectAccess soa(env);
2022 String* s = soa.Decode<String*>(java_string);
Mathieu Chartier423d2a32013-09-12 17:33:56 -07002023 CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002024 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002025 if (is_copy != nullptr) {
2026 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07002027 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002028 int32_t char_count = s->GetLength();
2029 int32_t offset = s->GetOffset();
2030 jchar* bytes = new jchar[char_count + 1];
2031 for (int32_t i = 0; i < char_count; i++) {
2032 bytes[i] = chars->Get(i + offset);
2033 }
2034 bytes[char_count] = '\0';
2035 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 }
2037
Mathieu Chartier590fee92013-09-13 13:46:47 -07002038 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogersbc939662013-08-15 10:26:54 -07002039 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002040 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002041 ScopedObjectAccess soa(env);
2042 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002046 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002050 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002054 if (java_string == NULL) {
2055 return NULL;
2056 }
2057 if (is_copy != NULL) {
2058 *is_copy = JNI_TRUE;
2059 }
Ian Rogersef28b142012-11-30 14:22:18 -08002060 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002061 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002062 size_t byte_count = s->GetUtfLength();
2063 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002064 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002065 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2066 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2067 bytes[byte_count] = '\0';
2068 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002069 }
2070
Elliott Hughes75770752011-08-24 17:52:38 -07002071 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002072 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002073 }
2074
Elliott Hughesbd935992011-08-22 11:59:34 -07002075 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002076 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
2078 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002079 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002080 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2081 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002082 Array* array = obj->AsArray();
2083 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002087 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002088 ScopedObjectAccess soa(env);
2089 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2090 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Ian Rogersbc939662013-08-15 10:26:54 -07002093 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2094 jobject java_value) {
2095 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ScopedObjectAccess soa(env);
2097 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2098 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 array->Set(index, value);
2100 }
2101
2102 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002103 ScopedObjectAccess soa(env);
2104 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
2107 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 ScopedObjectAccess soa(env);
2109 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
2112 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002113 ScopedObjectAccess soa(env);
2114 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
2117 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002118 ScopedObjectAccess soa(env);
2119 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
2122 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002123 ScopedObjectAccess soa(env);
2124 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
2127 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 ScopedObjectAccess soa(env);
2129 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
2132 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002133 ScopedObjectAccess soa(env);
2134 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Ian Rogers1d99e452014-01-02 17:36:41 -08002137 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2138 jobject initial_element) {
2139 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002140 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002141 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002142 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002143
2144 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002145 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002146 Class* array_class;
2147 {
2148 Class* element_class = soa.Decode<Class*>(element_jclass);
2149 if (UNLIKELY(element_class->IsPrimitive())) {
2150 JniAbortF("NewObjectArray", "not an object type: %s",
2151 PrettyDescriptor(element_class).c_str());
2152 return nullptr;
2153 }
2154 std::string descriptor("[");
2155 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002156
Ian Rogers1d99e452014-01-02 17:36:41 -08002157 // Find the class.
2158 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2159 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), element_class->GetClassLoader());
2160 array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
2161 if (UNLIKELY(array_class == nullptr)) {
2162 return nullptr;
2163 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes75770752011-08-24 17:52:38 -07002166 // Allocate and initialize if necessary.
Ian Rogers50b35e22012-10-04 10:09:15 -07002167 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002168 if (result != nullptr && initial_element != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 Object* initial_object = soa.Decode<Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002170 if (initial_object != nullptr) {
2171 mirror::Class* element_class = result->GetClass()->GetComponentType();
2172 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2173 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2174 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2175 PrettyDescriptor(element_class).c_str());
2176
2177 } else {
2178 for (jsize i = 0; i < length; ++i) {
2179 result->SetWithoutChecks(i, initial_object);
2180 }
2181 }
Elliott Hughes75770752011-08-24 17:52:38 -07002182 }
2183 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
2187 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002188 ScopedObjectAccess soa(env);
2189 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Ian Rogersa15e67d2012-02-28 13:51:55 -08002192 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002193 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002194 ScopedObjectAccess soa(env);
2195 Array* array = soa.Decode<Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002196 gc::Heap* heap = Runtime::Current()->GetHeap();
2197 if (heap->IsMovableObject(array)) {
2198 heap->IncrementDisableGC(soa.Self());
2199 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
2200 array = soa.Decode<Array*>(java_array);
2201 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002202 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002203 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002204 *is_copy = JNI_FALSE;
2205 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002206 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002207 }
2208
Mathieu Chartier590fee92013-09-13 13:46:47 -07002209 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002210 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002211 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002212 }
2213
Elliott Hughes75770752011-08-24 17:52:38 -07002214 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002215 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002216 ScopedObjectAccess soa(env);
2217 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Elliott Hughes75770752011-08-24 17:52:38 -07002220 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002221 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002222 ScopedObjectAccess soa(env);
2223 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Elliott Hughes75770752011-08-24 17:52:38 -07002226 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002227 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002228 ScopedObjectAccess soa(env);
2229 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Elliott Hughes75770752011-08-24 17:52:38 -07002232 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002233 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 ScopedObjectAccess soa(env);
2235 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Elliott Hughes75770752011-08-24 17:52:38 -07002238 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002239 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 ScopedObjectAccess soa(env);
2241 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughes75770752011-08-24 17:52:38 -07002244 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002245 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002246 ScopedObjectAccess soa(env);
2247 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Elliott Hughes75770752011-08-24 17:52:38 -07002250 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002251 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002252 ScopedObjectAccess soa(env);
2253 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Elliott Hughes75770752011-08-24 17:52:38 -07002256 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002257 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002258 ScopedObjectAccess soa(env);
2259 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
Mathieu Chartier590fee92013-09-13 13:46:47 -07002262 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2263 jint mode) {
2264 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
Mathieu Chartier590fee92013-09-13 13:46:47 -07002267 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2268 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Mathieu Chartier590fee92013-09-13 13:46:47 -07002271 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, 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 ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2276 jint mode) {
2277 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Mathieu Chartier590fee92013-09-13 13:46:47 -07002280 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2281 jint mode) {
2282 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284
Mathieu Chartier590fee92013-09-13 13:46:47 -07002285 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2286 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 }
2288
Mathieu Chartier590fee92013-09-13 13:46:47 -07002289 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2290 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 }
2292
Mathieu Chartier590fee92013-09-13 13:46:47 -07002293 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2294 jint mode) {
2295 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Ian Rogersbc939662013-08-15 10:26:54 -07002298 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2299 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002300 ScopedObjectAccess soa(env);
2301 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Ian Rogersbc939662013-08-15 10:26:54 -07002304 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2305 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002306 ScopedObjectAccess soa(env);
2307 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Ian Rogersbc939662013-08-15 10:26:54 -07002310 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2311 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002312 ScopedObjectAccess soa(env);
2313 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315
Ian Rogersbc939662013-08-15 10:26:54 -07002316 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2317 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002318 ScopedObjectAccess soa(env);
2319 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 }
2321
Ian Rogersbc939662013-08-15 10:26:54 -07002322 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2323 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 ScopedObjectAccess soa(env);
2325 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 }
2327
Ian Rogersbc939662013-08-15 10:26:54 -07002328 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2329 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ScopedObjectAccess soa(env);
2331 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002332 }
2333
Ian Rogersbc939662013-08-15 10:26:54 -07002334 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2335 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002336 ScopedObjectAccess soa(env);
2337 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 }
2339
Ian Rogersbc939662013-08-15 10:26:54 -07002340 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2341 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 ScopedObjectAccess soa(env);
2343 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 }
2345
Ian Rogersbc939662013-08-15 10:26:54 -07002346 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2347 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 ScopedObjectAccess soa(env);
2349 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2353 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 ScopedObjectAccess soa(env);
2355 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Ian Rogersbc939662013-08-15 10:26:54 -07002358 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2359 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 ScopedObjectAccess soa(env);
2361 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363
Ian Rogersbc939662013-08-15 10:26:54 -07002364 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2365 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002366 ScopedObjectAccess soa(env);
2367 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 }
2369
Ian Rogersbc939662013-08-15 10:26:54 -07002370 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2371 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 ScopedObjectAccess soa(env);
2373 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002374 }
2375
Ian Rogersbc939662013-08-15 10:26:54 -07002376 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2377 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 ScopedObjectAccess soa(env);
2379 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002380 }
2381
Ian Rogersbc939662013-08-15 10:26:54 -07002382 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2383 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 ScopedObjectAccess soa(env);
2385 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002386 }
2387
Ian Rogersbc939662013-08-15 10:26:54 -07002388 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2389 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002390 ScopedObjectAccess soa(env);
2391 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 }
2393
Ian Rogersbc939662013-08-15 10:26:54 -07002394 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2395 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002396 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2397 }
2398
Ian Rogersbc939662013-08-15 10:26:54 -07002399 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2400 jint method_count, bool return_errors) {
2401 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002402 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002403 return JNI_ERR; // Not reached.
2404 }
2405 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002406 ScopedObjectAccess soa(env);
2407 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002408 if (UNLIKELY(method_count == 0)) {
2409 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2410 << PrettyDescriptor(c);
2411 return JNI_OK;
2412 }
2413 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2414 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002415 const char* name = methods[i].name;
2416 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002417 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002418 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002419 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002420 ++sig;
2421 }
2422
Brian Carlstromea46f952013-07-30 01:26:50 -07002423 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002424 if (m == NULL) {
2425 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002426 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002427 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002428 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002429 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002430 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002431 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002432 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002433 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002434 << PrettyDescriptor(c) << "." << name << sig
2435 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002436 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002437 return JNI_ERR;
2438 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002439
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002440 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002441
Ian Rogers1eb512d2013-10-18 15:42:20 -07002442 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002443 }
2444 return JNI_OK;
2445 }
2446
Elliott Hughes5174fe62011-08-23 15:12:35 -07002447 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002448 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002449 ScopedObjectAccess soa(env);
2450 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002451
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002452 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002453
2454 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002455 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002456 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002457 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002458 }
2459 }
2460 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002461 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002462 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002463 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002464 }
2465 }
2466
2467 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002468 }
2469
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002470 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2471 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002472 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002473 ScopedObjectAccess soa(env);
2474 Object* o = soa.Decode<Object*>(java_object);
2475 o->MonitorEnter(soa.Self());
2476 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002477 return JNI_ERR;
2478 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002479 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002480 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002481 }
2482
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 static jint MonitorExit(JNIEnv* env, jobject java_object)
2484 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002485 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002486 ScopedObjectAccess soa(env);
2487 Object* o = soa.Decode<Object*>(java_object);
2488 o->MonitorExit(soa.Self());
2489 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002490 return JNI_ERR;
2491 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002492 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002493 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002494 }
2495
2496 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002497 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002498 Runtime* runtime = Runtime::Current();
2499 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002500 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002501 } else {
2502 *vm = NULL;
2503 }
2504 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2505 }
2506
Elliott Hughescdf53122011-08-19 15:46:09 -07002507 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002508 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002509 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002510 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002511 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002512 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002513 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002514
Elliott Hughes96a98872012-12-19 14:21:15 -08002515 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002516 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2517 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002518 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002519 jint capacity_arg = static_cast<jint>(capacity);
2520
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002521 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2522 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002523 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002524 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002525 }
2526
Elliott Hughesb465ab02011-08-24 11:21:21 -07002527 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002528 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002529 }
2530
Elliott Hughesb465ab02011-08-24 11:21:21 -07002531 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002532 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002533 }
2534
Elliott Hughesb465ab02011-08-24 11:21:21 -07002535 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002536 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002537
2538 // Do we definitely know what kind of reference this is?
2539 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2540 IndirectRefKind kind = GetIndirectRefKind(ref);
2541 switch (kind) {
2542 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002543 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002544 return JNILocalRefType;
2545 }
2546 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002547 case kGlobal:
2548 return JNIGlobalRefType;
2549 case kWeakGlobal:
2550 return JNIWeakGlobalRefType;
2551 case kSirtOrInvalid:
2552 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002553 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002554 return JNILocalRefType;
2555 }
2556
Ian Rogersef28b142012-11-30 14:22:18 -08002557 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002558 return JNIInvalidRefType;
2559 }
2560
Elliott Hughesb465ab02011-08-24 11:21:21 -07002561 // If we're handing out direct pointers, check whether it's a direct pointer
2562 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002563 {
2564 ScopedObjectAccess soa(env);
2565 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2566 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2567 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);
2633 Array* array = soa.Decode<Array*>(java_array);
2634 size_t component_size = array->GetClass()->GetComponentSize();
2635 void* array_data = array->GetRawData(component_size);
2636 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 Chartier590fee92013-09-13 13:46:47 -07002641 // Don't need to copy if we had a direct pointer.
2642 if (mode != JNI_ABORT && is_copy) {
2643 memcpy(array_data, elements, bytes);
2644 }
2645 if (mode != JNI_COMMIT) {
2646 if (is_copy) {
2647 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002648 } else if (heap->IsMovableObject(array)) {
2649 heap->DecrementDisableGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002650 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002651 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002652 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002653 }
2654
2655 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2656 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2657 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002658 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002659 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002660 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2661 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2662 ThrowAIOOBE(soa, array, start, length, "src");
2663 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002664 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002665 JavaT* data = array->GetData();
2666 memcpy(buf, data + start, length * sizeof(JavaT));
2667 }
2668 }
2669
2670 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2671 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2672 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002673 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002674 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002675 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2676 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2677 ThrowAIOOBE(soa, array, start, length, "dst");
2678 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002679 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002680 JavaT* data = array->GetData();
2681 memcpy(data + start, buf, length * sizeof(JavaT));
2682 }
2683 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002684};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002685
Elliott Hughes88c5c352012-03-15 18:49:48 -07002686const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002687 NULL, // reserved0.
2688 NULL, // reserved1.
2689 NULL, // reserved2.
2690 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002691 JNI::GetVersion,
2692 JNI::DefineClass,
2693 JNI::FindClass,
2694 JNI::FromReflectedMethod,
2695 JNI::FromReflectedField,
2696 JNI::ToReflectedMethod,
2697 JNI::GetSuperclass,
2698 JNI::IsAssignableFrom,
2699 JNI::ToReflectedField,
2700 JNI::Throw,
2701 JNI::ThrowNew,
2702 JNI::ExceptionOccurred,
2703 JNI::ExceptionDescribe,
2704 JNI::ExceptionClear,
2705 JNI::FatalError,
2706 JNI::PushLocalFrame,
2707 JNI::PopLocalFrame,
2708 JNI::NewGlobalRef,
2709 JNI::DeleteGlobalRef,
2710 JNI::DeleteLocalRef,
2711 JNI::IsSameObject,
2712 JNI::NewLocalRef,
2713 JNI::EnsureLocalCapacity,
2714 JNI::AllocObject,
2715 JNI::NewObject,
2716 JNI::NewObjectV,
2717 JNI::NewObjectA,
2718 JNI::GetObjectClass,
2719 JNI::IsInstanceOf,
2720 JNI::GetMethodID,
2721 JNI::CallObjectMethod,
2722 JNI::CallObjectMethodV,
2723 JNI::CallObjectMethodA,
2724 JNI::CallBooleanMethod,
2725 JNI::CallBooleanMethodV,
2726 JNI::CallBooleanMethodA,
2727 JNI::CallByteMethod,
2728 JNI::CallByteMethodV,
2729 JNI::CallByteMethodA,
2730 JNI::CallCharMethod,
2731 JNI::CallCharMethodV,
2732 JNI::CallCharMethodA,
2733 JNI::CallShortMethod,
2734 JNI::CallShortMethodV,
2735 JNI::CallShortMethodA,
2736 JNI::CallIntMethod,
2737 JNI::CallIntMethodV,
2738 JNI::CallIntMethodA,
2739 JNI::CallLongMethod,
2740 JNI::CallLongMethodV,
2741 JNI::CallLongMethodA,
2742 JNI::CallFloatMethod,
2743 JNI::CallFloatMethodV,
2744 JNI::CallFloatMethodA,
2745 JNI::CallDoubleMethod,
2746 JNI::CallDoubleMethodV,
2747 JNI::CallDoubleMethodA,
2748 JNI::CallVoidMethod,
2749 JNI::CallVoidMethodV,
2750 JNI::CallVoidMethodA,
2751 JNI::CallNonvirtualObjectMethod,
2752 JNI::CallNonvirtualObjectMethodV,
2753 JNI::CallNonvirtualObjectMethodA,
2754 JNI::CallNonvirtualBooleanMethod,
2755 JNI::CallNonvirtualBooleanMethodV,
2756 JNI::CallNonvirtualBooleanMethodA,
2757 JNI::CallNonvirtualByteMethod,
2758 JNI::CallNonvirtualByteMethodV,
2759 JNI::CallNonvirtualByteMethodA,
2760 JNI::CallNonvirtualCharMethod,
2761 JNI::CallNonvirtualCharMethodV,
2762 JNI::CallNonvirtualCharMethodA,
2763 JNI::CallNonvirtualShortMethod,
2764 JNI::CallNonvirtualShortMethodV,
2765 JNI::CallNonvirtualShortMethodA,
2766 JNI::CallNonvirtualIntMethod,
2767 JNI::CallNonvirtualIntMethodV,
2768 JNI::CallNonvirtualIntMethodA,
2769 JNI::CallNonvirtualLongMethod,
2770 JNI::CallNonvirtualLongMethodV,
2771 JNI::CallNonvirtualLongMethodA,
2772 JNI::CallNonvirtualFloatMethod,
2773 JNI::CallNonvirtualFloatMethodV,
2774 JNI::CallNonvirtualFloatMethodA,
2775 JNI::CallNonvirtualDoubleMethod,
2776 JNI::CallNonvirtualDoubleMethodV,
2777 JNI::CallNonvirtualDoubleMethodA,
2778 JNI::CallNonvirtualVoidMethod,
2779 JNI::CallNonvirtualVoidMethodV,
2780 JNI::CallNonvirtualVoidMethodA,
2781 JNI::GetFieldID,
2782 JNI::GetObjectField,
2783 JNI::GetBooleanField,
2784 JNI::GetByteField,
2785 JNI::GetCharField,
2786 JNI::GetShortField,
2787 JNI::GetIntField,
2788 JNI::GetLongField,
2789 JNI::GetFloatField,
2790 JNI::GetDoubleField,
2791 JNI::SetObjectField,
2792 JNI::SetBooleanField,
2793 JNI::SetByteField,
2794 JNI::SetCharField,
2795 JNI::SetShortField,
2796 JNI::SetIntField,
2797 JNI::SetLongField,
2798 JNI::SetFloatField,
2799 JNI::SetDoubleField,
2800 JNI::GetStaticMethodID,
2801 JNI::CallStaticObjectMethod,
2802 JNI::CallStaticObjectMethodV,
2803 JNI::CallStaticObjectMethodA,
2804 JNI::CallStaticBooleanMethod,
2805 JNI::CallStaticBooleanMethodV,
2806 JNI::CallStaticBooleanMethodA,
2807 JNI::CallStaticByteMethod,
2808 JNI::CallStaticByteMethodV,
2809 JNI::CallStaticByteMethodA,
2810 JNI::CallStaticCharMethod,
2811 JNI::CallStaticCharMethodV,
2812 JNI::CallStaticCharMethodA,
2813 JNI::CallStaticShortMethod,
2814 JNI::CallStaticShortMethodV,
2815 JNI::CallStaticShortMethodA,
2816 JNI::CallStaticIntMethod,
2817 JNI::CallStaticIntMethodV,
2818 JNI::CallStaticIntMethodA,
2819 JNI::CallStaticLongMethod,
2820 JNI::CallStaticLongMethodV,
2821 JNI::CallStaticLongMethodA,
2822 JNI::CallStaticFloatMethod,
2823 JNI::CallStaticFloatMethodV,
2824 JNI::CallStaticFloatMethodA,
2825 JNI::CallStaticDoubleMethod,
2826 JNI::CallStaticDoubleMethodV,
2827 JNI::CallStaticDoubleMethodA,
2828 JNI::CallStaticVoidMethod,
2829 JNI::CallStaticVoidMethodV,
2830 JNI::CallStaticVoidMethodA,
2831 JNI::GetStaticFieldID,
2832 JNI::GetStaticObjectField,
2833 JNI::GetStaticBooleanField,
2834 JNI::GetStaticByteField,
2835 JNI::GetStaticCharField,
2836 JNI::GetStaticShortField,
2837 JNI::GetStaticIntField,
2838 JNI::GetStaticLongField,
2839 JNI::GetStaticFloatField,
2840 JNI::GetStaticDoubleField,
2841 JNI::SetStaticObjectField,
2842 JNI::SetStaticBooleanField,
2843 JNI::SetStaticByteField,
2844 JNI::SetStaticCharField,
2845 JNI::SetStaticShortField,
2846 JNI::SetStaticIntField,
2847 JNI::SetStaticLongField,
2848 JNI::SetStaticFloatField,
2849 JNI::SetStaticDoubleField,
2850 JNI::NewString,
2851 JNI::GetStringLength,
2852 JNI::GetStringChars,
2853 JNI::ReleaseStringChars,
2854 JNI::NewStringUTF,
2855 JNI::GetStringUTFLength,
2856 JNI::GetStringUTFChars,
2857 JNI::ReleaseStringUTFChars,
2858 JNI::GetArrayLength,
2859 JNI::NewObjectArray,
2860 JNI::GetObjectArrayElement,
2861 JNI::SetObjectArrayElement,
2862 JNI::NewBooleanArray,
2863 JNI::NewByteArray,
2864 JNI::NewCharArray,
2865 JNI::NewShortArray,
2866 JNI::NewIntArray,
2867 JNI::NewLongArray,
2868 JNI::NewFloatArray,
2869 JNI::NewDoubleArray,
2870 JNI::GetBooleanArrayElements,
2871 JNI::GetByteArrayElements,
2872 JNI::GetCharArrayElements,
2873 JNI::GetShortArrayElements,
2874 JNI::GetIntArrayElements,
2875 JNI::GetLongArrayElements,
2876 JNI::GetFloatArrayElements,
2877 JNI::GetDoubleArrayElements,
2878 JNI::ReleaseBooleanArrayElements,
2879 JNI::ReleaseByteArrayElements,
2880 JNI::ReleaseCharArrayElements,
2881 JNI::ReleaseShortArrayElements,
2882 JNI::ReleaseIntArrayElements,
2883 JNI::ReleaseLongArrayElements,
2884 JNI::ReleaseFloatArrayElements,
2885 JNI::ReleaseDoubleArrayElements,
2886 JNI::GetBooleanArrayRegion,
2887 JNI::GetByteArrayRegion,
2888 JNI::GetCharArrayRegion,
2889 JNI::GetShortArrayRegion,
2890 JNI::GetIntArrayRegion,
2891 JNI::GetLongArrayRegion,
2892 JNI::GetFloatArrayRegion,
2893 JNI::GetDoubleArrayRegion,
2894 JNI::SetBooleanArrayRegion,
2895 JNI::SetByteArrayRegion,
2896 JNI::SetCharArrayRegion,
2897 JNI::SetShortArrayRegion,
2898 JNI::SetIntArrayRegion,
2899 JNI::SetLongArrayRegion,
2900 JNI::SetFloatArrayRegion,
2901 JNI::SetDoubleArrayRegion,
2902 JNI::RegisterNatives,
2903 JNI::UnregisterNatives,
2904 JNI::MonitorEnter,
2905 JNI::MonitorExit,
2906 JNI::GetJavaVM,
2907 JNI::GetStringRegion,
2908 JNI::GetStringUTFRegion,
2909 JNI::GetPrimitiveArrayCritical,
2910 JNI::ReleasePrimitiveArrayCritical,
2911 JNI::GetStringCritical,
2912 JNI::ReleaseStringCritical,
2913 JNI::NewWeakGlobalRef,
2914 JNI::DeleteWeakGlobalRef,
2915 JNI::ExceptionCheck,
2916 JNI::NewDirectByteBuffer,
2917 JNI::GetDirectBufferAddress,
2918 JNI::GetDirectBufferCapacity,
2919 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002920};
2921
Elliott Hughes75770752011-08-24 17:52:38 -07002922JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002923 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002924 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002925 local_ref_cookie(IRT_FIRST_SEGMENT),
2926 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002927 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002928 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002929 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002930 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002931 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002932 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002933 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002934 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2935 // errors will ensue.
2936 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2937 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002938}
2939
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002940JNIEnvExt::~JNIEnvExt() {
2941}
2942
Mathieu Chartier590fee92013-09-13 13:46:47 -07002943jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2944 if (obj == nullptr) {
2945 return nullptr;
2946 }
2947 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2948}
2949
2950void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2951 if (obj != nullptr) {
2952 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2953 }
2954}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002955void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2956 check_jni = enabled;
2957 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002958}
2959
Elliott Hughes73e66f72012-05-09 09:34:45 -07002960void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2961 locals.Dump(os);
2962 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002963}
2964
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002965void JNIEnvExt::PushFrame(int /*capacity*/) {
2966 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002967 stacked_local_ref_cookies.push_back(local_ref_cookie);
2968 local_ref_cookie = locals.GetSegmentState();
2969}
2970
2971void JNIEnvExt::PopFrame() {
2972 locals.SetSegmentState(local_ref_cookie);
2973 local_ref_cookie = stacked_local_ref_cookies.back();
2974 stacked_local_ref_cookies.pop_back();
2975}
2976
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002977Offset JNIEnvExt::SegmentStateOffset() {
2978 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2979 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2980}
2981
Carl Shapiroea4dca82011-08-01 13:45:38 -07002982// JNI Invocation interface.
2983
Brian Carlstrombddf9762013-05-14 11:35:37 -07002984extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002985 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002986 if (IsBadJniVersion(args->version)) {
2987 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002988 return JNI_EVERSION;
2989 }
2990 Runtime::Options options;
2991 for (int i = 0; i < args->nOptions; ++i) {
2992 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002993 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002994 }
2995 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002996 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002997 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002998 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002999 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003000 bool started = runtime->Start();
3001 if (!started) {
3002 delete Thread::Current()->GetJniEnv();
3003 delete runtime->GetJavaVM();
3004 LOG(WARNING) << "CreateJavaVM failed";
3005 return JNI_ERR;
3006 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003007 *p_env = Thread::Current()->GetJniEnv();
3008 *p_vm = runtime->GetJavaVM();
3009 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003010}
3011
Elliott Hughesf2682d52011-08-15 16:37:04 -07003012extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003013 Runtime* runtime = Runtime::Current();
3014 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003015 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003016 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003017 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003018 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003019 }
3020 return JNI_OK;
3021}
3022
3023// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003024extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003025 return JNI_ERR;
3026}
3027
Elliott Hughescdf53122011-08-19 15:46:09 -07003028class JII {
3029 public:
3030 static jint DestroyJavaVM(JavaVM* vm) {
3031 if (vm == NULL) {
3032 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003033 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003034 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3035 delete raw_vm->runtime;
3036 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003037 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003038
Elliott Hughescdf53122011-08-19 15:46:09 -07003039 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003040 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003041 }
3042
3043 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003044 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003045 }
3046
3047 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07003048 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003049 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003050 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003051 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3052 Runtime* runtime = raw_vm->runtime;
3053 runtime->DetachCurrentThread();
3054 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003055 }
3056
3057 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003058 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3059 // and unlike other calls that take a JNI version doesn't care if you supply
3060 // JNI_VERSION_1_1, which we don't otherwise support.
3061 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003062 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003063 return JNI_EVERSION;
3064 }
3065 if (vm == NULL || env == NULL) {
3066 return JNI_ERR;
3067 }
3068 Thread* thread = Thread::Current();
3069 if (thread == NULL) {
3070 *env = NULL;
3071 return JNI_EDETACHED;
3072 }
3073 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003074 return JNI_OK;
3075 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003076};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003077
Elliott Hughes88c5c352012-03-15 18:49:48 -07003078const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07003079 NULL, // reserved0
3080 NULL, // reserved1
3081 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003082 JII::DestroyJavaVM,
3083 JII::AttachCurrentThread,
3084 JII::DetachCurrentThread,
3085 JII::GetEnv,
3086 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003087};
3088
Elliott Hughesa0957642011-09-02 14:27:33 -07003089JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003090 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07003091 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07003092 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003093 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003094 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003095 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003096 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003097 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003098 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003099 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003100 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003101 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003102 libraries(new Libraries),
3103 weak_globals_lock_("JNI weak global reference table lock"),
3104 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3105 allow_new_weak_globals_(true),
3106 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003107 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003108 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003109 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003110 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003111}
3112
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003113JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003114 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003115}
3116
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003117jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3118 if (obj == nullptr) {
3119 return nullptr;
3120 }
3121 MutexLock mu(self, weak_globals_lock_);
3122 while (UNLIKELY(!allow_new_weak_globals_)) {
3123 weak_globals_add_condition_.WaitHoldingLocks(self);
3124 }
3125 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3126 return reinterpret_cast<jweak>(ref);
3127}
3128
3129void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3130 MutexLock mu(self, weak_globals_lock_);
3131 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3132 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3133 << "failed to find entry";
3134 }
3135}
3136
Elliott Hughes88c5c352012-03-15 18:49:48 -07003137void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3138 check_jni = enabled;
3139 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003140}
3141
Elliott Hughesae80b492012-04-24 10:43:17 -07003142void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3143 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3144 if (force_copy) {
3145 os << " (with forcecopy)";
3146 }
3147 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003148 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003149 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003150 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003151 os << "; pins=" << pin_table.Size();
3152 }
3153 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003154 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003155 os << "; globals=" << globals.Capacity();
3156 }
3157 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003158 MutexLock mu(self, weak_globals_lock_);
3159 if (weak_globals_.Capacity() > 0) {
3160 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003161 }
3162 }
3163 os << '\n';
3164
3165 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003166 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003167 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3168 }
3169}
3170
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003171void JavaVMExt::DisallowNewWeakGlobals() {
3172 MutexLock mu(Thread::Current(), weak_globals_lock_);
3173 allow_new_weak_globals_ = false;
3174}
3175
3176void JavaVMExt::AllowNewWeakGlobals() {
3177 Thread* self = Thread::Current();
3178 MutexLock mu(self, weak_globals_lock_);
3179 allow_new_weak_globals_ = true;
3180 weak_globals_add_condition_.Broadcast(self);
3181}
3182
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003183mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3184 MutexLock mu(self, weak_globals_lock_);
3185 while (UNLIKELY(!allow_new_weak_globals_)) {
3186 weak_globals_add_condition_.WaitHoldingLocks(self);
3187 }
3188 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3189}
3190
Elliott Hughes73e66f72012-05-09 09:34:45 -07003191void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003192 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003193 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003194 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003195 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003196 }
3197 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003198 MutexLock mu(self, weak_globals_lock_);
3199 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003200 }
3201 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003202 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003203 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003204 }
3205}
3206
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003207bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003208 std::string* detail) {
3209 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003210
3211 // See if we've already loaded this library. If we have, and the class loader
3212 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003213 // TODO: for better results we should canonicalize the pathname (or even compare
3214 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003215 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003216 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003217 {
3218 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003219 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003220 library = libraries->Get(path);
3221 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003222 if (library != NULL) {
3223 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003224 // The library will be associated with class_loader. The JNI
3225 // spec says we can't load the same library into more than one
3226 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003227 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003228 "ClassLoader %p; can't open in ClassLoader %p",
3229 path.c_str(), library->GetClassLoader(), class_loader);
3230 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003231 return false;
3232 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003233 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3234 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003235 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003236 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003237 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003238 return false;
3239 }
3240 return true;
3241 }
3242
3243 // Open the shared library. Because we're using a full path, the system
3244 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3245 // resolve this library's dependencies though.)
3246
3247 // Failures here are expected when java.library.path has several entries
3248 // and we have to hunt for the lib.
3249
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003250 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3251 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3252 // dlopen) becomes zero from dlclose.
3253
Elliott Hughescdf53122011-08-19 15:46:09 -07003254 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003255 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003256 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3257 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3258 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003259
Elliott Hughes84b2f142012-09-27 09:16:28 -07003260 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003261
3262 if (handle == NULL) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003263 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003264 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003265 return false;
3266 }
3267
3268 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003269 // TODO: move the locking (and more of this logic) into Libraries.
3270 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003271 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003272 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003273 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003274 if (library == NULL) { // We won race to get libraries_lock
3275 library = new SharedLibrary(path, handle, class_loader);
3276 libraries->Put(path, library);
3277 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003278 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003279 }
3280 if (!created_library) {
3281 LOG(INFO) << "WOW: we lost a race to add shared library: "
3282 << "\"" << path << "\" ClassLoader=" << class_loader;
3283 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003284 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003285
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003286 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003287
Elliott Hughes79353722013-08-02 16:52:18 -07003288 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003289 void* sym = dlsym(handle, "JNI_OnLoad");
3290 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003291 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003292 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003293 } else {
3294 // Call JNI_OnLoad. We have to override the current class
3295 // loader, which will always be "null" since the stuff at the
3296 // top of the stack is around Runtime.loadLibrary(). (See
3297 // the comments in the JNI FindClass function.)
3298 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3299 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003300 SirtRef<ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Elliott Hughes79082e32011-08-25 12:07:32 -07003301 self->SetClassLoaderOverride(class_loader);
3302
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003303 int version = 0;
3304 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003305 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003306 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003307 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003308 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003309
Mathieu Chartier590fee92013-09-13 13:46:47 -07003310 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003311
Elliott Hughes79353722013-08-02 16:52:18 -07003312 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003313 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003314 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003315 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003316 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003317 // It's unwise to call dlclose() here, but we can mark it
3318 // as bad and ensure that future load attempts will fail.
3319 // We don't know how far JNI_OnLoad got, so there could
3320 // be some partially-initialized stuff accessible through
3321 // newly-registered native method calls. We could try to
3322 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003323 } else {
3324 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003325 }
Elliott Hughes79353722013-08-02 16:52:18 -07003326 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003327 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003328 }
3329
Elliott Hughes79353722013-08-02 16:52:18 -07003330 library->SetResult(was_successful);
3331 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003332}
3333
Brian Carlstromea46f952013-07-30 01:26:50 -07003334void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003335 CHECK(m->IsNative());
3336
3337 Class* c = m->GetDeclaringClass();
3338
3339 // If this is a static method, it could be called before the class
3340 // has been initialized.
3341 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003342 c = EnsureInitialized(Thread::Current(), c);
3343 if (c == nullptr) {
3344 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003345 }
3346 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003347 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003348 }
3349
Brian Carlstrom16192862011-09-12 17:50:06 -07003350 std::string detail;
3351 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003352 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003353 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003354 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003355 native_method = libraries->FindNativeMethod(m, detail);
3356 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003357 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003358 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003359 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3360 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003361 }
3362 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003363}
3364
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003365void JavaVMExt::SweepJniWeakGlobals(RootVisitor visitor, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003366 MutexLock mu(Thread::Current(), weak_globals_lock_);
3367 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003368 mirror::Object* obj = *entry;
3369 mirror::Object* new_obj = visitor(obj, arg);
3370 if (new_obj == nullptr) {
3371 new_obj = kClearedJniWeakGlobal;
3372 }
3373 *entry = new_obj;
3374 }
3375}
3376
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003377void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003378 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003379 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003380 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003381 globals.VisitRoots(visitor, arg);
3382 }
3383 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003384 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003385 pin_table.VisitRoots(visitor, arg);
3386 }
3387 // The weak_globals table is visited by the GC itself (because it mutates the table).
3388}
3389
Elliott Hughesc8fece32013-01-02 11:27:23 -08003390void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003391 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003392 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3393 if (c.get() == NULL) {
3394 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3395 }
3396 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3397}
3398
Ian Rogersdf20fe02011-07-20 20:34:16 -07003399} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003400
3401std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3402 switch (rhs) {
3403 case JNIInvalidRefType:
3404 os << "JNIInvalidRefType";
3405 return os;
3406 case JNILocalRefType:
3407 os << "JNILocalRefType";
3408 return os;
3409 case JNIGlobalRefType:
3410 os << "JNIGlobalRefType";
3411 return os;
3412 case JNIWeakGlobalRefType:
3413 os << "JNIWeakGlobalRefType";
3414 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003415 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003416 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003417 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003418 }
3419}