blob: 915f2c90b5bff7d003a974f1488a610d3124b095 [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
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700518#define CHECK_NON_NULL_ARGUMENT(value) CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value)
519
520#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800521 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700522 JniAbortF(name, #value " == null"); \
Ian Rogersbc939662013-08-15 10:26:54 -0700523 }
524
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700525#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800526 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700527 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700528 }
529
Elliott Hughescdf53122011-08-19 15:46:09 -0700530class JNI {
531 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700532 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700533 return JNI_VERSION_1_6;
534 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700535
Ian Rogers25e8b912012-09-07 11:31:36 -0700536 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700537 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800538 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700539 }
540
Elliott Hughescdf53122011-08-19 15:46:09 -0700541 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700542 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700543 Runtime* runtime = Runtime::Current();
544 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700546 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800547 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700548 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700549 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800550 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700551 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800552 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700553 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700554 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700555 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700556
Ian Rogers62f05122014-03-21 11:21:29 -0700557 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700558 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700559 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700560 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700561 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700562
Ian Rogers62f05122014-03-21 11:21:29 -0700563 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700564 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700566 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700568
Elliott Hughescdf53122011-08-19 15:46:09 -0700569 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700570 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800572 mirror::ArtMethod* m = soa.DecodeMethod(mid);
573 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700574 jobject art_method = soa.AddLocalReference<jobject>(m);
575 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
576 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800577 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700578 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800579 SetObjectField(env, reflect_method,
580 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700581 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700582 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700583
Elliott Hughescdf53122011-08-19 15:46:09 -0700584 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700585 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700586 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800587 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700588 jobject art_field = soa.AddLocalReference<jobject>(f);
589 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
590 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800593 SetObjectField(env, reflect_field,
594 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700595 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700596 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700597
Elliott Hughes37f7a402011-08-22 18:56:01 -0700598 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700599 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800601 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700603 }
604
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700605 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700606 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800608 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700611
Elliott Hughes37f7a402011-08-22 18:56:01 -0700612 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700613 CHECK_NON_NULL_ARGUMENT(java_class1);
614 CHECK_NON_NULL_ARGUMENT(java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800616 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
617 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700618 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700620
Elliott Hughese84278b2012-03-22 10:06:53 -0700621 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700622 CHECK_NON_NULL_ARGUMENT(java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800623 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700624 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700625 return JNI_TRUE;
626 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700627 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800628 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
629 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700630 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700631 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughes37f7a402011-08-22 18:56:01 -0700634 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800636 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
637 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 return JNI_ERR;
639 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800640 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
641 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700642 return JNI_OK;
643 }
644
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700645 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700646 CHECK_NON_NULL_ARGUMENT(c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800647 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700648 }
649
650 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700651 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700652 }
653
654 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700655 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700656 }
657
658 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700660
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800661 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
662 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
663 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800664 uint32_t old_throw_dex_pc;
665 {
666 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800667 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 old_throw_this_object.reset(old_throw_location.GetThis());
669 old_throw_method.reset(old_throw_location.GetMethod());
670 old_exception.reset(old_exception_obj);
671 old_throw_dex_pc = old_throw_location.GetDexPc();
672 soa.Self()->ClearException();
673 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800674 ScopedLocalRef<jthrowable> exception(env,
675 soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700676 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
677 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800678 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700679 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800680 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700681 } else {
682 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800683 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800684 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700685 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 }
688 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
690 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700691
Ian Rogers62d6c772013-02-27 08:32:07 -0800692 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800697 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
700
Ian Rogers25e8b912012-09-07 11:31:36 -0700701 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700702 LOG(FATAL) << "JNI FatalError called: " << msg;
703 }
704
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700705 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800706 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700707 return JNI_ERR;
708 }
Ian Rogersef28b142012-11-30 14:22:18 -0800709 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 return JNI_OK;
711 }
712
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700713 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800715 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 soa.Env()->PopFrame();
717 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 }
719
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700720 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800721 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700722 }
723
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700725 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800726 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800727 // Check for null after decoding the object to handle cleared weak globals.
728 if (decoded_obj == nullptr) {
729 return nullptr;
730 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700731 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700733 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 return reinterpret_cast<jobject>(ref);
736 }
737
738 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800739 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 return;
741 }
Ian Rogersef28b142012-11-30 14:22:18 -0800742 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800744 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700745 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700746
747 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
748 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
749 << "failed to find entry";
750 }
751 }
752
753 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800755 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 }
757
758 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700759 if (obj != nullptr) {
760 ScopedObjectAccess soa(env);
761 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 }
763 }
764
765 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700766 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800767 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800768 // Check for null after decoding the object to handle cleared weak globals.
769 if (decoded_obj == nullptr) {
770 return nullptr;
771 }
772 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 }
774
775 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800776 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 return;
778 }
Ian Rogersef28b142012-11-30 14:22:18 -0800779 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700780
Ian Rogersef28b142012-11-30 14:22:18 -0800781 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 if (!locals.Remove(cookie, obj)) {
783 // Attempting to delete a local reference that is not in the
784 // topmost local reference frame is a no-op. DeleteLocalRef returns
785 // void and doesn't throw any exceptions, but we should probably
786 // complain about it so the user will notice that things aren't
787 // going quite the way they expect.
788 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
789 << "failed to find entry";
790 }
791 }
792
793 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700794 if (obj1 == obj2) {
795 return JNI_TRUE;
796 } else {
797 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800798 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
799 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700800 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 }
802
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700803 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700804 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700805 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800806 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800807 if (c == nullptr) {
808 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700809 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700810 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 }
812
Ian Rogersbc939662013-08-15 10:26:54 -0700813 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700815 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700816 CHECK_NON_NULL_ARGUMENT(java_class);
817 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700818 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 va_end(args);
820 return result;
821 }
822
Elliott Hughes72025e52011-08-23 17:50:30 -0700823 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700824 CHECK_NON_NULL_ARGUMENT(java_class);
825 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800827 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800828 if (c == nullptr) {
829 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700830 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800831 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800832 if (result == nullptr) {
833 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700834 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700836 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800837 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800838 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700839 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800840 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 }
842
Elliott Hughes72025e52011-08-23 17:50:30 -0700843 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700844 CHECK_NON_NULL_ARGUMENT(java_class);
845 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800847 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800848 if (c == nullptr) {
849 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700850 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800851 mirror::Object* result = c->AllocObject(soa.Self());
852 if (result == nullptr) {
853 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700854 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700855 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800857 if (soa.Self()->IsExceptionPending()) {
858 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700859 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800860 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 }
862
Ian Rogersbc939662013-08-15 10:26:54 -0700863 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700864 CHECK_NON_NULL_ARGUMENT(java_class);
865 CHECK_NON_NULL_ARGUMENT(name);
866 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700868 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 }
870
Ian Rogersbc939662013-08-15 10:26:54 -0700871 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
872 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700873 CHECK_NON_NULL_ARGUMENT(java_class);
874 CHECK_NON_NULL_ARGUMENT(name);
875 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700876 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700877 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 }
879
Elliott Hughes72025e52011-08-23 17:50:30 -0700880 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 va_list ap;
882 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700883 CHECK_NON_NULL_ARGUMENT(obj);
884 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700885 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 }
890
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700892 CHECK_NON_NULL_ARGUMENT(obj);
893 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 ScopedObjectAccess soa(env);
895 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
896 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700900 CHECK_NON_NULL_ARGUMENT(obj);
901 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700903 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
904 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700905 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 va_list ap;
910 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700911 CHECK_NON_NULL_ARGUMENT(obj);
912 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700913 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700916 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 }
918
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700920 CHECK_NON_NULL_ARGUMENT(obj);
921 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700922 ScopedObjectAccess soa(env);
923 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700927 CHECK_NON_NULL_ARGUMENT(obj);
928 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700930 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
931 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 }
933
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 va_list ap;
936 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700937 CHECK_NON_NULL_ARGUMENT(obj);
938 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700939 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700942 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 }
944
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700946 CHECK_NON_NULL_ARGUMENT(obj);
947 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700948 ScopedObjectAccess soa(env);
949 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700953 CHECK_NON_NULL_ARGUMENT(obj);
954 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700956 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
957 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_list ap;
962 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700963 CHECK_NON_NULL_ARGUMENT(obj);
964 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700965 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700968 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700972 CHECK_NON_NULL_ARGUMENT(obj);
973 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 ScopedObjectAccess soa(env);
975 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700979 CHECK_NON_NULL_ARGUMENT(obj);
980 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700981 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700982 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
983 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_list ap;
988 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700989 CHECK_NON_NULL_ARGUMENT(obj);
990 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700991 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700994 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700998 CHECK_NON_NULL_ARGUMENT(obj);
999 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001000 ScopedObjectAccess soa(env);
1001 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001005 CHECK_NON_NULL_ARGUMENT(obj);
1006 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001008 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1009 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_list ap;
1014 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001015 CHECK_NON_NULL_ARGUMENT(obj);
1016 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001017 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001020 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001024 CHECK_NON_NULL_ARGUMENT(obj);
1025 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026 ScopedObjectAccess soa(env);
1027 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001031 CHECK_NON_NULL_ARGUMENT(obj);
1032 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001034 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1035 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_list ap;
1040 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001041 CHECK_NON_NULL_ARGUMENT(obj);
1042 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001043 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001046 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001050 CHECK_NON_NULL_ARGUMENT(obj);
1051 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 ScopedObjectAccess soa(env);
1053 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001057 CHECK_NON_NULL_ARGUMENT(obj);
1058 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001059 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001060 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1061 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_list ap;
1066 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001067 CHECK_NON_NULL_ARGUMENT(obj);
1068 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001069 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001072 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001076 CHECK_NON_NULL_ARGUMENT(obj);
1077 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
1079 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001083 CHECK_NON_NULL_ARGUMENT(obj);
1084 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001086 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1087 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001093 CHECK_NON_NULL_ARGUMENT(obj);
1094 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001095 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001098 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001102 CHECK_NON_NULL_ARGUMENT(obj);
1103 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 ScopedObjectAccess soa(env);
1105 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001109 CHECK_NON_NULL_ARGUMENT(obj);
1110 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001112 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1113 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001119 CHECK_NON_NULL_ARGUMENT(obj);
1120 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001121 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001122 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001127 CHECK_NON_NULL_ARGUMENT(obj);
1128 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 ScopedObjectAccess soa(env);
1130 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001134 CHECK_NON_NULL_ARGUMENT(obj);
1135 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001137 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001140 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001143 CHECK_NON_NULL_ARGUMENT(obj);
1144 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001145 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1147 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 va_end(ap);
1149 return local_result;
1150 }
1151
Ian Rogersbc939662013-08-15 10:26:54 -07001152 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1153 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001154 CHECK_NON_NULL_ARGUMENT(obj);
1155 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1158 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Ian Rogersbc939662013-08-15 10:26:54 -07001161 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1162 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001163 CHECK_NON_NULL_ARGUMENT(obj);
1164 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001166 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Ian Rogersbc939662013-08-15 10:26:54 -07001170 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1171 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001174 CHECK_NON_NULL_ARGUMENT(obj);
1175 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001176 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001179 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Ian Rogersbc939662013-08-15 10:26:54 -07001182 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1183 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001184 CHECK_NON_NULL_ARGUMENT(obj);
1185 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 ScopedObjectAccess soa(env);
1187 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Ian Rogersbc939662013-08-15 10:26:54 -07001190 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1191 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001192 CHECK_NON_NULL_ARGUMENT(obj);
1193 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001195 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001198 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001201 CHECK_NON_NULL_ARGUMENT(obj);
1202 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001203 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001206 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
Ian Rogersbc939662013-08-15 10:26:54 -07001209 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1210 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001211 CHECK_NON_NULL_ARGUMENT(obj);
1212 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213 ScopedObjectAccess soa(env);
1214 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Ian Rogersbc939662013-08-15 10:26:54 -07001217 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1218 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001219 CHECK_NON_NULL_ARGUMENT(obj);
1220 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001222 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001225 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001228 CHECK_NON_NULL_ARGUMENT(obj);
1229 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001230 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001233 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Ian Rogersbc939662013-08-15 10:26:54 -07001236 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1237 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001238 CHECK_NON_NULL_ARGUMENT(obj);
1239 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 ScopedObjectAccess soa(env);
1241 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 }
1243
Ian Rogersbc939662013-08-15 10:26:54 -07001244 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1245 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001246 CHECK_NON_NULL_ARGUMENT(obj);
1247 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001248 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001249 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001252 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001255 CHECK_NON_NULL_ARGUMENT(obj);
1256 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001257 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001258 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001260 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
Ian Rogersbc939662013-08-15 10:26:54 -07001263 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1264 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001265 CHECK_NON_NULL_ARGUMENT(obj);
1266 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001267 ScopedObjectAccess soa(env);
1268 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Ian Rogersbc939662013-08-15 10:26:54 -07001271 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1272 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001273 CHECK_NON_NULL_ARGUMENT(obj);
1274 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001276 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001279 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001282 CHECK_NON_NULL_ARGUMENT(obj);
1283 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001284 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001287 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Ian Rogersbc939662013-08-15 10:26:54 -07001290 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1291 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001292 CHECK_NON_NULL_ARGUMENT(obj);
1293 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001294 ScopedObjectAccess soa(env);
1295 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Ian Rogersbc939662013-08-15 10:26:54 -07001298 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1299 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001300 CHECK_NON_NULL_ARGUMENT(obj);
1301 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001302 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001303 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001309 CHECK_NON_NULL_ARGUMENT(obj);
1310 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001311 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001314 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Ian Rogersbc939662013-08-15 10:26:54 -07001317 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1318 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001319 CHECK_NON_NULL_ARGUMENT(obj);
1320 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
1322 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Ian Rogersbc939662013-08-15 10:26:54 -07001325 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1326 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001327 CHECK_NON_NULL_ARGUMENT(obj);
1328 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001330 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001336 CHECK_NON_NULL_ARGUMENT(obj);
1337 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001338 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001341 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
Ian Rogersbc939662013-08-15 10:26:54 -07001344 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1345 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001346 CHECK_NON_NULL_ARGUMENT(obj);
1347 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001348 ScopedObjectAccess soa(env);
1349 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Ian Rogersbc939662013-08-15 10:26:54 -07001352 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1353 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001354 CHECK_NON_NULL_ARGUMENT(obj);
1355 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001356 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001357 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001360 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001363 CHECK_NON_NULL_ARGUMENT(obj);
1364 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001365 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001368 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Ian Rogersbc939662013-08-15 10:26:54 -07001371 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1372 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001373 CHECK_NON_NULL_ARGUMENT(obj);
1374 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001375 ScopedObjectAccess soa(env);
1376 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Ian Rogersbc939662013-08-15 10:26:54 -07001379 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1380 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001381 CHECK_NON_NULL_ARGUMENT(obj);
1382 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001383 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001384 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001390 CHECK_NON_NULL_ARGUMENT(obj);
1391 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001392 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 va_end(ap);
1395 }
1396
Brian Carlstromea46f952013-07-30 01:26:50 -07001397 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1398 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001399 CHECK_NON_NULL_ARGUMENT(obj);
1400 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001401 ScopedObjectAccess soa(env);
1402 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Ian Rogersbc939662013-08-15 10:26:54 -07001405 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1406 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001407 CHECK_NON_NULL_ARGUMENT(obj);
1408 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001409 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001410 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
Ian Rogersbc939662013-08-15 10:26:54 -07001413 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001414 CHECK_NON_NULL_ARGUMENT(java_class);
1415 CHECK_NON_NULL_ARGUMENT(name);
1416 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001418 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001420
Ian Rogersbc939662013-08-15 10:26:54 -07001421 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1422 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001423 CHECK_NON_NULL_ARGUMENT(java_class);
1424 CHECK_NON_NULL_ARGUMENT(name);
1425 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001426 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001427 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001431 CHECK_NON_NULL_ARGUMENT(obj);
1432 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001434 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1435 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001436 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001440 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001441 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001442 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001443 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001447 CHECK_NON_NULL_ARGUMENT(java_object);
1448 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001449 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001450 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1451 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1452 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001453 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001457 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001458 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001459 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1460 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001461 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464#define GET_PRIMITIVE_FIELD(fn, instance) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001465 CHECK_NON_NULL_ARGUMENT(instance); \
1466 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001468 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1469 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001470 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001472#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001473 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001474 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001475 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001476 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001477
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001479 CHECK_NON_NULL_ARGUMENT(instance); \
1480 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001481 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001482 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1483 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001484 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001485
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001486#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001487 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001488 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001489 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001490 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001493 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001497 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001501 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001505 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001509 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001513 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001517 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001521 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001525 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001532 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001533 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001537 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001549 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 }
1555
1556 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 }
1559
1560 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 }
1563
1564 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 }
1567
1568 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 }
1571
1572 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 }
1575
1576 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 }
1579
1580 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 }
1583
1584 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 }
1587
1588 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 }
1591
1592 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 }
1595
1596 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 }
1599
1600 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 }
1603
1604 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 }
1607
1608 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 }
1611
1612 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 }
1615
1616 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001622 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001623 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001624 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001625 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 va_end(ap);
1628 return local_result;
1629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001632 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001633 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001634 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001635 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001638 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001639 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001640 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001641 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001642 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001648 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001649 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001650 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001652 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001656 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001658 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001662 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001664 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001667 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001670 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001671 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001672 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001674 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001677 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001678 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001680 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001684 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001686 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001692 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001693 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001694 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001696 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001699 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001700 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001701 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001702 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001705 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001706 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001708 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001714 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001715 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001716 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001718 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001721 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001722 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001723 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001724 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001728 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001730 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001736 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001737 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001738 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001740 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001744 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001746 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001750 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001752 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001758 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001759 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001760 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001762 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001765 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001766 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001767 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001768 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001772 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001774 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001780 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001781 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001782 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001784 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001788 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001790 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001793 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001794 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001801 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001802 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001803 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001804 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001806 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001809 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001810 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001812 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001816 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001817 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001818 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001823 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001824 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001826 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 va_end(ap);
1828 }
1829
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001830 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001831 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001832 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001833 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001836 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001837 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001838 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001839 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Elliott Hughes814e4032011-08-23 12:07:56 -07001842 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001843 if (UNLIKELY(char_count < 0)) {
1844 JniAbortF("NewString", "char_count < 0: %d", char_count);
1845 return nullptr;
1846 }
1847 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1848 JniAbortF("NewString", "chars == null && char_count > 0");
1849 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001850 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001852 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001853 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
1856 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 if (utf == nullptr) {
1858 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001861 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes814e4032011-08-23 12:07:56 -07001865 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001866 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001868 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001869 }
1870
1871 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001872 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001874 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001875 }
1876
Ian Rogersbc939662013-08-15 10:26:54 -07001877 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1878 jchar* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001879 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001881 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001882 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001883 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001884 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001885 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001886 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1887 memcpy(buf, chars + start, length * sizeof(jchar));
1888 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001889 }
1890
Ian Rogersbc939662013-08-15 10:26:54 -07001891 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1892 char* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001893 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001894 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001895 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001898 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001899 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001900 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1901 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1902 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001903 }
1904
Elliott Hughes75770752011-08-24 17:52:38 -07001905 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001906 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001908 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1909 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001910 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001911 if (is_copy != nullptr) {
1912 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001913 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001914 int32_t char_count = s->GetLength();
1915 int32_t offset = s->GetOffset();
1916 jchar* bytes = new jchar[char_count + 1];
1917 for (int32_t i = 0; i < char_count; i++) {
1918 bytes[i] = chars->Get(i + offset);
1919 }
1920 bytes[char_count] = '\0';
1921 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001922 }
1923
Mathieu Chartier590fee92013-09-13 13:46:47 -07001924 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001925 CHECK_NON_NULL_ARGUMENT(java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001926 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001928 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
Elliott Hughes75770752011-08-24 17:52:38 -07001931 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001932 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001936 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes75770752011-08-24 17:52:38 -07001939 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001940 if (java_string == nullptr) {
1941 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001942 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001943 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001944 *is_copy = JNI_TRUE;
1945 }
Ian Rogersef28b142012-11-30 14:22:18 -08001946 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001947 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001948 size_t byte_count = s->GetUtfLength();
1949 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001950 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001951 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1952 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1953 bytes[byte_count] = '\0';
1954 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001958 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 }
1960
Elliott Hughesbd935992011-08-22 11:59:34 -07001961 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001962 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001963 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001964 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001965 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001966 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1967 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001968 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001969 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes814e4032011-08-23 12:07:56 -07001972 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001973 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001975 mirror::ObjectArray<mirror::Object>* array =
1976 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001977 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Ian Rogersbc939662013-08-15 10:26:54 -07001980 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1981 jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001982 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001984 mirror::ObjectArray<mirror::Object>* array =
1985 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1986 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001987 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
1990 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001992 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
1995 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001997 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
2000 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002002 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
2005 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002006 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002007 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
2010 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002012 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
2015 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002016 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002017 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
2020 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002022 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Ian Rogers1d99e452014-01-02 17:36:41 -08002025 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2026 jobject initial_element) {
2027 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002028 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002029 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002030 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002031
2032 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002033 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002034 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002035 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002036 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002037 if (UNLIKELY(element_class->IsPrimitive())) {
2038 JniAbortF("NewObjectArray", "not an object type: %s",
2039 PrettyDescriptor(element_class).c_str());
2040 return nullptr;
2041 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002042 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002043 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002044 if (UNLIKELY(array_class == nullptr)) {
2045 return nullptr;
2046 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002050 mirror::ObjectArray<mirror::Object>* result =
2051 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002052 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002053 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002054 if (initial_object != nullptr) {
2055 mirror::Class* element_class = result->GetClass()->GetComponentType();
2056 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2057 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2058 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2059 PrettyDescriptor(element_class).c_str());
2060
2061 } else {
2062 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002063 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002064 }
2065 }
Elliott Hughes75770752011-08-24 17:52:38 -07002066 }
2067 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
2071 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002072 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002073 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Ian Rogersa15e67d2012-02-28 13:51:55 -08002076 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002077 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002078 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002079 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002080 gc::Heap* heap = Runtime::Current()->GetHeap();
2081 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002082 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002083 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002084 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002085 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002086 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002087 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002088 *is_copy = JNI_FALSE;
2089 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002090 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002091 }
2092
Mathieu Chartier590fee92013-09-13 13:46:47 -07002093 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002094 CHECK_NON_NULL_ARGUMENT(array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002095 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002096 }
2097
Elliott Hughes75770752011-08-24 17:52:38 -07002098 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002099 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002101 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes75770752011-08-24 17:52:38 -07002104 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002105 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002107 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes75770752011-08-24 17:52:38 -07002110 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002111 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002112 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002113 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes75770752011-08-24 17:52:38 -07002116 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002117 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002118 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002119 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes75770752011-08-24 17:52:38 -07002122 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002123 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002124 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002125 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes75770752011-08-24 17:52:38 -07002128 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002129 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002130 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002131 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes75770752011-08-24 17:52:38 -07002134 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002135 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002136 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002137 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughes75770752011-08-24 17:52:38 -07002140 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002141 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002142 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002143 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Mathieu Chartier590fee92013-09-13 13:46:47 -07002146 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2147 jint mode) {
2148 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Mathieu Chartier590fee92013-09-13 13:46:47 -07002151 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2152 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Mathieu Chartier590fee92013-09-13 13:46:47 -07002155 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2156 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Mathieu Chartier590fee92013-09-13 13:46:47 -07002159 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2160 jint mode) {
2161 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163
Mathieu Chartier590fee92013-09-13 13:46:47 -07002164 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2165 jint mode) {
2166 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Mathieu Chartier590fee92013-09-13 13:46:47 -07002169 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2170 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Mathieu Chartier590fee92013-09-13 13:46:47 -07002173 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2174 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Mathieu Chartier590fee92013-09-13 13:46:47 -07002177 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2178 jint mode) {
2179 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Ian Rogersbc939662013-08-15 10:26:54 -07002182 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2183 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002185 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2186 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Ian Rogersbc939662013-08-15 10:26:54 -07002189 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2190 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002192 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Ian Rogersbc939662013-08-15 10:26:54 -07002195 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2196 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002198 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
Ian Rogersbc939662013-08-15 10:26:54 -07002201 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2202 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002203 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002204 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2205 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 Rogers00f7d0e2012-07-19 15:28:27 -07002210 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002211 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2212 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Ian Rogersbc939662013-08-15 10:26:54 -07002215 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2216 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002217 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002218 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Ian Rogersbc939662013-08-15 10:26:54 -07002221 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2222 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002223 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002224 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Ian Rogersbc939662013-08-15 10:26:54 -07002227 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2228 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002229 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002230 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2231 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Ian Rogersbc939662013-08-15 10:26:54 -07002234 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2235 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002237 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2238 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Ian Rogersbc939662013-08-15 10:26:54 -07002241 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2242 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002243 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002244 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Ian Rogersbc939662013-08-15 10:26:54 -07002247 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2248 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002250 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Ian Rogersbc939662013-08-15 10:26:54 -07002253 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2254 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002255 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002256 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2257 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259
Ian Rogersbc939662013-08-15 10:26:54 -07002260 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2261 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002262 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002263 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2264 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
Ian Rogersbc939662013-08-15 10:26:54 -07002267 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2268 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002269 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002270 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272
Ian Rogersbc939662013-08-15 10:26:54 -07002273 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2274 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002275 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002276 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Ian Rogersbc939662013-08-15 10:26:54 -07002279 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2280 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002282 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2283 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285
Ian Rogersbc939662013-08-15 10:26:54 -07002286 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2287 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002288 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2289 }
2290
Ian Rogersbc939662013-08-15 10:26:54 -07002291 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2292 jint method_count, bool return_errors) {
2293 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002294 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002295 return JNI_ERR; // Not reached.
2296 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002297 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002298 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002299 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002300 if (UNLIKELY(method_count == 0)) {
2301 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2302 << PrettyDescriptor(c);
2303 return JNI_OK;
2304 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002305 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods);
Ian Rogersbc939662013-08-15 10:26:54 -07002306 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 const char* name = methods[i].name;
2308 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002309 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002311 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 ++sig;
2313 }
2314
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002315 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2316 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002317 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002319 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002320 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002321 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002322 << PrettyDescriptor(c) << "." << name << sig << " in "
2323 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002326 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002327 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002328 << PrettyDescriptor(c) << "." << name << sig
2329 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 return JNI_ERR;
2332 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002333
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002334 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002335
Ian Rogers1eb512d2013-10-18 15:42:20 -07002336 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002337 }
2338 return JNI_OK;
2339 }
2340
Elliott Hughes5174fe62011-08-23 15:12:35 -07002341 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002342 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002343 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002344 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002345
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002346 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002347
2348 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002349 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002350 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002351 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002352 }
2353 }
2354 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002355 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002356 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002357 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002358 }
2359 }
2360
2361 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363
Ian Rogers719d1a32014-03-06 12:13:39 -08002364 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002365 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002366 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002367 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2368 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002369 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002370 return JNI_ERR;
2371 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002373 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002374 }
2375
Ian Rogers719d1a32014-03-06 12:13:39 -08002376 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002377 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002379 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002380 o->MonitorExit(soa.Self());
2381 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002382 return JNI_ERR;
2383 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002385 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002386 }
2387
2388 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002389 CHECK_NON_NULL_ARGUMENT(vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002390 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002391 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002392 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002393 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002394 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002395 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002396 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002397 }
2398
Elliott Hughescdf53122011-08-19 15:46:09 -07002399 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002400 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002401 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002402 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002403 if (address == nullptr && capacity != 0) {
2404 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002405 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002406
Ian Rogers936b37f2014-02-14 00:52:24 -08002407 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002408 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002409 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002410 jint capacity_arg = static_cast<jint>(capacity);
2411
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002412 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2413 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002414 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002415 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002416 }
2417
Elliott Hughesb465ab02011-08-24 11:21:21 -07002418 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002419 return reinterpret_cast<void*>(env->GetLongField(
2420 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002421 }
2422
Elliott Hughesb465ab02011-08-24 11:21:21 -07002423 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002424 return static_cast<jlong>(env->GetIntField(
2425 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002426 }
2427
Elliott Hughesb465ab02011-08-24 11:21:21 -07002428 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002429 CHECK_NON_NULL_ARGUMENT(java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002430
2431 // Do we definitely know what kind of reference this is?
2432 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2433 IndirectRefKind kind = GetIndirectRefKind(ref);
2434 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002435 case kLocal: {
2436 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002437 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002438 return JNILocalRefType;
2439 }
2440 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002441 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002442 case kGlobal:
2443 return JNIGlobalRefType;
2444 case kWeakGlobal:
2445 return JNIWeakGlobalRefType;
2446 case kSirtOrInvalid:
2447 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002448 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002449 return JNILocalRefType;
2450 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002451 return JNIInvalidRefType;
2452 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002453 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2454 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002455 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002456
2457 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002458 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2459 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002460 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002461 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002462 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2463 return JNI_ERR;
2464 }
2465 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002466 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002467 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2468 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002469 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002470 soa.Self()->ThrowOutOfMemoryError(caller);
2471 }
2472 return okay ? JNI_OK : JNI_ERR;
2473 }
2474
2475 template<typename JniT, typename ArtT>
2476 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002478 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002479 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002480 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002481 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002482 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 return soa.AddLocalReference<JniT>(result);
2484 }
2485
2486 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2487 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2488 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002489 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002490 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2491 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002492 // Only make a copy if necessary.
2493 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2494 if (is_copy != nullptr) {
2495 *is_copy = JNI_TRUE;
2496 }
2497 static const size_t component_size = array->GetClass()->GetComponentSize();
2498 size_t size = array->GetLength() * component_size;
2499 void* data = new uint64_t[RoundUp(size, 8) / 8];
2500 memcpy(data, array->GetData(), size);
2501 return reinterpret_cast<CArrayT>(data);
2502 } else {
2503 if (is_copy != nullptr) {
2504 *is_copy = JNI_FALSE;
2505 }
2506 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002507 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002508 }
2509
Mathieu Chartier590fee92013-09-13 13:46:47 -07002510 template <typename ArrayT, typename ElementT>
2511 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2512 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002513 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002514 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002515 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002516 gc::Heap* heap = Runtime::Current()->GetHeap();
2517 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2518 size_t bytes = array->GetLength() * component_size;
2519 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2520 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002521 if (is_copy) {
2522 // Sanity check: If elements is not the same as the java array's data, it better not be a
2523 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2524 // copies we make?
2525 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2526 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2527 reinterpret_cast<void*>(elements), array_data);
2528 return;
2529 }
2530 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002531 // Don't need to copy if we had a direct pointer.
2532 if (mode != JNI_ABORT && is_copy) {
2533 memcpy(array_data, elements, bytes);
2534 }
2535 if (mode != JNI_COMMIT) {
2536 if (is_copy) {
2537 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002538 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002539 // Non copy to a movable object must means that we had disabled the moving GC.
2540 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002541 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002542 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002543 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002544 }
2545
2546 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2547 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2548 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002549 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002550 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002551 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2552 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2553 ThrowAIOOBE(soa, array, start, length, "src");
2554 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002555 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002556 JavaT* data = array->GetData();
2557 memcpy(buf, data + start, length * sizeof(JavaT));
2558 }
2559 }
2560
2561 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2562 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2563 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002564 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002565 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002566 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2567 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2568 ThrowAIOOBE(soa, array, start, length, "dst");
2569 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002570 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002571 JavaT* data = array->GetData();
2572 memcpy(data + start, buf, length * sizeof(JavaT));
2573 }
2574 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002575};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002576
Elliott Hughes88c5c352012-03-15 18:49:48 -07002577const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002578 nullptr, // reserved0.
2579 nullptr, // reserved1.
2580 nullptr, // reserved2.
2581 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002582 JNI::GetVersion,
2583 JNI::DefineClass,
2584 JNI::FindClass,
2585 JNI::FromReflectedMethod,
2586 JNI::FromReflectedField,
2587 JNI::ToReflectedMethod,
2588 JNI::GetSuperclass,
2589 JNI::IsAssignableFrom,
2590 JNI::ToReflectedField,
2591 JNI::Throw,
2592 JNI::ThrowNew,
2593 JNI::ExceptionOccurred,
2594 JNI::ExceptionDescribe,
2595 JNI::ExceptionClear,
2596 JNI::FatalError,
2597 JNI::PushLocalFrame,
2598 JNI::PopLocalFrame,
2599 JNI::NewGlobalRef,
2600 JNI::DeleteGlobalRef,
2601 JNI::DeleteLocalRef,
2602 JNI::IsSameObject,
2603 JNI::NewLocalRef,
2604 JNI::EnsureLocalCapacity,
2605 JNI::AllocObject,
2606 JNI::NewObject,
2607 JNI::NewObjectV,
2608 JNI::NewObjectA,
2609 JNI::GetObjectClass,
2610 JNI::IsInstanceOf,
2611 JNI::GetMethodID,
2612 JNI::CallObjectMethod,
2613 JNI::CallObjectMethodV,
2614 JNI::CallObjectMethodA,
2615 JNI::CallBooleanMethod,
2616 JNI::CallBooleanMethodV,
2617 JNI::CallBooleanMethodA,
2618 JNI::CallByteMethod,
2619 JNI::CallByteMethodV,
2620 JNI::CallByteMethodA,
2621 JNI::CallCharMethod,
2622 JNI::CallCharMethodV,
2623 JNI::CallCharMethodA,
2624 JNI::CallShortMethod,
2625 JNI::CallShortMethodV,
2626 JNI::CallShortMethodA,
2627 JNI::CallIntMethod,
2628 JNI::CallIntMethodV,
2629 JNI::CallIntMethodA,
2630 JNI::CallLongMethod,
2631 JNI::CallLongMethodV,
2632 JNI::CallLongMethodA,
2633 JNI::CallFloatMethod,
2634 JNI::CallFloatMethodV,
2635 JNI::CallFloatMethodA,
2636 JNI::CallDoubleMethod,
2637 JNI::CallDoubleMethodV,
2638 JNI::CallDoubleMethodA,
2639 JNI::CallVoidMethod,
2640 JNI::CallVoidMethodV,
2641 JNI::CallVoidMethodA,
2642 JNI::CallNonvirtualObjectMethod,
2643 JNI::CallNonvirtualObjectMethodV,
2644 JNI::CallNonvirtualObjectMethodA,
2645 JNI::CallNonvirtualBooleanMethod,
2646 JNI::CallNonvirtualBooleanMethodV,
2647 JNI::CallNonvirtualBooleanMethodA,
2648 JNI::CallNonvirtualByteMethod,
2649 JNI::CallNonvirtualByteMethodV,
2650 JNI::CallNonvirtualByteMethodA,
2651 JNI::CallNonvirtualCharMethod,
2652 JNI::CallNonvirtualCharMethodV,
2653 JNI::CallNonvirtualCharMethodA,
2654 JNI::CallNonvirtualShortMethod,
2655 JNI::CallNonvirtualShortMethodV,
2656 JNI::CallNonvirtualShortMethodA,
2657 JNI::CallNonvirtualIntMethod,
2658 JNI::CallNonvirtualIntMethodV,
2659 JNI::CallNonvirtualIntMethodA,
2660 JNI::CallNonvirtualLongMethod,
2661 JNI::CallNonvirtualLongMethodV,
2662 JNI::CallNonvirtualLongMethodA,
2663 JNI::CallNonvirtualFloatMethod,
2664 JNI::CallNonvirtualFloatMethodV,
2665 JNI::CallNonvirtualFloatMethodA,
2666 JNI::CallNonvirtualDoubleMethod,
2667 JNI::CallNonvirtualDoubleMethodV,
2668 JNI::CallNonvirtualDoubleMethodA,
2669 JNI::CallNonvirtualVoidMethod,
2670 JNI::CallNonvirtualVoidMethodV,
2671 JNI::CallNonvirtualVoidMethodA,
2672 JNI::GetFieldID,
2673 JNI::GetObjectField,
2674 JNI::GetBooleanField,
2675 JNI::GetByteField,
2676 JNI::GetCharField,
2677 JNI::GetShortField,
2678 JNI::GetIntField,
2679 JNI::GetLongField,
2680 JNI::GetFloatField,
2681 JNI::GetDoubleField,
2682 JNI::SetObjectField,
2683 JNI::SetBooleanField,
2684 JNI::SetByteField,
2685 JNI::SetCharField,
2686 JNI::SetShortField,
2687 JNI::SetIntField,
2688 JNI::SetLongField,
2689 JNI::SetFloatField,
2690 JNI::SetDoubleField,
2691 JNI::GetStaticMethodID,
2692 JNI::CallStaticObjectMethod,
2693 JNI::CallStaticObjectMethodV,
2694 JNI::CallStaticObjectMethodA,
2695 JNI::CallStaticBooleanMethod,
2696 JNI::CallStaticBooleanMethodV,
2697 JNI::CallStaticBooleanMethodA,
2698 JNI::CallStaticByteMethod,
2699 JNI::CallStaticByteMethodV,
2700 JNI::CallStaticByteMethodA,
2701 JNI::CallStaticCharMethod,
2702 JNI::CallStaticCharMethodV,
2703 JNI::CallStaticCharMethodA,
2704 JNI::CallStaticShortMethod,
2705 JNI::CallStaticShortMethodV,
2706 JNI::CallStaticShortMethodA,
2707 JNI::CallStaticIntMethod,
2708 JNI::CallStaticIntMethodV,
2709 JNI::CallStaticIntMethodA,
2710 JNI::CallStaticLongMethod,
2711 JNI::CallStaticLongMethodV,
2712 JNI::CallStaticLongMethodA,
2713 JNI::CallStaticFloatMethod,
2714 JNI::CallStaticFloatMethodV,
2715 JNI::CallStaticFloatMethodA,
2716 JNI::CallStaticDoubleMethod,
2717 JNI::CallStaticDoubleMethodV,
2718 JNI::CallStaticDoubleMethodA,
2719 JNI::CallStaticVoidMethod,
2720 JNI::CallStaticVoidMethodV,
2721 JNI::CallStaticVoidMethodA,
2722 JNI::GetStaticFieldID,
2723 JNI::GetStaticObjectField,
2724 JNI::GetStaticBooleanField,
2725 JNI::GetStaticByteField,
2726 JNI::GetStaticCharField,
2727 JNI::GetStaticShortField,
2728 JNI::GetStaticIntField,
2729 JNI::GetStaticLongField,
2730 JNI::GetStaticFloatField,
2731 JNI::GetStaticDoubleField,
2732 JNI::SetStaticObjectField,
2733 JNI::SetStaticBooleanField,
2734 JNI::SetStaticByteField,
2735 JNI::SetStaticCharField,
2736 JNI::SetStaticShortField,
2737 JNI::SetStaticIntField,
2738 JNI::SetStaticLongField,
2739 JNI::SetStaticFloatField,
2740 JNI::SetStaticDoubleField,
2741 JNI::NewString,
2742 JNI::GetStringLength,
2743 JNI::GetStringChars,
2744 JNI::ReleaseStringChars,
2745 JNI::NewStringUTF,
2746 JNI::GetStringUTFLength,
2747 JNI::GetStringUTFChars,
2748 JNI::ReleaseStringUTFChars,
2749 JNI::GetArrayLength,
2750 JNI::NewObjectArray,
2751 JNI::GetObjectArrayElement,
2752 JNI::SetObjectArrayElement,
2753 JNI::NewBooleanArray,
2754 JNI::NewByteArray,
2755 JNI::NewCharArray,
2756 JNI::NewShortArray,
2757 JNI::NewIntArray,
2758 JNI::NewLongArray,
2759 JNI::NewFloatArray,
2760 JNI::NewDoubleArray,
2761 JNI::GetBooleanArrayElements,
2762 JNI::GetByteArrayElements,
2763 JNI::GetCharArrayElements,
2764 JNI::GetShortArrayElements,
2765 JNI::GetIntArrayElements,
2766 JNI::GetLongArrayElements,
2767 JNI::GetFloatArrayElements,
2768 JNI::GetDoubleArrayElements,
2769 JNI::ReleaseBooleanArrayElements,
2770 JNI::ReleaseByteArrayElements,
2771 JNI::ReleaseCharArrayElements,
2772 JNI::ReleaseShortArrayElements,
2773 JNI::ReleaseIntArrayElements,
2774 JNI::ReleaseLongArrayElements,
2775 JNI::ReleaseFloatArrayElements,
2776 JNI::ReleaseDoubleArrayElements,
2777 JNI::GetBooleanArrayRegion,
2778 JNI::GetByteArrayRegion,
2779 JNI::GetCharArrayRegion,
2780 JNI::GetShortArrayRegion,
2781 JNI::GetIntArrayRegion,
2782 JNI::GetLongArrayRegion,
2783 JNI::GetFloatArrayRegion,
2784 JNI::GetDoubleArrayRegion,
2785 JNI::SetBooleanArrayRegion,
2786 JNI::SetByteArrayRegion,
2787 JNI::SetCharArrayRegion,
2788 JNI::SetShortArrayRegion,
2789 JNI::SetIntArrayRegion,
2790 JNI::SetLongArrayRegion,
2791 JNI::SetFloatArrayRegion,
2792 JNI::SetDoubleArrayRegion,
2793 JNI::RegisterNatives,
2794 JNI::UnregisterNatives,
2795 JNI::MonitorEnter,
2796 JNI::MonitorExit,
2797 JNI::GetJavaVM,
2798 JNI::GetStringRegion,
2799 JNI::GetStringUTFRegion,
2800 JNI::GetPrimitiveArrayCritical,
2801 JNI::ReleasePrimitiveArrayCritical,
2802 JNI::GetStringCritical,
2803 JNI::ReleaseStringCritical,
2804 JNI::NewWeakGlobalRef,
2805 JNI::DeleteWeakGlobalRef,
2806 JNI::ExceptionCheck,
2807 JNI::NewDirectByteBuffer,
2808 JNI::GetDirectBufferAddress,
2809 JNI::GetDirectBufferCapacity,
2810 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002811};
2812
Elliott Hughes75770752011-08-24 17:52:38 -07002813JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002814 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002815 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002816 local_ref_cookie(IRT_FIRST_SEGMENT),
2817 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002818 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002819 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002820 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002821 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002822 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002823 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002824 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002825}
2826
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002827JNIEnvExt::~JNIEnvExt() {
2828}
2829
Mathieu Chartier590fee92013-09-13 13:46:47 -07002830jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2831 if (obj == nullptr) {
2832 return nullptr;
2833 }
2834 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2835}
2836
2837void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2838 if (obj != nullptr) {
2839 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2840 }
2841}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002842void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2843 check_jni = enabled;
2844 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002845}
2846
Elliott Hughes73e66f72012-05-09 09:34:45 -07002847void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2848 locals.Dump(os);
2849 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002850}
2851
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002852void JNIEnvExt::PushFrame(int /*capacity*/) {
2853 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002854 stacked_local_ref_cookies.push_back(local_ref_cookie);
2855 local_ref_cookie = locals.GetSegmentState();
2856}
2857
2858void JNIEnvExt::PopFrame() {
2859 locals.SetSegmentState(local_ref_cookie);
2860 local_ref_cookie = stacked_local_ref_cookies.back();
2861 stacked_local_ref_cookies.pop_back();
2862}
2863
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002864Offset JNIEnvExt::SegmentStateOffset() {
2865 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2866 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2867}
2868
Carl Shapiroea4dca82011-08-01 13:45:38 -07002869// JNI Invocation interface.
2870
Brian Carlstrombddf9762013-05-14 11:35:37 -07002871extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002872 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002873 if (IsBadJniVersion(args->version)) {
2874 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002875 return JNI_EVERSION;
2876 }
2877 Runtime::Options options;
2878 for (int i = 0; i < args->nOptions; ++i) {
2879 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002880 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002881 }
2882 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002883 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002884 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002885 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002886 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002887 bool started = runtime->Start();
2888 if (!started) {
2889 delete Thread::Current()->GetJniEnv();
2890 delete runtime->GetJavaVM();
2891 LOG(WARNING) << "CreateJavaVM failed";
2892 return JNI_ERR;
2893 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002894 *p_env = Thread::Current()->GetJniEnv();
2895 *p_vm = runtime->GetJavaVM();
2896 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002897}
2898
Elliott Hughesf2682d52011-08-15 16:37:04 -07002899extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002900 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002901 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002902 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002903 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002904 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002905 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002906 }
2907 return JNI_OK;
2908}
2909
2910// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002911extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002912 return JNI_ERR;
2913}
2914
Elliott Hughescdf53122011-08-19 15:46:09 -07002915class JII {
2916 public:
2917 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002918 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002919 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002920 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002921 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2922 delete raw_vm->runtime;
2923 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002924 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002925
Elliott Hughescdf53122011-08-19 15:46:09 -07002926 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002927 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002928 }
2929
2930 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002931 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002932 }
2933
2934 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002935 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002936 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002937 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002938 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2939 Runtime* runtime = raw_vm->runtime;
2940 runtime->DetachCurrentThread();
2941 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002942 }
2943
2944 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002945 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2946 // and unlike other calls that take a JNI version doesn't care if you supply
2947 // JNI_VERSION_1_1, which we don't otherwise support.
2948 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002949 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002950 return JNI_EVERSION;
2951 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002952 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002953 return JNI_ERR;
2954 }
2955 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002956 if (thread == nullptr) {
2957 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002958 return JNI_EDETACHED;
2959 }
2960 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002961 return JNI_OK;
2962 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002963};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002964
Elliott Hughes88c5c352012-03-15 18:49:48 -07002965const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002966 nullptr, // reserved0
2967 nullptr, // reserved1
2968 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002969 JII::DestroyJavaVM,
2970 JII::AttachCurrentThread,
2971 JII::DetachCurrentThread,
2972 JII::GetEnv,
2973 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002974};
2975
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002976JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002977 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002978 check_jni_abort_hook(nullptr),
2979 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002980 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002981 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002982 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08002983 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002984 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002985 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002986 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002987 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002988 libraries(new Libraries),
2989 weak_globals_lock_("JNI weak global reference table lock"),
2990 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2991 allow_new_weak_globals_(true),
2992 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002993 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002994 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002995 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002996 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002997}
2998
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002999JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003000 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003001}
3002
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003003jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3004 if (obj == nullptr) {
3005 return nullptr;
3006 }
3007 MutexLock mu(self, weak_globals_lock_);
3008 while (UNLIKELY(!allow_new_weak_globals_)) {
3009 weak_globals_add_condition_.WaitHoldingLocks(self);
3010 }
3011 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3012 return reinterpret_cast<jweak>(ref);
3013}
3014
3015void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3016 MutexLock mu(self, weak_globals_lock_);
3017 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3018 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3019 << "failed to find entry";
3020 }
3021}
3022
Elliott Hughes88c5c352012-03-15 18:49:48 -07003023void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3024 check_jni = enabled;
3025 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003026}
3027
Elliott Hughesae80b492012-04-24 10:43:17 -07003028void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3029 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3030 if (force_copy) {
3031 os << " (with forcecopy)";
3032 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003033 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003034 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003035 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003036 os << "; pins=" << pin_table.Size();
3037 }
3038 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003039 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003040 os << "; globals=" << globals.Capacity();
3041 }
3042 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003043 MutexLock mu(self, weak_globals_lock_);
3044 if (weak_globals_.Capacity() > 0) {
3045 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003046 }
3047 }
3048 os << '\n';
3049
3050 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003051 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003052 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3053 }
3054}
3055
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003056void JavaVMExt::DisallowNewWeakGlobals() {
3057 MutexLock mu(Thread::Current(), weak_globals_lock_);
3058 allow_new_weak_globals_ = false;
3059}
3060
3061void JavaVMExt::AllowNewWeakGlobals() {
3062 Thread* self = Thread::Current();
3063 MutexLock mu(self, weak_globals_lock_);
3064 allow_new_weak_globals_ = true;
3065 weak_globals_add_condition_.Broadcast(self);
3066}
3067
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003068mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3069 MutexLock mu(self, weak_globals_lock_);
3070 while (UNLIKELY(!allow_new_weak_globals_)) {
3071 weak_globals_add_condition_.WaitHoldingLocks(self);
3072 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003073 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003074}
3075
Elliott Hughes73e66f72012-05-09 09:34:45 -07003076void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003077 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003078 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003079 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003080 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003081 }
3082 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003083 MutexLock mu(self, weak_globals_lock_);
3084 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003085 }
3086 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003087 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003088 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003089 }
3090}
3091
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003092bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003093 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003094 std::string* detail) {
3095 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003096
3097 // See if we've already loaded this library. If we have, and the class loader
3098 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003099 // TODO: for better results we should canonicalize the pathname (or even compare
3100 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003101 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003102 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003103 {
3104 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003105 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003106 library = libraries->Get(path);
3107 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003108 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003109 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003110 // The library will be associated with class_loader. The JNI
3111 // spec says we can't load the same library into more than one
3112 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003113 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003114 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003115 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003116 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003117 return false;
3118 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003119 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003120 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003121 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003122 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003123 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003124 return false;
3125 }
3126 return true;
3127 }
3128
3129 // Open the shared library. Because we're using a full path, the system
3130 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3131 // resolve this library's dependencies though.)
3132
3133 // Failures here are expected when java.library.path has several entries
3134 // and we have to hunt for the lib.
3135
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003136 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3137 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3138 // dlopen) becomes zero from dlclose.
3139
Elliott Hughescdf53122011-08-19 15:46:09 -07003140 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003141 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003142 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003143 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003144 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003145
Elliott Hughes84b2f142012-09-27 09:16:28 -07003146 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003147
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003148 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003149 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003150 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003151 return false;
3152 }
3153
3154 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003155 // TODO: move the locking (and more of this logic) into Libraries.
3156 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003157 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003158 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003159 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003160 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003161 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003162 libraries->Put(path, library);
3163 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003164 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003165 }
3166 if (!created_library) {
3167 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003168 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003169 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003170 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003171
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003172 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3173 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003174
Elliott Hughes79353722013-08-02 16:52:18 -07003175 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003176 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003177 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003178 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003179 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003180 } else {
3181 // Call JNI_OnLoad. We have to override the current class
3182 // loader, which will always be "null" since the stuff at the
3183 // top of the stack is around Runtime.loadLibrary(). (See
3184 // the comments in the JNI FindClass function.)
3185 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3186 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003187 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003188 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003189
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003190 int version = 0;
3191 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003192 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003193 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003194 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003195 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003196
Mathieu Chartier590fee92013-09-13 13:46:47 -07003197 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003198
Elliott Hughes79353722013-08-02 16:52:18 -07003199 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003200 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003201 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003202 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003203 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003204 // It's unwise to call dlclose() here, but we can mark it
3205 // as bad and ensure that future load attempts will fail.
3206 // We don't know how far JNI_OnLoad got, so there could
3207 // be some partially-initialized stuff accessible through
3208 // newly-registered native method calls. We could try to
3209 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003210 } else {
3211 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003212 }
Elliott Hughes79353722013-08-02 16:52:18 -07003213 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003214 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003215 }
3216
Elliott Hughes79353722013-08-02 16:52:18 -07003217 library->SetResult(was_successful);
3218 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003219}
3220
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003221void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003222 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003223 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003224 // If this is a static method, it could be called before the class
3225 // has been initialized.
3226 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003227 c = EnsureInitialized(Thread::Current(), c);
3228 if (c == nullptr) {
3229 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003230 }
3231 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003232 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003233 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003234 std::string detail;
3235 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003236 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003237 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003238 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003239 native_method = libraries->FindNativeMethod(m, detail);
3240 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003241 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003242 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003243 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3244 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003245 }
3246 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003247}
3248
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003249void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003250 MutexLock mu(Thread::Current(), weak_globals_lock_);
3251 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003252 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003253 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003254 if (new_obj == nullptr) {
3255 new_obj = kClearedJniWeakGlobal;
3256 }
3257 *entry = new_obj;
3258 }
3259}
3260
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003261void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003262 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003263 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003264 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003265 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003266 }
3267 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003268 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003269 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003270 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003271 {
3272 MutexLock mu(self, libraries_lock);
3273 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003274 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003275 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003276 // The weak_globals table is visited by the GC itself (because it mutates the table).
3277}
3278
Elliott Hughesc8fece32013-01-02 11:27:23 -08003279void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003280 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003281 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003282 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003283 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3284 }
3285 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3286}
3287
Ian Rogersdf20fe02011-07-20 20:34:16 -07003288} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003289
3290std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3291 switch (rhs) {
3292 case JNIInvalidRefType:
3293 os << "JNIInvalidRefType";
3294 return os;
3295 case JNILocalRefType:
3296 os << "JNILocalRefType";
3297 return os;
3298 case JNIGlobalRefType:
3299 os << "JNIGlobalRefType";
3300 return os;
3301 case JNIWeakGlobalRefType:
3302 os << "JNIWeakGlobalRefType";
3303 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003304 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003305 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003306 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003307 }
3308}