blob: b7dd6b05e09e68cca12f2a667072d476a8e69aa4 [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"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070032#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070033#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_field-inl.h"
36#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
38#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070041#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "object_utils.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080044#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070045#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070046#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070052#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070053#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070054
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070055namespace art {
56
Brian Carlstrom7934ac22013-07-26 10:54:15 -070057static const size_t kMonitorsInitial = 32; // Arbitrary.
58static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070059
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060static const size_t kLocalsInitial = 64; // Arbitrary.
61static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070062
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063static const size_t kPinTableInitial = 16; // Arbitrary.
64static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070065
Brian Carlstrom7934ac22013-07-26 10:54:15 -070066static size_t gGlobalsInitial = 512; // Arbitrary.
67static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070068
Brian Carlstrom7934ac22013-07-26 10:54:15 -070069static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
70static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070071
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080072static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070074 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070075}
76
Jeff Hao19c5d372013-03-15 14:33:43 -070077static bool IsBadJniVersion(int version) {
78 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
79 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
80}
81
Elliott Hughes6b436852011-08-12 10:16:44 -070082// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
83// separated with slashes but aren't wrapped with "L;" like regular descriptors
84// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
85// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
86// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070087static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070088 std::string result;
89 // Add the missing "L;" if necessary.
90 if (name[0] == '[') {
91 result = name;
92 } else {
93 result += 'L';
94 result += name;
95 result += ';';
96 }
97 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070098 if (result.find('.') != std::string::npos) {
99 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
100 << "\"" << name << "\"";
101 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700102 }
103 return result;
104}
105
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800106static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
110 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
111 "no %s method \"%s.%s%s\"",
112 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700113}
114
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800115static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 if (LIKELY(klass->IsInitialized())) {
118 return klass;
119 }
120 SirtRef<mirror::Class> sirt_klass(self, klass);
121 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
122 return nullptr;
123 }
124 return sirt_klass.get();
125}
126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
128 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800130 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800131 if (c == nullptr) {
132 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700133 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800134 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700135 if (is_static) {
136 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700137 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700138 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800139 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700140 // No virtual method matching the signature. Search declared
141 // private methods and constructors.
142 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700143 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700144 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800147 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700148 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700150}
151
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800152static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800154 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700155 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
156 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800157 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700158 }
Brian Carlstromce888532013-10-10 00:32:58 -0700159 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800160 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700161 return method->GetDeclaringClass()->GetClassLoader();
162 }
163 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800164 mirror::ClassLoader* class_loader =
165 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
166 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700167 return class_loader;
168 }
169 // See if the override ClassLoader is set for gtests.
170 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800171 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800172 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700173 CHECK(Runtime::Current()->UseCompileTimeClassPath());
174 return class_loader;
175 }
176 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800177 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700178}
179
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
181 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800183 SirtRef<mirror::Class> c(soa.Self(), EnsureInitialized(soa.Self(),
184 soa.Decode<mirror::Class*>(jni_class)));
185 if (c.get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800186 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700187 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800188 mirror::ArtField* field = nullptr;
189 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
191 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800192 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Ian Rogers98379392014-02-24 16:53:16 -0800193 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700194 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700196 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800197 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 ThrowLocation throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800201 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800204 "no type \"%s\" found and so no field \"%s\" "
205 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800206 ClassHelper(c.get()).GetDescriptor());
207 soa.Self()->GetException(nullptr)->SetCause(cause.get());
208 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 }
210 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800213 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800215 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
217 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
218 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800219 sig, name, ClassHelper(c.get()).GetDescriptor());
220 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700221 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700223}
224
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800225static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700228 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700229 vm->pin_table.Add(array);
230}
231
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800232static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700235 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700236 vm->pin_table.Remove(array);
237}
238
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800239static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700242 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800243 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
244 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
245 "%s offset=%d length=%d %s.length=%d",
246 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700247}
Ian Rogers0571d352011-11-03 19:51:38 -0700248
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
250 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
253 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
254 "offset=%d length=%d string.length()=%d", start, length,
255 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700256}
Elliott Hughes814e4032011-08-23 12:07:56 -0700257
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 // Turn the const char* into a java.lang.String.
261 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800262 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700264 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 // Choose an appropriate constructor and set up the arguments.
267 jvalue args[2];
268 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800269 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800271 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 signature = "(Ljava/lang/String;)V";
273 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800274 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 signature = "(Ljava/lang/Throwable;)V";
276 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700277 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
279 args[0].l = s.get();
280 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700281 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800283 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800284 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800286 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 return JNI_ERR;
288 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700289
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800290 ScopedLocalRef<jthrowable> exception(
291 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
292 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 return JNI_ERR;
294 }
Ian Rogersef28b142012-11-30 14:22:18 -0800295 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800297 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700299}
300
Elliott Hughes462c9442012-03-23 18:47:50 -0700301static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800302 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700303 return JNI_ERR;
304 }
305
Elliott Hughes462c9442012-03-23 18:47:50 -0700306 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700307 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800308 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700309 *p_env = self->GetJniEnv();
310 return JNI_OK;
311 }
312
313 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
314
315 // No threads allowed in zygote mode.
316 if (runtime->IsZygote()) {
317 LOG(ERROR) << "Attempt to attach a thread in the zygote";
318 return JNI_ERR;
319 }
320
Elliott Hughes462c9442012-03-23 18:47:50 -0700321 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800322 const char* thread_name = nullptr;
323 jobject thread_group = nullptr;
324 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700325 if (IsBadJniVersion(args->version)) {
326 LOG(ERROR) << "Bad JNI version passed to "
327 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
328 << args->version;
329 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700330 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700331 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700332 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700333 }
Elliott Hughes75770752011-08-24 17:52:38 -0700334
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800335 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800336 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700337 return JNI_ERR;
338 } else {
339 *p_env = Thread::Current()->GetJniEnv();
340 return JNI_OK;
341 }
Elliott Hughes75770752011-08-24 17:52:38 -0700342}
343
Elliott Hughes79082e32011-08-25 12:07:32 -0700344class SharedLibrary {
345 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800346 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700347 : path_(path),
348 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700349 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700350 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700351 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700352 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700353 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700354 }
355
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800356 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700357 return class_loader_;
358 }
359
360 std::string GetPath() {
361 return path_;
362 }
363
364 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700365 * Check the result of an earlier call to JNI_OnLoad on this library.
366 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700367 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 bool CheckOnLoadResult()
369 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700371 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
373 bool okay;
374 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700375 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700376
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700377 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
379 // caller can continue.
380 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
381 okay = true;
382 } else {
383 while (jni_on_load_result_ == kPending) {
384 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700385 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700387
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 okay = (jni_on_load_result_ == kOkay);
389 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
390 << (okay ? "succeeded" : "failed") << "]";
391 }
392 }
393 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700394 return okay;
395 }
396
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700398 Thread* self = Thread::Current();
399 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700400
Elliott Hughes79082e32011-08-25 12:07:32 -0700401 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700402 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700403
404 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700405 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700406 }
407
408 void* FindSymbol(const std::string& symbol_name) {
409 return dlsym(handle_, symbol_name.c_str());
410 }
411
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800412 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800413 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800414 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800415 }
416 }
417
Elliott Hughes79082e32011-08-25 12:07:32 -0700418 private:
419 enum JNI_OnLoadState {
420 kPending,
421 kFailed,
422 kOkay,
423 };
424
425 // Path to library "/system/lib/libjni.so".
426 std::string path_;
427
428 // The void* returned by dlopen(3).
429 void* handle_;
430
431 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800432 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700433
434 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700435 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700436 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700437 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700438 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700439 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700440 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700441 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700442};
443
Elliott Hughes79082e32011-08-25 12:07:32 -0700444// This exists mainly to keep implementation details out of the header file.
445class Libraries {
446 public:
447 Libraries() {
448 }
449
450 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700451 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700452 }
453
Elliott Hughesae80b492012-04-24 10:43:17 -0700454 void Dump(std::ostream& os) const {
455 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700456 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700457 if (!first) {
458 os << ' ';
459 }
460 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700461 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700462 }
463 }
464
465 size_t size() const {
466 return libraries_.size();
467 }
468
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700470 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800471 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700472 }
473
474 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700475 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700476 }
477
478 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800479 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 std::string jni_short_name(JniShortName(m));
482 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800483 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700484 for (const auto& lib : libraries_) {
485 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 if (library->GetClassLoader() != declaring_class_loader) {
487 // We only search libraries loaded by the appropriate ClassLoader.
488 continue;
489 }
490 // Try the short name then the long name...
491 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800492 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700493 fn = library->FindSymbol(jni_long_name);
494 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800495 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800496 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
497 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700498 return fn;
499 }
500 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700502 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700503 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800505 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700506 }
507
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800508 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800509 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800510 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800511 }
512 }
513
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700515 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700516};
517
Ian Rogers2d10b202014-05-12 19:15:18 -0700518#define CHECK_NON_NULL_ARGUMENT(value) \
519 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700520
Ian Rogers2d10b202014-05-12 19:15:18 -0700521#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
522 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
523
524#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
525 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
526
527#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
528 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
529
530#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800531 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700532 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700533 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700534 }
535
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700536#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800537 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700538 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700539 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700540 }
541
Elliott Hughescdf53122011-08-19 15:46:09 -0700542class JNI {
543 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700544 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 return JNI_VERSION_1_6;
546 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700547
Ian Rogers25e8b912012-09-07 11:31:36 -0700548 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700549 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800550 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700551 }
552
Elliott Hughescdf53122011-08-19 15:46:09 -0700553 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700554 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700555 Runtime* runtime = Runtime::Current();
556 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700557 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700558 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800559 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700560 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700561 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800562 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700563 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800564 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700565 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700566 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700567 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700568
Ian Rogers62f05122014-03-21 11:21:29 -0700569 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700570 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700572 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700573 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700574
Ian Rogers62f05122014-03-21 11:21:29 -0700575 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700576 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700578 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700579 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700580
Elliott Hughescdf53122011-08-19 15:46:09 -0700581 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700582 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800584 mirror::ArtMethod* m = soa.DecodeMethod(mid);
585 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 jobject art_method = soa.AddLocalReference<jobject>(m);
587 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
588 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700590 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 SetObjectField(env, reflect_method,
592 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700594 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700595
Elliott Hughescdf53122011-08-19 15:46:09 -0700596 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700597 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800599 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700600 jobject art_field = soa.AddLocalReference<jobject>(f);
601 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
602 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800603 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700604 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800605 SetObjectField(env, reflect_field,
606 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700607 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700609
Elliott Hughes37f7a402011-08-22 18:56:01 -0700610 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700611 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800613 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700615 }
616
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700617 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700618 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800620 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700623
Elliott Hughes37f7a402011-08-22 18:56:01 -0700624 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700625 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
626 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800628 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
629 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700630 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700632
Elliott Hughese84278b2012-03-22 10:06:53 -0700633 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700634 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800635 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700636 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700637 return JNI_TRUE;
638 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700639 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800640 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
641 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700642 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700643 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughes37f7a402011-08-22 18:56:01 -0700646 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
649 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 return JNI_ERR;
651 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800652 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
653 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 return JNI_OK;
655 }
656
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700657 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700658 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800659 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700660 }
661
662 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700663 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 }
665
666 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700667 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 }
669
670 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700672
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800673 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
674 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
675 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800676 uint32_t old_throw_dex_pc;
677 {
678 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800679 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800680 old_throw_this_object.reset(old_throw_location.GetThis());
681 old_throw_method.reset(old_throw_location.GetMethod());
682 old_exception.reset(old_exception_obj);
683 old_throw_dex_pc = old_throw_location.GetDexPc();
684 soa.Self()->ClearException();
685 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800686 ScopedLocalRef<jthrowable> exception(env,
687 soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700688 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
689 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800690 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700691 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800692 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 } else {
694 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800695 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800696 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800698 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700699 }
700 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
702 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700703
Ian Rogers62d6c772013-02-27 08:32:07 -0800704 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700706
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800709 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
712
Ian Rogers25e8b912012-09-07 11:31:36 -0700713 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 LOG(FATAL) << "JNI FatalError called: " << msg;
715 }
716
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700717 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800718 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700719 return JNI_ERR;
720 }
Ian Rogersef28b142012-11-30 14:22:18 -0800721 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 return JNI_OK;
723 }
724
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700725 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800727 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 soa.Env()->PopFrame();
729 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
731
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700732 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800733 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700734 }
735
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700737 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800738 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800739 // Check for null after decoding the object to handle cleared weak globals.
740 if (decoded_obj == nullptr) {
741 return nullptr;
742 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700745 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 return reinterpret_cast<jobject>(ref);
748 }
749
750 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800751 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 return;
753 }
Ian Rogersef28b142012-11-30 14:22:18 -0800754 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800756 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700757 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700758
759 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
760 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
761 << "failed to find entry";
762 }
763 }
764
765 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800767 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 }
769
770 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700771 if (obj != nullptr) {
772 ScopedObjectAccess soa(env);
773 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 }
775 }
776
777 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700778 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800779 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800780 // Check for null after decoding the object to handle cleared weak globals.
781 if (decoded_obj == nullptr) {
782 return nullptr;
783 }
784 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 }
786
787 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800788 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 return;
790 }
Ian Rogersef28b142012-11-30 14:22:18 -0800791 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700792
Ian Rogersef28b142012-11-30 14:22:18 -0800793 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 if (!locals.Remove(cookie, obj)) {
795 // Attempting to delete a local reference that is not in the
796 // topmost local reference frame is a no-op. DeleteLocalRef returns
797 // void and doesn't throw any exceptions, but we should probably
798 // complain about it so the user will notice that things aren't
799 // going quite the way they expect.
800 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
801 << "failed to find entry";
802 }
803 }
804
805 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700806 if (obj1 == obj2) {
807 return JNI_TRUE;
808 } else {
809 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800810 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
811 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700812 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
814
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700815 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700816 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700817 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800818 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800819 if (c == nullptr) {
820 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700821 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700822 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 }
824
Ian Rogersbc939662013-08-15 10:26:54 -0700825 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700826 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700828 CHECK_NON_NULL_ARGUMENT(java_class);
829 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700830 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 va_end(args);
832 return result;
833 }
834
Elliott Hughes72025e52011-08-23 17:50:30 -0700835 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700836 CHECK_NON_NULL_ARGUMENT(java_class);
837 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800839 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800840 if (c == nullptr) {
841 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700842 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800843 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800844 if (result == nullptr) {
845 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700846 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700848 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800849 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800850 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700851 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800852 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 }
854
Elliott Hughes72025e52011-08-23 17:50:30 -0700855 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700856 CHECK_NON_NULL_ARGUMENT(java_class);
857 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800859 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800860 if (c == nullptr) {
861 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700862 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800863 mirror::Object* result = c->AllocObject(soa.Self());
864 if (result == nullptr) {
865 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700866 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700868 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800869 if (soa.Self()->IsExceptionPending()) {
870 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700871 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800872 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874
Ian Rogersbc939662013-08-15 10:26:54 -0700875 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700876 CHECK_NON_NULL_ARGUMENT(java_class);
877 CHECK_NON_NULL_ARGUMENT(name);
878 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700879 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700880 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 }
882
Ian Rogersbc939662013-08-15 10:26:54 -0700883 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
884 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700885 CHECK_NON_NULL_ARGUMENT(java_class);
886 CHECK_NON_NULL_ARGUMENT(name);
887 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700889 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 va_list ap;
894 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700895 CHECK_NON_NULL_ARGUMENT(obj);
896 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700897 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700900 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 CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700904 CHECK_NON_NULL_ARGUMENT(obj);
905 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 ScopedObjectAccess soa(env);
907 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
908 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700912 CHECK_NON_NULL_ARGUMENT(obj);
913 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700915 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
916 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 }
919
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 va_list ap;
922 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700923 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
924 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700925 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700928 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700932 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
933 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700934 ScopedObjectAccess soa(env);
935 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 }
937
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700939 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
940 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700942 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
943 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 va_list ap;
948 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700949 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
950 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700951 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700954 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700958 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
959 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
961 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700965 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
966 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700967 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700968 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
969 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 va_list ap;
974 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700975 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
976 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700977 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700980 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
985 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
987 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700991 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
992 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700994 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
995 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_list ap;
1000 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001001 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1002 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001003 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001006 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1011 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 ScopedObjectAccess soa(env);
1013 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1018 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001020 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1021 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_list ap;
1026 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001027 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1028 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001029 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001032 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(env);
1039 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001043 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1044 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001045 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001046 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1047 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_list ap;
1052 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001053 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1054 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001055 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001058 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1063 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064 ScopedObjectAccess soa(env);
1065 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001069 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1070 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001072 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1073 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_list ap;
1078 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001079 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1080 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001081 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001084 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001090 ScopedObjectAccess soa(env);
1091 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1096 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001098 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1099 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_list ap;
1104 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001105 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1106 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001107 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001110 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 ScopedObjectAccess soa(env);
1117 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001121 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1122 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001124 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1125 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001131 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1132 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001133 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001134 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001139 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1140 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141 ScopedObjectAccess soa(env);
1142 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001146 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1147 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001149 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001152 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001155 CHECK_NON_NULL_ARGUMENT(obj);
1156 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001157 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1159 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 va_end(ap);
1161 return local_result;
1162 }
1163
Ian Rogersbc939662013-08-15 10:26:54 -07001164 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1165 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001166 CHECK_NON_NULL_ARGUMENT(obj);
1167 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 ScopedObjectAccess soa(env);
1169 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1170 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Ian Rogersbc939662013-08-15 10:26:54 -07001173 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1174 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001175 CHECK_NON_NULL_ARGUMENT(obj);
1176 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001178 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001179 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Ian Rogersbc939662013-08-15 10:26:54 -07001182 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1183 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001186 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1187 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001188 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001191 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Ian Rogersbc939662013-08-15 10:26:54 -07001194 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1195 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001196 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1197 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 ScopedObjectAccess soa(env);
1199 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Ian Rogersbc939662013-08-15 10:26:54 -07001202 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1203 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001204 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1205 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001207 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001210 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001213 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1214 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001215 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001216 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001218 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Ian Rogersbc939662013-08-15 10:26:54 -07001221 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1222 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001223 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1224 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
1226 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Ian Rogersbc939662013-08-15 10:26:54 -07001229 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1230 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001231 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1232 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001234 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001237 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001240 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1241 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001242 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001245 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Ian Rogersbc939662013-08-15 10:26:54 -07001248 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1249 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001250 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1251 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env);
1253 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
Ian Rogersbc939662013-08-15 10:26:54 -07001256 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1257 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001258 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1259 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001260 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001261 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001264 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001267 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1268 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001269 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001272 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
Ian Rogersbc939662013-08-15 10:26:54 -07001275 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1276 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001277 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1278 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001279 ScopedObjectAccess soa(env);
1280 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Ian Rogersbc939662013-08-15 10:26:54 -07001283 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1284 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001285 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1286 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001288 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001291 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001294 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1295 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001296 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001297 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001299 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Ian Rogersbc939662013-08-15 10:26:54 -07001302 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1303 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001304 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1305 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001306 ScopedObjectAccess soa(env);
1307 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Ian Rogersbc939662013-08-15 10:26:54 -07001310 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1311 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001312 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1313 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001314 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001315 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001318 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001321 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1322 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001323 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001324 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001326 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Ian Rogersbc939662013-08-15 10:26:54 -07001329 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1330 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001331 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1332 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001333 ScopedObjectAccess soa(env);
1334 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Ian Rogersbc939662013-08-15 10:26:54 -07001337 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1338 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001339 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1340 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001342 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001345 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001348 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1349 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001350 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001351 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001353 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Ian Rogersbc939662013-08-15 10:26:54 -07001356 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1357 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001358 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1359 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001360 ScopedObjectAccess soa(env);
1361 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Ian Rogersbc939662013-08-15 10:26:54 -07001364 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1365 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001366 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1367 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001368 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001369 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001372 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001375 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1376 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001377 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001378 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001380 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Ian Rogersbc939662013-08-15 10:26:54 -07001383 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1384 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001385 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1386 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001387 ScopedObjectAccess soa(env);
1388 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Ian Rogersbc939662013-08-15 10:26:54 -07001391 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1392 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001393 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1394 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001395 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001396 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001399 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001401 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001402 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1403 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001404 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001405 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_end(ap);
1407 }
1408
Brian Carlstromea46f952013-07-30 01:26:50 -07001409 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1410 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001411 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1412 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 ScopedObjectAccess soa(env);
1414 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Ian Rogersbc939662013-08-15 10:26:54 -07001417 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1418 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001419 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1420 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001422 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Ian Rogersbc939662013-08-15 10:26:54 -07001425 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001426 CHECK_NON_NULL_ARGUMENT(java_class);
1427 CHECK_NON_NULL_ARGUMENT(name);
1428 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001430 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001432
Ian Rogersbc939662013-08-15 10:26:54 -07001433 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1434 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001435 CHECK_NON_NULL_ARGUMENT(java_class);
1436 CHECK_NON_NULL_ARGUMENT(name);
1437 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001439 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001443 CHECK_NON_NULL_ARGUMENT(obj);
1444 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001446 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1447 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001452 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001454 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001455 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001459 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1460 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001461 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001462 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1463 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>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001469 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001470 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001471 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1472 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001473 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001477 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1478 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001480 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1481 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001482 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001484#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001485 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001486 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001487 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001488 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001491 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1492 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001493 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001494 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1495 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001496 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001497
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001498#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001499 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001500 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001501 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001502 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001505 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001509 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001513 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001517 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001521 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001525 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001533 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001537 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001549 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 }
1559
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001560 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 }
1567
1568 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 }
1571
1572 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 }
1575
1576 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 }
1579
1580 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 }
1583
1584 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 }
1587
1588 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 }
1591
1592 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 }
1595
1596 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 }
1599
1600 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 }
1603
1604 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 }
1607
1608 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 }
1611
1612 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 }
1615
1616 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 }
1619
1620 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 }
1623
1624 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001626 }
1627
1628 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001635 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001636 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001637 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 va_end(ap);
1640 return local_result;
1641 }
1642
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001643 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001644 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001646 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001647 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001651 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001652 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001653 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001660 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001661 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001662 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001664 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001667 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001668 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001669 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001670 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001673 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001674 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001676 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001682 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001684 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001686 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001690 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001692 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001696 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001698 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001704 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001705 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001706 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001708 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001712 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001714 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001718 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001719 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001720 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001723 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001726 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001727 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001728 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001730 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001734 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001736 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001740 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001742 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001748 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001749 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001750 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001752 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001756 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001762 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001764 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001769 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001770 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001771 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001772 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001774 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001778 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001780 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001784 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001786 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001792 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001793 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001794 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001796 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001802 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001806 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001813 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001814 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001815 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001816 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001818 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001822 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001824 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001828 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001830 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001835 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001836 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001837 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001838 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 va_end(ap);
1840 }
1841
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001842 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001843 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001845 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001848 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001849 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001851 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
Elliott Hughes814e4032011-08-23 12:07:56 -07001854 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001855 if (UNLIKELY(char_count < 0)) {
1856 JniAbortF("NewString", "char_count < 0: %d", char_count);
1857 return nullptr;
1858 }
1859 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1860 JniAbortF("NewString", "chars == null && char_count > 0");
1861 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001862 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001863 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001864 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001865 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
1868 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001869 if (utf == nullptr) {
1870 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001873 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
Elliott Hughes814e4032011-08-23 12:07:56 -07001877 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001878 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001880 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001881 }
1882
1883 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001884 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001885 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001886 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001887 }
1888
Ian Rogersbc939662013-08-15 10:26:54 -07001889 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1890 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001891 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001893 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001897 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001898 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1899 memcpy(buf, chars + start, length * sizeof(jchar));
1900 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001901 }
1902
Ian Rogersbc939662013-08-15 10:26:54 -07001903 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1904 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001905 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001906 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001907 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001909 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001910 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001911 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1913 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1914 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001918 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001919 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001920 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1921 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001923 if (is_copy != nullptr) {
1924 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001925 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001926 int32_t char_count = s->GetLength();
1927 int32_t offset = s->GetOffset();
1928 jchar* bytes = new jchar[char_count + 1];
1929 for (int32_t i = 0; i < char_count; i++) {
1930 bytes[i] = chars->Get(i + offset);
1931 }
1932 bytes[char_count] = '\0';
1933 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001934 }
1935
Mathieu Chartier590fee92013-09-13 13:46:47 -07001936 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001937 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001938 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001940 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001944 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001948 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001952 if (java_string == nullptr) {
1953 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001954 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001955 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001956 *is_copy = JNI_TRUE;
1957 }
Ian Rogersef28b142012-11-30 14:22:18 -08001958 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001959 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 size_t byte_count = s->GetUtfLength();
1961 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001962 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001963 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1964 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1965 bytes[byte_count] = '\0';
1966 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001970 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001971 }
1972
Elliott Hughesbd935992011-08-22 11:59:34 -07001973 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001974 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001975 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001976 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001977 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001978 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1979 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001980 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001981 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes814e4032011-08-23 12:07:56 -07001984 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001985 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001986 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001987 mirror::ObjectArray<mirror::Object>* array =
1988 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001989 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Ian Rogersbc939662013-08-15 10:26:54 -07001992 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1993 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001994 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001996 mirror::ObjectArray<mirror::Object>* array =
1997 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1998 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001999 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
2002 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002003 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
2006 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002007 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
2010 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002011 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
2014 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002015 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
2018 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002019 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
2022 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002023 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
2026 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002027 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Ian Rogers1d99e452014-01-02 17:36:41 -08002030 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2031 jobject initial_element) {
2032 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002033 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002034 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002035 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002036 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037
2038 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002039 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002041 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002042 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002043 if (UNLIKELY(element_class->IsPrimitive())) {
2044 JniAbortF("NewObjectArray", "not an object type: %s",
2045 PrettyDescriptor(element_class).c_str());
2046 return nullptr;
2047 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002048 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002049 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002050 if (UNLIKELY(array_class == nullptr)) {
2051 return nullptr;
2052 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes75770752011-08-24 17:52:38 -07002055 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002056 mirror::ObjectArray<mirror::Object>* result =
2057 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002058 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002059 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002060 if (initial_object != nullptr) {
2061 mirror::Class* element_class = result->GetClass()->GetComponentType();
2062 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2063 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2064 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2065 PrettyDescriptor(element_class).c_str());
2066
2067 } else {
2068 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002069 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002070 }
2071 }
Elliott Hughes75770752011-08-24 17:52:38 -07002072 }
2073 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002074 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
2077 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002078 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Ian Rogersa15e67d2012-02-28 13:51:55 -08002081 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002082 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002083 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002084 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002085 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2086 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2087 PrettyDescriptor(array->GetClass()).c_str());
2088 return nullptr;
2089 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002090 gc::Heap* heap = Runtime::Current()->GetHeap();
2091 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002092 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002093 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002094 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002095 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002097 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002098 *is_copy = JNI_FALSE;
2099 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002100 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002101 }
2102
Ian Rogers2d10b202014-05-12 19:15:18 -07002103 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2104 jint mode) {
2105 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2106 ScopedObjectAccess soa(env);
2107 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2108 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2109 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2110 PrettyDescriptor(array->GetClass()).c_str());
2111 return;
2112 }
2113 const size_t component_size = array->GetClass()->GetComponentSize();
2114 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002115 }
2116
Elliott Hughes75770752011-08-24 17:52:38 -07002117 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002118 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes75770752011-08-24 17:52:38 -07002121 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002122 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes75770752011-08-24 17:52:38 -07002125 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002126 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes75770752011-08-24 17:52:38 -07002129 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002130 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes75770752011-08-24 17:52:38 -07002133 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002134 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes75770752011-08-24 17:52:38 -07002137 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002138 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes75770752011-08-24 17:52:38 -07002141 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002142 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes75770752011-08-24 17:52:38 -07002145 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002146 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Mathieu Chartier590fee92013-09-13 13:46:47 -07002149 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2150 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002151 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2152 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) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002156 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(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) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002160 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(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) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002165 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(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) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002170 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(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) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002174 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(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) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002178 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(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) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002183 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(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 Rogers2d10b202014-05-12 19:15:18 -07002188 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002189 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Ian Rogersbc939662013-08-15 10:26:54 -07002192 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2193 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002194 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Ian Rogersbc939662013-08-15 10:26:54 -07002197 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2198 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002199 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Ian Rogersbc939662013-08-15 10:26:54 -07002202 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2203 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002204 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002205 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Ian Rogersbc939662013-08-15 10:26:54 -07002208 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2209 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002210 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002211 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Ian Rogersbc939662013-08-15 10:26:54 -07002214 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2215 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002216 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Ian Rogersbc939662013-08-15 10:26:54 -07002219 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2220 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002221 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Ian Rogersbc939662013-08-15 10:26:54 -07002224 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2225 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002226 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002227 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Ian Rogersbc939662013-08-15 10:26:54 -07002230 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2231 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002232 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002233 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Ian Rogersbc939662013-08-15 10:26:54 -07002236 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2237 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002238 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Ian Rogersbc939662013-08-15 10:26:54 -07002241 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2242 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002243 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Ian Rogersbc939662013-08-15 10:26:54 -07002246 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2247 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002248 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002249 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Ian Rogersbc939662013-08-15 10:26:54 -07002252 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2253 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002254 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002255 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2259 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002260 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 }
2262
Ian Rogersbc939662013-08-15 10:26:54 -07002263 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2264 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002265 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 }
2267
Ian Rogersbc939662013-08-15 10:26:54 -07002268 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2269 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002270 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002271 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Ian Rogersbc939662013-08-15 10:26:54 -07002274 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2275 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002276 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2277 }
2278
Ian Rogersbc939662013-08-15 10:26:54 -07002279 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2280 jint method_count, bool return_errors) {
2281 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002282 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002283 return JNI_ERR; // Not reached.
2284 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002285 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002286 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002287 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002288 if (UNLIKELY(method_count == 0)) {
2289 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2290 << PrettyDescriptor(c);
2291 return JNI_OK;
2292 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002293 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002294 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 const char* name = methods[i].name;
2296 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002297 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002299 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 ++sig;
2301 }
2302
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002303 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2304 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002305 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002307 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002308 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002309 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002310 << PrettyDescriptor(c) << "." << name << sig << " in "
2311 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002312 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002314 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002315 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002316 << PrettyDescriptor(c) << "." << name << sig
2317 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002318 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 return JNI_ERR;
2320 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002321
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002322 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002323
Ian Rogers1eb512d2013-10-18 15:42:20 -07002324 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 }
2326 return JNI_OK;
2327 }
2328
Elliott Hughes5174fe62011-08-23 15:12:35 -07002329 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002330 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002331 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002332 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002333
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002334 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002335
Ian Rogers2d10b202014-05-12 19:15:18 -07002336 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002337 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002338 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002339 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002340 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002341 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002342 }
2343 }
2344 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002345 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002346 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002347 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002348 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002349 }
2350 }
2351
Ian Rogers2d10b202014-05-12 19:15:18 -07002352 if (unregistered_count == 0) {
2353 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2354 << PrettyDescriptor(c) << "' that contains no native methods";
2355 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002356 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002357 }
2358
Ian Rogers719d1a32014-03-06 12:13:39 -08002359 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002360 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002361 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002362 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2363 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002364 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002365 return JNI_ERR;
2366 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002367 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002368 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 }
2370
Ian Rogers719d1a32014-03-06 12:13:39 -08002371 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002372 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002373 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002374 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002375 o->MonitorExit(soa.Self());
2376 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002377 return JNI_ERR;
2378 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002379 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002380 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002381 }
2382
2383 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002384 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002385 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002386 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002387 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002388 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002389 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002390 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002391 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 }
2393
Elliott Hughescdf53122011-08-19 15:46:09 -07002394 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002395 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002396 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002397 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002398 if (address == nullptr && capacity != 0) {
2399 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002400 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002401
Ian Rogers936b37f2014-02-14 00:52:24 -08002402 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002403 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002404 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002405 jint capacity_arg = static_cast<jint>(capacity);
2406
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002407 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2408 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002409 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002410 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002411 }
2412
Elliott Hughesb465ab02011-08-24 11:21:21 -07002413 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002414 return reinterpret_cast<void*>(env->GetLongField(
2415 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002416 }
2417
Elliott Hughesb465ab02011-08-24 11:21:21 -07002418 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002419 return static_cast<jlong>(env->GetIntField(
2420 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002421 }
2422
Elliott Hughesb465ab02011-08-24 11:21:21 -07002423 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002424 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002425
2426 // Do we definitely know what kind of reference this is?
2427 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2428 IndirectRefKind kind = GetIndirectRefKind(ref);
2429 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002430 case kLocal: {
2431 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002432 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002433 return JNILocalRefType;
2434 }
2435 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002436 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002437 case kGlobal:
2438 return JNIGlobalRefType;
2439 case kWeakGlobal:
2440 return JNIWeakGlobalRefType;
2441 case kSirtOrInvalid:
2442 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002443 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002444 return JNILocalRefType;
2445 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002446 return JNIInvalidRefType;
2447 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002448 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2449 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002450 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002451
2452 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002453 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2454 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002455 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002456 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002457 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2458 return JNI_ERR;
2459 }
2460 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002461 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002462 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2463 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002464 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002465 soa.Self()->ThrowOutOfMemoryError(caller);
2466 }
2467 return okay ? JNI_OK : JNI_ERR;
2468 }
2469
2470 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002471 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002472 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002473 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002474 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002475 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002476 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002477 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002478 return soa.AddLocalReference<JniT>(result);
2479 }
2480
Ian Rogers2d10b202014-05-12 19:15:18 -07002481 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2482 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2483 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002485 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002486 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2487 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2488 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2489 PrettyDescriptor(array->GetClass()).c_str());
2490 return nullptr;
2491 }
2492 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2493 return array;
2494 }
2495
2496 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2497 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2498 CHECK_NON_NULL_ARGUMENT(java_array);
2499 ScopedObjectAccess soa(env);
2500 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2501 "GetArrayElements",
2502 "get");
2503 if (UNLIKELY(array == nullptr)) {
2504 return nullptr;
2505 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002506 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002507 // Only make a copy if necessary.
2508 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2509 if (is_copy != nullptr) {
2510 *is_copy = JNI_TRUE;
2511 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002512 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002513 size_t size = array->GetLength() * component_size;
2514 void* data = new uint64_t[RoundUp(size, 8) / 8];
2515 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002516 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002517 } else {
2518 if (is_copy != nullptr) {
2519 *is_copy = JNI_FALSE;
2520 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002521 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002522 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002523 }
2524
Ian Rogers2d10b202014-05-12 19:15:18 -07002525 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002526 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002527 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002528 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002529 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2530 "ReleaseArrayElements",
2531 "release");
2532 if (array == nullptr) {
2533 return;
2534 }
2535 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2536 }
2537
2538 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2539 size_t component_size, void* elements, jint mode)
2540 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002541 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002542 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002543 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002544 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002545 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2546 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002547 if (is_copy) {
2548 // Sanity check: If elements is not the same as the java array's data, it better not be a
2549 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2550 // copies we make?
2551 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2552 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2553 reinterpret_cast<void*>(elements), array_data);
2554 return;
2555 }
2556 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002557 // Don't need to copy if we had a direct pointer.
2558 if (mode != JNI_ABORT && is_copy) {
2559 memcpy(array_data, elements, bytes);
2560 }
2561 if (mode != JNI_COMMIT) {
2562 if (is_copy) {
2563 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002564 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002565 // Non copy to a movable object must means that we had disabled the moving GC.
2566 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002567 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002568 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002569 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570 }
2571
Ian Rogers2d10b202014-05-12 19:15:18 -07002572 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2573 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2574 jsize start, jsize length, ElementT* buf) {
2575 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2576 ScopedObjectAccess soa(env);
2577 ArtArrayT* array =
2578 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2579 "GetPrimitiveArrayRegion",
2580 "get region of");
2581 if (array != nullptr) {
2582 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2583 ThrowAIOOBE(soa, array, start, length, "src");
2584 } else {
2585 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2586 ElementT* data = array->GetData();
2587 memcpy(buf, data + start, length * sizeof(ElementT));
2588 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002589 }
2590 }
2591
Ian Rogers2d10b202014-05-12 19:15:18 -07002592 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2593 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2594 jsize start, jsize length, const ElementT* buf) {
2595 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2596 ScopedObjectAccess soa(env);
2597 ArtArrayT* array =
2598 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2599 "SetPrimitiveArrayRegion",
2600 "set region of");
2601 if (array != nullptr) {
2602 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2603 ThrowAIOOBE(soa, array, start, length, "dst");
2604 } else {
2605 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2606 ElementT* data = array->GetData();
2607 memcpy(data + start, buf, length * sizeof(ElementT));
2608 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002609 }
2610 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002611};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002612
Elliott Hughes88c5c352012-03-15 18:49:48 -07002613const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002614 nullptr, // reserved0.
2615 nullptr, // reserved1.
2616 nullptr, // reserved2.
2617 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002618 JNI::GetVersion,
2619 JNI::DefineClass,
2620 JNI::FindClass,
2621 JNI::FromReflectedMethod,
2622 JNI::FromReflectedField,
2623 JNI::ToReflectedMethod,
2624 JNI::GetSuperclass,
2625 JNI::IsAssignableFrom,
2626 JNI::ToReflectedField,
2627 JNI::Throw,
2628 JNI::ThrowNew,
2629 JNI::ExceptionOccurred,
2630 JNI::ExceptionDescribe,
2631 JNI::ExceptionClear,
2632 JNI::FatalError,
2633 JNI::PushLocalFrame,
2634 JNI::PopLocalFrame,
2635 JNI::NewGlobalRef,
2636 JNI::DeleteGlobalRef,
2637 JNI::DeleteLocalRef,
2638 JNI::IsSameObject,
2639 JNI::NewLocalRef,
2640 JNI::EnsureLocalCapacity,
2641 JNI::AllocObject,
2642 JNI::NewObject,
2643 JNI::NewObjectV,
2644 JNI::NewObjectA,
2645 JNI::GetObjectClass,
2646 JNI::IsInstanceOf,
2647 JNI::GetMethodID,
2648 JNI::CallObjectMethod,
2649 JNI::CallObjectMethodV,
2650 JNI::CallObjectMethodA,
2651 JNI::CallBooleanMethod,
2652 JNI::CallBooleanMethodV,
2653 JNI::CallBooleanMethodA,
2654 JNI::CallByteMethod,
2655 JNI::CallByteMethodV,
2656 JNI::CallByteMethodA,
2657 JNI::CallCharMethod,
2658 JNI::CallCharMethodV,
2659 JNI::CallCharMethodA,
2660 JNI::CallShortMethod,
2661 JNI::CallShortMethodV,
2662 JNI::CallShortMethodA,
2663 JNI::CallIntMethod,
2664 JNI::CallIntMethodV,
2665 JNI::CallIntMethodA,
2666 JNI::CallLongMethod,
2667 JNI::CallLongMethodV,
2668 JNI::CallLongMethodA,
2669 JNI::CallFloatMethod,
2670 JNI::CallFloatMethodV,
2671 JNI::CallFloatMethodA,
2672 JNI::CallDoubleMethod,
2673 JNI::CallDoubleMethodV,
2674 JNI::CallDoubleMethodA,
2675 JNI::CallVoidMethod,
2676 JNI::CallVoidMethodV,
2677 JNI::CallVoidMethodA,
2678 JNI::CallNonvirtualObjectMethod,
2679 JNI::CallNonvirtualObjectMethodV,
2680 JNI::CallNonvirtualObjectMethodA,
2681 JNI::CallNonvirtualBooleanMethod,
2682 JNI::CallNonvirtualBooleanMethodV,
2683 JNI::CallNonvirtualBooleanMethodA,
2684 JNI::CallNonvirtualByteMethod,
2685 JNI::CallNonvirtualByteMethodV,
2686 JNI::CallNonvirtualByteMethodA,
2687 JNI::CallNonvirtualCharMethod,
2688 JNI::CallNonvirtualCharMethodV,
2689 JNI::CallNonvirtualCharMethodA,
2690 JNI::CallNonvirtualShortMethod,
2691 JNI::CallNonvirtualShortMethodV,
2692 JNI::CallNonvirtualShortMethodA,
2693 JNI::CallNonvirtualIntMethod,
2694 JNI::CallNonvirtualIntMethodV,
2695 JNI::CallNonvirtualIntMethodA,
2696 JNI::CallNonvirtualLongMethod,
2697 JNI::CallNonvirtualLongMethodV,
2698 JNI::CallNonvirtualLongMethodA,
2699 JNI::CallNonvirtualFloatMethod,
2700 JNI::CallNonvirtualFloatMethodV,
2701 JNI::CallNonvirtualFloatMethodA,
2702 JNI::CallNonvirtualDoubleMethod,
2703 JNI::CallNonvirtualDoubleMethodV,
2704 JNI::CallNonvirtualDoubleMethodA,
2705 JNI::CallNonvirtualVoidMethod,
2706 JNI::CallNonvirtualVoidMethodV,
2707 JNI::CallNonvirtualVoidMethodA,
2708 JNI::GetFieldID,
2709 JNI::GetObjectField,
2710 JNI::GetBooleanField,
2711 JNI::GetByteField,
2712 JNI::GetCharField,
2713 JNI::GetShortField,
2714 JNI::GetIntField,
2715 JNI::GetLongField,
2716 JNI::GetFloatField,
2717 JNI::GetDoubleField,
2718 JNI::SetObjectField,
2719 JNI::SetBooleanField,
2720 JNI::SetByteField,
2721 JNI::SetCharField,
2722 JNI::SetShortField,
2723 JNI::SetIntField,
2724 JNI::SetLongField,
2725 JNI::SetFloatField,
2726 JNI::SetDoubleField,
2727 JNI::GetStaticMethodID,
2728 JNI::CallStaticObjectMethod,
2729 JNI::CallStaticObjectMethodV,
2730 JNI::CallStaticObjectMethodA,
2731 JNI::CallStaticBooleanMethod,
2732 JNI::CallStaticBooleanMethodV,
2733 JNI::CallStaticBooleanMethodA,
2734 JNI::CallStaticByteMethod,
2735 JNI::CallStaticByteMethodV,
2736 JNI::CallStaticByteMethodA,
2737 JNI::CallStaticCharMethod,
2738 JNI::CallStaticCharMethodV,
2739 JNI::CallStaticCharMethodA,
2740 JNI::CallStaticShortMethod,
2741 JNI::CallStaticShortMethodV,
2742 JNI::CallStaticShortMethodA,
2743 JNI::CallStaticIntMethod,
2744 JNI::CallStaticIntMethodV,
2745 JNI::CallStaticIntMethodA,
2746 JNI::CallStaticLongMethod,
2747 JNI::CallStaticLongMethodV,
2748 JNI::CallStaticLongMethodA,
2749 JNI::CallStaticFloatMethod,
2750 JNI::CallStaticFloatMethodV,
2751 JNI::CallStaticFloatMethodA,
2752 JNI::CallStaticDoubleMethod,
2753 JNI::CallStaticDoubleMethodV,
2754 JNI::CallStaticDoubleMethodA,
2755 JNI::CallStaticVoidMethod,
2756 JNI::CallStaticVoidMethodV,
2757 JNI::CallStaticVoidMethodA,
2758 JNI::GetStaticFieldID,
2759 JNI::GetStaticObjectField,
2760 JNI::GetStaticBooleanField,
2761 JNI::GetStaticByteField,
2762 JNI::GetStaticCharField,
2763 JNI::GetStaticShortField,
2764 JNI::GetStaticIntField,
2765 JNI::GetStaticLongField,
2766 JNI::GetStaticFloatField,
2767 JNI::GetStaticDoubleField,
2768 JNI::SetStaticObjectField,
2769 JNI::SetStaticBooleanField,
2770 JNI::SetStaticByteField,
2771 JNI::SetStaticCharField,
2772 JNI::SetStaticShortField,
2773 JNI::SetStaticIntField,
2774 JNI::SetStaticLongField,
2775 JNI::SetStaticFloatField,
2776 JNI::SetStaticDoubleField,
2777 JNI::NewString,
2778 JNI::GetStringLength,
2779 JNI::GetStringChars,
2780 JNI::ReleaseStringChars,
2781 JNI::NewStringUTF,
2782 JNI::GetStringUTFLength,
2783 JNI::GetStringUTFChars,
2784 JNI::ReleaseStringUTFChars,
2785 JNI::GetArrayLength,
2786 JNI::NewObjectArray,
2787 JNI::GetObjectArrayElement,
2788 JNI::SetObjectArrayElement,
2789 JNI::NewBooleanArray,
2790 JNI::NewByteArray,
2791 JNI::NewCharArray,
2792 JNI::NewShortArray,
2793 JNI::NewIntArray,
2794 JNI::NewLongArray,
2795 JNI::NewFloatArray,
2796 JNI::NewDoubleArray,
2797 JNI::GetBooleanArrayElements,
2798 JNI::GetByteArrayElements,
2799 JNI::GetCharArrayElements,
2800 JNI::GetShortArrayElements,
2801 JNI::GetIntArrayElements,
2802 JNI::GetLongArrayElements,
2803 JNI::GetFloatArrayElements,
2804 JNI::GetDoubleArrayElements,
2805 JNI::ReleaseBooleanArrayElements,
2806 JNI::ReleaseByteArrayElements,
2807 JNI::ReleaseCharArrayElements,
2808 JNI::ReleaseShortArrayElements,
2809 JNI::ReleaseIntArrayElements,
2810 JNI::ReleaseLongArrayElements,
2811 JNI::ReleaseFloatArrayElements,
2812 JNI::ReleaseDoubleArrayElements,
2813 JNI::GetBooleanArrayRegion,
2814 JNI::GetByteArrayRegion,
2815 JNI::GetCharArrayRegion,
2816 JNI::GetShortArrayRegion,
2817 JNI::GetIntArrayRegion,
2818 JNI::GetLongArrayRegion,
2819 JNI::GetFloatArrayRegion,
2820 JNI::GetDoubleArrayRegion,
2821 JNI::SetBooleanArrayRegion,
2822 JNI::SetByteArrayRegion,
2823 JNI::SetCharArrayRegion,
2824 JNI::SetShortArrayRegion,
2825 JNI::SetIntArrayRegion,
2826 JNI::SetLongArrayRegion,
2827 JNI::SetFloatArrayRegion,
2828 JNI::SetDoubleArrayRegion,
2829 JNI::RegisterNatives,
2830 JNI::UnregisterNatives,
2831 JNI::MonitorEnter,
2832 JNI::MonitorExit,
2833 JNI::GetJavaVM,
2834 JNI::GetStringRegion,
2835 JNI::GetStringUTFRegion,
2836 JNI::GetPrimitiveArrayCritical,
2837 JNI::ReleasePrimitiveArrayCritical,
2838 JNI::GetStringCritical,
2839 JNI::ReleaseStringCritical,
2840 JNI::NewWeakGlobalRef,
2841 JNI::DeleteWeakGlobalRef,
2842 JNI::ExceptionCheck,
2843 JNI::NewDirectByteBuffer,
2844 JNI::GetDirectBufferAddress,
2845 JNI::GetDirectBufferCapacity,
2846 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002847};
2848
Elliott Hughes75770752011-08-24 17:52:38 -07002849JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002850 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002851 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002852 local_ref_cookie(IRT_FIRST_SEGMENT),
2853 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002854 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002855 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002856 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002857 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002858 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002859 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002860 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002861}
2862
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002863JNIEnvExt::~JNIEnvExt() {
2864}
2865
Mathieu Chartier590fee92013-09-13 13:46:47 -07002866jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2867 if (obj == nullptr) {
2868 return nullptr;
2869 }
2870 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2871}
2872
2873void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2874 if (obj != nullptr) {
2875 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2876 }
2877}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002878void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2879 check_jni = enabled;
2880 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002881}
2882
Elliott Hughes73e66f72012-05-09 09:34:45 -07002883void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2884 locals.Dump(os);
2885 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002886}
2887
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002888void JNIEnvExt::PushFrame(int /*capacity*/) {
2889 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002890 stacked_local_ref_cookies.push_back(local_ref_cookie);
2891 local_ref_cookie = locals.GetSegmentState();
2892}
2893
2894void JNIEnvExt::PopFrame() {
2895 locals.SetSegmentState(local_ref_cookie);
2896 local_ref_cookie = stacked_local_ref_cookies.back();
2897 stacked_local_ref_cookies.pop_back();
2898}
2899
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002900Offset JNIEnvExt::SegmentStateOffset() {
2901 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2902 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2903}
2904
Carl Shapiroea4dca82011-08-01 13:45:38 -07002905// JNI Invocation interface.
2906
Brian Carlstrombddf9762013-05-14 11:35:37 -07002907extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002908 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002909 if (IsBadJniVersion(args->version)) {
2910 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002911 return JNI_EVERSION;
2912 }
2913 Runtime::Options options;
2914 for (int i = 0; i < args->nOptions; ++i) {
2915 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002916 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002917 }
2918 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002919 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002920 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002921 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002922 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002923 bool started = runtime->Start();
2924 if (!started) {
2925 delete Thread::Current()->GetJniEnv();
2926 delete runtime->GetJavaVM();
2927 LOG(WARNING) << "CreateJavaVM failed";
2928 return JNI_ERR;
2929 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002930 *p_env = Thread::Current()->GetJniEnv();
2931 *p_vm = runtime->GetJavaVM();
2932 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002933}
2934
Elliott Hughesf2682d52011-08-15 16:37:04 -07002935extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002936 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002937 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002938 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002939 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002940 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002941 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002942 }
2943 return JNI_OK;
2944}
2945
2946// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002947extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002948 return JNI_ERR;
2949}
2950
Elliott Hughescdf53122011-08-19 15:46:09 -07002951class JII {
2952 public:
2953 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002954 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002955 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002956 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002957 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2958 delete raw_vm->runtime;
2959 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002960 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002961
Elliott Hughescdf53122011-08-19 15:46:09 -07002962 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002963 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002964 }
2965
2966 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002967 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002968 }
2969
2970 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002971 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002972 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002973 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002974 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2975 Runtime* runtime = raw_vm->runtime;
2976 runtime->DetachCurrentThread();
2977 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002978 }
2979
2980 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002981 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2982 // and unlike other calls that take a JNI version doesn't care if you supply
2983 // JNI_VERSION_1_1, which we don't otherwise support.
2984 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002985 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002986 return JNI_EVERSION;
2987 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002988 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002989 return JNI_ERR;
2990 }
2991 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002992 if (thread == nullptr) {
2993 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002994 return JNI_EDETACHED;
2995 }
2996 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002997 return JNI_OK;
2998 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002999};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003000
Elliott Hughes88c5c352012-03-15 18:49:48 -07003001const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003002 nullptr, // reserved0
3003 nullptr, // reserved1
3004 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003005 JII::DestroyJavaVM,
3006 JII::AttachCurrentThread,
3007 JII::DetachCurrentThread,
3008 JII::GetEnv,
3009 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003010};
3011
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003012JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003013 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003014 check_jni_abort_hook(nullptr),
3015 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003016 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003017 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003018 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003019 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003020 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003021 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003022 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003023 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003024 libraries(new Libraries),
3025 weak_globals_lock_("JNI weak global reference table lock"),
3026 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3027 allow_new_weak_globals_(true),
3028 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003029 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003030 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003031 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003032 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003033}
3034
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003035JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003036 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003037}
3038
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003039jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3040 if (obj == nullptr) {
3041 return nullptr;
3042 }
3043 MutexLock mu(self, weak_globals_lock_);
3044 while (UNLIKELY(!allow_new_weak_globals_)) {
3045 weak_globals_add_condition_.WaitHoldingLocks(self);
3046 }
3047 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3048 return reinterpret_cast<jweak>(ref);
3049}
3050
3051void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3052 MutexLock mu(self, weak_globals_lock_);
3053 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3054 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3055 << "failed to find entry";
3056 }
3057}
3058
Elliott Hughes88c5c352012-03-15 18:49:48 -07003059void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3060 check_jni = enabled;
3061 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003062}
3063
Elliott Hughesae80b492012-04-24 10:43:17 -07003064void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3065 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3066 if (force_copy) {
3067 os << " (with forcecopy)";
3068 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003069 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003070 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003071 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003072 os << "; pins=" << pin_table.Size();
3073 }
3074 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003075 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003076 os << "; globals=" << globals.Capacity();
3077 }
3078 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003079 MutexLock mu(self, weak_globals_lock_);
3080 if (weak_globals_.Capacity() > 0) {
3081 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003082 }
3083 }
3084 os << '\n';
3085
3086 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003087 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003088 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3089 }
3090}
3091
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003092void JavaVMExt::DisallowNewWeakGlobals() {
3093 MutexLock mu(Thread::Current(), weak_globals_lock_);
3094 allow_new_weak_globals_ = false;
3095}
3096
3097void JavaVMExt::AllowNewWeakGlobals() {
3098 Thread* self = Thread::Current();
3099 MutexLock mu(self, weak_globals_lock_);
3100 allow_new_weak_globals_ = true;
3101 weak_globals_add_condition_.Broadcast(self);
3102}
3103
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003104mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3105 MutexLock mu(self, weak_globals_lock_);
3106 while (UNLIKELY(!allow_new_weak_globals_)) {
3107 weak_globals_add_condition_.WaitHoldingLocks(self);
3108 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003109 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003110}
3111
Elliott Hughes73e66f72012-05-09 09:34:45 -07003112void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003113 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003114 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003115 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003116 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003117 }
3118 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003119 MutexLock mu(self, weak_globals_lock_);
3120 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003121 }
3122 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003123 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003124 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003125 }
3126}
3127
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003128bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003129 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003130 std::string* detail) {
3131 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003132
3133 // See if we've already loaded this library. If we have, and the class loader
3134 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003135 // TODO: for better results we should canonicalize the pathname (or even compare
3136 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003137 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003138 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003139 {
3140 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003141 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003142 library = libraries->Get(path);
3143 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003144 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003145 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003146 // The library will be associated with class_loader. The JNI
3147 // spec says we can't load the same library into more than one
3148 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003149 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003150 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003151 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003152 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003153 return false;
3154 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003155 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003156 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003157 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003158 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003159 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003160 return false;
3161 }
3162 return true;
3163 }
3164
3165 // Open the shared library. Because we're using a full path, the system
3166 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3167 // resolve this library's dependencies though.)
3168
3169 // Failures here are expected when java.library.path has several entries
3170 // and we have to hunt for the lib.
3171
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003172 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3173 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3174 // dlopen) becomes zero from dlclose.
3175
Elliott Hughescdf53122011-08-19 15:46:09 -07003176 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003177 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003178 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003179 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003180 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003181
Elliott Hughes84b2f142012-09-27 09:16:28 -07003182 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003183
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003184 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003185 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003186 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003187 return false;
3188 }
3189
3190 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003191 // TODO: move the locking (and more of this logic) into Libraries.
3192 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003193 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003194 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003195 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003196 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003197 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003198 libraries->Put(path, library);
3199 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003200 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003201 }
3202 if (!created_library) {
3203 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003204 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003205 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003206 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003207
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003208 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3209 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003210
Elliott Hughes79353722013-08-02 16:52:18 -07003211 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003212 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003213 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003214 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003215 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003216 } else {
3217 // Call JNI_OnLoad. We have to override the current class
3218 // loader, which will always be "null" since the stuff at the
3219 // top of the stack is around Runtime.loadLibrary(). (See
3220 // the comments in the JNI FindClass function.)
3221 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3222 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003223 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003224 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003225
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003226 int version = 0;
3227 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003228 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003229 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003230 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003231 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003232
Mathieu Chartier590fee92013-09-13 13:46:47 -07003233 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003234
Elliott Hughes79353722013-08-02 16:52:18 -07003235 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003236 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003237 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003238 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003239 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003240 // It's unwise to call dlclose() here, but we can mark it
3241 // as bad and ensure that future load attempts will fail.
3242 // We don't know how far JNI_OnLoad got, so there could
3243 // be some partially-initialized stuff accessible through
3244 // newly-registered native method calls. We could try to
3245 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003246 } else {
3247 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003248 }
Elliott Hughes79353722013-08-02 16:52:18 -07003249 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003250 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003251 }
3252
Elliott Hughes79353722013-08-02 16:52:18 -07003253 library->SetResult(was_successful);
3254 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003255}
3256
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003257void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003258 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003259 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003260 // If this is a static method, it could be called before the class
3261 // has been initialized.
3262 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003263 c = EnsureInitialized(Thread::Current(), c);
3264 if (c == nullptr) {
3265 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003266 }
3267 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003268 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003269 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003270 std::string detail;
3271 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003272 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003273 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003274 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003275 native_method = libraries->FindNativeMethod(m, detail);
3276 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003277 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003278 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003279 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3280 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003281 }
3282 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003283}
3284
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003285void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003286 MutexLock mu(Thread::Current(), weak_globals_lock_);
3287 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003288 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003289 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003290 if (new_obj == nullptr) {
3291 new_obj = kClearedJniWeakGlobal;
3292 }
3293 *entry = new_obj;
3294 }
3295}
3296
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003297void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003298 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003299 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003300 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003301 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003302 }
3303 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003304 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003305 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003306 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003307 {
3308 MutexLock mu(self, libraries_lock);
3309 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003310 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003311 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003312 // The weak_globals table is visited by the GC itself (because it mutates the table).
3313}
3314
Elliott Hughesc8fece32013-01-02 11:27:23 -08003315void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003316 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003317 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003318 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003319 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3320 }
3321 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3322}
3323
Ian Rogersdf20fe02011-07-20 20:34:16 -07003324} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003325
3326std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3327 switch (rhs) {
3328 case JNIInvalidRefType:
3329 os << "JNIInvalidRefType";
3330 return os;
3331 case JNILocalRefType:
3332 os << "JNILocalRefType";
3333 return os;
3334 case JNIGlobalRefType:
3335 os << "JNIGlobalRefType";
3336 return os;
3337 case JNIWeakGlobalRefType:
3338 os << "JNIWeakGlobalRefType";
3339 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003340 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003341 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003342 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003343 }
3344}