blob: fd9c40be13e94489c6934cf921e10f82a7c5d0bb [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Ian Rogersef7d42f2014-01-06 12:55:46 -080025#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080029#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070033#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class-inl.h"
37#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
40#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080041#include "object_utils.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080042#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070043#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070044#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070045#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070051#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070052
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053namespace art {
54
Brian Carlstrom7934ac22013-07-26 10:54:15 -070055static const size_t kMonitorsInitial = 32; // Arbitrary.
56static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070057
Brian Carlstrom7934ac22013-07-26 10:54:15 -070058static const size_t kLocalsInitial = 64; // Arbitrary.
59static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070060
Brian Carlstrom7934ac22013-07-26 10:54:15 -070061static const size_t kPinTableInitial = 16; // Arbitrary.
62static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070063
Brian Carlstrom7934ac22013-07-26 10:54:15 -070064static size_t gGlobalsInitial = 512; // Arbitrary.
65static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070066
Brian Carlstrom7934ac22013-07-26 10:54:15 -070067static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
68static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070069
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080070static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070072 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070073}
74
Jeff Hao19c5d372013-03-15 14:33:43 -070075static bool IsBadJniVersion(int version) {
76 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
77 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
78}
79
Elliott Hughes6b436852011-08-12 10:16:44 -070080// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
81// separated with slashes but aren't wrapped with "L;" like regular descriptors
82// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
83// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
84// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070085static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070086 std::string result;
87 // Add the missing "L;" if necessary.
88 if (name[0] == '[') {
89 result = name;
90 } else {
91 result += 'L';
92 result += name;
93 result += ';';
94 }
95 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070096 if (result.find('.') != std::string::npos) {
97 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
98 << "\"" << name << "\"";
99 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700100 }
101 return result;
102}
103
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800104static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800107 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
108 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
109 "no %s method \"%s.%s%s\"",
110 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700111}
112
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800113static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
115 if (LIKELY(klass->IsInitialized())) {
116 return klass;
117 }
118 SirtRef<mirror::Class> sirt_klass(self, klass);
119 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
120 return nullptr;
121 }
122 return sirt_klass.get();
123}
124
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
126 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800128 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800129 if (c == nullptr) {
130 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700131 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800132 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700133 if (is_static) {
134 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700135 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700136 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800137 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700138 // No virtual method matching the signature. Search declared
139 // private methods and constructors.
140 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700141 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700142 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800143 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700146 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700148}
149
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800150static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800152 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700153 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
154 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800155 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700156 }
Brian Carlstromce888532013-10-10 00:32:58 -0700157 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800158 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700159 return method->GetDeclaringClass()->GetClassLoader();
160 }
161 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800162 mirror::ClassLoader* class_loader =
163 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
164 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700165 return class_loader;
166 }
167 // See if the override ClassLoader is set for gtests.
168 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800169 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800170 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700171 CHECK(Runtime::Current()->UseCompileTimeClassPath());
172 return class_loader;
173 }
174 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800175 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700176}
177
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
179 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800181 SirtRef<mirror::Class> c(soa.Self(), EnsureInitialized(soa.Self(),
182 soa.Decode<mirror::Class*>(jni_class)));
183 if (c.get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800184 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700185 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800186 mirror::ArtField* field = nullptr;
187 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
189 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800190 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Ian Rogers98379392014-02-24 16:53:16 -0800191 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700192 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700194 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800195 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 ThrowLocation throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800199 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800201 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800202 "no type \"%s\" found and so no field \"%s\" "
203 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800204 ClassHelper(c.get()).GetDescriptor());
205 soa.Self()->GetException(nullptr)->SetCause(cause.get());
206 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 }
208 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800209 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800213 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
215 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
216 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800217 sig, name, ClassHelper(c.get()).GetDescriptor());
218 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700219 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700221}
222
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800223static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700226 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700227 vm->pin_table.Add(array);
228}
229
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800230static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700233 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700234 vm->pin_table.Remove(array);
235}
236
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800237static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700240 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
242 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
243 "%s offset=%d length=%d %s.length=%d",
244 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700245}
Ian Rogers0571d352011-11-03 19:51:38 -0700246
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
248 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800250 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
251 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
252 "offset=%d length=%d string.length()=%d", start, length,
253 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700254}
Elliott Hughes814e4032011-08-23 12:07:56 -0700255
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 // Turn the const char* into a java.lang.String.
259 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800260 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700262 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700263
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 // Choose an appropriate constructor and set up the arguments.
265 jvalue args[2];
266 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800267 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800269 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 signature = "(Ljava/lang/String;)V";
271 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800272 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 signature = "(Ljava/lang/Throwable;)V";
274 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700275 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
277 args[0].l = s.get();
278 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700279 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800281 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800282 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800284 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 return JNI_ERR;
286 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700287
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800288 ScopedLocalRef<jthrowable> exception(
289 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
290 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 return JNI_ERR;
292 }
Ian Rogersef28b142012-11-30 14:22:18 -0800293 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800294 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800295 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700297}
298
Elliott Hughes462c9442012-03-23 18:47:50 -0700299static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800300 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700301 return JNI_ERR;
302 }
303
Elliott Hughes462c9442012-03-23 18:47:50 -0700304 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700305 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800306 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700307 *p_env = self->GetJniEnv();
308 return JNI_OK;
309 }
310
311 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
312
313 // No threads allowed in zygote mode.
314 if (runtime->IsZygote()) {
315 LOG(ERROR) << "Attempt to attach a thread in the zygote";
316 return JNI_ERR;
317 }
318
Elliott Hughes462c9442012-03-23 18:47:50 -0700319 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800320 const char* thread_name = nullptr;
321 jobject thread_group = nullptr;
322 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700323 if (IsBadJniVersion(args->version)) {
324 LOG(ERROR) << "Bad JNI version passed to "
325 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
326 << args->version;
327 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700328 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700329 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700330 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700331 }
Elliott Hughes75770752011-08-24 17:52:38 -0700332
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800333 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800334 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700335 return JNI_ERR;
336 } else {
337 *p_env = Thread::Current()->GetJniEnv();
338 return JNI_OK;
339 }
Elliott Hughes75770752011-08-24 17:52:38 -0700340}
341
Elliott Hughes79082e32011-08-25 12:07:32 -0700342class SharedLibrary {
343 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800344 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700345 : path_(path),
346 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700347 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700348 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700349 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700350 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700351 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700352 }
353
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800354 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700355 return class_loader_;
356 }
357
358 std::string GetPath() {
359 return path_;
360 }
361
362 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700363 * Check the result of an earlier call to JNI_OnLoad on this library.
364 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700365 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 bool CheckOnLoadResult()
367 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700368 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700369 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
371 bool okay;
372 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700373 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700374
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700375 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
377 // caller can continue.
378 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
379 okay = true;
380 } else {
381 while (jni_on_load_result_ == kPending) {
382 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700383 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700385
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 okay = (jni_on_load_result_ == kOkay);
387 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
388 << (okay ? "succeeded" : "failed") << "]";
389 }
390 }
391 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700392 return okay;
393 }
394
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700396 Thread* self = Thread::Current();
397 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700398
Elliott Hughes79082e32011-08-25 12:07:32 -0700399 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700400 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700401
402 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700403 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700404 }
405
406 void* FindSymbol(const std::string& symbol_name) {
407 return dlsym(handle_, symbol_name.c_str());
408 }
409
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800410 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800411 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800412 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800413 }
414 }
415
Elliott Hughes79082e32011-08-25 12:07:32 -0700416 private:
417 enum JNI_OnLoadState {
418 kPending,
419 kFailed,
420 kOkay,
421 };
422
423 // Path to library "/system/lib/libjni.so".
424 std::string path_;
425
426 // The void* returned by dlopen(3).
427 void* handle_;
428
429 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800430 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700431
432 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700433 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700434 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700435 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700436 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700437 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700438 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700439 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700440};
441
Elliott Hughes79082e32011-08-25 12:07:32 -0700442// This exists mainly to keep implementation details out of the header file.
443class Libraries {
444 public:
445 Libraries() {
446 }
447
448 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700449 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700450 }
451
Elliott Hughesae80b492012-04-24 10:43:17 -0700452 void Dump(std::ostream& os) const {
453 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700454 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700455 if (!first) {
456 os << ' ';
457 }
458 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700459 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700460 }
461 }
462
463 size_t size() const {
464 return libraries_.size();
465 }
466
Elliott Hughes79082e32011-08-25 12:07:32 -0700467 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700468 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800469 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 }
471
472 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700473 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700474 }
475
476 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800477 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700478 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700479 std::string jni_short_name(JniShortName(m));
480 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800481 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700482 for (const auto& lib : libraries_) {
483 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 if (library->GetClassLoader() != declaring_class_loader) {
485 // We only search libraries loaded by the appropriate ClassLoader.
486 continue;
487 }
488 // Try the short name then the long name...
489 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800490 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700491 fn = library->FindSymbol(jni_long_name);
492 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800493 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800494 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
495 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700496 return fn;
497 }
498 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700500 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700501 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800503 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 }
505
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800506 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800507 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800508 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800509 }
510 }
511
Elliott Hughes79082e32011-08-25 12:07:32 -0700512 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700513 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514};
515
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700516#define CHECK_NON_NULL_ARGUMENT(value) CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value)
517
518#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800519 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700520 JniAbortF(name, #value " == null"); \
Ian Rogersbc939662013-08-15 10:26:54 -0700521 }
522
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700523#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800524 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700525 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700526 }
527
Elliott Hughescdf53122011-08-19 15:46:09 -0700528class JNI {
529 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700530 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700531 return JNI_VERSION_1_6;
532 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700533
Ian Rogers25e8b912012-09-07 11:31:36 -0700534 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700535 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800536 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700537 }
538
Elliott Hughescdf53122011-08-19 15:46:09 -0700539 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700540 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700541 Runtime* runtime = Runtime::Current();
542 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700543 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700544 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800545 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700546 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700547 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800548 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700549 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800550 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700551 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700552 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700553 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700554
Ian Rogers62f05122014-03-21 11:21:29 -0700555 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700556 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700557 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700558 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700559 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700560
Ian Rogers62f05122014-03-21 11:21:29 -0700561 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700562 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700563 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700564 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700565 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700566
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700568 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700569 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800570 mirror::ArtMethod* m = soa.DecodeMethod(mid);
571 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700572 jobject art_method = soa.AddLocalReference<jobject>(m);
573 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
574 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800575 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800577 SetObjectField(env, reflect_method,
578 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700579 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700580 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700581
Elliott Hughescdf53122011-08-19 15:46:09 -0700582 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700583 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700584 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800585 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 jobject art_field = soa.AddLocalReference<jobject>(f);
587 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
588 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700590 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 SetObjectField(env, reflect_field,
592 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700594 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700595
Elliott Hughes37f7a402011-08-22 18:56:01 -0700596 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700597 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800599 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700601 }
602
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700603 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700604 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800606 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700609
Elliott Hughes37f7a402011-08-22 18:56:01 -0700610 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700611 CHECK_NON_NULL_ARGUMENT(java_class1);
612 CHECK_NON_NULL_ARGUMENT(java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800614 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
615 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700616 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Elliott Hughese84278b2012-03-22 10:06:53 -0700619 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700620 CHECK_NON_NULL_ARGUMENT(java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800621 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700622 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700623 return JNI_TRUE;
624 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700625 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800626 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
627 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700628 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700629 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughes37f7a402011-08-22 18:56:01 -0700632 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700633 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800634 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
635 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700636 return JNI_ERR;
637 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800638 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
639 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700640 return JNI_OK;
641 }
642
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700643 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700644 CHECK_NON_NULL_ARGUMENT(c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800645 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700646 }
647
648 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700649 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 }
651
652 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700653 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 }
655
656 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700658
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800659 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
660 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
661 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 uint32_t old_throw_dex_pc;
663 {
664 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800665 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800666 old_throw_this_object.reset(old_throw_location.GetThis());
667 old_throw_method.reset(old_throw_location.GetMethod());
668 old_exception.reset(old_exception_obj);
669 old_throw_dex_pc = old_throw_location.GetDexPc();
670 soa.Self()->ClearException();
671 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800672 ScopedLocalRef<jthrowable> exception(env,
673 soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700674 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
675 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800676 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700677 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800678 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700679 } else {
680 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800681 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800682 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700683 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800684 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700685 }
686 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
688 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700689
Ian Rogers62d6c772013-02-27 08:32:07 -0800690 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800695 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 }
698
Ian Rogers25e8b912012-09-07 11:31:36 -0700699 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 LOG(FATAL) << "JNI FatalError called: " << msg;
701 }
702
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700703 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800704 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700705 return JNI_ERR;
706 }
Ian Rogersef28b142012-11-30 14:22:18 -0800707 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 return JNI_OK;
709 }
710
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700711 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800713 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 soa.Env()->PopFrame();
715 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 }
717
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700718 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800719 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 }
721
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700723 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800724 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800725 // Check for null after decoding the object to handle cleared weak globals.
726 if (decoded_obj == nullptr) {
727 return nullptr;
728 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700729 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700731 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 return reinterpret_cast<jobject>(ref);
734 }
735
736 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800737 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 return;
739 }
Ian Rogersef28b142012-11-30 14:22:18 -0800740 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800742 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700743 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700744
745 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
746 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
747 << "failed to find entry";
748 }
749 }
750
751 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800753 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 }
755
756 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700757 if (obj != nullptr) {
758 ScopedObjectAccess soa(env);
759 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 }
761 }
762
763 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700764 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800765 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800766 // Check for null after decoding the object to handle cleared weak globals.
767 if (decoded_obj == nullptr) {
768 return nullptr;
769 }
770 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 }
772
773 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800774 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 return;
776 }
Ian Rogersef28b142012-11-30 14:22:18 -0800777 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700778
Ian Rogersef28b142012-11-30 14:22:18 -0800779 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 if (!locals.Remove(cookie, obj)) {
781 // Attempting to delete a local reference that is not in the
782 // topmost local reference frame is a no-op. DeleteLocalRef returns
783 // void and doesn't throw any exceptions, but we should probably
784 // complain about it so the user will notice that things aren't
785 // going quite the way they expect.
786 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
787 << "failed to find entry";
788 }
789 }
790
791 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700792 if (obj1 == obj2) {
793 return JNI_TRUE;
794 } else {
795 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800796 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
797 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700798 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 }
800
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700801 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700802 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800804 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800805 if (c == nullptr) {
806 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700807 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700808 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 }
810
Ian Rogersbc939662013-08-15 10:26:54 -0700811 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700813 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700814 CHECK_NON_NULL_ARGUMENT(java_class);
815 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700816 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 va_end(args);
818 return result;
819 }
820
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700822 CHECK_NON_NULL_ARGUMENT(java_class);
823 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700824 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800825 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800826 if (c == nullptr) {
827 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700828 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800829 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800830 if (result == nullptr) {
831 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700832 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700833 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700834 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800835 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800836 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700837 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800838 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
840
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700842 CHECK_NON_NULL_ARGUMENT(java_class);
843 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800845 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800846 if (c == nullptr) {
847 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700848 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800849 mirror::Object* result = c->AllocObject(soa.Self());
850 if (result == nullptr) {
851 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700852 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700853 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700854 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800855 if (soa.Self()->IsExceptionPending()) {
856 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700857 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800858 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700859 }
860
Ian Rogersbc939662013-08-15 10:26:54 -0700861 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700862 CHECK_NON_NULL_ARGUMENT(java_class);
863 CHECK_NON_NULL_ARGUMENT(name);
864 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700866 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 }
868
Ian Rogersbc939662013-08-15 10:26:54 -0700869 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
870 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700871 CHECK_NON_NULL_ARGUMENT(java_class);
872 CHECK_NON_NULL_ARGUMENT(name);
873 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700874 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700875 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 }
877
Elliott Hughes72025e52011-08-23 17:50:30 -0700878 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700879 va_list ap;
880 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700881 CHECK_NON_NULL_ARGUMENT(obj);
882 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700883 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700885 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 }
888
Elliott Hughes72025e52011-08-23 17:50:30 -0700889 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700890 CHECK_NON_NULL_ARGUMENT(obj);
891 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 ScopedObjectAccess soa(env);
893 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
894 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700898 CHECK_NON_NULL_ARGUMENT(obj);
899 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700900 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700901 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
902 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700903 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 }
905
Elliott Hughes72025e52011-08-23 17:50:30 -0700906 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 va_list ap;
908 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700909 CHECK_NON_NULL_ARGUMENT(obj);
910 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700911 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700914 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700918 CHECK_NON_NULL_ARGUMENT(obj);
919 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 ScopedObjectAccess soa(env);
921 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700925 CHECK_NON_NULL_ARGUMENT(obj);
926 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700928 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
929 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 va_list ap;
934 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700935 CHECK_NON_NULL_ARGUMENT(obj);
936 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700937 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700940 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700944 CHECK_NON_NULL_ARGUMENT(obj);
945 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 ScopedObjectAccess soa(env);
947 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700951 CHECK_NON_NULL_ARGUMENT(obj);
952 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700954 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
955 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 va_list ap;
960 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700961 CHECK_NON_NULL_ARGUMENT(obj);
962 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700963 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700966 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700970 CHECK_NON_NULL_ARGUMENT(obj);
971 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 ScopedObjectAccess soa(env);
973 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700977 CHECK_NON_NULL_ARGUMENT(obj);
978 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700980 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
981 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_list ap;
986 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700987 CHECK_NON_NULL_ARGUMENT(obj);
988 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700989 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700992 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700996 CHECK_NON_NULL_ARGUMENT(obj);
997 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ScopedObjectAccess soa(env);
999 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001003 CHECK_NON_NULL_ARGUMENT(obj);
1004 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001006 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1007 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_list ap;
1012 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001013 CHECK_NON_NULL_ARGUMENT(obj);
1014 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001015 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001018 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001022 CHECK_NON_NULL_ARGUMENT(obj);
1023 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 ScopedObjectAccess soa(env);
1025 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001029 CHECK_NON_NULL_ARGUMENT(obj);
1030 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001032 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1033 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_list ap;
1038 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001039 CHECK_NON_NULL_ARGUMENT(obj);
1040 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001041 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001044 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001048 CHECK_NON_NULL_ARGUMENT(obj);
1049 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 ScopedObjectAccess soa(env);
1051 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001055 CHECK_NON_NULL_ARGUMENT(obj);
1056 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001058 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1059 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_list ap;
1064 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001065 CHECK_NON_NULL_ARGUMENT(obj);
1066 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001067 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001070 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001074 CHECK_NON_NULL_ARGUMENT(obj);
1075 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001081 CHECK_NON_NULL_ARGUMENT(obj);
1082 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001084 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1085 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_list ap;
1090 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001091 CHECK_NON_NULL_ARGUMENT(obj);
1092 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001093 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001096 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001100 CHECK_NON_NULL_ARGUMENT(obj);
1101 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 ScopedObjectAccess soa(env);
1103 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001107 CHECK_NON_NULL_ARGUMENT(obj);
1108 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001110 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1111 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001117 CHECK_NON_NULL_ARGUMENT(obj);
1118 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001119 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001120 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001125 CHECK_NON_NULL_ARGUMENT(obj);
1126 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 ScopedObjectAccess soa(env);
1128 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001132 CHECK_NON_NULL_ARGUMENT(obj);
1133 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001135 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001138 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001141 CHECK_NON_NULL_ARGUMENT(obj);
1142 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001143 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1145 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 va_end(ap);
1147 return local_result;
1148 }
1149
Ian Rogersbc939662013-08-15 10:26:54 -07001150 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1151 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001152 CHECK_NON_NULL_ARGUMENT(obj);
1153 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 ScopedObjectAccess soa(env);
1155 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1156 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Ian Rogersbc939662013-08-15 10:26:54 -07001159 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1160 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001161 CHECK_NON_NULL_ARGUMENT(obj);
1162 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001164 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Ian Rogersbc939662013-08-15 10:26:54 -07001168 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1169 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001172 CHECK_NON_NULL_ARGUMENT(obj);
1173 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001174 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001177 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Ian Rogersbc939662013-08-15 10:26:54 -07001180 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1181 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001182 CHECK_NON_NULL_ARGUMENT(obj);
1183 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 ScopedObjectAccess soa(env);
1185 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
Ian Rogersbc939662013-08-15 10:26:54 -07001188 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1189 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001190 CHECK_NON_NULL_ARGUMENT(obj);
1191 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001192 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001193 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001196 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001199 CHECK_NON_NULL_ARGUMENT(obj);
1200 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001201 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001204 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Ian Rogersbc939662013-08-15 10:26:54 -07001207 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1208 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001209 CHECK_NON_NULL_ARGUMENT(obj);
1210 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
1212 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Ian Rogersbc939662013-08-15 10:26:54 -07001215 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1216 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001217 CHECK_NON_NULL_ARGUMENT(obj);
1218 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001220 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001223 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001226 CHECK_NON_NULL_ARGUMENT(obj);
1227 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001228 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001231 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Ian Rogersbc939662013-08-15 10:26:54 -07001234 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1235 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001236 CHECK_NON_NULL_ARGUMENT(obj);
1237 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
1239 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Ian Rogersbc939662013-08-15 10:26:54 -07001242 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1243 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001244 CHECK_NON_NULL_ARGUMENT(obj);
1245 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001247 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001253 CHECK_NON_NULL_ARGUMENT(obj);
1254 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001255 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001258 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
Ian Rogersbc939662013-08-15 10:26:54 -07001261 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1262 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001263 CHECK_NON_NULL_ARGUMENT(obj);
1264 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 ScopedObjectAccess soa(env);
1266 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Ian Rogersbc939662013-08-15 10:26:54 -07001269 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1270 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001271 CHECK_NON_NULL_ARGUMENT(obj);
1272 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001274 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001277 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001280 CHECK_NON_NULL_ARGUMENT(obj);
1281 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001282 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001283 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001285 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Ian Rogersbc939662013-08-15 10:26:54 -07001288 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1289 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001290 CHECK_NON_NULL_ARGUMENT(obj);
1291 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001292 ScopedObjectAccess soa(env);
1293 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Ian Rogersbc939662013-08-15 10:26:54 -07001296 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1297 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001298 CHECK_NON_NULL_ARGUMENT(obj);
1299 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001301 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001304 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001307 CHECK_NON_NULL_ARGUMENT(obj);
1308 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001309 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001312 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Ian Rogersbc939662013-08-15 10:26:54 -07001315 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1316 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001317 CHECK_NON_NULL_ARGUMENT(obj);
1318 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001319 ScopedObjectAccess soa(env);
1320 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Ian Rogersbc939662013-08-15 10:26:54 -07001323 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1324 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001325 CHECK_NON_NULL_ARGUMENT(obj);
1326 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001328 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001334 CHECK_NON_NULL_ARGUMENT(obj);
1335 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001336 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001339 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Ian Rogersbc939662013-08-15 10:26:54 -07001342 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1343 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001344 CHECK_NON_NULL_ARGUMENT(obj);
1345 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001346 ScopedObjectAccess soa(env);
1347 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Ian Rogersbc939662013-08-15 10:26:54 -07001350 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1351 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001352 CHECK_NON_NULL_ARGUMENT(obj);
1353 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001355 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001358 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001361 CHECK_NON_NULL_ARGUMENT(obj);
1362 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001363 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001364 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001366 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
Ian Rogersbc939662013-08-15 10:26:54 -07001369 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1370 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001371 CHECK_NON_NULL_ARGUMENT(obj);
1372 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 ScopedObjectAccess soa(env);
1374 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Ian Rogersbc939662013-08-15 10:26:54 -07001377 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1378 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001379 CHECK_NON_NULL_ARGUMENT(obj);
1380 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001381 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001382 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001385 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001388 CHECK_NON_NULL_ARGUMENT(obj);
1389 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001390 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001391 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 va_end(ap);
1393 }
1394
Brian Carlstromea46f952013-07-30 01:26:50 -07001395 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1396 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001397 CHECK_NON_NULL_ARGUMENT(obj);
1398 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 ScopedObjectAccess soa(env);
1400 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Ian Rogersbc939662013-08-15 10:26:54 -07001403 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1404 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001405 CHECK_NON_NULL_ARGUMENT(obj);
1406 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001407 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001408 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Ian Rogersbc939662013-08-15 10:26:54 -07001411 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001412 CHECK_NON_NULL_ARGUMENT(java_class);
1413 CHECK_NON_NULL_ARGUMENT(name);
1414 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001415 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001416 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001418
Ian Rogersbc939662013-08-15 10:26:54 -07001419 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1420 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001421 CHECK_NON_NULL_ARGUMENT(java_class);
1422 CHECK_NON_NULL_ARGUMENT(name);
1423 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001424 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001425 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001429 CHECK_NON_NULL_ARGUMENT(obj);
1430 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001431 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001432 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1433 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001434 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001436
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001437 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001438 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001440 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001441 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001445 CHECK_NON_NULL_ARGUMENT(java_object);
1446 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001448 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1449 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1450 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001451 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001455 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001457 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1458 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001459 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462#define GET_PRIMITIVE_FIELD(fn, instance) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001463 CHECK_NON_NULL_ARGUMENT(instance); \
1464 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001465 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001466 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1467 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001468 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001470#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001471 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001472 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001473 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001474 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001477 CHECK_NON_NULL_ARGUMENT(instance); \
1478 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001480 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1481 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001482 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001484#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001485 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001486 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001487 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001488 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001491 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001495 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001499 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001503 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001507 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001511 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001515 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001519 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001522 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001523 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001526 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001527 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001531 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001535 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001538 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001539 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001543 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001547 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 }
1549
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001550 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001551 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 }
1553
1554 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001555 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 }
1557
1558 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001559 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560 }
1561
1562 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001563 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 }
1565
1566 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001567 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 }
1569
1570 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001571 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 }
1573
1574 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001575 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 }
1577
1578 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001579 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 }
1581
1582 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001583 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 }
1585
1586 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001587 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 }
1589
1590 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001591 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 }
1593
1594 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001595 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 }
1597
1598 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001599 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 }
1601
1602 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001603 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 }
1605
1606 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001607 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 }
1609
1610 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001611 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 }
1613
1614 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001615 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001621 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001623 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001624 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 va_end(ap);
1626 return local_result;
1627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001630 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001632 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001633 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001637 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001639 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001640 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001643 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001646 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001647 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001648 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001650 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001654 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001656 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001659 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001660 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001662 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001665 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001668 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001669 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001670 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001672 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001676 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001678 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001681 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001682 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001684 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001687 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001690 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001691 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001692 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001694 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001698 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001699 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001700 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001703 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001704 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001705 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001706 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001709 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001712 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001714 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001716 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001720 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001722 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001726 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001728 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001734 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001735 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001736 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001738 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001741 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001742 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001744 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 }
1746
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001747 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001748 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001749 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001750 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001753 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001756 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001757 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001760 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001763 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001764 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001765 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001766 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001769 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001770 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001772 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001775 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001777 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001778 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001779 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001780 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001782 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 }
1784
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001785 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001786 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001787 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001788 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001791 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001792 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001794 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001797 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001799 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001800 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001802 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001804 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001807 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001808 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001809 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001810 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001813 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001814 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001815 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001816 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001821 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001822 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001823 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001824 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 va_end(ap);
1826 }
1827
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001828 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001829 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001830 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001831 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001835 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001837 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes814e4032011-08-23 12:07:56 -07001840 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001841 if (UNLIKELY(char_count < 0)) {
1842 JniAbortF("NewString", "char_count < 0: %d", char_count);
1843 return nullptr;
1844 }
1845 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1846 JniAbortF("NewString", "chars == null && char_count > 0");
1847 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001848 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001850 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
1854 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001855 if (utf == nullptr) {
1856 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001859 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
Elliott Hughes814e4032011-08-23 12:07:56 -07001863 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001864 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001865 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001866 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 }
1868
1869 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001870 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001872 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 }
1874
Ian Rogersbc939662013-08-15 10:26:54 -07001875 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1876 jchar* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001877 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001879 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001880 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001882 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001883 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001884 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1885 memcpy(buf, chars + start, length * sizeof(jchar));
1886 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001887 }
1888
Ian Rogersbc939662013-08-15 10:26:54 -07001889 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1890 char* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001891 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001893 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001897 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001898 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1899 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1900 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001901 }
1902
Elliott Hughes75770752011-08-24 17:52:38 -07001903 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001904 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001905 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001906 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1907 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001909 if (is_copy != nullptr) {
1910 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001911 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001912 int32_t char_count = s->GetLength();
1913 int32_t offset = s->GetOffset();
1914 jchar* bytes = new jchar[char_count + 1];
1915 for (int32_t i = 0; i < char_count; i++) {
1916 bytes[i] = chars->Get(i + offset);
1917 }
1918 bytes[char_count] = '\0';
1919 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001920 }
1921
Mathieu Chartier590fee92013-09-13 13:46:47 -07001922 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001923 CHECK_NON_NULL_ARGUMENT(java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001924 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001926 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001930 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001934 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001938 if (java_string == nullptr) {
1939 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001940 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001941 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001942 *is_copy = JNI_TRUE;
1943 }
Ian Rogersef28b142012-11-30 14:22:18 -08001944 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001945 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001946 size_t byte_count = s->GetUtfLength();
1947 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001948 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001949 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1950 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1951 bytes[byte_count] = '\0';
1952 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001956 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001957 }
1958
Elliott Hughesbd935992011-08-22 11:59:34 -07001959 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001960 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001962 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001963 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001964 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1965 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001966 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001967 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001971 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001972 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001973 mirror::ObjectArray<mirror::Object>* array =
1974 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001975 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Ian Rogersbc939662013-08-15 10:26:54 -07001978 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1979 jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001980 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001982 mirror::ObjectArray<mirror::Object>* array =
1983 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1984 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001985 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
1988 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001989 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001990 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
1993 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001995 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
1998 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001999 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002000 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
2003 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002004 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002005 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
2008 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002009 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002010 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
2013 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002014 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002015 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
2018 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002019 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002020 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Ian Rogers1d99e452014-01-02 17:36:41 -08002023 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2024 jobject initial_element) {
2025 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002026 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002027 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002028 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002029
2030 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002031 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002032 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002033 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002034 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002035 if (UNLIKELY(element_class->IsPrimitive())) {
2036 JniAbortF("NewObjectArray", "not an object type: %s",
2037 PrettyDescriptor(element_class).c_str());
2038 return nullptr;
2039 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002040 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002041 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002042 if (UNLIKELY(array_class == nullptr)) {
2043 return nullptr;
2044 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes75770752011-08-24 17:52:38 -07002047 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002048 mirror::ObjectArray<mirror::Object>* result =
2049 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002050 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002051 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002052 if (initial_object != nullptr) {
2053 mirror::Class* element_class = result->GetClass()->GetComponentType();
2054 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2055 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2056 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2057 PrettyDescriptor(element_class).c_str());
2058
2059 } else {
2060 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002061 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002062 }
2063 }
Elliott Hughes75770752011-08-24 17:52:38 -07002064 }
2065 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002066 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
2069 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002070 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002071 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Ian Rogersa15e67d2012-02-28 13:51:55 -08002074 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002075 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002076 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002077 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002078 gc::Heap* heap = Runtime::Current()->GetHeap();
2079 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002080 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002081 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002082 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002083 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002085 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002086 *is_copy = JNI_FALSE;
2087 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002088 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002089 }
2090
Mathieu Chartier590fee92013-09-13 13:46:47 -07002091 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002092 CHECK_NON_NULL_ARGUMENT(array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002093 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002094 }
2095
Elliott Hughes75770752011-08-24 17:52:38 -07002096 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002097 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002098 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002099 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes75770752011-08-24 17:52:38 -07002102 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002103 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002105 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes75770752011-08-24 17:52:38 -07002108 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002109 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002110 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002111 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes75770752011-08-24 17:52:38 -07002114 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002115 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002117 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes75770752011-08-24 17:52:38 -07002120 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002121 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002123 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes75770752011-08-24 17:52:38 -07002126 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002127 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002129 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes75770752011-08-24 17:52:38 -07002132 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002133 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002134 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002135 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes75770752011-08-24 17:52:38 -07002138 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002139 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002140 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002141 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Mathieu Chartier590fee92013-09-13 13:46:47 -07002144 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2145 jint mode) {
2146 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Mathieu Chartier590fee92013-09-13 13:46:47 -07002149 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2150 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Mathieu Chartier590fee92013-09-13 13:46:47 -07002153 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2154 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Mathieu Chartier590fee92013-09-13 13:46:47 -07002157 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2158 jint mode) {
2159 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161
Mathieu Chartier590fee92013-09-13 13:46:47 -07002162 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2163 jint mode) {
2164 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Mathieu Chartier590fee92013-09-13 13:46:47 -07002167 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2168 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Mathieu Chartier590fee92013-09-13 13:46:47 -07002171 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2172 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Mathieu Chartier590fee92013-09-13 13:46:47 -07002175 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2176 jint mode) {
2177 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 }
2179
Ian Rogersbc939662013-08-15 10:26:54 -07002180 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2181 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002182 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002183 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2184 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Ian Rogersbc939662013-08-15 10:26:54 -07002187 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2188 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002189 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002190 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Ian Rogersbc939662013-08-15 10:26:54 -07002193 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2194 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002195 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002196 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Ian Rogersbc939662013-08-15 10:26:54 -07002199 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2200 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002201 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002202 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2203 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
Ian Rogersbc939662013-08-15 10:26:54 -07002206 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2207 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002208 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002209 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2210 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Ian Rogersbc939662013-08-15 10:26:54 -07002213 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2214 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002216 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Ian Rogersbc939662013-08-15 10:26:54 -07002219 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2220 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002221 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002222 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Ian Rogersbc939662013-08-15 10:26:54 -07002225 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2226 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002227 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002228 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2229 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Ian Rogersbc939662013-08-15 10:26:54 -07002232 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2233 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002235 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2236 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Ian Rogersbc939662013-08-15 10:26:54 -07002239 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2240 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002241 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002242 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
Ian Rogersbc939662013-08-15 10:26:54 -07002245 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2246 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002247 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002248 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 }
2250
Ian Rogersbc939662013-08-15 10:26:54 -07002251 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2252 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002253 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002254 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2255 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2259 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002261 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2262 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Ian Rogersbc939662013-08-15 10:26:54 -07002265 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2266 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002267 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002268 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Ian Rogersbc939662013-08-15 10:26:54 -07002271 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2272 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002274 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Ian Rogersbc939662013-08-15 10:26:54 -07002277 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2278 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002279 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002280 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2281 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283
Ian Rogersbc939662013-08-15 10:26:54 -07002284 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2285 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002286 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2287 }
2288
Ian Rogersbc939662013-08-15 10:26:54 -07002289 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2290 jint method_count, bool return_errors) {
2291 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002292 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002293 return JNI_ERR; // Not reached.
2294 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002295 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002296 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002297 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002298 if (UNLIKELY(method_count == 0)) {
2299 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2300 << PrettyDescriptor(c);
2301 return JNI_OK;
2302 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002303 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods);
Ian Rogersbc939662013-08-15 10:26:54 -07002304 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 const char* name = methods[i].name;
2306 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002307 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002309 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 ++sig;
2311 }
2312
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002313 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2314 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002315 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002317 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002318 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002319 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002320 << PrettyDescriptor(c) << "." << name << sig << " in "
2321 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002322 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002324 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002325 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002326 << PrettyDescriptor(c) << "." << name << sig
2327 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002328 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 return JNI_ERR;
2330 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002331
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002332 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002333
Ian Rogers1eb512d2013-10-18 15:42:20 -07002334 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002335 }
2336 return JNI_OK;
2337 }
2338
Elliott Hughes5174fe62011-08-23 15:12:35 -07002339 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002340 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002341 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002342 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002343
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002344 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002345
2346 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002347 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002348 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002350 }
2351 }
2352 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002353 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002354 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002355 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002356 }
2357 }
2358
2359 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002360 }
2361
Ian Rogers719d1a32014-03-06 12:13:39 -08002362 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002363 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002364 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002365 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2366 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002367 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002368 return JNI_ERR;
2369 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002370 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002371 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002372 }
2373
Ian Rogers719d1a32014-03-06 12:13:39 -08002374 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002375 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002376 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002377 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 o->MonitorExit(soa.Self());
2379 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002380 return JNI_ERR;
2381 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002382 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002383 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002384 }
2385
2386 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002387 CHECK_NON_NULL_ARGUMENT(vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002388 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002389 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002390 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002391 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002392 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002393 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002394 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002395 }
2396
Elliott Hughescdf53122011-08-19 15:46:09 -07002397 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002398 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002399 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002400 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002401 if (address == nullptr && capacity != 0) {
2402 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002403 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002404
Ian Rogers936b37f2014-02-14 00:52:24 -08002405 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002406 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002407 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002408 jint capacity_arg = static_cast<jint>(capacity);
2409
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002410 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2411 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002412 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002413 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002414 }
2415
Elliott Hughesb465ab02011-08-24 11:21:21 -07002416 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002417 return reinterpret_cast<void*>(env->GetLongField(
2418 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002419 }
2420
Elliott Hughesb465ab02011-08-24 11:21:21 -07002421 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002422 return static_cast<jlong>(env->GetIntField(
2423 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002424 }
2425
Elliott Hughesb465ab02011-08-24 11:21:21 -07002426 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002427 CHECK_NON_NULL_ARGUMENT(java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002428
2429 // Do we definitely know what kind of reference this is?
2430 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2431 IndirectRefKind kind = GetIndirectRefKind(ref);
2432 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002433 case kLocal: {
2434 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002435 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002436 return JNILocalRefType;
2437 }
2438 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002439 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002440 case kGlobal:
2441 return JNIGlobalRefType;
2442 case kWeakGlobal:
2443 return JNIWeakGlobalRefType;
2444 case kSirtOrInvalid:
2445 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002446 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002447 return JNILocalRefType;
2448 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002449 return JNIInvalidRefType;
2450 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002451 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2452 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002453 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002454
2455 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002456 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2457 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002458 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002459 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002460 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2461 return JNI_ERR;
2462 }
2463 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002464 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002465 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2466 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002467 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002468 soa.Self()->ThrowOutOfMemoryError(caller);
2469 }
2470 return okay ? JNI_OK : JNI_ERR;
2471 }
2472
2473 template<typename JniT, typename ArtT>
2474 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002476 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002477 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002478 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002479 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002480 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002481 return soa.AddLocalReference<JniT>(result);
2482 }
2483
2484 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2485 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2486 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002488 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2489 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002490 // Only make a copy if necessary.
2491 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2492 if (is_copy != nullptr) {
2493 *is_copy = JNI_TRUE;
2494 }
2495 static const size_t component_size = array->GetClass()->GetComponentSize();
2496 size_t size = array->GetLength() * component_size;
2497 void* data = new uint64_t[RoundUp(size, 8) / 8];
2498 memcpy(data, array->GetData(), size);
2499 return reinterpret_cast<CArrayT>(data);
2500 } else {
2501 if (is_copy != nullptr) {
2502 *is_copy = JNI_FALSE;
2503 }
2504 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002505 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002506 }
2507
Mathieu Chartier590fee92013-09-13 13:46:47 -07002508 template <typename ArrayT, typename ElementT>
2509 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2510 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002511 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002512 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002513 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002514 gc::Heap* heap = Runtime::Current()->GetHeap();
2515 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2516 size_t bytes = array->GetLength() * component_size;
2517 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2518 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002519 if (is_copy) {
2520 // Sanity check: If elements is not the same as the java array's data, it better not be a
2521 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2522 // copies we make?
2523 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2524 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2525 reinterpret_cast<void*>(elements), array_data);
2526 return;
2527 }
2528 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002529 // Don't need to copy if we had a direct pointer.
2530 if (mode != JNI_ABORT && is_copy) {
2531 memcpy(array_data, elements, bytes);
2532 }
2533 if (mode != JNI_COMMIT) {
2534 if (is_copy) {
2535 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002536 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002537 // Non copy to a movable object must means that we had disabled the moving GC.
2538 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002539 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002540 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002541 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002542 }
2543
2544 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2545 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2546 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002547 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002548 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002549 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2550 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2551 ThrowAIOOBE(soa, array, start, length, "src");
2552 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002553 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002554 JavaT* data = array->GetData();
2555 memcpy(buf, data + start, length * sizeof(JavaT));
2556 }
2557 }
2558
2559 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2560 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2561 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002563 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002564 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2565 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2566 ThrowAIOOBE(soa, array, start, length, "dst");
2567 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002568 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002569 JavaT* data = array->GetData();
2570 memcpy(data + start, buf, length * sizeof(JavaT));
2571 }
2572 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002573};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002574
Elliott Hughes88c5c352012-03-15 18:49:48 -07002575const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002576 nullptr, // reserved0.
2577 nullptr, // reserved1.
2578 nullptr, // reserved2.
2579 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002580 JNI::GetVersion,
2581 JNI::DefineClass,
2582 JNI::FindClass,
2583 JNI::FromReflectedMethod,
2584 JNI::FromReflectedField,
2585 JNI::ToReflectedMethod,
2586 JNI::GetSuperclass,
2587 JNI::IsAssignableFrom,
2588 JNI::ToReflectedField,
2589 JNI::Throw,
2590 JNI::ThrowNew,
2591 JNI::ExceptionOccurred,
2592 JNI::ExceptionDescribe,
2593 JNI::ExceptionClear,
2594 JNI::FatalError,
2595 JNI::PushLocalFrame,
2596 JNI::PopLocalFrame,
2597 JNI::NewGlobalRef,
2598 JNI::DeleteGlobalRef,
2599 JNI::DeleteLocalRef,
2600 JNI::IsSameObject,
2601 JNI::NewLocalRef,
2602 JNI::EnsureLocalCapacity,
2603 JNI::AllocObject,
2604 JNI::NewObject,
2605 JNI::NewObjectV,
2606 JNI::NewObjectA,
2607 JNI::GetObjectClass,
2608 JNI::IsInstanceOf,
2609 JNI::GetMethodID,
2610 JNI::CallObjectMethod,
2611 JNI::CallObjectMethodV,
2612 JNI::CallObjectMethodA,
2613 JNI::CallBooleanMethod,
2614 JNI::CallBooleanMethodV,
2615 JNI::CallBooleanMethodA,
2616 JNI::CallByteMethod,
2617 JNI::CallByteMethodV,
2618 JNI::CallByteMethodA,
2619 JNI::CallCharMethod,
2620 JNI::CallCharMethodV,
2621 JNI::CallCharMethodA,
2622 JNI::CallShortMethod,
2623 JNI::CallShortMethodV,
2624 JNI::CallShortMethodA,
2625 JNI::CallIntMethod,
2626 JNI::CallIntMethodV,
2627 JNI::CallIntMethodA,
2628 JNI::CallLongMethod,
2629 JNI::CallLongMethodV,
2630 JNI::CallLongMethodA,
2631 JNI::CallFloatMethod,
2632 JNI::CallFloatMethodV,
2633 JNI::CallFloatMethodA,
2634 JNI::CallDoubleMethod,
2635 JNI::CallDoubleMethodV,
2636 JNI::CallDoubleMethodA,
2637 JNI::CallVoidMethod,
2638 JNI::CallVoidMethodV,
2639 JNI::CallVoidMethodA,
2640 JNI::CallNonvirtualObjectMethod,
2641 JNI::CallNonvirtualObjectMethodV,
2642 JNI::CallNonvirtualObjectMethodA,
2643 JNI::CallNonvirtualBooleanMethod,
2644 JNI::CallNonvirtualBooleanMethodV,
2645 JNI::CallNonvirtualBooleanMethodA,
2646 JNI::CallNonvirtualByteMethod,
2647 JNI::CallNonvirtualByteMethodV,
2648 JNI::CallNonvirtualByteMethodA,
2649 JNI::CallNonvirtualCharMethod,
2650 JNI::CallNonvirtualCharMethodV,
2651 JNI::CallNonvirtualCharMethodA,
2652 JNI::CallNonvirtualShortMethod,
2653 JNI::CallNonvirtualShortMethodV,
2654 JNI::CallNonvirtualShortMethodA,
2655 JNI::CallNonvirtualIntMethod,
2656 JNI::CallNonvirtualIntMethodV,
2657 JNI::CallNonvirtualIntMethodA,
2658 JNI::CallNonvirtualLongMethod,
2659 JNI::CallNonvirtualLongMethodV,
2660 JNI::CallNonvirtualLongMethodA,
2661 JNI::CallNonvirtualFloatMethod,
2662 JNI::CallNonvirtualFloatMethodV,
2663 JNI::CallNonvirtualFloatMethodA,
2664 JNI::CallNonvirtualDoubleMethod,
2665 JNI::CallNonvirtualDoubleMethodV,
2666 JNI::CallNonvirtualDoubleMethodA,
2667 JNI::CallNonvirtualVoidMethod,
2668 JNI::CallNonvirtualVoidMethodV,
2669 JNI::CallNonvirtualVoidMethodA,
2670 JNI::GetFieldID,
2671 JNI::GetObjectField,
2672 JNI::GetBooleanField,
2673 JNI::GetByteField,
2674 JNI::GetCharField,
2675 JNI::GetShortField,
2676 JNI::GetIntField,
2677 JNI::GetLongField,
2678 JNI::GetFloatField,
2679 JNI::GetDoubleField,
2680 JNI::SetObjectField,
2681 JNI::SetBooleanField,
2682 JNI::SetByteField,
2683 JNI::SetCharField,
2684 JNI::SetShortField,
2685 JNI::SetIntField,
2686 JNI::SetLongField,
2687 JNI::SetFloatField,
2688 JNI::SetDoubleField,
2689 JNI::GetStaticMethodID,
2690 JNI::CallStaticObjectMethod,
2691 JNI::CallStaticObjectMethodV,
2692 JNI::CallStaticObjectMethodA,
2693 JNI::CallStaticBooleanMethod,
2694 JNI::CallStaticBooleanMethodV,
2695 JNI::CallStaticBooleanMethodA,
2696 JNI::CallStaticByteMethod,
2697 JNI::CallStaticByteMethodV,
2698 JNI::CallStaticByteMethodA,
2699 JNI::CallStaticCharMethod,
2700 JNI::CallStaticCharMethodV,
2701 JNI::CallStaticCharMethodA,
2702 JNI::CallStaticShortMethod,
2703 JNI::CallStaticShortMethodV,
2704 JNI::CallStaticShortMethodA,
2705 JNI::CallStaticIntMethod,
2706 JNI::CallStaticIntMethodV,
2707 JNI::CallStaticIntMethodA,
2708 JNI::CallStaticLongMethod,
2709 JNI::CallStaticLongMethodV,
2710 JNI::CallStaticLongMethodA,
2711 JNI::CallStaticFloatMethod,
2712 JNI::CallStaticFloatMethodV,
2713 JNI::CallStaticFloatMethodA,
2714 JNI::CallStaticDoubleMethod,
2715 JNI::CallStaticDoubleMethodV,
2716 JNI::CallStaticDoubleMethodA,
2717 JNI::CallStaticVoidMethod,
2718 JNI::CallStaticVoidMethodV,
2719 JNI::CallStaticVoidMethodA,
2720 JNI::GetStaticFieldID,
2721 JNI::GetStaticObjectField,
2722 JNI::GetStaticBooleanField,
2723 JNI::GetStaticByteField,
2724 JNI::GetStaticCharField,
2725 JNI::GetStaticShortField,
2726 JNI::GetStaticIntField,
2727 JNI::GetStaticLongField,
2728 JNI::GetStaticFloatField,
2729 JNI::GetStaticDoubleField,
2730 JNI::SetStaticObjectField,
2731 JNI::SetStaticBooleanField,
2732 JNI::SetStaticByteField,
2733 JNI::SetStaticCharField,
2734 JNI::SetStaticShortField,
2735 JNI::SetStaticIntField,
2736 JNI::SetStaticLongField,
2737 JNI::SetStaticFloatField,
2738 JNI::SetStaticDoubleField,
2739 JNI::NewString,
2740 JNI::GetStringLength,
2741 JNI::GetStringChars,
2742 JNI::ReleaseStringChars,
2743 JNI::NewStringUTF,
2744 JNI::GetStringUTFLength,
2745 JNI::GetStringUTFChars,
2746 JNI::ReleaseStringUTFChars,
2747 JNI::GetArrayLength,
2748 JNI::NewObjectArray,
2749 JNI::GetObjectArrayElement,
2750 JNI::SetObjectArrayElement,
2751 JNI::NewBooleanArray,
2752 JNI::NewByteArray,
2753 JNI::NewCharArray,
2754 JNI::NewShortArray,
2755 JNI::NewIntArray,
2756 JNI::NewLongArray,
2757 JNI::NewFloatArray,
2758 JNI::NewDoubleArray,
2759 JNI::GetBooleanArrayElements,
2760 JNI::GetByteArrayElements,
2761 JNI::GetCharArrayElements,
2762 JNI::GetShortArrayElements,
2763 JNI::GetIntArrayElements,
2764 JNI::GetLongArrayElements,
2765 JNI::GetFloatArrayElements,
2766 JNI::GetDoubleArrayElements,
2767 JNI::ReleaseBooleanArrayElements,
2768 JNI::ReleaseByteArrayElements,
2769 JNI::ReleaseCharArrayElements,
2770 JNI::ReleaseShortArrayElements,
2771 JNI::ReleaseIntArrayElements,
2772 JNI::ReleaseLongArrayElements,
2773 JNI::ReleaseFloatArrayElements,
2774 JNI::ReleaseDoubleArrayElements,
2775 JNI::GetBooleanArrayRegion,
2776 JNI::GetByteArrayRegion,
2777 JNI::GetCharArrayRegion,
2778 JNI::GetShortArrayRegion,
2779 JNI::GetIntArrayRegion,
2780 JNI::GetLongArrayRegion,
2781 JNI::GetFloatArrayRegion,
2782 JNI::GetDoubleArrayRegion,
2783 JNI::SetBooleanArrayRegion,
2784 JNI::SetByteArrayRegion,
2785 JNI::SetCharArrayRegion,
2786 JNI::SetShortArrayRegion,
2787 JNI::SetIntArrayRegion,
2788 JNI::SetLongArrayRegion,
2789 JNI::SetFloatArrayRegion,
2790 JNI::SetDoubleArrayRegion,
2791 JNI::RegisterNatives,
2792 JNI::UnregisterNatives,
2793 JNI::MonitorEnter,
2794 JNI::MonitorExit,
2795 JNI::GetJavaVM,
2796 JNI::GetStringRegion,
2797 JNI::GetStringUTFRegion,
2798 JNI::GetPrimitiveArrayCritical,
2799 JNI::ReleasePrimitiveArrayCritical,
2800 JNI::GetStringCritical,
2801 JNI::ReleaseStringCritical,
2802 JNI::NewWeakGlobalRef,
2803 JNI::DeleteWeakGlobalRef,
2804 JNI::ExceptionCheck,
2805 JNI::NewDirectByteBuffer,
2806 JNI::GetDirectBufferAddress,
2807 JNI::GetDirectBufferCapacity,
2808 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002809};
2810
Elliott Hughes75770752011-08-24 17:52:38 -07002811JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002812 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002813 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002814 local_ref_cookie(IRT_FIRST_SEGMENT),
2815 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002816 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002817 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002818 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002819 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002820 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002821 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002822 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002823}
2824
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002825JNIEnvExt::~JNIEnvExt() {
2826}
2827
Mathieu Chartier590fee92013-09-13 13:46:47 -07002828jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2829 if (obj == nullptr) {
2830 return nullptr;
2831 }
2832 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2833}
2834
2835void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2836 if (obj != nullptr) {
2837 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2838 }
2839}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002840void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2841 check_jni = enabled;
2842 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002843}
2844
Elliott Hughes73e66f72012-05-09 09:34:45 -07002845void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2846 locals.Dump(os);
2847 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002848}
2849
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002850void JNIEnvExt::PushFrame(int /*capacity*/) {
2851 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002852 stacked_local_ref_cookies.push_back(local_ref_cookie);
2853 local_ref_cookie = locals.GetSegmentState();
2854}
2855
2856void JNIEnvExt::PopFrame() {
2857 locals.SetSegmentState(local_ref_cookie);
2858 local_ref_cookie = stacked_local_ref_cookies.back();
2859 stacked_local_ref_cookies.pop_back();
2860}
2861
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002862Offset JNIEnvExt::SegmentStateOffset() {
2863 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2864 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2865}
2866
Carl Shapiroea4dca82011-08-01 13:45:38 -07002867// JNI Invocation interface.
2868
Brian Carlstrombddf9762013-05-14 11:35:37 -07002869extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002870 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002871 if (IsBadJniVersion(args->version)) {
2872 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002873 return JNI_EVERSION;
2874 }
2875 Runtime::Options options;
2876 for (int i = 0; i < args->nOptions; ++i) {
2877 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002878 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002879 }
2880 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002881 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002882 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002883 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002884 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002885 bool started = runtime->Start();
2886 if (!started) {
2887 delete Thread::Current()->GetJniEnv();
2888 delete runtime->GetJavaVM();
2889 LOG(WARNING) << "CreateJavaVM failed";
2890 return JNI_ERR;
2891 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002892 *p_env = Thread::Current()->GetJniEnv();
2893 *p_vm = runtime->GetJavaVM();
2894 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002895}
2896
Elliott Hughesf2682d52011-08-15 16:37:04 -07002897extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002898 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002899 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002900 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002901 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002902 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002903 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002904 }
2905 return JNI_OK;
2906}
2907
2908// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002909extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002910 return JNI_ERR;
2911}
2912
Elliott Hughescdf53122011-08-19 15:46:09 -07002913class JII {
2914 public:
2915 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002916 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002917 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002918 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002919 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2920 delete raw_vm->runtime;
2921 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002922 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002923
Elliott Hughescdf53122011-08-19 15:46:09 -07002924 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002925 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002926 }
2927
2928 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002929 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002930 }
2931
2932 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002933 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002934 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002935 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002936 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2937 Runtime* runtime = raw_vm->runtime;
2938 runtime->DetachCurrentThread();
2939 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002940 }
2941
2942 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002943 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2944 // and unlike other calls that take a JNI version doesn't care if you supply
2945 // JNI_VERSION_1_1, which we don't otherwise support.
2946 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002947 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002948 return JNI_EVERSION;
2949 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002950 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002951 return JNI_ERR;
2952 }
2953 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002954 if (thread == nullptr) {
2955 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002956 return JNI_EDETACHED;
2957 }
2958 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002959 return JNI_OK;
2960 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002961};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002962
Elliott Hughes88c5c352012-03-15 18:49:48 -07002963const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002964 nullptr, // reserved0
2965 nullptr, // reserved1
2966 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002967 JII::DestroyJavaVM,
2968 JII::AttachCurrentThread,
2969 JII::DetachCurrentThread,
2970 JII::GetEnv,
2971 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002972};
2973
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002974JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002975 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002976 check_jni_abort_hook(nullptr),
2977 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002978 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002979 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002980 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08002981 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002982 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002983 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002984 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002985 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002986 libraries(new Libraries),
2987 weak_globals_lock_("JNI weak global reference table lock"),
2988 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2989 allow_new_weak_globals_(true),
2990 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002991 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002992 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002993 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002994 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002995}
2996
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002997JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002998 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002999}
3000
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003001jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3002 if (obj == nullptr) {
3003 return nullptr;
3004 }
3005 MutexLock mu(self, weak_globals_lock_);
3006 while (UNLIKELY(!allow_new_weak_globals_)) {
3007 weak_globals_add_condition_.WaitHoldingLocks(self);
3008 }
3009 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3010 return reinterpret_cast<jweak>(ref);
3011}
3012
3013void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3014 MutexLock mu(self, weak_globals_lock_);
3015 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3016 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3017 << "failed to find entry";
3018 }
3019}
3020
Elliott Hughes88c5c352012-03-15 18:49:48 -07003021void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3022 check_jni = enabled;
3023 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003024}
3025
Elliott Hughesae80b492012-04-24 10:43:17 -07003026void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3027 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3028 if (force_copy) {
3029 os << " (with forcecopy)";
3030 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003031 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003032 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003033 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003034 os << "; pins=" << pin_table.Size();
3035 }
3036 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003037 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003038 os << "; globals=" << globals.Capacity();
3039 }
3040 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003041 MutexLock mu(self, weak_globals_lock_);
3042 if (weak_globals_.Capacity() > 0) {
3043 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003044 }
3045 }
3046 os << '\n';
3047
3048 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003049 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003050 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3051 }
3052}
3053
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003054void JavaVMExt::DisallowNewWeakGlobals() {
3055 MutexLock mu(Thread::Current(), weak_globals_lock_);
3056 allow_new_weak_globals_ = false;
3057}
3058
3059void JavaVMExt::AllowNewWeakGlobals() {
3060 Thread* self = Thread::Current();
3061 MutexLock mu(self, weak_globals_lock_);
3062 allow_new_weak_globals_ = true;
3063 weak_globals_add_condition_.Broadcast(self);
3064}
3065
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003066mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3067 MutexLock mu(self, weak_globals_lock_);
3068 while (UNLIKELY(!allow_new_weak_globals_)) {
3069 weak_globals_add_condition_.WaitHoldingLocks(self);
3070 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003071 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003072}
3073
Elliott Hughes73e66f72012-05-09 09:34:45 -07003074void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003075 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003076 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003077 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003078 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003079 }
3080 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003081 MutexLock mu(self, weak_globals_lock_);
3082 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003083 }
3084 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003085 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003086 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003087 }
3088}
3089
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003090bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003091 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003092 std::string* detail) {
3093 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003094
3095 // See if we've already loaded this library. If we have, and the class loader
3096 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003097 // TODO: for better results we should canonicalize the pathname (or even compare
3098 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003099 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003100 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003101 {
3102 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003103 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003104 library = libraries->Get(path);
3105 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003106 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003107 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003108 // The library will be associated with class_loader. The JNI
3109 // spec says we can't load the same library into more than one
3110 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003111 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003112 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003113 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003114 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003115 return false;
3116 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003117 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003118 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003119 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003120 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003121 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003122 return false;
3123 }
3124 return true;
3125 }
3126
3127 // Open the shared library. Because we're using a full path, the system
3128 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3129 // resolve this library's dependencies though.)
3130
3131 // Failures here are expected when java.library.path has several entries
3132 // and we have to hunt for the lib.
3133
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003134 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3135 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3136 // dlopen) becomes zero from dlclose.
3137
Elliott Hughescdf53122011-08-19 15:46:09 -07003138 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003139 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003140 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003141 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003142 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003143
Elliott Hughes84b2f142012-09-27 09:16:28 -07003144 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003145
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003146 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003147 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003148 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003149 return false;
3150 }
3151
3152 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003153 // TODO: move the locking (and more of this logic) into Libraries.
3154 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003155 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003156 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003157 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003158 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003159 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003160 libraries->Put(path, library);
3161 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003162 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003163 }
3164 if (!created_library) {
3165 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003166 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003167 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003168 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003169
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003170 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3171 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003172
Elliott Hughes79353722013-08-02 16:52:18 -07003173 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003174 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003175 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003176 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003177 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003178 } else {
3179 // Call JNI_OnLoad. We have to override the current class
3180 // loader, which will always be "null" since the stuff at the
3181 // top of the stack is around Runtime.loadLibrary(). (See
3182 // the comments in the JNI FindClass function.)
3183 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3184 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003185 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003186 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003187
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003188 int version = 0;
3189 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003190 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003191 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003192 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003193 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003194
Mathieu Chartier590fee92013-09-13 13:46:47 -07003195 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003196
Elliott Hughes79353722013-08-02 16:52:18 -07003197 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003198 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003199 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003200 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003201 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003202 // It's unwise to call dlclose() here, but we can mark it
3203 // as bad and ensure that future load attempts will fail.
3204 // We don't know how far JNI_OnLoad got, so there could
3205 // be some partially-initialized stuff accessible through
3206 // newly-registered native method calls. We could try to
3207 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003208 } else {
3209 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003210 }
Elliott Hughes79353722013-08-02 16:52:18 -07003211 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003212 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003213 }
3214
Elliott Hughes79353722013-08-02 16:52:18 -07003215 library->SetResult(was_successful);
3216 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003217}
3218
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003219void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003220 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003221 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003222 // If this is a static method, it could be called before the class
3223 // has been initialized.
3224 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003225 c = EnsureInitialized(Thread::Current(), c);
3226 if (c == nullptr) {
3227 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003228 }
3229 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003230 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003231 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003232 std::string detail;
3233 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003234 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003235 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003236 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003237 native_method = libraries->FindNativeMethod(m, detail);
3238 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003239 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003240 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003241 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3242 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003243 }
3244 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003245}
3246
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003247void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003248 MutexLock mu(Thread::Current(), weak_globals_lock_);
3249 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003250 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003251 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003252 if (new_obj == nullptr) {
3253 new_obj = kClearedJniWeakGlobal;
3254 }
3255 *entry = new_obj;
3256 }
3257}
3258
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003259void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003260 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003261 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003262 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003263 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003264 }
3265 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003266 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003267 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003268 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003269 {
3270 MutexLock mu(self, libraries_lock);
3271 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003272 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003273 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003274 // The weak_globals table is visited by the GC itself (because it mutates the table).
3275}
3276
Elliott Hughesc8fece32013-01-02 11:27:23 -08003277void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003278 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003279 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003280 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003281 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3282 }
3283 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3284}
3285
Ian Rogersdf20fe02011-07-20 20:34:16 -07003286} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003287
3288std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3289 switch (rhs) {
3290 case JNIInvalidRefType:
3291 os << "JNIInvalidRefType";
3292 return os;
3293 case JNILocalRefType:
3294 os << "JNILocalRefType";
3295 return os;
3296 case JNIGlobalRefType:
3297 os << "JNIGlobalRefType";
3298 return os;
3299 case JNIWeakGlobalRefType:
3300 os << "JNIWeakGlobalRefType";
3301 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003302 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003303 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003304 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003305 }
3306}