blob: 030b2134cc2ff4829c8ec3a3bdd5c1e9361d2b74 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Ian Rogersef7d42f2014-01-06 12:55:46 -080025#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080029#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070030#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070033#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070034#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070035#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070036#include "mirror/art_field-inl.h"
37#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class-inl.h"
39#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
42#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070044#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070045#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070051#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070052
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
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800526 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800527 if (class_loader_ != nullptr) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800528 class_loader_ = visitor(class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800529 }
530 }
531
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 private:
533 enum JNI_OnLoadState {
534 kPending,
535 kFailed,
536 kOkay,
537 };
538
539 // Path to library "/system/lib/libjni.so".
540 std::string path_;
541
542 // The void* returned by dlopen(3).
543 void* handle_;
544
545 // The ClassLoader this library is associated with.
546 Object* class_loader_;
547
548 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700549 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700551 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700553 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700555 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700556};
557
Elliott Hughes79082e32011-08-25 12:07:32 -0700558// This exists mainly to keep implementation details out of the header file.
559class Libraries {
560 public:
561 Libraries() {
562 }
563
564 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700565 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 }
567
Elliott Hughesae80b492012-04-24 10:43:17 -0700568 void Dump(std::ostream& os) const {
569 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700570 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700571 if (!first) {
572 os << ' ';
573 }
574 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700575 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700576 }
577 }
578
579 size_t size() const {
580 return libraries_.size();
581 }
582
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700584 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700585 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 }
587
588 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700589 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 }
591
592 // See section 11.3 "Linking Native Methods" of the JNI spec.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800593 void* FindNativeMethod(ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700594 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700595 std::string jni_short_name(JniShortName(m));
596 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700597 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700598 for (const auto& lib : libraries_) {
599 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 if (library->GetClassLoader() != declaring_class_loader) {
601 // We only search libraries loaded by the appropriate ClassLoader.
602 continue;
603 }
604 // Try the short name then the long name...
605 void* fn = library->FindSymbol(jni_short_name);
606 if (fn == NULL) {
607 fn = library->FindSymbol(jni_long_name);
608 }
609 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800610 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
611 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700612 return fn;
613 }
614 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700615 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700616 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700617 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700618 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700619 return NULL;
620 }
621
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800622 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800623 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800624 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800625 }
626 }
627
Elliott Hughes79082e32011-08-25 12:07:32 -0700628 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700629 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700630};
631
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
633 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700634 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700635 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700636 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800637 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700638 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800639 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700640 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
641 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800642}
643
Ian Rogersbc939662013-08-15 10:26:54 -0700644#define CHECK_NON_NULL_ARGUMENT(fn, value) \
645 if (UNLIKELY(value == NULL)) { \
646 JniAbortF(#fn, #value " == null"); \
647 }
648
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700649#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
650 if (UNLIKELY(length != 0 && value == NULL)) { \
651 JniAbortF(#fn, #value " == null"); \
652 }
653
Elliott Hughescdf53122011-08-19 15:46:09 -0700654class JNI {
655 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700656 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 return JNI_VERSION_1_6;
658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Ian Rogers25e8b912012-09-07 11:31:36 -0700660 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700662 return NULL;
663 }
664
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700666 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700667 Runtime* runtime = Runtime::Current();
668 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700670 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700671 Class* c = NULL;
672 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700673 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
674 c = class_linker->FindClass(descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700675 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800676 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700677 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700679 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700680
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700682 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700684 jobject art_method = env->GetObjectField(java_method,
685 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
686 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
687 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700689 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700690
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700692 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700694 jobject art_field = env->GetObjectField(java_field,
695 WellKnownClasses::java_lang_reflect_Field_artField);
696 ArtField* field = soa.Decode<ArtField*>(art_field);
697 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700700
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700702 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700703 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700704 ArtMethod* m = soa.DecodeMethod(mid);
705 jobject art_method = soa.AddLocalReference<jobject>(m);
706 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
707 if (env->ExceptionCheck()) {
708 return NULL;
709 }
710 SetObjectField(env,
711 reflect_method,
712 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
713 art_method);
714 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700716
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700718 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700720 ArtField* f = soa.DecodeField(fid);
721 jobject art_field = soa.AddLocalReference<jobject>(f);
722 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
723 if (env->ExceptionCheck()) {
724 return NULL;
725 }
726 SetObjectField(env,
727 reflect_field,
728 WellKnownClasses::java_lang_reflect_Field_artField,
729 art_field);
730 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700734 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 ScopedObjectAccess soa(env);
736 Object* o = soa.Decode<Object*>(java_object);
737 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 }
739
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700740 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700741 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 ScopedObjectAccess soa(env);
743 Class* c = soa.Decode<Class*>(java_class);
744 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700746
Elliott Hughes37f7a402011-08-22 18:56:01 -0700747 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700748 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
749 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 ScopedObjectAccess soa(env);
751 Class* c1 = soa.Decode<Class*>(java_class1);
752 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700753 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700755
Elliott Hughese84278b2012-03-22 10:06:53 -0700756 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700757 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700759 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 return JNI_TRUE;
761 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700762 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 Object* obj = soa.Decode<Object*>(jobj);
764 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700765 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700768
Elliott Hughes37f7a402011-08-22 18:56:01 -0700769 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770 ScopedObjectAccess soa(env);
771 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700772 if (exception == NULL) {
773 return JNI_ERR;
774 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800775 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
776 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700777 return JNI_OK;
778 }
779
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700780 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700781 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700782 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700783 }
784
785 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700786 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700787 }
788
789 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700790 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700791 }
792
793 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700795
Brian Carlstromea46f952013-07-30 01:26:50 -0700796 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
797 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
798 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800799 uint32_t old_throw_dex_pc;
800 {
801 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700802 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 old_throw_this_object.reset(old_throw_location.GetThis());
804 old_throw_method.reset(old_throw_location.GetMethod());
805 old_exception.reset(old_exception_obj);
806 old_throw_dex_pc = old_throw_location.GetDexPc();
807 soa.Self()->ClearException();
808 }
809 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700810 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
811 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
812 if (mid == NULL) {
813 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800814 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700815 } else {
816 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800817 if (soa.Self()->IsExceptionPending()) {
818 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700819 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800820 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 }
822 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800823 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
824 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700825
Ian Rogers62d6c772013-02-27 08:32:07 -0800826 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700828
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700830 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800831 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 }
834
Ian Rogers25e8b912012-09-07 11:31:36 -0700835 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 LOG(FATAL) << "JNI FatalError called: " << msg;
837 }
838
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700839 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800840 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700841 return JNI_ERR;
842 }
Ian Rogersef28b142012-11-30 14:22:18 -0800843 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 return JNI_OK;
845 }
846
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700847 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848 ScopedObjectAccess soa(env);
849 Object* survivor = soa.Decode<Object*>(java_survivor);
850 soa.Env()->PopFrame();
851 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 }
853
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700854 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800855 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 }
857
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700859 ScopedObjectAccess soa(env);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800860 Object* decoded_obj = soa.Decode<Object*>(obj);
861 // Check for null after decoding the object to handle cleared weak globals.
862 if (decoded_obj == nullptr) {
863 return nullptr;
864 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700867 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700868 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 return reinterpret_cast<jobject>(ref);
870 }
871
872 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 if (obj == NULL) {
874 return;
875 }
Ian Rogersef28b142012-11-30 14:22:18 -0800876 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800878 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700879 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700880
881 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
882 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
883 << "failed to find entry";
884 }
885 }
886
887 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 ScopedObjectAccess soa(env);
889 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
892 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700893 if (obj != nullptr) {
894 ScopedObjectAccess soa(env);
895 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 }
897 }
898
899 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700900 ScopedObjectAccess soa(env);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800901 mirror::Object* decoded_obj = soa.Decode<Object*>(obj);
902 // Check for null after decoding the object to handle cleared weak globals.
903 if (decoded_obj == nullptr) {
904 return nullptr;
905 }
906 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
909 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 if (obj == NULL) {
911 return;
912 }
Ian Rogersef28b142012-11-30 14:22:18 -0800913 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700914
Ian Rogersef28b142012-11-30 14:22:18 -0800915 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 if (!locals.Remove(cookie, obj)) {
917 // Attempting to delete a local reference that is not in the
918 // topmost local reference frame is a no-op. DeleteLocalRef returns
919 // void and doesn't throw any exceptions, but we should probably
920 // complain about it so the user will notice that things aren't
921 // going quite the way they expect.
922 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
923 << "failed to find entry";
924 }
925 }
926
927 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700928 if (obj1 == obj2) {
929 return JNI_TRUE;
930 } else {
931 ScopedObjectAccess soa(env);
932 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
933 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700936 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700937 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 ScopedObjectAccess soa(env);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800939 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(java_class));
940 if (c == nullptr) {
941 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700942 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700943 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Ian Rogersbc939662013-08-15 10:26:54 -0700946 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700949 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
950 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
951 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 va_end(args);
953 return result;
954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700957 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
958 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 ScopedObjectAccess soa(env);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800960 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(java_class));
961 if (c == nullptr) {
962 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700963 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700964 Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800965 if (result == nullptr) {
966 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700967 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700971 return local_result;
972 } else {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800973 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700974 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700978 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
979 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800981 Class* c = EnsureInitialized(soa.Self(), soa.Decode<Class*>(java_class));
982 if (c == nullptr) {
983 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700984 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700985 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700986 if (result == NULL) {
987 return NULL;
988 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700992 return local_result;
993 } else {
994 return NULL;
995 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Ian Rogersbc939662013-08-15 10:26:54 -0700998 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
999 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
1000 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
1001 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001003 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Ian Rogersbc939662013-08-15 10:26:54 -07001006 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
1007 const char* sig) {
1008 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
1009 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
1010 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001012 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 va_list ap;
1017 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001018 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
1019 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001020 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001027 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1028 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
1030 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1031 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001035 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1036 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1039 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_list ap;
1044 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001045 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1046 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001047 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001050 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001054 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1055 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 ScopedObjectAccess soa(env);
1057 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001061 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1062 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
1064 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001070 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1071 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1072 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001075 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001079 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1080 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001086 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1087 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 ScopedObjectAccess soa(env);
1089 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_list ap;
1094 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001095 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1096 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001097 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001100 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001104 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1105 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001111 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1112 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
1114 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_list ap;
1119 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001120 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1121 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001122 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001125 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001129 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1130 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131 ScopedObjectAccess soa(env);
1132 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001136 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1137 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
1139 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_list ap;
1144 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001145 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1146 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1147 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001154 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1155 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001161 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1162 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
1164 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_list ap;
1169 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001170 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1171 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001172 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001175 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001179 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1180 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 ScopedObjectAccess soa(env);
1182 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001186 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1187 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
1189 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 va_list ap;
1194 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001195 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1196 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001197 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001204 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1205 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
1207 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001211 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1212 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213 ScopedObjectAccess soa(env);
1214 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_list ap;
1219 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001220 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1221 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001222 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001223 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001225 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001229 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1230 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 ScopedObjectAccess soa(env);
1232 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001236 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1237 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
1239 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 va_list ap;
1244 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001245 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1246 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001247 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001248 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001253 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1254 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 ScopedObjectAccess soa(env);
1256 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001260 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1261 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 ScopedObjectAccess soa(env);
1263 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001266 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001269 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1270 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001271 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1273 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
1275 return local_result;
1276 }
1277
Ian Rogersbc939662013-08-15 10:26:54 -07001278 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1279 va_list args) {
1280 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1281 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 ScopedObjectAccess soa(env);
1283 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1284 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Ian Rogersbc939662013-08-15 10:26:54 -07001287 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1288 jvalue* args) {
1289 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1290 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 JValue result(InvokeWithJValues(soa, obj, mid, args));
1293 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Ian Rogersbc939662013-08-15 10:26:54 -07001296 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1297 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001300 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1301 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001302 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001305 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Ian Rogersbc939662013-08-15 10:26:54 -07001308 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1309 va_list args) {
1310 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1311 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(env);
1313 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Ian Rogersbc939662013-08-15 10:26:54 -07001316 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1317 jvalue* args) {
1318 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1319 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(env);
1321 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001327 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1328 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001329 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001332 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Ian Rogersbc939662013-08-15 10:26:54 -07001335 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1336 va_list args) {
1337 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1338 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env);
1340 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Ian Rogersbc939662013-08-15 10:26:54 -07001343 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1344 jvalue* args) {
1345 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1346 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
1348 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001354 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1355 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1356 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001359 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Ian Rogersbc939662013-08-15 10:26:54 -07001362 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1363 va_list args) {
1364 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1365 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Ian Rogersbc939662013-08-15 10:26:54 -07001370 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1371 jvalue* args) {
1372 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1373 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(env);
1375 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001381 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1382 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001383 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001386 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Ian Rogersbc939662013-08-15 10:26:54 -07001389 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1390 va_list args) {
1391 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1392 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 ScopedObjectAccess soa(env);
1394 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Ian Rogersbc939662013-08-15 10:26:54 -07001397 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1398 jvalue* args) {
1399 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1400 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001401 ScopedObjectAccess soa(env);
1402 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001408 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1409 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001410 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001413 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Ian Rogersbc939662013-08-15 10:26:54 -07001416 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1417 va_list args) {
1418 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1419 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001420 ScopedObjectAccess soa(env);
1421 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Ian Rogersbc939662013-08-15 10:26:54 -07001424 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1425 jvalue* args) {
1426 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1427 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 ScopedObjectAccess soa(env);
1429 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001434 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001435 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1436 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001437 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001440 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Ian Rogersbc939662013-08-15 10:26:54 -07001443 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1444 va_list args) {
1445 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1446 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
1448 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Ian Rogersbc939662013-08-15 10:26:54 -07001451 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1452 jvalue* args) {
1453 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1454 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 ScopedObjectAccess soa(env);
1456 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001459 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001461 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001462 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1463 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001464 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001465 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001467 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Ian Rogersbc939662013-08-15 10:26:54 -07001470 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1471 va_list args) {
1472 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1473 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 ScopedObjectAccess soa(env);
1475 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Ian Rogersbc939662013-08-15 10:26:54 -07001478 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1479 jvalue* args) {
1480 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1481 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 ScopedObjectAccess soa(env);
1483 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001486 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001488 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001489 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1490 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001491 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001494 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Ian Rogersbc939662013-08-15 10:26:54 -07001497 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1498 va_list args) {
1499 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1500 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 ScopedObjectAccess soa(env);
1502 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Ian Rogersbc939662013-08-15 10:26:54 -07001505 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1506 jvalue* args) {
1507 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1508 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 ScopedObjectAccess soa(env);
1510 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 }
1512
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001513 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001515 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001516 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1517 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001518 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001519 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 va_end(ap);
1521 }
1522
Brian Carlstromea46f952013-07-30 01:26:50 -07001523 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1524 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001525 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1526 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001527 ScopedObjectAccess soa(env);
1528 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Ian Rogersbc939662013-08-15 10:26:54 -07001531 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1532 jvalue* args) {
1533 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1534 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001535 ScopedObjectAccess soa(env);
1536 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Ian Rogersbc939662013-08-15 10:26:54 -07001539 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1540 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1541 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1542 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001543 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001544 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001546
Ian Rogersbc939662013-08-15 10:26:54 -07001547 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1548 const char* sig) {
1549 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1550 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1551 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001552 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001553 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001555
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1558 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001559 ScopedObjectAccess soa(env);
1560 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001561 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001562 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001564
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001566 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001567 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001568 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001569 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1574 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001575 ScopedObjectAccess soa(env);
1576 Object* o = soa.Decode<Object*>(java_object);
1577 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001578 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001579 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001583 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001584 ScopedObjectAccess soa(env);
1585 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001586 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001587 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001591 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1592 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 ScopedObjectAccess soa(env); \
1594 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001595 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001596 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001598#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001599 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001600 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001601 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001602 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001603
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001605 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1606 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001607 ScopedObjectAccess soa(env); \
1608 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001609 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001610 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001611
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001612#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001613 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001614 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001615 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001616 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001617
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001619 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001623 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001626 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001627 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001630 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001631 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001635 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001638 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001639 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001642 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001643 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001647 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001651 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001654 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001655 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001658 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001659 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001662 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001663 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001667 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001670 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001671 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001675 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001676 }
1677
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001678 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001679 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001680 }
1681
1682 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001683 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001684 }
1685
1686 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001687 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001688 }
1689
1690 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001691 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001692 }
1693
1694 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001695 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001696 }
1697
1698 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001699 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001700 }
1701
1702 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001703 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001704 }
1705
1706 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001707 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001708 }
1709
1710 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001711 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001712 }
1713
1714 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001715 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001716 }
1717
1718 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001719 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001720 }
1721
1722 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001723 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001724 }
1725
1726 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001727 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001728 }
1729
1730 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001731 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001732 }
1733
1734 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001735 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001736 }
1737
1738 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001739 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001740 }
1741
1742 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001743 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001746 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001749 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001750 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1752 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 va_end(ap);
1754 return local_result;
1755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001758 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
1760 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1761 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001764 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001765 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 ScopedObjectAccess soa(env);
1767 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1768 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001773 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001774 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001775 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001776 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001778 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001782 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ScopedObjectAccess soa(env);
1784 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001788 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
1790 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001793 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001795 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001796 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001797 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001800 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001803 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001804 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 ScopedObjectAccess soa(env);
1806 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001809 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001810 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
1812 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001817 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001818 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001819 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001820 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001822 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001826 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 ScopedObjectAccess soa(env);
1828 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001831 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001832 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001833 ScopedObjectAccess soa(env);
1834 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001837 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001839 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001840 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001841 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001844 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001847 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001848 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 ScopedObjectAccess soa(env);
1850 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001853 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001854 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001855 ScopedObjectAccess soa(env);
1856 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001859 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001861 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001862 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001863 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001864 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001866 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001869 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001870 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
1872 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001875 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001876 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001877 ScopedObjectAccess soa(env);
1878 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001881 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001883 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001884 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001885 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001888 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001891 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001892 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 ScopedObjectAccess soa(env);
1894 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001897 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001898 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001899 ScopedObjectAccess soa(env);
1900 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 }
1902
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001903 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001905 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001906 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001907 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001910 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001913 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001914 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001915 ScopedObjectAccess soa(env);
1916 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 }
1918
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001919 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001920 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 ScopedObjectAccess soa(env);
1922 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001925 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001927 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001928 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001929 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001930 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001932 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001935 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001936 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
1938 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001941 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001942 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001943 ScopedObjectAccess soa(env);
1944 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001947 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001949 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001950 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001951 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 va_end(ap);
1954 }
1955
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001956 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001957 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001958 ScopedObjectAccess soa(env);
1959 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001962 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001963 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001964 ScopedObjectAccess soa(env);
1965 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes814e4032011-08-23 12:07:56 -07001968 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001969 if (UNLIKELY(char_count < 0)) {
1970 JniAbortF("NewString", "char_count < 0: %d", char_count);
1971 return nullptr;
1972 }
1973 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1974 JniAbortF("NewString", "chars == null && char_count > 0");
1975 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001976 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001977 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001978 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001979 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
1982 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 if (utf == NULL) {
1984 return NULL;
1985 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001986 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001987 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001988 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001992 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001993 ScopedObjectAccess soa(env);
1994 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001995 }
1996
1997 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001998 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001999 ScopedObjectAccess soa(env);
2000 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07002001 }
2002
Ian Rogersbc939662013-08-15 10:26:54 -07002003 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
2004 jchar* buf) {
2005 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002006 ScopedObjectAccess soa(env);
2007 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002008 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002009 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002010 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002011 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002012 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2013 memcpy(buf, chars + start, length * sizeof(jchar));
2014 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002015 }
2016
Ian Rogersbc939662013-08-15 10:26:54 -07002017 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
2018 char* buf) {
2019 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002020 ScopedObjectAccess soa(env);
2021 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002022 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002023 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002024 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002025 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002026 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2027 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
2028 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002032 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002033 ScopedObjectAccess soa(env);
2034 String* s = soa.Decode<String*>(java_string);
Mathieu Chartier423d2a32013-09-12 17:33:56 -07002035 CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002036 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002037 if (is_copy != nullptr) {
2038 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07002039 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002040 int32_t char_count = s->GetLength();
2041 int32_t offset = s->GetOffset();
2042 jchar* bytes = new jchar[char_count + 1];
2043 for (int32_t i = 0; i < char_count; i++) {
2044 bytes[i] = chars->Get(i + offset);
2045 }
2046 bytes[char_count] = '\0';
2047 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 }
2049
Mathieu Chartier590fee92013-09-13 13:46:47 -07002050 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogersbc939662013-08-15 10:26:54 -07002051 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002052 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002053 ScopedObjectAccess soa(env);
2054 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes75770752011-08-24 17:52:38 -07002057 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002058 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes75770752011-08-24 17:52:38 -07002061 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002062 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes75770752011-08-24 17:52:38 -07002065 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002066 if (java_string == NULL) {
2067 return NULL;
2068 }
2069 if (is_copy != NULL) {
2070 *is_copy = JNI_TRUE;
2071 }
Ian Rogersef28b142012-11-30 14:22:18 -08002072 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002073 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002074 size_t byte_count = s->GetUtfLength();
2075 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002076 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002077 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2078 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2079 bytes[byte_count] = '\0';
2080 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002081 }
2082
Elliott Hughes75770752011-08-24 17:52:38 -07002083 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002084 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002085 }
2086
Elliott Hughesbd935992011-08-22 11:59:34 -07002087 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002088 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002089 ScopedObjectAccess soa(env);
2090 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002091 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002092 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2093 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002094 Array* array = obj->AsArray();
2095 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002099 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ScopedObjectAccess soa(env);
2101 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2102 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Ian Rogersbc939662013-08-15 10:26:54 -07002105 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2106 jobject java_value) {
2107 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 ScopedObjectAccess soa(env);
2109 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2110 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 array->Set(index, value);
2112 }
2113
2114 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002115 ScopedObjectAccess soa(env);
2116 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
2119 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002120 ScopedObjectAccess soa(env);
2121 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
2124 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002125 ScopedObjectAccess soa(env);
2126 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
2129 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002130 ScopedObjectAccess soa(env);
2131 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
2134 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002135 ScopedObjectAccess soa(env);
2136 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
2139 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002140 ScopedObjectAccess soa(env);
2141 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
2144 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002145 ScopedObjectAccess soa(env);
2146 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Ian Rogers1d99e452014-01-02 17:36:41 -08002149 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2150 jobject initial_element) {
2151 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002152 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002153 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002154 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002155
2156 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002157 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002158 Class* array_class;
2159 {
2160 Class* element_class = soa.Decode<Class*>(element_jclass);
2161 if (UNLIKELY(element_class->IsPrimitive())) {
2162 JniAbortF("NewObjectArray", "not an object type: %s",
2163 PrettyDescriptor(element_class).c_str());
2164 return nullptr;
2165 }
2166 std::string descriptor("[");
2167 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002168
Ian Rogers1d99e452014-01-02 17:36:41 -08002169 // Find the class.
2170 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2171 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), element_class->GetClassLoader());
2172 array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
2173 if (UNLIKELY(array_class == nullptr)) {
2174 return nullptr;
2175 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes75770752011-08-24 17:52:38 -07002178 // Allocate and initialize if necessary.
Ian Rogers50b35e22012-10-04 10:09:15 -07002179 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002180 if (result != nullptr && initial_element != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002181 Object* initial_object = soa.Decode<Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002182 if (initial_object != nullptr) {
2183 mirror::Class* element_class = result->GetClass()->GetComponentType();
2184 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2185 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2186 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2187 PrettyDescriptor(element_class).c_str());
2188
2189 } else {
2190 for (jsize i = 0; i < length; ++i) {
2191 result->SetWithoutChecks(i, initial_object);
2192 }
2193 }
Elliott Hughes75770752011-08-24 17:52:38 -07002194 }
2195 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
2199 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 ScopedObjectAccess soa(env);
2201 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Ian Rogersa15e67d2012-02-28 13:51:55 -08002204 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002205 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206 ScopedObjectAccess soa(env);
2207 Array* array = soa.Decode<Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002208 gc::Heap* heap = Runtime::Current()->GetHeap();
2209 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002210 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002211 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
2212 array = soa.Decode<Array*>(java_array);
2213 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002214 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002215 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002216 *is_copy = JNI_FALSE;
2217 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002218 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002219 }
2220
Mathieu Chartier590fee92013-09-13 13:46:47 -07002221 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002222 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002223 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002224 }
2225
Elliott Hughes75770752011-08-24 17:52:38 -07002226 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002227 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002228 ScopedObjectAccess soa(env);
2229 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Elliott Hughes75770752011-08-24 17:52:38 -07002232 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002233 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 ScopedObjectAccess soa(env);
2235 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Elliott Hughes75770752011-08-24 17:52:38 -07002238 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002239 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 ScopedObjectAccess soa(env);
2241 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughes75770752011-08-24 17:52:38 -07002244 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002245 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002246 ScopedObjectAccess soa(env);
2247 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Elliott Hughes75770752011-08-24 17:52:38 -07002250 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002251 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002252 ScopedObjectAccess soa(env);
2253 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Elliott Hughes75770752011-08-24 17:52:38 -07002256 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002257 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002258 ScopedObjectAccess soa(env);
2259 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
Elliott Hughes75770752011-08-24 17:52:38 -07002262 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002263 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002264 ScopedObjectAccess soa(env);
2265 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 }
2267
Elliott Hughes75770752011-08-24 17:52:38 -07002268 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002269 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002270 ScopedObjectAccess soa(env);
2271 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Mathieu Chartier590fee92013-09-13 13:46:47 -07002274 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2275 jint mode) {
2276 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Mathieu Chartier590fee92013-09-13 13:46:47 -07002279 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2280 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Mathieu Chartier590fee92013-09-13 13:46:47 -07002283 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2284 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Mathieu Chartier590fee92013-09-13 13:46:47 -07002287 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2288 jint mode) {
2289 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Mathieu Chartier590fee92013-09-13 13:46:47 -07002292 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2293 jint mode) {
2294 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Mathieu Chartier590fee92013-09-13 13:46:47 -07002297 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2298 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 }
2300
Mathieu Chartier590fee92013-09-13 13:46:47 -07002301 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2302 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 }
2304
Mathieu Chartier590fee92013-09-13 13:46:47 -07002305 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2306 jint mode) {
2307 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Ian Rogersbc939662013-08-15 10:26:54 -07002310 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2311 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002312 ScopedObjectAccess soa(env);
2313 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315
Ian Rogersbc939662013-08-15 10:26:54 -07002316 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2317 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002318 ScopedObjectAccess soa(env);
2319 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 }
2321
Ian Rogersbc939662013-08-15 10:26:54 -07002322 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2323 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 ScopedObjectAccess soa(env);
2325 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 }
2327
Ian Rogersbc939662013-08-15 10:26:54 -07002328 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2329 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ScopedObjectAccess soa(env);
2331 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002332 }
2333
Ian Rogersbc939662013-08-15 10:26:54 -07002334 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2335 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002336 ScopedObjectAccess soa(env);
2337 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 }
2339
Ian Rogersbc939662013-08-15 10:26:54 -07002340 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2341 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 ScopedObjectAccess soa(env);
2343 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 }
2345
Ian Rogersbc939662013-08-15 10:26:54 -07002346 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2347 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 ScopedObjectAccess soa(env);
2349 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2353 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 ScopedObjectAccess soa(env);
2355 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Ian Rogersbc939662013-08-15 10:26:54 -07002358 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2359 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 ScopedObjectAccess soa(env);
2361 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363
Ian Rogersbc939662013-08-15 10:26:54 -07002364 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2365 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002366 ScopedObjectAccess soa(env);
2367 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 }
2369
Ian Rogersbc939662013-08-15 10:26:54 -07002370 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2371 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 ScopedObjectAccess soa(env);
2373 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002374 }
2375
Ian Rogersbc939662013-08-15 10:26:54 -07002376 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2377 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 ScopedObjectAccess soa(env);
2379 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002380 }
2381
Ian Rogersbc939662013-08-15 10:26:54 -07002382 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2383 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 ScopedObjectAccess soa(env);
2385 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002386 }
2387
Ian Rogersbc939662013-08-15 10:26:54 -07002388 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2389 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002390 ScopedObjectAccess soa(env);
2391 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 }
2393
Ian Rogersbc939662013-08-15 10:26:54 -07002394 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2395 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002396 ScopedObjectAccess soa(env);
2397 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002398 }
2399
Ian Rogersbc939662013-08-15 10:26:54 -07002400 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2401 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002402 ScopedObjectAccess soa(env);
2403 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002404 }
2405
Ian Rogersbc939662013-08-15 10:26:54 -07002406 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2407 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002408 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2409 }
2410
Ian Rogersbc939662013-08-15 10:26:54 -07002411 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2412 jint method_count, bool return_errors) {
2413 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002414 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002415 return JNI_ERR; // Not reached.
2416 }
2417 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002418 ScopedObjectAccess soa(env);
2419 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002420 if (UNLIKELY(method_count == 0)) {
2421 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2422 << PrettyDescriptor(c);
2423 return JNI_OK;
2424 }
2425 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2426 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002427 const char* name = methods[i].name;
2428 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002429 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002430 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002431 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002432 ++sig;
2433 }
2434
Brian Carlstromea46f952013-07-30 01:26:50 -07002435 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002436 if (m == NULL) {
2437 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002438 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002439 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002440 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002441 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002442 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002443 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002444 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002445 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002446 << PrettyDescriptor(c) << "." << name << sig
2447 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002449 return JNI_ERR;
2450 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002451
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002452 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002453
Ian Rogers1eb512d2013-10-18 15:42:20 -07002454 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002455 }
2456 return JNI_OK;
2457 }
2458
Elliott Hughes5174fe62011-08-23 15:12:35 -07002459 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002460 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002461 ScopedObjectAccess soa(env);
2462 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002463
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002464 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002465
2466 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002467 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002468 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002470 }
2471 }
2472 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002473 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002474 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002475 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002476 }
2477 }
2478
2479 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002480 }
2481
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002482 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2483 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002484 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002485 ScopedObjectAccess soa(env);
2486 Object* o = soa.Decode<Object*>(java_object);
2487 o->MonitorEnter(soa.Self());
2488 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002489 return JNI_ERR;
2490 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002491 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002492 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002493 }
2494
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002495 static jint MonitorExit(JNIEnv* env, jobject java_object)
2496 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002497 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002498 ScopedObjectAccess soa(env);
2499 Object* o = soa.Decode<Object*>(java_object);
2500 o->MonitorExit(soa.Self());
2501 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002502 return JNI_ERR;
2503 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002504 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002505 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002506 }
2507
2508 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002509 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002510 Runtime* runtime = Runtime::Current();
2511 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002512 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002513 } else {
2514 *vm = NULL;
2515 }
2516 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2517 }
2518
Elliott Hughescdf53122011-08-19 15:46:09 -07002519 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002520 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002521 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002522 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002523 if (address == NULL && capacity != 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002524 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002525 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002526
Elliott Hughes96a98872012-12-19 14:21:15 -08002527 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002528 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2529 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002530 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002531 jint capacity_arg = static_cast<jint>(capacity);
2532
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002533 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2534 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002535 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002536 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002537 }
2538
Elliott Hughesb465ab02011-08-24 11:21:21 -07002539 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002540 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002541 }
2542
Elliott Hughesb465ab02011-08-24 11:21:21 -07002543 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002544 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002545 }
2546
Elliott Hughesb465ab02011-08-24 11:21:21 -07002547 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002548 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002549
2550 // Do we definitely know what kind of reference this is?
2551 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2552 IndirectRefKind kind = GetIndirectRefKind(ref);
2553 switch (kind) {
2554 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002555 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002556 return JNILocalRefType;
2557 }
2558 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002559 case kGlobal:
2560 return JNIGlobalRefType;
2561 case kWeakGlobal:
2562 return JNIWeakGlobalRefType;
2563 case kSirtOrInvalid:
2564 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002565 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002566 return JNILocalRefType;
2567 }
2568
Ian Rogersef28b142012-11-30 14:22:18 -08002569 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002570 return JNIInvalidRefType;
2571 }
2572
Elliott Hughesb465ab02011-08-24 11:21:21 -07002573 // If we're handing out direct pointers, check whether it's a direct pointer
2574 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002575 {
2576 ScopedObjectAccess soa(env);
2577 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2578 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2579 return JNILocalRefType;
2580 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002581 }
2582 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002583 return JNIInvalidRefType;
2584 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002585 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2586 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002587 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002588
2589 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002590 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2591 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002592 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002593 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002594 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2595 return JNI_ERR;
2596 }
2597 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002598 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002599 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2600 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002601 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002602 soa.Self()->ThrowOutOfMemoryError(caller);
2603 }
2604 return okay ? JNI_OK : JNI_ERR;
2605 }
2606
2607 template<typename JniT, typename ArtT>
2608 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002609 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002610 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002611 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002612 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002613 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002614 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002615 return soa.AddLocalReference<JniT>(result);
2616 }
2617
2618 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2619 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2620 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002621 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002622 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2623 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002624 // Only make a copy if necessary.
2625 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2626 if (is_copy != nullptr) {
2627 *is_copy = JNI_TRUE;
2628 }
2629 static const size_t component_size = array->GetClass()->GetComponentSize();
2630 size_t size = array->GetLength() * component_size;
2631 void* data = new uint64_t[RoundUp(size, 8) / 8];
2632 memcpy(data, array->GetData(), size);
2633 return reinterpret_cast<CArrayT>(data);
2634 } else {
2635 if (is_copy != nullptr) {
2636 *is_copy = JNI_FALSE;
2637 }
2638 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002639 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002640 }
2641
Mathieu Chartier590fee92013-09-13 13:46:47 -07002642 template <typename ArrayT, typename ElementT>
2643 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2644 ScopedObjectAccess soa(env);
2645 Array* array = soa.Decode<Array*>(java_array);
2646 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002647 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002648 gc::Heap* heap = Runtime::Current()->GetHeap();
2649 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2650 size_t bytes = array->GetLength() * component_size;
2651 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2652 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002653 // Don't need to copy if we had a direct pointer.
2654 if (mode != JNI_ABORT && is_copy) {
2655 memcpy(array_data, elements, bytes);
2656 }
2657 if (mode != JNI_COMMIT) {
2658 if (is_copy) {
2659 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002660 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002661 // Non copy to a movable object must means that we had disabled the moving GC.
2662 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002663 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002664 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002665 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002666 }
2667
2668 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2669 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2670 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002672 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002673 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2674 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2675 ThrowAIOOBE(soa, array, start, length, "src");
2676 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002677 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002678 JavaT* data = array->GetData();
2679 memcpy(buf, data + start, length * sizeof(JavaT));
2680 }
2681 }
2682
2683 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2684 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2685 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002686 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002687 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002688 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2689 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2690 ThrowAIOOBE(soa, array, start, length, "dst");
2691 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002692 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002693 JavaT* data = array->GetData();
2694 memcpy(data + start, buf, length * sizeof(JavaT));
2695 }
2696 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002697};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002698
Elliott Hughes88c5c352012-03-15 18:49:48 -07002699const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002700 NULL, // reserved0.
2701 NULL, // reserved1.
2702 NULL, // reserved2.
2703 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 JNI::GetVersion,
2705 JNI::DefineClass,
2706 JNI::FindClass,
2707 JNI::FromReflectedMethod,
2708 JNI::FromReflectedField,
2709 JNI::ToReflectedMethod,
2710 JNI::GetSuperclass,
2711 JNI::IsAssignableFrom,
2712 JNI::ToReflectedField,
2713 JNI::Throw,
2714 JNI::ThrowNew,
2715 JNI::ExceptionOccurred,
2716 JNI::ExceptionDescribe,
2717 JNI::ExceptionClear,
2718 JNI::FatalError,
2719 JNI::PushLocalFrame,
2720 JNI::PopLocalFrame,
2721 JNI::NewGlobalRef,
2722 JNI::DeleteGlobalRef,
2723 JNI::DeleteLocalRef,
2724 JNI::IsSameObject,
2725 JNI::NewLocalRef,
2726 JNI::EnsureLocalCapacity,
2727 JNI::AllocObject,
2728 JNI::NewObject,
2729 JNI::NewObjectV,
2730 JNI::NewObjectA,
2731 JNI::GetObjectClass,
2732 JNI::IsInstanceOf,
2733 JNI::GetMethodID,
2734 JNI::CallObjectMethod,
2735 JNI::CallObjectMethodV,
2736 JNI::CallObjectMethodA,
2737 JNI::CallBooleanMethod,
2738 JNI::CallBooleanMethodV,
2739 JNI::CallBooleanMethodA,
2740 JNI::CallByteMethod,
2741 JNI::CallByteMethodV,
2742 JNI::CallByteMethodA,
2743 JNI::CallCharMethod,
2744 JNI::CallCharMethodV,
2745 JNI::CallCharMethodA,
2746 JNI::CallShortMethod,
2747 JNI::CallShortMethodV,
2748 JNI::CallShortMethodA,
2749 JNI::CallIntMethod,
2750 JNI::CallIntMethodV,
2751 JNI::CallIntMethodA,
2752 JNI::CallLongMethod,
2753 JNI::CallLongMethodV,
2754 JNI::CallLongMethodA,
2755 JNI::CallFloatMethod,
2756 JNI::CallFloatMethodV,
2757 JNI::CallFloatMethodA,
2758 JNI::CallDoubleMethod,
2759 JNI::CallDoubleMethodV,
2760 JNI::CallDoubleMethodA,
2761 JNI::CallVoidMethod,
2762 JNI::CallVoidMethodV,
2763 JNI::CallVoidMethodA,
2764 JNI::CallNonvirtualObjectMethod,
2765 JNI::CallNonvirtualObjectMethodV,
2766 JNI::CallNonvirtualObjectMethodA,
2767 JNI::CallNonvirtualBooleanMethod,
2768 JNI::CallNonvirtualBooleanMethodV,
2769 JNI::CallNonvirtualBooleanMethodA,
2770 JNI::CallNonvirtualByteMethod,
2771 JNI::CallNonvirtualByteMethodV,
2772 JNI::CallNonvirtualByteMethodA,
2773 JNI::CallNonvirtualCharMethod,
2774 JNI::CallNonvirtualCharMethodV,
2775 JNI::CallNonvirtualCharMethodA,
2776 JNI::CallNonvirtualShortMethod,
2777 JNI::CallNonvirtualShortMethodV,
2778 JNI::CallNonvirtualShortMethodA,
2779 JNI::CallNonvirtualIntMethod,
2780 JNI::CallNonvirtualIntMethodV,
2781 JNI::CallNonvirtualIntMethodA,
2782 JNI::CallNonvirtualLongMethod,
2783 JNI::CallNonvirtualLongMethodV,
2784 JNI::CallNonvirtualLongMethodA,
2785 JNI::CallNonvirtualFloatMethod,
2786 JNI::CallNonvirtualFloatMethodV,
2787 JNI::CallNonvirtualFloatMethodA,
2788 JNI::CallNonvirtualDoubleMethod,
2789 JNI::CallNonvirtualDoubleMethodV,
2790 JNI::CallNonvirtualDoubleMethodA,
2791 JNI::CallNonvirtualVoidMethod,
2792 JNI::CallNonvirtualVoidMethodV,
2793 JNI::CallNonvirtualVoidMethodA,
2794 JNI::GetFieldID,
2795 JNI::GetObjectField,
2796 JNI::GetBooleanField,
2797 JNI::GetByteField,
2798 JNI::GetCharField,
2799 JNI::GetShortField,
2800 JNI::GetIntField,
2801 JNI::GetLongField,
2802 JNI::GetFloatField,
2803 JNI::GetDoubleField,
2804 JNI::SetObjectField,
2805 JNI::SetBooleanField,
2806 JNI::SetByteField,
2807 JNI::SetCharField,
2808 JNI::SetShortField,
2809 JNI::SetIntField,
2810 JNI::SetLongField,
2811 JNI::SetFloatField,
2812 JNI::SetDoubleField,
2813 JNI::GetStaticMethodID,
2814 JNI::CallStaticObjectMethod,
2815 JNI::CallStaticObjectMethodV,
2816 JNI::CallStaticObjectMethodA,
2817 JNI::CallStaticBooleanMethod,
2818 JNI::CallStaticBooleanMethodV,
2819 JNI::CallStaticBooleanMethodA,
2820 JNI::CallStaticByteMethod,
2821 JNI::CallStaticByteMethodV,
2822 JNI::CallStaticByteMethodA,
2823 JNI::CallStaticCharMethod,
2824 JNI::CallStaticCharMethodV,
2825 JNI::CallStaticCharMethodA,
2826 JNI::CallStaticShortMethod,
2827 JNI::CallStaticShortMethodV,
2828 JNI::CallStaticShortMethodA,
2829 JNI::CallStaticIntMethod,
2830 JNI::CallStaticIntMethodV,
2831 JNI::CallStaticIntMethodA,
2832 JNI::CallStaticLongMethod,
2833 JNI::CallStaticLongMethodV,
2834 JNI::CallStaticLongMethodA,
2835 JNI::CallStaticFloatMethod,
2836 JNI::CallStaticFloatMethodV,
2837 JNI::CallStaticFloatMethodA,
2838 JNI::CallStaticDoubleMethod,
2839 JNI::CallStaticDoubleMethodV,
2840 JNI::CallStaticDoubleMethodA,
2841 JNI::CallStaticVoidMethod,
2842 JNI::CallStaticVoidMethodV,
2843 JNI::CallStaticVoidMethodA,
2844 JNI::GetStaticFieldID,
2845 JNI::GetStaticObjectField,
2846 JNI::GetStaticBooleanField,
2847 JNI::GetStaticByteField,
2848 JNI::GetStaticCharField,
2849 JNI::GetStaticShortField,
2850 JNI::GetStaticIntField,
2851 JNI::GetStaticLongField,
2852 JNI::GetStaticFloatField,
2853 JNI::GetStaticDoubleField,
2854 JNI::SetStaticObjectField,
2855 JNI::SetStaticBooleanField,
2856 JNI::SetStaticByteField,
2857 JNI::SetStaticCharField,
2858 JNI::SetStaticShortField,
2859 JNI::SetStaticIntField,
2860 JNI::SetStaticLongField,
2861 JNI::SetStaticFloatField,
2862 JNI::SetStaticDoubleField,
2863 JNI::NewString,
2864 JNI::GetStringLength,
2865 JNI::GetStringChars,
2866 JNI::ReleaseStringChars,
2867 JNI::NewStringUTF,
2868 JNI::GetStringUTFLength,
2869 JNI::GetStringUTFChars,
2870 JNI::ReleaseStringUTFChars,
2871 JNI::GetArrayLength,
2872 JNI::NewObjectArray,
2873 JNI::GetObjectArrayElement,
2874 JNI::SetObjectArrayElement,
2875 JNI::NewBooleanArray,
2876 JNI::NewByteArray,
2877 JNI::NewCharArray,
2878 JNI::NewShortArray,
2879 JNI::NewIntArray,
2880 JNI::NewLongArray,
2881 JNI::NewFloatArray,
2882 JNI::NewDoubleArray,
2883 JNI::GetBooleanArrayElements,
2884 JNI::GetByteArrayElements,
2885 JNI::GetCharArrayElements,
2886 JNI::GetShortArrayElements,
2887 JNI::GetIntArrayElements,
2888 JNI::GetLongArrayElements,
2889 JNI::GetFloatArrayElements,
2890 JNI::GetDoubleArrayElements,
2891 JNI::ReleaseBooleanArrayElements,
2892 JNI::ReleaseByteArrayElements,
2893 JNI::ReleaseCharArrayElements,
2894 JNI::ReleaseShortArrayElements,
2895 JNI::ReleaseIntArrayElements,
2896 JNI::ReleaseLongArrayElements,
2897 JNI::ReleaseFloatArrayElements,
2898 JNI::ReleaseDoubleArrayElements,
2899 JNI::GetBooleanArrayRegion,
2900 JNI::GetByteArrayRegion,
2901 JNI::GetCharArrayRegion,
2902 JNI::GetShortArrayRegion,
2903 JNI::GetIntArrayRegion,
2904 JNI::GetLongArrayRegion,
2905 JNI::GetFloatArrayRegion,
2906 JNI::GetDoubleArrayRegion,
2907 JNI::SetBooleanArrayRegion,
2908 JNI::SetByteArrayRegion,
2909 JNI::SetCharArrayRegion,
2910 JNI::SetShortArrayRegion,
2911 JNI::SetIntArrayRegion,
2912 JNI::SetLongArrayRegion,
2913 JNI::SetFloatArrayRegion,
2914 JNI::SetDoubleArrayRegion,
2915 JNI::RegisterNatives,
2916 JNI::UnregisterNatives,
2917 JNI::MonitorEnter,
2918 JNI::MonitorExit,
2919 JNI::GetJavaVM,
2920 JNI::GetStringRegion,
2921 JNI::GetStringUTFRegion,
2922 JNI::GetPrimitiveArrayCritical,
2923 JNI::ReleasePrimitiveArrayCritical,
2924 JNI::GetStringCritical,
2925 JNI::ReleaseStringCritical,
2926 JNI::NewWeakGlobalRef,
2927 JNI::DeleteWeakGlobalRef,
2928 JNI::ExceptionCheck,
2929 JNI::NewDirectByteBuffer,
2930 JNI::GetDirectBufferAddress,
2931 JNI::GetDirectBufferCapacity,
2932 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002933};
2934
Elliott Hughes75770752011-08-24 17:52:38 -07002935JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002936 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002937 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002938 local_ref_cookie(IRT_FIRST_SEGMENT),
2939 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002940 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002941 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002942 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002943 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002944 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002945 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002946 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002947}
2948
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002949JNIEnvExt::~JNIEnvExt() {
2950}
2951
Mathieu Chartier590fee92013-09-13 13:46:47 -07002952jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2953 if (obj == nullptr) {
2954 return nullptr;
2955 }
2956 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2957}
2958
2959void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2960 if (obj != nullptr) {
2961 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2962 }
2963}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002964void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2965 check_jni = enabled;
2966 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002967}
2968
Elliott Hughes73e66f72012-05-09 09:34:45 -07002969void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2970 locals.Dump(os);
2971 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002972}
2973
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002974void JNIEnvExt::PushFrame(int /*capacity*/) {
2975 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002976 stacked_local_ref_cookies.push_back(local_ref_cookie);
2977 local_ref_cookie = locals.GetSegmentState();
2978}
2979
2980void JNIEnvExt::PopFrame() {
2981 locals.SetSegmentState(local_ref_cookie);
2982 local_ref_cookie = stacked_local_ref_cookies.back();
2983 stacked_local_ref_cookies.pop_back();
2984}
2985
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002986Offset JNIEnvExt::SegmentStateOffset() {
2987 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2988 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2989}
2990
Carl Shapiroea4dca82011-08-01 13:45:38 -07002991// JNI Invocation interface.
2992
Brian Carlstrombddf9762013-05-14 11:35:37 -07002993extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002994 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002995 if (IsBadJniVersion(args->version)) {
2996 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002997 return JNI_EVERSION;
2998 }
2999 Runtime::Options options;
3000 for (int i = 0; i < args->nOptions; ++i) {
3001 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003002 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003003 }
3004 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003005 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003006 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003007 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003008 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003009 bool started = runtime->Start();
3010 if (!started) {
3011 delete Thread::Current()->GetJniEnv();
3012 delete runtime->GetJavaVM();
3013 LOG(WARNING) << "CreateJavaVM failed";
3014 return JNI_ERR;
3015 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003016 *p_env = Thread::Current()->GetJniEnv();
3017 *p_vm = runtime->GetJavaVM();
3018 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003019}
3020
Elliott Hughesf2682d52011-08-15 16:37:04 -07003021extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003022 Runtime* runtime = Runtime::Current();
3023 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003024 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003025 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003026 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003027 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003028 }
3029 return JNI_OK;
3030}
3031
3032// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003033extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003034 return JNI_ERR;
3035}
3036
Elliott Hughescdf53122011-08-19 15:46:09 -07003037class JII {
3038 public:
3039 static jint DestroyJavaVM(JavaVM* vm) {
3040 if (vm == NULL) {
3041 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003042 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003043 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3044 delete raw_vm->runtime;
3045 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003046 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003047
Elliott Hughescdf53122011-08-19 15:46:09 -07003048 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003049 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003050 }
3051
3052 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003053 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003054 }
3055
3056 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07003057 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003058 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003059 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003060 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3061 Runtime* runtime = raw_vm->runtime;
3062 runtime->DetachCurrentThread();
3063 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003064 }
3065
3066 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003067 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3068 // and unlike other calls that take a JNI version doesn't care if you supply
3069 // JNI_VERSION_1_1, which we don't otherwise support.
3070 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003071 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003072 return JNI_EVERSION;
3073 }
3074 if (vm == NULL || env == NULL) {
3075 return JNI_ERR;
3076 }
3077 Thread* thread = Thread::Current();
3078 if (thread == NULL) {
3079 *env = NULL;
3080 return JNI_EDETACHED;
3081 }
3082 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003083 return JNI_OK;
3084 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003085};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003086
Elliott Hughes88c5c352012-03-15 18:49:48 -07003087const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07003088 NULL, // reserved0
3089 NULL, // reserved1
3090 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003091 JII::DestroyJavaVM,
3092 JII::AttachCurrentThread,
3093 JII::DetachCurrentThread,
3094 JII::GetEnv,
3095 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003096};
3097
Elliott Hughesa0957642011-09-02 14:27:33 -07003098JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003099 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07003100 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07003101 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003102 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003103 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003104 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003105 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003106 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003107 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003108 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003109 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003110 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003111 libraries(new Libraries),
3112 weak_globals_lock_("JNI weak global reference table lock"),
3113 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3114 allow_new_weak_globals_(true),
3115 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003116 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003117 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003118 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003119 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003120}
3121
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003122JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003123 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003124}
3125
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003126jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3127 if (obj == nullptr) {
3128 return nullptr;
3129 }
3130 MutexLock mu(self, weak_globals_lock_);
3131 while (UNLIKELY(!allow_new_weak_globals_)) {
3132 weak_globals_add_condition_.WaitHoldingLocks(self);
3133 }
3134 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3135 return reinterpret_cast<jweak>(ref);
3136}
3137
3138void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3139 MutexLock mu(self, weak_globals_lock_);
3140 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3141 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3142 << "failed to find entry";
3143 }
3144}
3145
Elliott Hughes88c5c352012-03-15 18:49:48 -07003146void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3147 check_jni = enabled;
3148 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003149}
3150
Elliott Hughesae80b492012-04-24 10:43:17 -07003151void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3152 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3153 if (force_copy) {
3154 os << " (with forcecopy)";
3155 }
3156 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003157 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003158 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003159 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003160 os << "; pins=" << pin_table.Size();
3161 }
3162 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003163 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003164 os << "; globals=" << globals.Capacity();
3165 }
3166 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003167 MutexLock mu(self, weak_globals_lock_);
3168 if (weak_globals_.Capacity() > 0) {
3169 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003170 }
3171 }
3172 os << '\n';
3173
3174 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003175 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003176 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3177 }
3178}
3179
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003180void JavaVMExt::DisallowNewWeakGlobals() {
3181 MutexLock mu(Thread::Current(), weak_globals_lock_);
3182 allow_new_weak_globals_ = false;
3183}
3184
3185void JavaVMExt::AllowNewWeakGlobals() {
3186 Thread* self = Thread::Current();
3187 MutexLock mu(self, weak_globals_lock_);
3188 allow_new_weak_globals_ = true;
3189 weak_globals_add_condition_.Broadcast(self);
3190}
3191
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003192mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3193 MutexLock mu(self, weak_globals_lock_);
3194 while (UNLIKELY(!allow_new_weak_globals_)) {
3195 weak_globals_add_condition_.WaitHoldingLocks(self);
3196 }
3197 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3198}
3199
Elliott Hughes73e66f72012-05-09 09:34:45 -07003200void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003201 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003202 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003203 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003204 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003205 }
3206 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003207 MutexLock mu(self, weak_globals_lock_);
3208 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003209 }
3210 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003211 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003212 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003213 }
3214}
3215
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003216bool JavaVMExt::LoadNativeLibrary(const std::string& path,
3217 const SirtRef<ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003218 std::string* detail) {
3219 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003220
3221 // See if we've already loaded this library. If we have, and the class loader
3222 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003223 // TODO: for better results we should canonicalize the pathname (or even compare
3224 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003225 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003226 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003227 {
3228 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003229 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003230 library = libraries->Get(path);
3231 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003232 if (library != NULL) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003233 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003234 // The library will be associated with class_loader. The JNI
3235 // spec says we can't load the same library into more than one
3236 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003237 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003238 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003239 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003240 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003241 return false;
3242 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003243 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003244 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003245 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003246 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003247 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003248 return false;
3249 }
3250 return true;
3251 }
3252
3253 // Open the shared library. Because we're using a full path, the system
3254 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3255 // resolve this library's dependencies though.)
3256
3257 // Failures here are expected when java.library.path has several entries
3258 // and we have to hunt for the lib.
3259
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003260 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3261 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3262 // dlopen) becomes zero from dlclose.
3263
Elliott Hughescdf53122011-08-19 15:46:09 -07003264 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003265 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003266 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3267 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3268 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003269
Elliott Hughes84b2f142012-09-27 09:16:28 -07003270 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003271
3272 if (handle == NULL) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003273 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003274 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003275 return false;
3276 }
3277
3278 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003279 // TODO: move the locking (and more of this logic) into Libraries.
3280 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003281 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003282 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003283 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003284 if (library == NULL) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003285 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003286 libraries->Put(path, library);
3287 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003288 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003289 }
3290 if (!created_library) {
3291 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003292 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003293 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003294 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003295
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003296 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3297 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003298
Elliott Hughes79353722013-08-02 16:52:18 -07003299 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003300 void* sym = dlsym(handle, "JNI_OnLoad");
3301 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003302 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003303 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003304 } else {
3305 // Call JNI_OnLoad. We have to override the current class
3306 // loader, which will always be "null" since the stuff at the
3307 // top of the stack is around Runtime.loadLibrary(). (See
3308 // the comments in the JNI FindClass function.)
3309 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3310 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003311 SirtRef<ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003312 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003313
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003314 int version = 0;
3315 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003316 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003317 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003318 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003319 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003320
Mathieu Chartier590fee92013-09-13 13:46:47 -07003321 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003322
Elliott Hughes79353722013-08-02 16:52:18 -07003323 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003324 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003325 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003326 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003327 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003328 // It's unwise to call dlclose() here, but we can mark it
3329 // as bad and ensure that future load attempts will fail.
3330 // We don't know how far JNI_OnLoad got, so there could
3331 // be some partially-initialized stuff accessible through
3332 // newly-registered native method calls. We could try to
3333 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003334 } else {
3335 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003336 }
Elliott Hughes79353722013-08-02 16:52:18 -07003337 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003338 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003339 }
3340
Elliott Hughes79353722013-08-02 16:52:18 -07003341 library->SetResult(was_successful);
3342 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003343}
3344
Brian Carlstromea46f952013-07-30 01:26:50 -07003345void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003346 CHECK(m->IsNative());
3347
3348 Class* c = m->GetDeclaringClass();
3349
3350 // If this is a static method, it could be called before the class
3351 // has been initialized.
3352 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003353 c = EnsureInitialized(Thread::Current(), c);
3354 if (c == nullptr) {
3355 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003356 }
3357 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003358 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003359 }
3360
Brian Carlstrom16192862011-09-12 17:50:06 -07003361 std::string detail;
3362 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003363 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003364 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003365 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003366 native_method = libraries->FindNativeMethod(m, detail);
3367 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003368 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003369 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003370 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3371 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003372 }
3373 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003374}
3375
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003376void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003377 MutexLock mu(Thread::Current(), weak_globals_lock_);
3378 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003379 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003380 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003381 if (new_obj == nullptr) {
3382 new_obj = kClearedJniWeakGlobal;
3383 }
3384 *entry = new_obj;
3385 }
3386}
3387
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003388void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003389 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003390 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003391 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003392 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003393 }
3394 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003395 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003396 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003397 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003398 {
3399 MutexLock mu(self, libraries_lock);
3400 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003401 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003402 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003403 // The weak_globals table is visited by the GC itself (because it mutates the table).
3404}
3405
Elliott Hughesc8fece32013-01-02 11:27:23 -08003406void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003407 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003408 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3409 if (c.get() == NULL) {
3410 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3411 }
3412 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3413}
3414
Ian Rogersdf20fe02011-07-20 20:34:16 -07003415} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003416
3417std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3418 switch (rhs) {
3419 case JNIInvalidRefType:
3420 os << "JNIInvalidRefType";
3421 return os;
3422 case JNILocalRefType:
3423 os << "JNILocalRefType";
3424 return os;
3425 case JNIGlobalRefType:
3426 os << "JNIGlobalRefType";
3427 return os;
3428 case JNIWeakGlobalRefType:
3429 os << "JNIWeakGlobalRefType";
3430 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003431 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003432 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003433 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003434 }
3435}