blob: c04aabfffa5a4daad89b6cbdd8c1627c54ab5c96 [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
Ian Rogersbc939662013-08-15 10:26:54 -0700516#define CHECK_NON_NULL_ARGUMENT(fn, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800517 if (UNLIKELY(value == nullptr)) { \
Ian Rogersbc939662013-08-15 10:26:54 -0700518 JniAbortF(#fn, #value " == null"); \
519 }
520
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700521#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800522 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700523 JniAbortF(#fn, #value " == null"); \
524 }
525
Elliott Hughescdf53122011-08-19 15:46:09 -0700526class JNI {
527 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700528 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700529 return JNI_VERSION_1_6;
530 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700531
Ian Rogers25e8b912012-09-07 11:31:36 -0700532 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700533 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800534 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700535 }
536
Elliott Hughescdf53122011-08-19 15:46:09 -0700537 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700538 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700539 Runtime* runtime = Runtime::Current();
540 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700541 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700542 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800543 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700544 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700545 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800546 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700547 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800548 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700549 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700550 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700551 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700552
Ian Rogers62f05122014-03-21 11:21:29 -0700553 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
554 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700556 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700557 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700558
Ian Rogers62f05122014-03-21 11:21:29 -0700559 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
560 CHECK_NON_NULL_ARGUMENT(FromReflectedField, jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700561 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700562 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700563 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700564
Elliott Hughescdf53122011-08-19 15:46:09 -0700565 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700566 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700567 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800568 mirror::ArtMethod* m = soa.DecodeMethod(mid);
569 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700570 jobject art_method = soa.AddLocalReference<jobject>(m);
571 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
572 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800573 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700574 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800575 SetObjectField(env, reflect_method,
576 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700578 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700579
Elliott Hughescdf53122011-08-19 15:46:09 -0700580 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700581 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700582 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800583 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 jobject art_field = soa.AddLocalReference<jobject>(f);
585 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
586 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800587 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700588 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 SetObjectField(env, reflect_field,
590 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700591 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700592 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700593
Elliott Hughes37f7a402011-08-22 18:56:01 -0700594 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700595 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700596 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800597 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700599 }
600
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700601 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700602 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800604 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700606 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700607
Elliott Hughes37f7a402011-08-22 18:56:01 -0700608 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700609 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
610 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
613 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700614 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700615 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Elliott Hughese84278b2012-03-22 10:06:53 -0700617 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700618 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800619 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700620 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700621 return JNI_TRUE;
622 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700623 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800624 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
625 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700626 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700627 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700628 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700629
Elliott Hughes37f7a402011-08-22 18:56:01 -0700630 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
633 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700634 return JNI_ERR;
635 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800636 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
637 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 return JNI_OK;
639 }
640
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700641 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700642 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800643 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700644 }
645
646 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700647 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700648 }
649
650 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700651 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700652 }
653
654 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700656
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800657 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
658 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
659 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800660 uint32_t old_throw_dex_pc;
661 {
662 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800664 old_throw_this_object.reset(old_throw_location.GetThis());
665 old_throw_method.reset(old_throw_location.GetMethod());
666 old_exception.reset(old_exception_obj);
667 old_throw_dex_pc = old_throw_location.GetDexPc();
668 soa.Self()->ClearException();
669 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800670 ScopedLocalRef<jthrowable> exception(env,
671 soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700672 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
673 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800674 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700675 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800676 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700677 } else {
678 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800679 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800680 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700681 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700683 }
684 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
686 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700687
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700690
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800693 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
696
Ian Rogers25e8b912012-09-07 11:31:36 -0700697 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 LOG(FATAL) << "JNI FatalError called: " << msg;
699 }
700
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700701 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800702 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700703 return JNI_ERR;
704 }
Ian Rogersef28b142012-11-30 14:22:18 -0800705 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 return JNI_OK;
707 }
708
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700709 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800711 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 soa.Env()->PopFrame();
713 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 }
715
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700716 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800717 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700718 }
719
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800722 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800723 // Check for null after decoding the object to handle cleared weak globals.
724 if (decoded_obj == nullptr) {
725 return nullptr;
726 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700729 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 return reinterpret_cast<jobject>(ref);
732 }
733
734 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800735 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 return;
737 }
Ian Rogersef28b142012-11-30 14:22:18 -0800738 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800740 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700741 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700742
743 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
744 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
745 << "failed to find entry";
746 }
747 }
748
749 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800751 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
753
754 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700755 if (obj != nullptr) {
756 ScopedObjectAccess soa(env);
757 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 }
759 }
760
761 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700762 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800763 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800764 // Check for null after decoding the object to handle cleared weak globals.
765 if (decoded_obj == nullptr) {
766 return nullptr;
767 }
768 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 }
770
771 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800772 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 return;
774 }
Ian Rogersef28b142012-11-30 14:22:18 -0800775 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700776
Ian Rogersef28b142012-11-30 14:22:18 -0800777 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 if (!locals.Remove(cookie, obj)) {
779 // Attempting to delete a local reference that is not in the
780 // topmost local reference frame is a no-op. DeleteLocalRef returns
781 // void and doesn't throw any exceptions, but we should probably
782 // complain about it so the user will notice that things aren't
783 // going quite the way they expect.
784 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
785 << "failed to find entry";
786 }
787 }
788
789 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700790 if (obj1 == obj2) {
791 return JNI_TRUE;
792 } else {
793 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800794 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
795 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700796 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 }
798
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700799 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700800 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800802 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800803 if (c == nullptr) {
804 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700805 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700806 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 }
808
Ian Rogersbc939662013-08-15 10:26:54 -0700809 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700811 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700812 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
813 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
814 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 va_end(args);
816 return result;
817 }
818
Elliott Hughes72025e52011-08-23 17:50:30 -0700819 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700820 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
821 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700822 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800823 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800824 if (c == nullptr) {
825 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700826 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800827 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800828 if (result == nullptr) {
829 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700830 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700831 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800833 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800834 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700835 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800836 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 }
838
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700840 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
841 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800843 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800844 if (c == nullptr) {
845 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700846 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800847 mirror::Object* result = c->AllocObject(soa.Self());
848 if (result == nullptr) {
849 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700850 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700852 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800853 if (soa.Self()->IsExceptionPending()) {
854 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700855 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800856 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 }
858
Ian Rogersbc939662013-08-15 10:26:54 -0700859 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
860 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
861 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
862 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700864 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
Ian Rogersbc939662013-08-15 10:26:54 -0700867 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
868 const char* sig) {
869 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
870 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
871 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700872 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700873 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 }
875
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700877 va_list ap;
878 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700879 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
880 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700881 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 }
886
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700888 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
889 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700890 ScopedObjectAccess soa(env);
891 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
892 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 }
894
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700896 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
897 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700899 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
900 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700901 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 }
903
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 va_list ap;
906 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700907 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
908 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700909 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700912 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700916 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
917 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
919 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700923 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
924 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700926 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
927 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 va_list ap;
932 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700933 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
934 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
935 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700938 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700942 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
943 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 ScopedObjectAccess soa(env);
945 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 }
947
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700949 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
950 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700952 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
953 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 va_list ap;
958 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700959 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
960 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700961 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700964 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700968 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
969 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
971 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700975 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
976 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700978 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
979 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 va_list ap;
984 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700985 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
986 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700987 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700988 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700990 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700994 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
995 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 ScopedObjectAccess soa(env);
997 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001001 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1002 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001004 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1005 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_list ap;
1010 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001011 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1012 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1013 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001014 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001016 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001020 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1021 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 ScopedObjectAccess soa(env);
1023 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001027 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1028 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001030 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1031 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_list ap;
1036 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001037 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1038 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001039 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001040 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001042 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001046 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1047 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(env);
1049 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001053 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1054 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001056 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1057 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 va_list ap;
1062 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001063 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1064 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001065 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001066 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001068 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001072 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1073 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 ScopedObjectAccess soa(env);
1075 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001079 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1080 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001082 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1083 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_list ap;
1088 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001089 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1090 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001091 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001094 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001098 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1099 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001105 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1106 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001108 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1109 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_list ap;
1114 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001115 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1116 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001117 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001118 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001123 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1124 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 ScopedObjectAccess soa(env);
1126 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001130 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1131 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001133 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001136 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001139 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1140 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001141 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1143 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 va_end(ap);
1145 return local_result;
1146 }
1147
Ian Rogersbc939662013-08-15 10:26:54 -07001148 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1149 va_list args) {
1150 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1151 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001152 ScopedObjectAccess soa(env);
1153 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1154 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Ian Rogersbc939662013-08-15 10:26:54 -07001157 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1158 jvalue* args) {
1159 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1160 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001162 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Ian Rogersbc939662013-08-15 10:26:54 -07001166 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1167 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001170 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1171 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001172 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001175 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Ian Rogersbc939662013-08-15 10:26:54 -07001178 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1179 va_list args) {
1180 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1181 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 ScopedObjectAccess soa(env);
1183 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
Ian Rogersbc939662013-08-15 10:26:54 -07001186 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1187 jvalue* args) {
1188 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1189 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001191 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001194 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001197 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1198 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001199 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001200 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001202 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Ian Rogersbc939662013-08-15 10:26:54 -07001205 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1206 va_list args) {
1207 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1208 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 ScopedObjectAccess soa(env);
1210 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Ian Rogersbc939662013-08-15 10:26:54 -07001213 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1214 jvalue* args) {
1215 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1216 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001218 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001221 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001224 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1225 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1226 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001229 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
Ian Rogersbc939662013-08-15 10:26:54 -07001232 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1233 va_list args) {
1234 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1235 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001236 ScopedObjectAccess soa(env);
1237 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Ian Rogersbc939662013-08-15 10:26:54 -07001240 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1241 jvalue* args) {
1242 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1243 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001245 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001248 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001251 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1252 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001253 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001256 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Ian Rogersbc939662013-08-15 10:26:54 -07001259 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1260 va_list args) {
1261 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1262 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001263 ScopedObjectAccess soa(env);
1264 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
Ian Rogersbc939662013-08-15 10:26:54 -07001267 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1268 jvalue* args) {
1269 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1270 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001271 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001272 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001275 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001278 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1279 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001280 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001281 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001283 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Ian Rogersbc939662013-08-15 10:26:54 -07001286 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1287 va_list args) {
1288 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1289 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001290 ScopedObjectAccess soa(env);
1291 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Ian Rogersbc939662013-08-15 10:26:54 -07001294 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1295 jvalue* args) {
1296 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1297 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001298 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001299 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001302 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001305 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1306 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001307 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001310 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Ian Rogersbc939662013-08-15 10:26:54 -07001313 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1314 va_list args) {
1315 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1316 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001317 ScopedObjectAccess soa(env);
1318 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Ian Rogersbc939662013-08-15 10:26:54 -07001321 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1322 jvalue* args) {
1323 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1324 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001325 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001326 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001329 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001332 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1333 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001334 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001335 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001337 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 }
1339
Ian Rogersbc939662013-08-15 10:26:54 -07001340 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1341 va_list args) {
1342 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1343 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001344 ScopedObjectAccess soa(env);
1345 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
Ian Rogersbc939662013-08-15 10:26:54 -07001348 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1349 jvalue* args) {
1350 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1351 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001352 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001353 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001356 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001359 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1360 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001361 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001362 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001364 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Ian Rogersbc939662013-08-15 10:26:54 -07001367 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1368 va_list args) {
1369 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1370 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001371 ScopedObjectAccess soa(env);
1372 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Ian Rogersbc939662013-08-15 10:26:54 -07001375 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1376 jvalue* args) {
1377 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1378 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001379 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001380 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001385 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001386 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1387 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001388 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001389 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 va_end(ap);
1391 }
1392
Brian Carlstromea46f952013-07-30 01:26:50 -07001393 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1394 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001395 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1396 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001397 ScopedObjectAccess soa(env);
1398 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Ian Rogersbc939662013-08-15 10:26:54 -07001401 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1402 jvalue* args) {
1403 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1404 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001405 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001406 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
Ian Rogersbc939662013-08-15 10:26:54 -07001409 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1410 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1411 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1412 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001414 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001416
Ian Rogersbc939662013-08-15 10:26:54 -07001417 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1418 const char* sig) {
1419 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1420 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1421 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001422 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001423 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001427 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1428 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001430 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1431 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001436 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001438 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001439 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001443 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1444 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001446 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1447 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1448 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001449 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001453 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001454 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001455 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1456 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001457 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001461 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1462 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001463 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001464 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1465 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001466 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001468#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001469 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001470 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001471 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001472 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001475 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1476 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001478 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1479 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001480 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001481
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001482#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001483 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001484 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001485 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001486 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001489 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001493 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001497 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001501 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001505 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001509 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001513 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001517 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001520 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001521 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001525 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001532 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001533 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001537 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001549 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 }
1551
1552 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 }
1555
1556 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 }
1559
1560 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 }
1563
1564 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 }
1567
1568 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 }
1571
1572 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 }
1575
1576 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 }
1579
1580 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 }
1583
1584 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 }
1587
1588 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 }
1591
1592 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 }
1595
1596 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 }
1599
1600 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 }
1603
1604 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 }
1607
1608 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 }
1611
1612 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001619 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001620 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001621 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001622 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 va_end(ap);
1624 return local_result;
1625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001628 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001629 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001630 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001631 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001634 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001635 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001636 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001637 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001644 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001645 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001646 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001648 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001652 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001653 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001654 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001660 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001663 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001666 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001667 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001668 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001670 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001673 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001674 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001676 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001680 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001682 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001688 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001689 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001690 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001692 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001696 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001698 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001702 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001704 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001710 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001711 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001712 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001714 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001718 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001719 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001720 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001723 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001724 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001725 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001726 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001732 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001733 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001734 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001736 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001740 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001742 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001746 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001748 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001754 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001755 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001756 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001758 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001762 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001764 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001768 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001769 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001775 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001776 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001777 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001778 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001780 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001784 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001786 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001790 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001792 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001797 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001798 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001799 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001800 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001802 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001806 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001812 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001814 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001819 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001820 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001821 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001822 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 va_end(ap);
1824 }
1825
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001826 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001827 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001828 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001829 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001832 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001833 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001834 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001835 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes814e4032011-08-23 12:07:56 -07001838 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001839 if (UNLIKELY(char_count < 0)) {
1840 JniAbortF("NewString", "char_count < 0: %d", char_count);
1841 return nullptr;
1842 }
1843 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1844 JniAbortF("NewString", "chars == null && char_count > 0");
1845 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001846 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001848 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
1852 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001853 if (utf == nullptr) {
1854 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
1860
Elliott Hughes814e4032011-08-23 12:07:56 -07001861 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001862 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001863 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001864 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001865 }
1866
1867 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001868 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001869 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001870 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 }
1872
Ian Rogersbc939662013-08-15 10:26:54 -07001873 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1874 jchar* buf) {
1875 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001876 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001877 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001878 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001880 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001881 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001882 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1883 memcpy(buf, chars + start, length * sizeof(jchar));
1884 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001885 }
1886
Ian Rogersbc939662013-08-15 10:26:54 -07001887 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1888 char* buf) {
1889 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001891 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001892 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001895 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1897 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1898 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001899 }
1900
Elliott Hughes75770752011-08-24 17:52:38 -07001901 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00001902 CHECK_NON_NULL_ARGUMENT(GetStringChars, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001904 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1905 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001906 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001907 if (is_copy != nullptr) {
1908 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001909 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001910 int32_t char_count = s->GetLength();
1911 int32_t offset = s->GetOffset();
1912 jchar* bytes = new jchar[char_count + 1];
1913 for (int32_t i = 0; i < char_count; i++) {
1914 bytes[i] = chars->Get(i + offset);
1915 }
1916 bytes[char_count] = '\0';
1917 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001918 }
1919
Mathieu Chartier590fee92013-09-13 13:46:47 -07001920 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00001921 CHECK_NON_NULL_ARGUMENT(ReleaseStringChars, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001922 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001923 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001924 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001928 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
Elliott Hughes75770752011-08-24 17:52:38 -07001931 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001932 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001936 if (java_string == nullptr) {
1937 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001938 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001939 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001940 *is_copy = JNI_TRUE;
1941 }
Ian Rogersef28b142012-11-30 14:22:18 -08001942 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001943 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001944 size_t byte_count = s->GetUtfLength();
1945 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001946 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001947 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1948 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1949 bytes[byte_count] = '\0';
1950 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001954 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 }
1956
Elliott Hughesbd935992011-08-22 11:59:34 -07001957 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07001958 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001959 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001960 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001961 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001962 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1963 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001964 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001965 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes814e4032011-08-23 12:07:56 -07001968 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07001969 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001970 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001971 mirror::ObjectArray<mirror::Object>* array =
1972 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Ian Rogersbc939662013-08-15 10:26:54 -07001976 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1977 jobject java_value) {
1978 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001979 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001980 mirror::ObjectArray<mirror::Object>* array =
1981 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1982 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001983 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
1986 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001988 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
1991 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001992 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001993 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
1996 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001998 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
2001 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002003 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
2006 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002008 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
2011 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002013 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
2016 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002018 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Ian Rogers1d99e452014-01-02 17:36:41 -08002021 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2022 jobject initial_element) {
2023 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002024 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002025 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002026 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002027
2028 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002029 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002030 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002031 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002032 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002033 if (UNLIKELY(element_class->IsPrimitive())) {
2034 JniAbortF("NewObjectArray", "not an object type: %s",
2035 PrettyDescriptor(element_class).c_str());
2036 return nullptr;
2037 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002038 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002039 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002040 if (UNLIKELY(array_class == nullptr)) {
2041 return nullptr;
2042 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002046 mirror::ObjectArray<mirror::Object>* result =
2047 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002048 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002049 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002050 if (initial_object != nullptr) {
2051 mirror::Class* element_class = result->GetClass()->GetComponentType();
2052 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2053 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2054 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2055 PrettyDescriptor(element_class).c_str());
2056
2057 } else {
2058 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002059 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002060 }
2061 }
Elliott Hughes75770752011-08-24 17:52:38 -07002062 }
2063 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002064 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
2067 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002069 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Ian Rogersa15e67d2012-02-28 13:51:55 -08002072 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002073 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002074 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002075 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002076 gc::Heap* heap = Runtime::Current()->GetHeap();
2077 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002078 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002079 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002080 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002081 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002083 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002084 *is_copy = JNI_FALSE;
2085 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002086 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002087 }
2088
Mathieu Chartier590fee92013-09-13 13:46:47 -07002089 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002090 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002091 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002092 }
2093
Elliott Hughes75770752011-08-24 17:52:38 -07002094 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002095 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002097 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes75770752011-08-24 17:52:38 -07002100 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002101 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002103 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes75770752011-08-24 17:52:38 -07002106 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002107 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002109 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes75770752011-08-24 17:52:38 -07002112 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002113 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002114 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002115 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes75770752011-08-24 17:52:38 -07002118 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002119 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002120 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002121 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes75770752011-08-24 17:52:38 -07002124 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002125 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002126 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002127 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes75770752011-08-24 17:52:38 -07002130 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002131 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002132 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002133 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Elliott Hughes75770752011-08-24 17:52:38 -07002136 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002137 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002138 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002139 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Mathieu Chartier590fee92013-09-13 13:46:47 -07002142 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2143 jint mode) {
2144 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Mathieu Chartier590fee92013-09-13 13:46:47 -07002147 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2148 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Mathieu Chartier590fee92013-09-13 13:46:47 -07002151 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2152 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Mathieu Chartier590fee92013-09-13 13:46:47 -07002155 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2156 jint mode) {
2157 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159
Mathieu Chartier590fee92013-09-13 13:46:47 -07002160 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2161 jint mode) {
2162 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Mathieu Chartier590fee92013-09-13 13:46:47 -07002165 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2166 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Mathieu Chartier590fee92013-09-13 13:46:47 -07002169 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2170 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Mathieu Chartier590fee92013-09-13 13:46:47 -07002173 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2174 jint mode) {
2175 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Ian Rogersbc939662013-08-15 10:26:54 -07002178 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2179 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002181 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2182 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Ian Rogersbc939662013-08-15 10:26:54 -07002185 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2186 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002188 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Ian Rogersbc939662013-08-15 10:26:54 -07002191 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2192 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002193 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002194 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Ian Rogersbc939662013-08-15 10:26:54 -07002197 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2198 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002199 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002200 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2201 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Ian Rogersbc939662013-08-15 10:26:54 -07002204 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2205 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002207 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2208 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Ian Rogersbc939662013-08-15 10:26:54 -07002211 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2212 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002213 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002214 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Ian Rogersbc939662013-08-15 10:26:54 -07002217 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2218 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002219 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002220 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Ian Rogersbc939662013-08-15 10:26:54 -07002223 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2224 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002225 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002226 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2227 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Ian Rogersbc939662013-08-15 10:26:54 -07002230 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2231 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002232 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002233 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2234 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Ian Rogersbc939662013-08-15 10:26:54 -07002237 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2238 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002239 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002240 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Ian Rogersbc939662013-08-15 10:26:54 -07002243 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2244 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002245 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002246 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Ian Rogersbc939662013-08-15 10:26:54 -07002249 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2250 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002251 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002252 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2253 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Ian Rogersbc939662013-08-15 10:26:54 -07002256 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2257 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002258 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002259 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2260 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 }
2262
Ian Rogersbc939662013-08-15 10:26:54 -07002263 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2264 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002265 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002266 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Ian Rogersbc939662013-08-15 10:26:54 -07002269 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2270 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002271 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002272 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 }
2274
Ian Rogersbc939662013-08-15 10:26:54 -07002275 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2276 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002277 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002278 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2279 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2283 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002284 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2285 }
2286
Ian Rogersbc939662013-08-15 10:26:54 -07002287 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2288 jint method_count, bool return_errors) {
2289 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002290 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002291 return JNI_ERR; // Not reached.
2292 }
2293 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002294 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002295 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002296 if (UNLIKELY(method_count == 0)) {
2297 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2298 << PrettyDescriptor(c);
2299 return JNI_OK;
2300 }
2301 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2302 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 const char* name = methods[i].name;
2304 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002305 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002307 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 ++sig;
2309 }
2310
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002311 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2312 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002313 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002315 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002316 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002317 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002318 << PrettyDescriptor(c) << "." << name << sig << " in "
2319 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002320 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002321 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002322 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002323 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002324 << PrettyDescriptor(c) << "." << name << sig
2325 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002326 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002327 return JNI_ERR;
2328 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002329
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002330 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002331
Ian Rogers1eb512d2013-10-18 15:42:20 -07002332 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002333 }
2334 return JNI_OK;
2335 }
2336
Elliott Hughes5174fe62011-08-23 15:12:35 -07002337 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002338 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002339 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002340 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002341
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002342 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002343
2344 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002345 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002346 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002347 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002348 }
2349 }
2350 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002351 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002352 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002353 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002354 }
2355 }
2356
2357 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002358 }
2359
Ian Rogers719d1a32014-03-06 12:13:39 -08002360 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogersbc939662013-08-15 10:26:54 -07002361 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002362 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002363 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2364 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002365 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002366 return JNI_ERR;
2367 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002368 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002369 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002370 }
2371
Ian Rogers719d1a32014-03-06 12:13:39 -08002372 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogersbc939662013-08-15 10:26:54 -07002373 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002374 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002375 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002376 o->MonitorExit(soa.Self());
2377 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002378 return JNI_ERR;
2379 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002380 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002381 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002382 }
2383
2384 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002385 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002386 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002387 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002388 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002389 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002390 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002391 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002392 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002393 }
2394
Elliott Hughescdf53122011-08-19 15:46:09 -07002395 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002396 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002397 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002398 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002399 if (address == nullptr && capacity != 0) {
2400 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002401 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002402
Ian Rogers936b37f2014-02-14 00:52:24 -08002403 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002404 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002405 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002406 jint capacity_arg = static_cast<jint>(capacity);
2407
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002408 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2409 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002410 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002411 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002412 }
2413
Elliott Hughesb465ab02011-08-24 11:21:21 -07002414 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002415 return reinterpret_cast<void*>(env->GetLongField(
2416 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002417 }
2418
Elliott Hughesb465ab02011-08-24 11:21:21 -07002419 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002420 return static_cast<jlong>(env->GetIntField(
2421 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 }
2423
Elliott Hughesb465ab02011-08-24 11:21:21 -07002424 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002425 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002426
2427 // Do we definitely know what kind of reference this is?
2428 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2429 IndirectRefKind kind = GetIndirectRefKind(ref);
2430 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002431 case kLocal: {
2432 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002433 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002434 return JNILocalRefType;
2435 }
2436 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002437 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002438 case kGlobal:
2439 return JNIGlobalRefType;
2440 case kWeakGlobal:
2441 return JNIWeakGlobalRefType;
2442 case kSirtOrInvalid:
2443 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002444 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002445 return JNILocalRefType;
2446 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002447 return JNIInvalidRefType;
2448 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002449 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2450 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002451 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002452
2453 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002454 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2455 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002456 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002457 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002458 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2459 return JNI_ERR;
2460 }
2461 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002462 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002463 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2464 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002465 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002466 soa.Self()->ThrowOutOfMemoryError(caller);
2467 }
2468 return okay ? JNI_OK : JNI_ERR;
2469 }
2470
2471 template<typename JniT, typename ArtT>
2472 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002473 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002474 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002475 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002476 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002477 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002478 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002479 return soa.AddLocalReference<JniT>(result);
2480 }
2481
2482 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2483 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2484 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002486 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2487 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002488 // Only make a copy if necessary.
2489 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2490 if (is_copy != nullptr) {
2491 *is_copy = JNI_TRUE;
2492 }
2493 static const size_t component_size = array->GetClass()->GetComponentSize();
2494 size_t size = array->GetLength() * component_size;
2495 void* data = new uint64_t[RoundUp(size, 8) / 8];
2496 memcpy(data, array->GetData(), size);
2497 return reinterpret_cast<CArrayT>(data);
2498 } else {
2499 if (is_copy != nullptr) {
2500 *is_copy = JNI_FALSE;
2501 }
2502 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002503 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002504 }
2505
Mathieu Chartier590fee92013-09-13 13:46:47 -07002506 template <typename ArrayT, typename ElementT>
2507 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2508 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002509 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002510 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002511 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002512 gc::Heap* heap = Runtime::Current()->GetHeap();
2513 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2514 size_t bytes = array->GetLength() * component_size;
2515 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2516 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002517 if (is_copy) {
2518 // Sanity check: If elements is not the same as the java array's data, it better not be a
2519 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2520 // copies we make?
2521 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2522 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2523 reinterpret_cast<void*>(elements), array_data);
2524 return;
2525 }
2526 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002527 // Don't need to copy if we had a direct pointer.
2528 if (mode != JNI_ABORT && is_copy) {
2529 memcpy(array_data, elements, bytes);
2530 }
2531 if (mode != JNI_COMMIT) {
2532 if (is_copy) {
2533 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002534 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002535 // Non copy to a movable object must means that we had disabled the moving GC.
2536 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002537 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002538 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002539 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002540 }
2541
2542 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2543 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2544 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002546 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002547 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2548 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2549 ThrowAIOOBE(soa, array, start, length, "src");
2550 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002551 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002552 JavaT* data = array->GetData();
2553 memcpy(buf, data + start, length * sizeof(JavaT));
2554 }
2555 }
2556
2557 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2558 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2559 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002561 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002562 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2563 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2564 ThrowAIOOBE(soa, array, start, length, "dst");
2565 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002566 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002567 JavaT* data = array->GetData();
2568 memcpy(data + start, buf, length * sizeof(JavaT));
2569 }
2570 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002571};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002572
Elliott Hughes88c5c352012-03-15 18:49:48 -07002573const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002574 nullptr, // reserved0.
2575 nullptr, // reserved1.
2576 nullptr, // reserved2.
2577 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002578 JNI::GetVersion,
2579 JNI::DefineClass,
2580 JNI::FindClass,
2581 JNI::FromReflectedMethod,
2582 JNI::FromReflectedField,
2583 JNI::ToReflectedMethod,
2584 JNI::GetSuperclass,
2585 JNI::IsAssignableFrom,
2586 JNI::ToReflectedField,
2587 JNI::Throw,
2588 JNI::ThrowNew,
2589 JNI::ExceptionOccurred,
2590 JNI::ExceptionDescribe,
2591 JNI::ExceptionClear,
2592 JNI::FatalError,
2593 JNI::PushLocalFrame,
2594 JNI::PopLocalFrame,
2595 JNI::NewGlobalRef,
2596 JNI::DeleteGlobalRef,
2597 JNI::DeleteLocalRef,
2598 JNI::IsSameObject,
2599 JNI::NewLocalRef,
2600 JNI::EnsureLocalCapacity,
2601 JNI::AllocObject,
2602 JNI::NewObject,
2603 JNI::NewObjectV,
2604 JNI::NewObjectA,
2605 JNI::GetObjectClass,
2606 JNI::IsInstanceOf,
2607 JNI::GetMethodID,
2608 JNI::CallObjectMethod,
2609 JNI::CallObjectMethodV,
2610 JNI::CallObjectMethodA,
2611 JNI::CallBooleanMethod,
2612 JNI::CallBooleanMethodV,
2613 JNI::CallBooleanMethodA,
2614 JNI::CallByteMethod,
2615 JNI::CallByteMethodV,
2616 JNI::CallByteMethodA,
2617 JNI::CallCharMethod,
2618 JNI::CallCharMethodV,
2619 JNI::CallCharMethodA,
2620 JNI::CallShortMethod,
2621 JNI::CallShortMethodV,
2622 JNI::CallShortMethodA,
2623 JNI::CallIntMethod,
2624 JNI::CallIntMethodV,
2625 JNI::CallIntMethodA,
2626 JNI::CallLongMethod,
2627 JNI::CallLongMethodV,
2628 JNI::CallLongMethodA,
2629 JNI::CallFloatMethod,
2630 JNI::CallFloatMethodV,
2631 JNI::CallFloatMethodA,
2632 JNI::CallDoubleMethod,
2633 JNI::CallDoubleMethodV,
2634 JNI::CallDoubleMethodA,
2635 JNI::CallVoidMethod,
2636 JNI::CallVoidMethodV,
2637 JNI::CallVoidMethodA,
2638 JNI::CallNonvirtualObjectMethod,
2639 JNI::CallNonvirtualObjectMethodV,
2640 JNI::CallNonvirtualObjectMethodA,
2641 JNI::CallNonvirtualBooleanMethod,
2642 JNI::CallNonvirtualBooleanMethodV,
2643 JNI::CallNonvirtualBooleanMethodA,
2644 JNI::CallNonvirtualByteMethod,
2645 JNI::CallNonvirtualByteMethodV,
2646 JNI::CallNonvirtualByteMethodA,
2647 JNI::CallNonvirtualCharMethod,
2648 JNI::CallNonvirtualCharMethodV,
2649 JNI::CallNonvirtualCharMethodA,
2650 JNI::CallNonvirtualShortMethod,
2651 JNI::CallNonvirtualShortMethodV,
2652 JNI::CallNonvirtualShortMethodA,
2653 JNI::CallNonvirtualIntMethod,
2654 JNI::CallNonvirtualIntMethodV,
2655 JNI::CallNonvirtualIntMethodA,
2656 JNI::CallNonvirtualLongMethod,
2657 JNI::CallNonvirtualLongMethodV,
2658 JNI::CallNonvirtualLongMethodA,
2659 JNI::CallNonvirtualFloatMethod,
2660 JNI::CallNonvirtualFloatMethodV,
2661 JNI::CallNonvirtualFloatMethodA,
2662 JNI::CallNonvirtualDoubleMethod,
2663 JNI::CallNonvirtualDoubleMethodV,
2664 JNI::CallNonvirtualDoubleMethodA,
2665 JNI::CallNonvirtualVoidMethod,
2666 JNI::CallNonvirtualVoidMethodV,
2667 JNI::CallNonvirtualVoidMethodA,
2668 JNI::GetFieldID,
2669 JNI::GetObjectField,
2670 JNI::GetBooleanField,
2671 JNI::GetByteField,
2672 JNI::GetCharField,
2673 JNI::GetShortField,
2674 JNI::GetIntField,
2675 JNI::GetLongField,
2676 JNI::GetFloatField,
2677 JNI::GetDoubleField,
2678 JNI::SetObjectField,
2679 JNI::SetBooleanField,
2680 JNI::SetByteField,
2681 JNI::SetCharField,
2682 JNI::SetShortField,
2683 JNI::SetIntField,
2684 JNI::SetLongField,
2685 JNI::SetFloatField,
2686 JNI::SetDoubleField,
2687 JNI::GetStaticMethodID,
2688 JNI::CallStaticObjectMethod,
2689 JNI::CallStaticObjectMethodV,
2690 JNI::CallStaticObjectMethodA,
2691 JNI::CallStaticBooleanMethod,
2692 JNI::CallStaticBooleanMethodV,
2693 JNI::CallStaticBooleanMethodA,
2694 JNI::CallStaticByteMethod,
2695 JNI::CallStaticByteMethodV,
2696 JNI::CallStaticByteMethodA,
2697 JNI::CallStaticCharMethod,
2698 JNI::CallStaticCharMethodV,
2699 JNI::CallStaticCharMethodA,
2700 JNI::CallStaticShortMethod,
2701 JNI::CallStaticShortMethodV,
2702 JNI::CallStaticShortMethodA,
2703 JNI::CallStaticIntMethod,
2704 JNI::CallStaticIntMethodV,
2705 JNI::CallStaticIntMethodA,
2706 JNI::CallStaticLongMethod,
2707 JNI::CallStaticLongMethodV,
2708 JNI::CallStaticLongMethodA,
2709 JNI::CallStaticFloatMethod,
2710 JNI::CallStaticFloatMethodV,
2711 JNI::CallStaticFloatMethodA,
2712 JNI::CallStaticDoubleMethod,
2713 JNI::CallStaticDoubleMethodV,
2714 JNI::CallStaticDoubleMethodA,
2715 JNI::CallStaticVoidMethod,
2716 JNI::CallStaticVoidMethodV,
2717 JNI::CallStaticVoidMethodA,
2718 JNI::GetStaticFieldID,
2719 JNI::GetStaticObjectField,
2720 JNI::GetStaticBooleanField,
2721 JNI::GetStaticByteField,
2722 JNI::GetStaticCharField,
2723 JNI::GetStaticShortField,
2724 JNI::GetStaticIntField,
2725 JNI::GetStaticLongField,
2726 JNI::GetStaticFloatField,
2727 JNI::GetStaticDoubleField,
2728 JNI::SetStaticObjectField,
2729 JNI::SetStaticBooleanField,
2730 JNI::SetStaticByteField,
2731 JNI::SetStaticCharField,
2732 JNI::SetStaticShortField,
2733 JNI::SetStaticIntField,
2734 JNI::SetStaticLongField,
2735 JNI::SetStaticFloatField,
2736 JNI::SetStaticDoubleField,
2737 JNI::NewString,
2738 JNI::GetStringLength,
2739 JNI::GetStringChars,
2740 JNI::ReleaseStringChars,
2741 JNI::NewStringUTF,
2742 JNI::GetStringUTFLength,
2743 JNI::GetStringUTFChars,
2744 JNI::ReleaseStringUTFChars,
2745 JNI::GetArrayLength,
2746 JNI::NewObjectArray,
2747 JNI::GetObjectArrayElement,
2748 JNI::SetObjectArrayElement,
2749 JNI::NewBooleanArray,
2750 JNI::NewByteArray,
2751 JNI::NewCharArray,
2752 JNI::NewShortArray,
2753 JNI::NewIntArray,
2754 JNI::NewLongArray,
2755 JNI::NewFloatArray,
2756 JNI::NewDoubleArray,
2757 JNI::GetBooleanArrayElements,
2758 JNI::GetByteArrayElements,
2759 JNI::GetCharArrayElements,
2760 JNI::GetShortArrayElements,
2761 JNI::GetIntArrayElements,
2762 JNI::GetLongArrayElements,
2763 JNI::GetFloatArrayElements,
2764 JNI::GetDoubleArrayElements,
2765 JNI::ReleaseBooleanArrayElements,
2766 JNI::ReleaseByteArrayElements,
2767 JNI::ReleaseCharArrayElements,
2768 JNI::ReleaseShortArrayElements,
2769 JNI::ReleaseIntArrayElements,
2770 JNI::ReleaseLongArrayElements,
2771 JNI::ReleaseFloatArrayElements,
2772 JNI::ReleaseDoubleArrayElements,
2773 JNI::GetBooleanArrayRegion,
2774 JNI::GetByteArrayRegion,
2775 JNI::GetCharArrayRegion,
2776 JNI::GetShortArrayRegion,
2777 JNI::GetIntArrayRegion,
2778 JNI::GetLongArrayRegion,
2779 JNI::GetFloatArrayRegion,
2780 JNI::GetDoubleArrayRegion,
2781 JNI::SetBooleanArrayRegion,
2782 JNI::SetByteArrayRegion,
2783 JNI::SetCharArrayRegion,
2784 JNI::SetShortArrayRegion,
2785 JNI::SetIntArrayRegion,
2786 JNI::SetLongArrayRegion,
2787 JNI::SetFloatArrayRegion,
2788 JNI::SetDoubleArrayRegion,
2789 JNI::RegisterNatives,
2790 JNI::UnregisterNatives,
2791 JNI::MonitorEnter,
2792 JNI::MonitorExit,
2793 JNI::GetJavaVM,
2794 JNI::GetStringRegion,
2795 JNI::GetStringUTFRegion,
2796 JNI::GetPrimitiveArrayCritical,
2797 JNI::ReleasePrimitiveArrayCritical,
2798 JNI::GetStringCritical,
2799 JNI::ReleaseStringCritical,
2800 JNI::NewWeakGlobalRef,
2801 JNI::DeleteWeakGlobalRef,
2802 JNI::ExceptionCheck,
2803 JNI::NewDirectByteBuffer,
2804 JNI::GetDirectBufferAddress,
2805 JNI::GetDirectBufferCapacity,
2806 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002807};
2808
Elliott Hughes75770752011-08-24 17:52:38 -07002809JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002810 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002811 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002812 local_ref_cookie(IRT_FIRST_SEGMENT),
2813 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002814 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002815 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002816 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002817 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002818 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002819 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002820 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002821}
2822
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002823JNIEnvExt::~JNIEnvExt() {
2824}
2825
Mathieu Chartier590fee92013-09-13 13:46:47 -07002826jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2827 if (obj == nullptr) {
2828 return nullptr;
2829 }
2830 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2831}
2832
2833void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2834 if (obj != nullptr) {
2835 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2836 }
2837}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002838void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2839 check_jni = enabled;
2840 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002841}
2842
Elliott Hughes73e66f72012-05-09 09:34:45 -07002843void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2844 locals.Dump(os);
2845 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002846}
2847
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002848void JNIEnvExt::PushFrame(int /*capacity*/) {
2849 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002850 stacked_local_ref_cookies.push_back(local_ref_cookie);
2851 local_ref_cookie = locals.GetSegmentState();
2852}
2853
2854void JNIEnvExt::PopFrame() {
2855 locals.SetSegmentState(local_ref_cookie);
2856 local_ref_cookie = stacked_local_ref_cookies.back();
2857 stacked_local_ref_cookies.pop_back();
2858}
2859
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002860Offset JNIEnvExt::SegmentStateOffset() {
2861 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2862 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2863}
2864
Carl Shapiroea4dca82011-08-01 13:45:38 -07002865// JNI Invocation interface.
2866
Brian Carlstrombddf9762013-05-14 11:35:37 -07002867extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002868 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002869 if (IsBadJniVersion(args->version)) {
2870 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002871 return JNI_EVERSION;
2872 }
2873 Runtime::Options options;
2874 for (int i = 0; i < args->nOptions; ++i) {
2875 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002876 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002877 }
2878 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002879 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002880 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002881 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002882 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002883 bool started = runtime->Start();
2884 if (!started) {
2885 delete Thread::Current()->GetJniEnv();
2886 delete runtime->GetJavaVM();
2887 LOG(WARNING) << "CreateJavaVM failed";
2888 return JNI_ERR;
2889 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002890 *p_env = Thread::Current()->GetJniEnv();
2891 *p_vm = runtime->GetJavaVM();
2892 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002893}
2894
Elliott Hughesf2682d52011-08-15 16:37:04 -07002895extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002896 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002897 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002898 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002899 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002900 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002901 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002902 }
2903 return JNI_OK;
2904}
2905
2906// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002907extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002908 return JNI_ERR;
2909}
2910
Elliott Hughescdf53122011-08-19 15:46:09 -07002911class JII {
2912 public:
2913 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002914 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002915 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002916 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002917 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2918 delete raw_vm->runtime;
2919 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002920 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002921
Elliott Hughescdf53122011-08-19 15:46:09 -07002922 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002923 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002924 }
2925
2926 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002927 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002928 }
2929
2930 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002931 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002932 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002933 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002934 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2935 Runtime* runtime = raw_vm->runtime;
2936 runtime->DetachCurrentThread();
2937 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002938 }
2939
2940 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002941 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2942 // and unlike other calls that take a JNI version doesn't care if you supply
2943 // JNI_VERSION_1_1, which we don't otherwise support.
2944 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002945 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002946 return JNI_EVERSION;
2947 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002948 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002949 return JNI_ERR;
2950 }
2951 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002952 if (thread == nullptr) {
2953 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002954 return JNI_EDETACHED;
2955 }
2956 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002957 return JNI_OK;
2958 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002959};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002960
Elliott Hughes88c5c352012-03-15 18:49:48 -07002961const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002962 nullptr, // reserved0
2963 nullptr, // reserved1
2964 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002965 JII::DestroyJavaVM,
2966 JII::AttachCurrentThread,
2967 JII::DetachCurrentThread,
2968 JII::GetEnv,
2969 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002970};
2971
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002972JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002973 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002974 check_jni_abort_hook(nullptr),
2975 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002976 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002977 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002978 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08002979 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002980 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002981 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002982 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002983 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002984 libraries(new Libraries),
2985 weak_globals_lock_("JNI weak global reference table lock"),
2986 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2987 allow_new_weak_globals_(true),
2988 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002989 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002990 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002991 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002992 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002993}
2994
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002995JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002996 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002997}
2998
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002999jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3000 if (obj == nullptr) {
3001 return nullptr;
3002 }
3003 MutexLock mu(self, weak_globals_lock_);
3004 while (UNLIKELY(!allow_new_weak_globals_)) {
3005 weak_globals_add_condition_.WaitHoldingLocks(self);
3006 }
3007 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3008 return reinterpret_cast<jweak>(ref);
3009}
3010
3011void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3012 MutexLock mu(self, weak_globals_lock_);
3013 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3014 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3015 << "failed to find entry";
3016 }
3017}
3018
Elliott Hughes88c5c352012-03-15 18:49:48 -07003019void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3020 check_jni = enabled;
3021 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003022}
3023
Elliott Hughesae80b492012-04-24 10:43:17 -07003024void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3025 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3026 if (force_copy) {
3027 os << " (with forcecopy)";
3028 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003029 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003030 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003031 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003032 os << "; pins=" << pin_table.Size();
3033 }
3034 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003035 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003036 os << "; globals=" << globals.Capacity();
3037 }
3038 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003039 MutexLock mu(self, weak_globals_lock_);
3040 if (weak_globals_.Capacity() > 0) {
3041 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003042 }
3043 }
3044 os << '\n';
3045
3046 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003047 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003048 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3049 }
3050}
3051
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003052void JavaVMExt::DisallowNewWeakGlobals() {
3053 MutexLock mu(Thread::Current(), weak_globals_lock_);
3054 allow_new_weak_globals_ = false;
3055}
3056
3057void JavaVMExt::AllowNewWeakGlobals() {
3058 Thread* self = Thread::Current();
3059 MutexLock mu(self, weak_globals_lock_);
3060 allow_new_weak_globals_ = true;
3061 weak_globals_add_condition_.Broadcast(self);
3062}
3063
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003064mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3065 MutexLock mu(self, weak_globals_lock_);
3066 while (UNLIKELY(!allow_new_weak_globals_)) {
3067 weak_globals_add_condition_.WaitHoldingLocks(self);
3068 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003069 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003070}
3071
Elliott Hughes73e66f72012-05-09 09:34:45 -07003072void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003073 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003074 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003075 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003076 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003077 }
3078 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003079 MutexLock mu(self, weak_globals_lock_);
3080 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003081 }
3082 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003083 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003084 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003085 }
3086}
3087
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003088bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003089 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003090 std::string* detail) {
3091 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003092
3093 // See if we've already loaded this library. If we have, and the class loader
3094 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003095 // TODO: for better results we should canonicalize the pathname (or even compare
3096 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003097 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003098 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003099 {
3100 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003101 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003102 library = libraries->Get(path);
3103 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003104 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003105 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003106 // The library will be associated with class_loader. The JNI
3107 // spec says we can't load the same library into more than one
3108 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003109 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003110 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003111 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003112 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003113 return false;
3114 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003115 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003116 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003117 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003118 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003119 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003120 return false;
3121 }
3122 return true;
3123 }
3124
3125 // Open the shared library. Because we're using a full path, the system
3126 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3127 // resolve this library's dependencies though.)
3128
3129 // Failures here are expected when java.library.path has several entries
3130 // and we have to hunt for the lib.
3131
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003132 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3133 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3134 // dlopen) becomes zero from dlclose.
3135
Elliott Hughescdf53122011-08-19 15:46:09 -07003136 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003137 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003138 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003139 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003140 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003141
Elliott Hughes84b2f142012-09-27 09:16:28 -07003142 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003143
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003144 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003145 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003146 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003147 return false;
3148 }
3149
3150 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003151 // TODO: move the locking (and more of this logic) into Libraries.
3152 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003153 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003154 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003155 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003156 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003157 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003158 libraries->Put(path, library);
3159 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003160 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003161 }
3162 if (!created_library) {
3163 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003164 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003165 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003166 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003167
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003168 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3169 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003170
Elliott Hughes79353722013-08-02 16:52:18 -07003171 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003172 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003173 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003174 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003175 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003176 } else {
3177 // Call JNI_OnLoad. We have to override the current class
3178 // loader, which will always be "null" since the stuff at the
3179 // top of the stack is around Runtime.loadLibrary(). (See
3180 // the comments in the JNI FindClass function.)
3181 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3182 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003183 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003184 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003185
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003186 int version = 0;
3187 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003188 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003189 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003190 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003191 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003192
Mathieu Chartier590fee92013-09-13 13:46:47 -07003193 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003194
Elliott Hughes79353722013-08-02 16:52:18 -07003195 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003196 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003197 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003198 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003199 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003200 // It's unwise to call dlclose() here, but we can mark it
3201 // as bad and ensure that future load attempts will fail.
3202 // We don't know how far JNI_OnLoad got, so there could
3203 // be some partially-initialized stuff accessible through
3204 // newly-registered native method calls. We could try to
3205 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003206 } else {
3207 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003208 }
Elliott Hughes79353722013-08-02 16:52:18 -07003209 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003210 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003211 }
3212
Elliott Hughes79353722013-08-02 16:52:18 -07003213 library->SetResult(was_successful);
3214 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003215}
3216
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003217void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003218 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003219 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003220 // If this is a static method, it could be called before the class
3221 // has been initialized.
3222 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003223 c = EnsureInitialized(Thread::Current(), c);
3224 if (c == nullptr) {
3225 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003226 }
3227 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003228 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003229 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003230 std::string detail;
3231 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003232 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003233 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003234 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003235 native_method = libraries->FindNativeMethod(m, detail);
3236 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003237 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003238 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003239 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3240 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003241 }
3242 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003243}
3244
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003245void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003246 MutexLock mu(Thread::Current(), weak_globals_lock_);
3247 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003248 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003249 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003250 if (new_obj == nullptr) {
3251 new_obj = kClearedJniWeakGlobal;
3252 }
3253 *entry = new_obj;
3254 }
3255}
3256
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003257void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003258 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003259 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003260 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003261 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003262 }
3263 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003264 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003265 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003266 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003267 {
3268 MutexLock mu(self, libraries_lock);
3269 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003270 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003271 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003272 // The weak_globals table is visited by the GC itself (because it mutates the table).
3273}
3274
Elliott Hughesc8fece32013-01-02 11:27:23 -08003275void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003276 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003277 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003278 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003279 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3280 }
3281 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3282}
3283
Ian Rogersdf20fe02011-07-20 20:34:16 -07003284} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003285
3286std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3287 switch (rhs) {
3288 case JNIInvalidRefType:
3289 os << "JNIInvalidRefType";
3290 return os;
3291 case JNILocalRefType:
3292 os << "JNILocalRefType";
3293 return os;
3294 case JNIGlobalRefType:
3295 os << "JNIGlobalRefType";
3296 return os;
3297 case JNIWeakGlobalRefType:
3298 os << "JNIWeakGlobalRefType";
3299 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003300 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003301 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003302 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003303 }
3304}