blob: 43db7eccb0e3b11b4833f8c68b7bf3b96eaf85c9 [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
Elliott Hughescdf53122011-08-19 15:46:09 -0700553 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700554 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800556 jobject art_method = env->GetObjectField(
557 java_method, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
558 mirror::ArtMethod* method = soa.Decode<mirror::ArtMethod*>(art_method);
559 DCHECK(method != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700560 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700561 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700562
Elliott Hughescdf53122011-08-19 15:46:09 -0700563 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700564 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700566 jobject art_field = env->GetObjectField(java_field,
567 WellKnownClasses::java_lang_reflect_Field_artField);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800568 mirror::ArtField* field = soa.Decode<mirror::ArtField*>(art_field);
569 DCHECK(field != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700570 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700571 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700572
Elliott Hughescdf53122011-08-19 15:46:09 -0700573 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700574 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800576 mirror::ArtMethod* m = soa.DecodeMethod(mid);
577 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700578 jobject art_method = soa.AddLocalReference<jobject>(m);
579 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
580 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800581 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700582 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800583 SetObjectField(env, reflect_method,
584 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700585 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700587
Elliott Hughescdf53122011-08-19 15:46:09 -0700588 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700589 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 jobject art_field = soa.AddLocalReference<jobject>(f);
593 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
594 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800595 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700596 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800597 SetObjectField(env, reflect_field,
598 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700600 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700601
Elliott Hughes37f7a402011-08-22 18:56:01 -0700602 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700603 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800605 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700607 }
608
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700609 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700610 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700615
Elliott Hughes37f7a402011-08-22 18:56:01 -0700616 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700617 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
618 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800620 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
621 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700622 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughese84278b2012-03-22 10:06:53 -0700625 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700626 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800627 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700628 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700629 return JNI_TRUE;
630 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
633 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700634 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700635 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700637
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800640 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
641 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700642 return JNI_ERR;
643 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800644 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
645 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700646 return JNI_OK;
647 }
648
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700649 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700650 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800651 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700652 }
653
654 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700655 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700656 }
657
658 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700659 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700660 }
661
662 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700664
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800665 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
666 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
667 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 uint32_t old_throw_dex_pc;
669 {
670 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800671 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 old_throw_this_object.reset(old_throw_location.GetThis());
673 old_throw_method.reset(old_throw_location.GetMethod());
674 old_exception.reset(old_exception_obj);
675 old_throw_dex_pc = old_throw_location.GetDexPc();
676 soa.Self()->ClearException();
677 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800678 ScopedLocalRef<jthrowable> exception(env,
679 soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700680 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
681 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800682 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700683 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800684 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700685 } else {
686 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800688 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700689 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800690 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700691 }
692 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800693 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
694 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700695
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700698
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800701 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
704
Ian Rogers25e8b912012-09-07 11:31:36 -0700705 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 LOG(FATAL) << "JNI FatalError called: " << msg;
707 }
708
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700709 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800710 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700711 return JNI_ERR;
712 }
Ian Rogersef28b142012-11-30 14:22:18 -0800713 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 return JNI_OK;
715 }
716
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700717 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800719 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 soa.Env()->PopFrame();
721 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
723
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700724 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800725 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700726 }
727
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800730 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800731 // Check for null after decoding the object to handle cleared weak globals.
732 if (decoded_obj == nullptr) {
733 return nullptr;
734 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700737 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 return reinterpret_cast<jobject>(ref);
740 }
741
742 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800743 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 return;
745 }
Ian Rogersef28b142012-11-30 14:22:18 -0800746 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800748 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700749 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700750
751 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
752 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
753 << "failed to find entry";
754 }
755 }
756
757 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800759 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 }
761
762 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700763 if (obj != nullptr) {
764 ScopedObjectAccess soa(env);
765 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
767 }
768
769 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700770 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800771 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800772 // Check for null after decoding the object to handle cleared weak globals.
773 if (decoded_obj == nullptr) {
774 return nullptr;
775 }
776 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
778
779 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800780 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 return;
782 }
Ian Rogersef28b142012-11-30 14:22:18 -0800783 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700784
Ian Rogersef28b142012-11-30 14:22:18 -0800785 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 if (!locals.Remove(cookie, obj)) {
787 // Attempting to delete a local reference that is not in the
788 // topmost local reference frame is a no-op. DeleteLocalRef returns
789 // void and doesn't throw any exceptions, but we should probably
790 // complain about it so the user will notice that things aren't
791 // going quite the way they expect.
792 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
793 << "failed to find entry";
794 }
795 }
796
797 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700798 if (obj1 == obj2) {
799 return JNI_TRUE;
800 } else {
801 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800802 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
803 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700804 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 }
806
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700807 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700808 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800810 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800811 if (c == nullptr) {
812 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700813 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700814 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
816
Ian Rogersbc939662013-08-15 10:26:54 -0700817 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700819 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700820 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
821 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
822 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 va_end(args);
824 return result;
825 }
826
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700828 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
829 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700830 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800831 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800832 if (c == nullptr) {
833 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700834 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800835 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800836 if (result == nullptr) {
837 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700838 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700840 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800841 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800842 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700843 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800844 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 }
846
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700848 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
849 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800851 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800852 if (c == nullptr) {
853 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700854 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800855 mirror::Object* result = c->AllocObject(soa.Self());
856 if (result == nullptr) {
857 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700858 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700860 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800861 if (soa.Self()->IsExceptionPending()) {
862 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700863 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800864 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
Ian Rogersbc939662013-08-15 10:26:54 -0700867 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
868 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
869 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
870 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700872 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874
Ian Rogersbc939662013-08-15 10:26:54 -0700875 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
876 const char* sig) {
877 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
878 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
879 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700881 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 }
883
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700885 va_list ap;
886 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700887 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
888 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700889 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700890 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 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 CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700896 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
897 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
899 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
900 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700904 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
905 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700907 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
908 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 va_list ap;
914 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700915 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
916 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700917 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700920 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700924 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
925 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 ScopedObjectAccess soa(env);
927 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700931 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
932 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700934 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
935 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 }
937
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_list ap;
940 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700941 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
942 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
943 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700946 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 }
948
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700950 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
951 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 ScopedObjectAccess soa(env);
953 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700957 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
958 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700960 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
961 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_list ap;
966 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700967 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
968 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700969 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700972 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700976 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
977 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 ScopedObjectAccess soa(env);
979 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700983 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
984 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700986 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
987 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700993 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
994 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700995 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700998 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001002 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1003 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001009 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1010 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001012 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1013 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_list ap;
1018 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001019 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1020 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1021 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001024 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001028 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1029 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 ScopedObjectAccess soa(env);
1031 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001035 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1036 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001038 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1039 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_list ap;
1044 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001045 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1046 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001047 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001050 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001054 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1055 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 ScopedObjectAccess soa(env);
1057 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001061 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1062 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001064 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1065 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_list ap;
1070 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001071 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1072 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001073 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001076 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001080 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1081 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 ScopedObjectAccess soa(env);
1083 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001087 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1088 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001090 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1091 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_list ap;
1096 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001097 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1098 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001099 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001102 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001106 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1107 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 ScopedObjectAccess soa(env);
1109 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001113 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1114 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001116 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1117 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_list ap;
1122 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001123 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1124 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001125 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001126 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001131 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1132 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 ScopedObjectAccess soa(env);
1134 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001138 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1139 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001141 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001144 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001147 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1148 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001149 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001150 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1151 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 va_end(ap);
1153 return local_result;
1154 }
1155
Ian Rogersbc939662013-08-15 10:26:54 -07001156 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1157 va_list args) {
1158 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1159 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(env);
1161 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1162 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Ian Rogersbc939662013-08-15 10:26:54 -07001165 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1166 jvalue* args) {
1167 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1168 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001170 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001171 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Ian Rogersbc939662013-08-15 10:26:54 -07001174 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1175 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001178 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1179 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001180 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001183 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
Ian Rogersbc939662013-08-15 10:26:54 -07001186 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1187 va_list args) {
1188 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1189 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 ScopedObjectAccess soa(env);
1191 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Ian Rogersbc939662013-08-15 10:26:54 -07001194 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1195 jvalue* args) {
1196 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1197 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001199 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001202 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001205 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1206 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001207 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001210 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Ian Rogersbc939662013-08-15 10:26:54 -07001213 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1214 va_list args) {
1215 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1216 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
1218 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Ian Rogersbc939662013-08-15 10:26:54 -07001221 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1222 jvalue* args) {
1223 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1224 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001226 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001232 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1233 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1234 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001237 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Ian Rogersbc939662013-08-15 10:26:54 -07001240 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1241 va_list args) {
1242 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1243 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 ScopedObjectAccess soa(env);
1245 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Ian Rogersbc939662013-08-15 10:26:54 -07001248 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1249 jvalue* args) {
1250 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1251 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001253 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001256 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001259 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1260 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001261 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001264 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
Ian Rogersbc939662013-08-15 10:26:54 -07001267 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1268 va_list args) {
1269 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1270 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001271 ScopedObjectAccess soa(env);
1272 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
Ian Rogersbc939662013-08-15 10:26:54 -07001275 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1276 jvalue* args) {
1277 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1278 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001279 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001280 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001283 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001286 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1287 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001288 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001291 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Ian Rogersbc939662013-08-15 10:26:54 -07001294 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1295 va_list args) {
1296 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1297 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001298 ScopedObjectAccess soa(env);
1299 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Ian Rogersbc939662013-08-15 10:26:54 -07001302 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1303 jvalue* args) {
1304 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1305 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001306 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001307 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001313 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1314 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001315 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001316 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001318 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Ian Rogersbc939662013-08-15 10:26:54 -07001321 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1322 va_list args) {
1323 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1324 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001325 ScopedObjectAccess soa(env);
1326 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Ian Rogersbc939662013-08-15 10:26:54 -07001329 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1330 jvalue* args) {
1331 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1332 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001333 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001334 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001340 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1341 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001342 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001343 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001345 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
Ian Rogersbc939662013-08-15 10:26:54 -07001348 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1349 va_list args) {
1350 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1351 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001352 ScopedObjectAccess soa(env);
1353 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Ian Rogersbc939662013-08-15 10:26:54 -07001356 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1357 jvalue* args) {
1358 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1359 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001360 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001361 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001364 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001367 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1368 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001369 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001370 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001372 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Ian Rogersbc939662013-08-15 10:26:54 -07001375 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1376 va_list args) {
1377 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1378 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001379 ScopedObjectAccess soa(env);
1380 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Ian Rogersbc939662013-08-15 10:26:54 -07001383 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1384 jvalue* args) {
1385 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1386 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001387 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001388 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001393 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001394 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1395 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001396 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001397 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 va_end(ap);
1399 }
1400
Brian Carlstromea46f952013-07-30 01:26:50 -07001401 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1402 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001403 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1404 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001405 ScopedObjectAccess soa(env);
1406 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
Ian Rogersbc939662013-08-15 10:26:54 -07001409 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1410 jvalue* args) {
1411 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1412 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001414 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Ian Rogersbc939662013-08-15 10:26:54 -07001417 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1418 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1419 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1420 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001422 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001424
Ian Rogersbc939662013-08-15 10:26:54 -07001425 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1426 const char* sig) {
1427 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1428 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1429 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001431 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001435 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1436 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001438 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1439 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001440 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001444 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001446 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001447 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001451 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1452 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001454 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1455 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>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001461 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001462 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001463 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1464 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001465 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001469 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1470 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001471 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001472 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1473 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001474 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001476#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001477 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001478 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001479 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001480 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001483 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1484 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001485 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001486 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1487 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001488 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001489
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001490#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001491 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001492 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001493 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001494 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001497 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001501 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001505 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001509 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001513 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001517 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001521 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001525 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001532 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001533 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001537 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001549 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 }
1559
1560 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 }
1563
1564 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 }
1567
1568 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 }
1571
1572 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 }
1575
1576 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 }
1579
1580 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 }
1583
1584 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 }
1587
1588 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 }
1591
1592 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 }
1595
1596 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 }
1599
1600 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 }
1603
1604 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 }
1607
1608 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 }
1611
1612 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 }
1615
1616 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 }
1619
1620 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001627 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001628 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001629 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001630 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_end(ap);
1632 return local_result;
1633 }
1634
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001635 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001636 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001637 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001638 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001639 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001643 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001645 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001652 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001653 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001654 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001656 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001659 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001660 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001662 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001665 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001666 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001668 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001671 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001674 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001675 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001676 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001678 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001681 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001682 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001684 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001687 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001688 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001690 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001696 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001697 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001698 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001700 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001703 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001704 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001705 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001706 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001709 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001710 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001711 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001712 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001715 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001718 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001719 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001720 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001722 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001726 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001728 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001732 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001733 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001734 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001737 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001740 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001742 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001744 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 }
1746
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001747 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001748 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001749 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001750 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001753 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001754 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001755 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001756 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001759 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001761 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001762 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001764 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001766 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001769 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001770 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001772 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001775 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001776 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001777 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001778 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001783 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001784 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001785 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001786 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001788 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001791 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001792 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001794 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001797 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001798 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001799 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001800 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001803 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001805 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001806 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001810 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001813 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001814 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001815 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001816 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001820 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001822 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001827 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001828 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001829 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001830 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 va_end(ap);
1832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001835 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001837 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001840 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001841 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001843 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
Elliott Hughes814e4032011-08-23 12:07:56 -07001846 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001847 if (UNLIKELY(char_count < 0)) {
1848 JniAbortF("NewString", "char_count < 0: %d", char_count);
1849 return nullptr;
1850 }
1851 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1852 JniAbortF("NewString", "chars == null && char_count > 0");
1853 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001854 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001855 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001857 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
1860 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001861 if (utf == nullptr) {
1862 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001864 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001865 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes814e4032011-08-23 12:07:56 -07001869 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001870 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001872 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 }
1874
1875 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001876 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001877 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001878 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001879 }
1880
Ian Rogersbc939662013-08-15 10:26:54 -07001881 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1882 jchar* buf) {
1883 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001885 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001886 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001888 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001889 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001890 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1891 memcpy(buf, chars + start, length * sizeof(jchar));
1892 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001893 }
1894
Ian Rogersbc939662013-08-15 10:26:54 -07001895 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1896 char* buf) {
1897 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001899 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001900 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001902 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001903 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001904 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1905 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1906 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001907 }
1908
Elliott Hughes75770752011-08-24 17:52:38 -07001909 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00001910 CHECK_NON_NULL_ARGUMENT(GetStringChars, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001912 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1913 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001914 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001915 if (is_copy != nullptr) {
1916 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001917 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001918 int32_t char_count = s->GetLength();
1919 int32_t offset = s->GetOffset();
1920 jchar* bytes = new jchar[char_count + 1];
1921 for (int32_t i = 0; i < char_count; i++) {
1922 bytes[i] = chars->Get(i + offset);
1923 }
1924 bytes[char_count] = '\0';
1925 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 }
1927
Mathieu Chartier590fee92013-09-13 13:46:47 -07001928 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00001929 CHECK_NON_NULL_ARGUMENT(ReleaseStringChars, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001930 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001931 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001932 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001936 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes75770752011-08-24 17:52:38 -07001939 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001940 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001944 if (java_string == nullptr) {
1945 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001946 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001947 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001948 *is_copy = JNI_TRUE;
1949 }
Ian Rogersef28b142012-11-30 14:22:18 -08001950 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001951 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001952 size_t byte_count = s->GetUtfLength();
1953 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001954 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001955 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1956 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1957 bytes[byte_count] = '\0';
1958 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001962 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001963 }
1964
Elliott Hughesbd935992011-08-22 11:59:34 -07001965 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07001966 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001968 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001969 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001970 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1971 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001972 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001973 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07001977 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001979 mirror::ObjectArray<mirror::Object>* array =
1980 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Ian Rogersbc939662013-08-15 10:26:54 -07001984 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1985 jobject java_value) {
1986 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001988 mirror::ObjectArray<mirror::Object>* array =
1989 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1990 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001991 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
1994 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001996 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
1999 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002000 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002001 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
2004 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002006 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
2009 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002010 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002011 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
2014 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002016 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
2019 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002020 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002021 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
2024 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002026 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Ian Rogers1d99e452014-01-02 17:36:41 -08002029 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2030 jobject initial_element) {
2031 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002032 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002033 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002034 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002035
2036 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002037 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002038 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002039 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002041 if (UNLIKELY(element_class->IsPrimitive())) {
2042 JniAbortF("NewObjectArray", "not an object type: %s",
2043 PrettyDescriptor(element_class).c_str());
2044 return nullptr;
2045 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002046 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002047 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002048 if (UNLIKELY(array_class == nullptr)) {
2049 return nullptr;
2050 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002054 mirror::ObjectArray<mirror::Object>* result =
2055 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002056 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002057 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002058 if (initial_object != nullptr) {
2059 mirror::Class* element_class = result->GetClass()->GetComponentType();
2060 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2061 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2062 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2063 PrettyDescriptor(element_class).c_str());
2064
2065 } else {
2066 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002067 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002068 }
2069 }
Elliott Hughes75770752011-08-24 17:52:38 -07002070 }
2071 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002072 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
2075 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002076 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002077 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Ian Rogersa15e67d2012-02-28 13:51:55 -08002080 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002081 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002083 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002084 gc::Heap* heap = Runtime::Current()->GetHeap();
2085 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002086 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002087 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002088 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002089 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002090 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002091 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002092 *is_copy = JNI_FALSE;
2093 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002094 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002095 }
2096
Mathieu Chartier590fee92013-09-13 13:46:47 -07002097 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002098 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002099 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002100 }
2101
Elliott Hughes75770752011-08-24 17:52:38 -07002102 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002103 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002105 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes75770752011-08-24 17:52:38 -07002108 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002109 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002110 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002111 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes75770752011-08-24 17:52:38 -07002114 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002115 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002117 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes75770752011-08-24 17:52:38 -07002120 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002121 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002123 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes75770752011-08-24 17:52:38 -07002126 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002127 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002129 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes75770752011-08-24 17:52:38 -07002132 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002133 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002134 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002135 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes75770752011-08-24 17:52:38 -07002138 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002139 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002140 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002141 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Elliott Hughes75770752011-08-24 17:52:38 -07002144 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002145 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002147 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Mathieu Chartier590fee92013-09-13 13:46:47 -07002150 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2151 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 ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2156 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Mathieu Chartier590fee92013-09-13 13:46:47 -07002159 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2160 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Mathieu Chartier590fee92013-09-13 13:46:47 -07002163 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2164 jint mode) {
2165 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Mathieu Chartier590fee92013-09-13 13:46:47 -07002168 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2169 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 ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2174 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Mathieu Chartier590fee92013-09-13 13:46:47 -07002177 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2178 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Mathieu Chartier590fee92013-09-13 13:46:47 -07002181 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2182 jint mode) {
2183 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
2185
Ian Rogersbc939662013-08-15 10:26:54 -07002186 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2187 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002188 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002189 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2190 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Ian Rogersbc939662013-08-15 10:26:54 -07002193 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2194 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002195 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002196 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Ian Rogersbc939662013-08-15 10:26:54 -07002199 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2200 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002201 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002202 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 }
2204
Ian Rogersbc939662013-08-15 10:26:54 -07002205 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2206 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002207 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002208 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2209 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Ian Rogersbc939662013-08-15 10:26:54 -07002212 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2213 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002214 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002215 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2216 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Ian Rogersbc939662013-08-15 10:26:54 -07002219 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2220 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002221 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002222 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Ian Rogersbc939662013-08-15 10:26:54 -07002225 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2226 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002227 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002228 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Ian Rogersbc939662013-08-15 10:26:54 -07002231 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2232 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002233 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002234 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2235 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Ian Rogersbc939662013-08-15 10:26:54 -07002238 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2239 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002241 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2242 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
Ian Rogersbc939662013-08-15 10:26:54 -07002245 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2246 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002247 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002248 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 }
2250
Ian Rogersbc939662013-08-15 10:26:54 -07002251 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2252 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002253 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002254 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 }
2256
Ian Rogersbc939662013-08-15 10:26:54 -07002257 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2258 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002259 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002260 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2261 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Ian Rogersbc939662013-08-15 10:26:54 -07002264 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2265 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002266 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002267 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2268 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Ian Rogersbc939662013-08-15 10:26:54 -07002271 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2272 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002274 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Ian Rogersbc939662013-08-15 10:26:54 -07002277 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2278 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002279 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002280 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Ian Rogersbc939662013-08-15 10:26:54 -07002283 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2284 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002286 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2287 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 }
2289
Ian Rogersbc939662013-08-15 10:26:54 -07002290 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2291 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002292 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2293 }
2294
Ian Rogersbc939662013-08-15 10:26:54 -07002295 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2296 jint method_count, bool return_errors) {
2297 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002298 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002299 return JNI_ERR; // Not reached.
2300 }
2301 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002302 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002303 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002304 if (UNLIKELY(method_count == 0)) {
2305 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2306 << PrettyDescriptor(c);
2307 return JNI_OK;
2308 }
2309 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2310 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 const char* name = methods[i].name;
2312 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002313 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002315 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 ++sig;
2317 }
2318
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002319 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2320 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002321 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002322 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002323 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002324 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002325 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002326 << PrettyDescriptor(c) << "." << name << sig << " in "
2327 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002328 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002330 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002331 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002332 << PrettyDescriptor(c) << "." << name << sig
2333 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002334 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002335 return JNI_ERR;
2336 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002337
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002338 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002339
Ian Rogers1eb512d2013-10-18 15:42:20 -07002340 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002341 }
2342 return JNI_OK;
2343 }
2344
Elliott Hughes5174fe62011-08-23 15:12:35 -07002345 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002346 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002347 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002348 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002349
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002350 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002351
2352 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002353 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002354 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002355 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002356 }
2357 }
2358 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002359 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002360 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002361 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002362 }
2363 }
2364
2365 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002366 }
2367
Ian Rogers719d1a32014-03-06 12:13:39 -08002368 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogersbc939662013-08-15 10:26:54 -07002369 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002370 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002371 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2372 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002373 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002374 return JNI_ERR;
2375 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002376 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002377 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002378 }
2379
Ian Rogers719d1a32014-03-06 12:13:39 -08002380 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogersbc939662013-08-15 10:26:54 -07002381 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002382 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002383 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 o->MonitorExit(soa.Self());
2385 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002386 return JNI_ERR;
2387 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002388 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002389 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002390 }
2391
2392 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002393 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002394 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002395 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002396 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002397 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002398 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002399 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002400 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002401 }
2402
Elliott Hughescdf53122011-08-19 15:46:09 -07002403 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002404 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002405 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002406 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002407 if (address == nullptr && capacity != 0) {
2408 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002409 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002410
Ian Rogers936b37f2014-02-14 00:52:24 -08002411 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002412 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002413 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002414 jint capacity_arg = static_cast<jint>(capacity);
2415
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002416 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2417 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002418 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002419 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002420 }
2421
Elliott Hughesb465ab02011-08-24 11:21:21 -07002422 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002423 return reinterpret_cast<void*>(env->GetLongField(
2424 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002425 }
2426
Elliott Hughesb465ab02011-08-24 11:21:21 -07002427 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002428 return static_cast<jlong>(env->GetIntField(
2429 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002430 }
2431
Elliott Hughesb465ab02011-08-24 11:21:21 -07002432 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002433 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002434
2435 // Do we definitely know what kind of reference this is?
2436 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2437 IndirectRefKind kind = GetIndirectRefKind(ref);
2438 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002439 case kLocal: {
2440 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002441 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002442 return JNILocalRefType;
2443 }
2444 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002445 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002446 case kGlobal:
2447 return JNIGlobalRefType;
2448 case kWeakGlobal:
2449 return JNIWeakGlobalRefType;
2450 case kSirtOrInvalid:
2451 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002452 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002453 return JNILocalRefType;
2454 }
2455
Ian Rogersef28b142012-11-30 14:22:18 -08002456 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002457 return JNIInvalidRefType;
2458 }
2459
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002460 // If we're handing out direct pointers, check whether it's a direct pointer to a local
2461 // reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002462 {
2463 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002464 if (soa.Decode<mirror::Object*>(java_object) ==
2465 reinterpret_cast<mirror::Object*>(java_object)) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002466 mirror::Object* object = reinterpret_cast<mirror::Object*>(java_object);
2467 if (soa.Env()->locals.ContainsDirectPointer(object)) {
Ian Rogersef28b142012-11-30 14:22:18 -08002468 return JNILocalRefType;
2469 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002470 }
2471 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002472 return JNIInvalidRefType;
2473 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002474 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2475 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002476 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002477
2478 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002479 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2480 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002481 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002482 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2484 return JNI_ERR;
2485 }
2486 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002487 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002488 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2489 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002490 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002491 soa.Self()->ThrowOutOfMemoryError(caller);
2492 }
2493 return okay ? JNI_OK : JNI_ERR;
2494 }
2495
2496 template<typename JniT, typename ArtT>
2497 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002498 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002499 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002500 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002501 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002502 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002503 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002504 return soa.AddLocalReference<JniT>(result);
2505 }
2506
2507 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2508 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2509 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002510 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002511 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2512 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002513 // Only make a copy if necessary.
2514 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2515 if (is_copy != nullptr) {
2516 *is_copy = JNI_TRUE;
2517 }
2518 static const size_t component_size = array->GetClass()->GetComponentSize();
2519 size_t size = array->GetLength() * component_size;
2520 void* data = new uint64_t[RoundUp(size, 8) / 8];
2521 memcpy(data, array->GetData(), size);
2522 return reinterpret_cast<CArrayT>(data);
2523 } else {
2524 if (is_copy != nullptr) {
2525 *is_copy = JNI_FALSE;
2526 }
2527 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002528 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002529 }
2530
Mathieu Chartier590fee92013-09-13 13:46:47 -07002531 template <typename ArrayT, typename ElementT>
2532 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2533 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002534 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002535 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002536 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002537 gc::Heap* heap = Runtime::Current()->GetHeap();
2538 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2539 size_t bytes = array->GetLength() * component_size;
2540 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2541 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002542 if (is_copy) {
2543 // Sanity check: If elements is not the same as the java array's data, it better not be a
2544 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2545 // copies we make?
2546 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2547 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2548 reinterpret_cast<void*>(elements), array_data);
2549 return;
2550 }
2551 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002552 // Don't need to copy if we had a direct pointer.
2553 if (mode != JNI_ABORT && is_copy) {
2554 memcpy(array_data, elements, bytes);
2555 }
2556 if (mode != JNI_COMMIT) {
2557 if (is_copy) {
2558 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002559 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002560 // Non copy to a movable object must means that we had disabled the moving GC.
2561 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002562 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002563 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002564 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002565 }
2566
2567 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2568 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2569 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002571 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002572 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2573 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2574 ThrowAIOOBE(soa, array, start, length, "src");
2575 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002576 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002577 JavaT* data = array->GetData();
2578 memcpy(buf, data + start, length * sizeof(JavaT));
2579 }
2580 }
2581
2582 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2583 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2584 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002585 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002586 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002587 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2588 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2589 ThrowAIOOBE(soa, array, start, length, "dst");
2590 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002591 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002592 JavaT* data = array->GetData();
2593 memcpy(data + start, buf, length * sizeof(JavaT));
2594 }
2595 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002596};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002597
Elliott Hughes88c5c352012-03-15 18:49:48 -07002598const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002599 nullptr, // reserved0.
2600 nullptr, // reserved1.
2601 nullptr, // reserved2.
2602 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002603 JNI::GetVersion,
2604 JNI::DefineClass,
2605 JNI::FindClass,
2606 JNI::FromReflectedMethod,
2607 JNI::FromReflectedField,
2608 JNI::ToReflectedMethod,
2609 JNI::GetSuperclass,
2610 JNI::IsAssignableFrom,
2611 JNI::ToReflectedField,
2612 JNI::Throw,
2613 JNI::ThrowNew,
2614 JNI::ExceptionOccurred,
2615 JNI::ExceptionDescribe,
2616 JNI::ExceptionClear,
2617 JNI::FatalError,
2618 JNI::PushLocalFrame,
2619 JNI::PopLocalFrame,
2620 JNI::NewGlobalRef,
2621 JNI::DeleteGlobalRef,
2622 JNI::DeleteLocalRef,
2623 JNI::IsSameObject,
2624 JNI::NewLocalRef,
2625 JNI::EnsureLocalCapacity,
2626 JNI::AllocObject,
2627 JNI::NewObject,
2628 JNI::NewObjectV,
2629 JNI::NewObjectA,
2630 JNI::GetObjectClass,
2631 JNI::IsInstanceOf,
2632 JNI::GetMethodID,
2633 JNI::CallObjectMethod,
2634 JNI::CallObjectMethodV,
2635 JNI::CallObjectMethodA,
2636 JNI::CallBooleanMethod,
2637 JNI::CallBooleanMethodV,
2638 JNI::CallBooleanMethodA,
2639 JNI::CallByteMethod,
2640 JNI::CallByteMethodV,
2641 JNI::CallByteMethodA,
2642 JNI::CallCharMethod,
2643 JNI::CallCharMethodV,
2644 JNI::CallCharMethodA,
2645 JNI::CallShortMethod,
2646 JNI::CallShortMethodV,
2647 JNI::CallShortMethodA,
2648 JNI::CallIntMethod,
2649 JNI::CallIntMethodV,
2650 JNI::CallIntMethodA,
2651 JNI::CallLongMethod,
2652 JNI::CallLongMethodV,
2653 JNI::CallLongMethodA,
2654 JNI::CallFloatMethod,
2655 JNI::CallFloatMethodV,
2656 JNI::CallFloatMethodA,
2657 JNI::CallDoubleMethod,
2658 JNI::CallDoubleMethodV,
2659 JNI::CallDoubleMethodA,
2660 JNI::CallVoidMethod,
2661 JNI::CallVoidMethodV,
2662 JNI::CallVoidMethodA,
2663 JNI::CallNonvirtualObjectMethod,
2664 JNI::CallNonvirtualObjectMethodV,
2665 JNI::CallNonvirtualObjectMethodA,
2666 JNI::CallNonvirtualBooleanMethod,
2667 JNI::CallNonvirtualBooleanMethodV,
2668 JNI::CallNonvirtualBooleanMethodA,
2669 JNI::CallNonvirtualByteMethod,
2670 JNI::CallNonvirtualByteMethodV,
2671 JNI::CallNonvirtualByteMethodA,
2672 JNI::CallNonvirtualCharMethod,
2673 JNI::CallNonvirtualCharMethodV,
2674 JNI::CallNonvirtualCharMethodA,
2675 JNI::CallNonvirtualShortMethod,
2676 JNI::CallNonvirtualShortMethodV,
2677 JNI::CallNonvirtualShortMethodA,
2678 JNI::CallNonvirtualIntMethod,
2679 JNI::CallNonvirtualIntMethodV,
2680 JNI::CallNonvirtualIntMethodA,
2681 JNI::CallNonvirtualLongMethod,
2682 JNI::CallNonvirtualLongMethodV,
2683 JNI::CallNonvirtualLongMethodA,
2684 JNI::CallNonvirtualFloatMethod,
2685 JNI::CallNonvirtualFloatMethodV,
2686 JNI::CallNonvirtualFloatMethodA,
2687 JNI::CallNonvirtualDoubleMethod,
2688 JNI::CallNonvirtualDoubleMethodV,
2689 JNI::CallNonvirtualDoubleMethodA,
2690 JNI::CallNonvirtualVoidMethod,
2691 JNI::CallNonvirtualVoidMethodV,
2692 JNI::CallNonvirtualVoidMethodA,
2693 JNI::GetFieldID,
2694 JNI::GetObjectField,
2695 JNI::GetBooleanField,
2696 JNI::GetByteField,
2697 JNI::GetCharField,
2698 JNI::GetShortField,
2699 JNI::GetIntField,
2700 JNI::GetLongField,
2701 JNI::GetFloatField,
2702 JNI::GetDoubleField,
2703 JNI::SetObjectField,
2704 JNI::SetBooleanField,
2705 JNI::SetByteField,
2706 JNI::SetCharField,
2707 JNI::SetShortField,
2708 JNI::SetIntField,
2709 JNI::SetLongField,
2710 JNI::SetFloatField,
2711 JNI::SetDoubleField,
2712 JNI::GetStaticMethodID,
2713 JNI::CallStaticObjectMethod,
2714 JNI::CallStaticObjectMethodV,
2715 JNI::CallStaticObjectMethodA,
2716 JNI::CallStaticBooleanMethod,
2717 JNI::CallStaticBooleanMethodV,
2718 JNI::CallStaticBooleanMethodA,
2719 JNI::CallStaticByteMethod,
2720 JNI::CallStaticByteMethodV,
2721 JNI::CallStaticByteMethodA,
2722 JNI::CallStaticCharMethod,
2723 JNI::CallStaticCharMethodV,
2724 JNI::CallStaticCharMethodA,
2725 JNI::CallStaticShortMethod,
2726 JNI::CallStaticShortMethodV,
2727 JNI::CallStaticShortMethodA,
2728 JNI::CallStaticIntMethod,
2729 JNI::CallStaticIntMethodV,
2730 JNI::CallStaticIntMethodA,
2731 JNI::CallStaticLongMethod,
2732 JNI::CallStaticLongMethodV,
2733 JNI::CallStaticLongMethodA,
2734 JNI::CallStaticFloatMethod,
2735 JNI::CallStaticFloatMethodV,
2736 JNI::CallStaticFloatMethodA,
2737 JNI::CallStaticDoubleMethod,
2738 JNI::CallStaticDoubleMethodV,
2739 JNI::CallStaticDoubleMethodA,
2740 JNI::CallStaticVoidMethod,
2741 JNI::CallStaticVoidMethodV,
2742 JNI::CallStaticVoidMethodA,
2743 JNI::GetStaticFieldID,
2744 JNI::GetStaticObjectField,
2745 JNI::GetStaticBooleanField,
2746 JNI::GetStaticByteField,
2747 JNI::GetStaticCharField,
2748 JNI::GetStaticShortField,
2749 JNI::GetStaticIntField,
2750 JNI::GetStaticLongField,
2751 JNI::GetStaticFloatField,
2752 JNI::GetStaticDoubleField,
2753 JNI::SetStaticObjectField,
2754 JNI::SetStaticBooleanField,
2755 JNI::SetStaticByteField,
2756 JNI::SetStaticCharField,
2757 JNI::SetStaticShortField,
2758 JNI::SetStaticIntField,
2759 JNI::SetStaticLongField,
2760 JNI::SetStaticFloatField,
2761 JNI::SetStaticDoubleField,
2762 JNI::NewString,
2763 JNI::GetStringLength,
2764 JNI::GetStringChars,
2765 JNI::ReleaseStringChars,
2766 JNI::NewStringUTF,
2767 JNI::GetStringUTFLength,
2768 JNI::GetStringUTFChars,
2769 JNI::ReleaseStringUTFChars,
2770 JNI::GetArrayLength,
2771 JNI::NewObjectArray,
2772 JNI::GetObjectArrayElement,
2773 JNI::SetObjectArrayElement,
2774 JNI::NewBooleanArray,
2775 JNI::NewByteArray,
2776 JNI::NewCharArray,
2777 JNI::NewShortArray,
2778 JNI::NewIntArray,
2779 JNI::NewLongArray,
2780 JNI::NewFloatArray,
2781 JNI::NewDoubleArray,
2782 JNI::GetBooleanArrayElements,
2783 JNI::GetByteArrayElements,
2784 JNI::GetCharArrayElements,
2785 JNI::GetShortArrayElements,
2786 JNI::GetIntArrayElements,
2787 JNI::GetLongArrayElements,
2788 JNI::GetFloatArrayElements,
2789 JNI::GetDoubleArrayElements,
2790 JNI::ReleaseBooleanArrayElements,
2791 JNI::ReleaseByteArrayElements,
2792 JNI::ReleaseCharArrayElements,
2793 JNI::ReleaseShortArrayElements,
2794 JNI::ReleaseIntArrayElements,
2795 JNI::ReleaseLongArrayElements,
2796 JNI::ReleaseFloatArrayElements,
2797 JNI::ReleaseDoubleArrayElements,
2798 JNI::GetBooleanArrayRegion,
2799 JNI::GetByteArrayRegion,
2800 JNI::GetCharArrayRegion,
2801 JNI::GetShortArrayRegion,
2802 JNI::GetIntArrayRegion,
2803 JNI::GetLongArrayRegion,
2804 JNI::GetFloatArrayRegion,
2805 JNI::GetDoubleArrayRegion,
2806 JNI::SetBooleanArrayRegion,
2807 JNI::SetByteArrayRegion,
2808 JNI::SetCharArrayRegion,
2809 JNI::SetShortArrayRegion,
2810 JNI::SetIntArrayRegion,
2811 JNI::SetLongArrayRegion,
2812 JNI::SetFloatArrayRegion,
2813 JNI::SetDoubleArrayRegion,
2814 JNI::RegisterNatives,
2815 JNI::UnregisterNatives,
2816 JNI::MonitorEnter,
2817 JNI::MonitorExit,
2818 JNI::GetJavaVM,
2819 JNI::GetStringRegion,
2820 JNI::GetStringUTFRegion,
2821 JNI::GetPrimitiveArrayCritical,
2822 JNI::ReleasePrimitiveArrayCritical,
2823 JNI::GetStringCritical,
2824 JNI::ReleaseStringCritical,
2825 JNI::NewWeakGlobalRef,
2826 JNI::DeleteWeakGlobalRef,
2827 JNI::ExceptionCheck,
2828 JNI::NewDirectByteBuffer,
2829 JNI::GetDirectBufferAddress,
2830 JNI::GetDirectBufferCapacity,
2831 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002832};
2833
Elliott Hughes75770752011-08-24 17:52:38 -07002834JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002835 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002836 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002837 local_ref_cookie(IRT_FIRST_SEGMENT),
2838 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002839 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002840 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002841 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002842 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002843 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002844 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002845 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002846}
2847
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002848JNIEnvExt::~JNIEnvExt() {
2849}
2850
Mathieu Chartier590fee92013-09-13 13:46:47 -07002851jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2852 if (obj == nullptr) {
2853 return nullptr;
2854 }
2855 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2856}
2857
2858void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2859 if (obj != nullptr) {
2860 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2861 }
2862}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002863void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2864 check_jni = enabled;
2865 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002866}
2867
Elliott Hughes73e66f72012-05-09 09:34:45 -07002868void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2869 locals.Dump(os);
2870 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002871}
2872
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002873void JNIEnvExt::PushFrame(int /*capacity*/) {
2874 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002875 stacked_local_ref_cookies.push_back(local_ref_cookie);
2876 local_ref_cookie = locals.GetSegmentState();
2877}
2878
2879void JNIEnvExt::PopFrame() {
2880 locals.SetSegmentState(local_ref_cookie);
2881 local_ref_cookie = stacked_local_ref_cookies.back();
2882 stacked_local_ref_cookies.pop_back();
2883}
2884
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002885Offset JNIEnvExt::SegmentStateOffset() {
2886 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2887 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2888}
2889
Carl Shapiroea4dca82011-08-01 13:45:38 -07002890// JNI Invocation interface.
2891
Brian Carlstrombddf9762013-05-14 11:35:37 -07002892extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002893 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002894 if (IsBadJniVersion(args->version)) {
2895 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002896 return JNI_EVERSION;
2897 }
2898 Runtime::Options options;
2899 for (int i = 0; i < args->nOptions; ++i) {
2900 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002901 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002902 }
2903 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002904 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002905 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002906 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002907 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002908 bool started = runtime->Start();
2909 if (!started) {
2910 delete Thread::Current()->GetJniEnv();
2911 delete runtime->GetJavaVM();
2912 LOG(WARNING) << "CreateJavaVM failed";
2913 return JNI_ERR;
2914 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002915 *p_env = Thread::Current()->GetJniEnv();
2916 *p_vm = runtime->GetJavaVM();
2917 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002918}
2919
Elliott Hughesf2682d52011-08-15 16:37:04 -07002920extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002921 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002922 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002923 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002924 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002925 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002926 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002927 }
2928 return JNI_OK;
2929}
2930
2931// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002932extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002933 return JNI_ERR;
2934}
2935
Elliott Hughescdf53122011-08-19 15:46:09 -07002936class JII {
2937 public:
2938 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002939 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002940 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002941 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002942 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2943 delete raw_vm->runtime;
2944 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002945 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002946
Elliott Hughescdf53122011-08-19 15:46:09 -07002947 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002948 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002949 }
2950
2951 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002952 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002953 }
2954
2955 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002956 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002957 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002958 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002959 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2960 Runtime* runtime = raw_vm->runtime;
2961 runtime->DetachCurrentThread();
2962 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002963 }
2964
2965 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002966 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2967 // and unlike other calls that take a JNI version doesn't care if you supply
2968 // JNI_VERSION_1_1, which we don't otherwise support.
2969 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002970 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002971 return JNI_EVERSION;
2972 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002973 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002974 return JNI_ERR;
2975 }
2976 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002977 if (thread == nullptr) {
2978 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002979 return JNI_EDETACHED;
2980 }
2981 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002982 return JNI_OK;
2983 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002984};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002985
Elliott Hughes88c5c352012-03-15 18:49:48 -07002986const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002987 nullptr, // reserved0
2988 nullptr, // reserved1
2989 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002990 JII::DestroyJavaVM,
2991 JII::AttachCurrentThread,
2992 JII::DetachCurrentThread,
2993 JII::GetEnv,
2994 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002995};
2996
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002997JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002998 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002999 check_jni_abort_hook(nullptr),
3000 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003001 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003002 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003003 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003004 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003005 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003006 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003007 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003008 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003009 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003010 libraries(new Libraries),
3011 weak_globals_lock_("JNI weak global reference table lock"),
3012 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3013 allow_new_weak_globals_(true),
3014 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003015 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003016 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003017 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003018 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003019}
3020
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003021JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003022 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003023}
3024
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003025jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3026 if (obj == nullptr) {
3027 return nullptr;
3028 }
3029 MutexLock mu(self, weak_globals_lock_);
3030 while (UNLIKELY(!allow_new_weak_globals_)) {
3031 weak_globals_add_condition_.WaitHoldingLocks(self);
3032 }
3033 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3034 return reinterpret_cast<jweak>(ref);
3035}
3036
3037void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3038 MutexLock mu(self, weak_globals_lock_);
3039 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3040 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3041 << "failed to find entry";
3042 }
3043}
3044
Elliott Hughes88c5c352012-03-15 18:49:48 -07003045void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3046 check_jni = enabled;
3047 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003048}
3049
Elliott Hughesae80b492012-04-24 10:43:17 -07003050void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3051 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3052 if (force_copy) {
3053 os << " (with forcecopy)";
3054 }
3055 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003056 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003057 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003058 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003059 os << "; pins=" << pin_table.Size();
3060 }
3061 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003062 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003063 os << "; globals=" << globals.Capacity();
3064 }
3065 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003066 MutexLock mu(self, weak_globals_lock_);
3067 if (weak_globals_.Capacity() > 0) {
3068 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003069 }
3070 }
3071 os << '\n';
3072
3073 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003074 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003075 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3076 }
3077}
3078
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003079void JavaVMExt::DisallowNewWeakGlobals() {
3080 MutexLock mu(Thread::Current(), weak_globals_lock_);
3081 allow_new_weak_globals_ = false;
3082}
3083
3084void JavaVMExt::AllowNewWeakGlobals() {
3085 Thread* self = Thread::Current();
3086 MutexLock mu(self, weak_globals_lock_);
3087 allow_new_weak_globals_ = true;
3088 weak_globals_add_condition_.Broadcast(self);
3089}
3090
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003091mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3092 MutexLock mu(self, weak_globals_lock_);
3093 while (UNLIKELY(!allow_new_weak_globals_)) {
3094 weak_globals_add_condition_.WaitHoldingLocks(self);
3095 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003096 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003097}
3098
Elliott Hughes73e66f72012-05-09 09:34:45 -07003099void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003100 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003101 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003102 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003103 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003104 }
3105 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003106 MutexLock mu(self, weak_globals_lock_);
3107 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003108 }
3109 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003110 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003111 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003112 }
3113}
3114
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003115bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003116 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003117 std::string* detail) {
3118 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003119
3120 // See if we've already loaded this library. If we have, and the class loader
3121 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003122 // TODO: for better results we should canonicalize the pathname (or even compare
3123 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003124 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003125 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003126 {
3127 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003128 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003129 library = libraries->Get(path);
3130 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003131 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003132 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003133 // The library will be associated with class_loader. The JNI
3134 // spec says we can't load the same library into more than one
3135 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003136 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003137 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003138 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003139 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003140 return false;
3141 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003142 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003143 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003144 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003145 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003146 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003147 return false;
3148 }
3149 return true;
3150 }
3151
3152 // Open the shared library. Because we're using a full path, the system
3153 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3154 // resolve this library's dependencies though.)
3155
3156 // Failures here are expected when java.library.path has several entries
3157 // and we have to hunt for the lib.
3158
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003159 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3160 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3161 // dlopen) becomes zero from dlclose.
3162
Elliott Hughescdf53122011-08-19 15:46:09 -07003163 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003164 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003165 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003166 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003167 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003168
Elliott Hughes84b2f142012-09-27 09:16:28 -07003169 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003170
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003171 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003172 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003173 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003174 return false;
3175 }
3176
3177 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003178 // TODO: move the locking (and more of this logic) into Libraries.
3179 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003180 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003181 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003182 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003183 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003184 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003185 libraries->Put(path, library);
3186 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003187 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003188 }
3189 if (!created_library) {
3190 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003191 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003192 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003193 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003194
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003195 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3196 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003197
Elliott Hughes79353722013-08-02 16:52:18 -07003198 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003199 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003200 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003201 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003202 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003203 } else {
3204 // Call JNI_OnLoad. We have to override the current class
3205 // loader, which will always be "null" since the stuff at the
3206 // top of the stack is around Runtime.loadLibrary(). (See
3207 // the comments in the JNI FindClass function.)
3208 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3209 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003210 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003211 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003212
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003213 int version = 0;
3214 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003215 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003216 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003217 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003218 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003219
Mathieu Chartier590fee92013-09-13 13:46:47 -07003220 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003221
Elliott Hughes79353722013-08-02 16:52:18 -07003222 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003223 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003224 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003225 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003226 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003227 // It's unwise to call dlclose() here, but we can mark it
3228 // as bad and ensure that future load attempts will fail.
3229 // We don't know how far JNI_OnLoad got, so there could
3230 // be some partially-initialized stuff accessible through
3231 // newly-registered native method calls. We could try to
3232 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003233 } else {
3234 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003235 }
Elliott Hughes79353722013-08-02 16:52:18 -07003236 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003237 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003238 }
3239
Elliott Hughes79353722013-08-02 16:52:18 -07003240 library->SetResult(was_successful);
3241 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003242}
3243
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003244void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003245 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003246 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003247 // If this is a static method, it could be called before the class
3248 // has been initialized.
3249 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003250 c = EnsureInitialized(Thread::Current(), c);
3251 if (c == nullptr) {
3252 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003253 }
3254 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003255 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003256 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003257 std::string detail;
3258 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003259 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003260 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003261 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003262 native_method = libraries->FindNativeMethod(m, detail);
3263 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003264 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003265 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003266 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3267 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003268 }
3269 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003270}
3271
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003272void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003273 MutexLock mu(Thread::Current(), weak_globals_lock_);
3274 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003275 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003276 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003277 if (new_obj == nullptr) {
3278 new_obj = kClearedJniWeakGlobal;
3279 }
3280 *entry = new_obj;
3281 }
3282}
3283
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003284void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003285 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003286 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003287 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003288 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003289 }
3290 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003291 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003292 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003293 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003294 {
3295 MutexLock mu(self, libraries_lock);
3296 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003297 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003298 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003299 // The weak_globals table is visited by the GC itself (because it mutates the table).
3300}
3301
Elliott Hughesc8fece32013-01-02 11:27:23 -08003302void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003303 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003304 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003305 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003306 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3307 }
3308 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3309}
3310
Ian Rogersdf20fe02011-07-20 20:34:16 -07003311} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003312
3313std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3314 switch (rhs) {
3315 case JNIInvalidRefType:
3316 os << "JNIInvalidRefType";
3317 return os;
3318 case JNILocalRefType:
3319 os << "JNILocalRefType";
3320 return os;
3321 case JNIGlobalRefType:
3322 os << "JNIGlobalRefType";
3323 return os;
3324 case JNIWeakGlobalRefType:
3325 os << "JNIWeakGlobalRefType";
3326 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003327 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003328 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003329 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003330 }
3331}