blob: e6a35d071f1a6051d38d979738992e6b979e4683 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Ian Rogersef7d42f2014-01-06 12:55:46 -080025#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080029#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070033#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class-inl.h"
37#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070040#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080043#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070044#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070045#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070048#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070051#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070052#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070053
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070054namespace art {
55
Brian Carlstrom7934ac22013-07-26 10:54:15 -070056static const size_t kMonitorsInitial = 32; // Arbitrary.
57static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070058
Brian Carlstrom7934ac22013-07-26 10:54:15 -070059static const size_t kLocalsInitial = 64; // Arbitrary.
60static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070061
Brian Carlstrom7934ac22013-07-26 10:54:15 -070062static const size_t kPinTableInitial = 16; // Arbitrary.
63static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070064
Brian Carlstrom7934ac22013-07-26 10:54:15 -070065static size_t gGlobalsInitial = 512; // Arbitrary.
66static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070067
Brian Carlstrom7934ac22013-07-26 10:54:15 -070068static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
69static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070070
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080071static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070073 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070074}
75
Jeff Hao19c5d372013-03-15 14:33:43 -070076static bool IsBadJniVersion(int version) {
77 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
78 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
79}
80
Elliott Hughes6b436852011-08-12 10:16:44 -070081// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
82// separated with slashes but aren't wrapped with "L;" like regular descriptors
83// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
84// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
85// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070086static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070087 std::string result;
88 // Add the missing "L;" if necessary.
89 if (name[0] == '[') {
90 result = name;
91 } else {
92 result += 'L';
93 result += name;
94 result += ';';
95 }
96 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070097 if (result.find('.') != std::string::npos) {
98 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
99 << "\"" << name << "\"";
100 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700101 }
102 return result;
103}
104
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800105static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
109 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
110 "no %s method \"%s.%s%s\"",
111 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700112}
113
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800114static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
116 if (LIKELY(klass->IsInitialized())) {
117 return klass;
118 }
119 SirtRef<mirror::Class> sirt_klass(self, klass);
120 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
121 return nullptr;
122 }
123 return sirt_klass.get();
124}
125
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
127 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800129 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800130 if (c == nullptr) {
131 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700132 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800133 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700134 if (is_static) {
135 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700136 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700137 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800138 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700139 // No virtual method matching the signature. Search declared
140 // private methods and constructors.
141 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700142 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700143 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800144 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800146 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700147 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700148 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700149}
150
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800151static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800153 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700154 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
155 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800156 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700157 }
Brian Carlstromce888532013-10-10 00:32:58 -0700158 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800159 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700160 return method->GetDeclaringClass()->GetClassLoader();
161 }
162 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800163 mirror::ClassLoader* class_loader =
164 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
165 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700166 return class_loader;
167 }
168 // See if the override ClassLoader is set for gtests.
169 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800170 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800171 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700172 CHECK(Runtime::Current()->UseCompileTimeClassPath());
173 return class_loader;
174 }
175 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800176 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700177}
178
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
180 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800182 SirtRef<mirror::Class> c(soa.Self(), EnsureInitialized(soa.Self(),
183 soa.Decode<mirror::Class*>(jni_class)));
184 if (c.get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800185 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700186 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800187 mirror::ArtField* field = nullptr;
188 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
190 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800191 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Ian Rogers98379392014-02-24 16:53:16 -0800192 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700193 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700195 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800196 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 ThrowLocation throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800200 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800203 "no type \"%s\" found and so no field \"%s\" "
204 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800205 ClassHelper(c.get()).GetDescriptor());
206 soa.Self()->GetException(nullptr)->SetCause(cause.get());
207 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 }
209 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800210 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800214 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
216 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
217 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800218 sig, name, ClassHelper(c.get()).GetDescriptor());
219 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700220 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700222}
223
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800224static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700227 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700228 vm->pin_table.Add(array);
229}
230
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800231static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700234 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700235 vm->pin_table.Remove(array);
236}
237
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800238static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700241 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800242 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
243 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
244 "%s offset=%d length=%d %s.length=%d",
245 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700246}
Ian Rogers0571d352011-11-03 19:51:38 -0700247
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
249 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
252 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
253 "offset=%d length=%d string.length()=%d", start, length,
254 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700255}
Elliott Hughes814e4032011-08-23 12:07:56 -0700256
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 // Turn the const char* into a java.lang.String.
260 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800261 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700263 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 // Choose an appropriate constructor and set up the arguments.
266 jvalue args[2];
267 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800268 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800270 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 signature = "(Ljava/lang/String;)V";
272 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800273 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 signature = "(Ljava/lang/Throwable;)V";
275 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700276 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
278 args[0].l = s.get();
279 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700280 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800282 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800283 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800285 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 return JNI_ERR;
287 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700288
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800289 ScopedLocalRef<jthrowable> exception(
290 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
291 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 return JNI_ERR;
293 }
Ian Rogersef28b142012-11-30 14:22:18 -0800294 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800296 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700298}
299
Elliott Hughes462c9442012-03-23 18:47:50 -0700300static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800301 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700302 return JNI_ERR;
303 }
304
Elliott Hughes462c9442012-03-23 18:47:50 -0700305 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700306 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800307 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700308 *p_env = self->GetJniEnv();
309 return JNI_OK;
310 }
311
312 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
313
314 // No threads allowed in zygote mode.
315 if (runtime->IsZygote()) {
316 LOG(ERROR) << "Attempt to attach a thread in the zygote";
317 return JNI_ERR;
318 }
319
Elliott Hughes462c9442012-03-23 18:47:50 -0700320 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800321 const char* thread_name = nullptr;
322 jobject thread_group = nullptr;
323 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700324 if (IsBadJniVersion(args->version)) {
325 LOG(ERROR) << "Bad JNI version passed to "
326 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
327 << args->version;
328 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700329 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700330 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700331 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700332 }
Elliott Hughes75770752011-08-24 17:52:38 -0700333
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800334 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800335 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700336 return JNI_ERR;
337 } else {
338 *p_env = Thread::Current()->GetJniEnv();
339 return JNI_OK;
340 }
Elliott Hughes75770752011-08-24 17:52:38 -0700341}
342
Elliott Hughes79082e32011-08-25 12:07:32 -0700343class SharedLibrary {
344 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800345 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700346 : path_(path),
347 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700348 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700349 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700350 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700351 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700352 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700353 }
354
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800355 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700356 return class_loader_;
357 }
358
359 std::string GetPath() {
360 return path_;
361 }
362
363 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700364 * Check the result of an earlier call to JNI_OnLoad on this library.
365 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700366 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700367 bool CheckOnLoadResult()
368 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700369 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700370 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
372 bool okay;
373 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700374 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700375
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700376 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
378 // caller can continue.
379 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
380 okay = true;
381 } else {
382 while (jni_on_load_result_ == kPending) {
383 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700384 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700386
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 okay = (jni_on_load_result_ == kOkay);
388 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
389 << (okay ? "succeeded" : "failed") << "]";
390 }
391 }
392 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700393 return okay;
394 }
395
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700397 Thread* self = Thread::Current();
398 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700399
Elliott Hughes79082e32011-08-25 12:07:32 -0700400 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700401 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700402
403 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700404 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700405 }
406
407 void* FindSymbol(const std::string& symbol_name) {
408 return dlsym(handle_, symbol_name.c_str());
409 }
410
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800411 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800412 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800413 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800414 }
415 }
416
Elliott Hughes79082e32011-08-25 12:07:32 -0700417 private:
418 enum JNI_OnLoadState {
419 kPending,
420 kFailed,
421 kOkay,
422 };
423
424 // Path to library "/system/lib/libjni.so".
425 std::string path_;
426
427 // The void* returned by dlopen(3).
428 void* handle_;
429
430 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800431 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700432
433 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700435 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700436 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700437 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700438 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700439 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700440 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700441};
442
Elliott Hughes79082e32011-08-25 12:07:32 -0700443// This exists mainly to keep implementation details out of the header file.
444class Libraries {
445 public:
446 Libraries() {
447 }
448
449 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700450 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700451 }
452
Elliott Hughesae80b492012-04-24 10:43:17 -0700453 void Dump(std::ostream& os) const {
454 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700455 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700456 if (!first) {
457 os << ' ';
458 }
459 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700460 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700461 }
462 }
463
464 size_t size() const {
465 return libraries_.size();
466 }
467
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700469 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800470 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 }
472
473 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700474 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700475 }
476
477 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800478 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700480 std::string jni_short_name(JniShortName(m));
481 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800482 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700483 for (const auto& lib : libraries_) {
484 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 if (library->GetClassLoader() != declaring_class_loader) {
486 // We only search libraries loaded by the appropriate ClassLoader.
487 continue;
488 }
489 // Try the short name then the long name...
490 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800491 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 fn = library->FindSymbol(jni_long_name);
493 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800494 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800495 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
496 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 return fn;
498 }
499 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700501 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700502 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700503 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800504 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 }
506
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800507 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800508 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800509 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800510 }
511 }
512
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700514 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700515};
516
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700517#define CHECK_NON_NULL_ARGUMENT(value) CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value)
518
519#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800520 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700521 JniAbortF(name, #value " == null"); \
Ian Rogersbc939662013-08-15 10:26:54 -0700522 }
523
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700524#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800525 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700526 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700527 }
528
Elliott Hughescdf53122011-08-19 15:46:09 -0700529class JNI {
530 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700531 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700532 return JNI_VERSION_1_6;
533 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700534
Ian Rogers25e8b912012-09-07 11:31:36 -0700535 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700536 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800537 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700538 }
539
Elliott Hughescdf53122011-08-19 15:46:09 -0700540 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700541 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700542 Runtime* runtime = Runtime::Current();
543 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700544 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700545 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800546 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700547 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700548 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800549 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700550 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800551 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700552 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700553 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700554 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700555
Ian Rogers62f05122014-03-21 11:21:29 -0700556 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700557 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700559 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700560 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700561
Ian Rogers62f05122014-03-21 11:21:29 -0700562 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700563 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700564 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700565 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700566 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700567
Elliott Hughescdf53122011-08-19 15:46:09 -0700568 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700569 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700570 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800571 mirror::ArtMethod* m = soa.DecodeMethod(mid);
572 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700573 jobject art_method = soa.AddLocalReference<jobject>(m);
574 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
575 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800576 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800578 SetObjectField(env, reflect_method,
579 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700580 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700581 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700582
Elliott Hughescdf53122011-08-19 15:46:09 -0700583 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700584 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800586 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700587 jobject art_field = soa.AddLocalReference<jobject>(f);
588 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
589 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800590 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700591 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800592 SetObjectField(env, reflect_field,
593 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700595 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700596
Elliott Hughes37f7a402011-08-22 18:56:01 -0700597 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700598 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800600 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700601 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700602 }
603
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700604 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700605 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800607 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700608 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700610
Elliott Hughes37f7a402011-08-22 18:56:01 -0700611 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700612 CHECK_NON_NULL_ARGUMENT(java_class1);
613 CHECK_NON_NULL_ARGUMENT(java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800615 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
616 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700617 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Elliott Hughese84278b2012-03-22 10:06:53 -0700620 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700621 CHECK_NON_NULL_ARGUMENT(java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800622 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700623 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700624 return JNI_TRUE;
625 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700626 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800627 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
628 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700629 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700630 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700632
Elliott Hughes37f7a402011-08-22 18:56:01 -0700633 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800635 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
636 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700637 return JNI_ERR;
638 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
640 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700641 return JNI_OK;
642 }
643
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700644 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700645 CHECK_NON_NULL_ARGUMENT(c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800646 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700647 }
648
649 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700650 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700651 }
652
653 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700654 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700655 }
656
657 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700659
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800660 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
661 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
662 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800663 uint32_t old_throw_dex_pc;
664 {
665 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800666 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800667 old_throw_this_object.reset(old_throw_location.GetThis());
668 old_throw_method.reset(old_throw_location.GetMethod());
669 old_exception.reset(old_exception_obj);
670 old_throw_dex_pc = old_throw_location.GetDexPc();
671 soa.Self()->ClearException();
672 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800673 ScopedLocalRef<jthrowable> exception(env,
674 soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700675 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
676 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800677 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700678 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800679 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700680 } else {
681 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800683 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700684 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700686 }
687 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
689 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700690
Ian Rogers62d6c772013-02-27 08:32:07 -0800691 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700693
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800696 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 }
699
Ian Rogers25e8b912012-09-07 11:31:36 -0700700 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 LOG(FATAL) << "JNI FatalError called: " << msg;
702 }
703
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700704 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800705 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700706 return JNI_ERR;
707 }
Ian Rogersef28b142012-11-30 14:22:18 -0800708 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 return JNI_OK;
710 }
711
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700712 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800714 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 soa.Env()->PopFrame();
716 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 }
718
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700719 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800720 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 }
722
Elliott Hughescdf53122011-08-19 15:46:09 -0700723 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700724 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800725 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800726 // Check for null after decoding the object to handle cleared weak globals.
727 if (decoded_obj == nullptr) {
728 return nullptr;
729 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700732 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700733 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 return reinterpret_cast<jobject>(ref);
735 }
736
737 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800738 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 return;
740 }
Ian Rogersef28b142012-11-30 14:22:18 -0800741 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800743 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700744 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700745
746 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
747 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
748 << "failed to find entry";
749 }
750 }
751
752 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800754 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 }
756
757 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700758 if (obj != nullptr) {
759 ScopedObjectAccess soa(env);
760 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 }
762 }
763
764 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700765 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800766 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800767 // Check for null after decoding the object to handle cleared weak globals.
768 if (decoded_obj == nullptr) {
769 return nullptr;
770 }
771 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 }
773
774 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800775 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 return;
777 }
Ian Rogersef28b142012-11-30 14:22:18 -0800778 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700779
Ian Rogersef28b142012-11-30 14:22:18 -0800780 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 if (!locals.Remove(cookie, obj)) {
782 // Attempting to delete a local reference that is not in the
783 // topmost local reference frame is a no-op. DeleteLocalRef returns
784 // void and doesn't throw any exceptions, but we should probably
785 // complain about it so the user will notice that things aren't
786 // going quite the way they expect.
787 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
788 << "failed to find entry";
789 }
790 }
791
792 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700793 if (obj1 == obj2) {
794 return JNI_TRUE;
795 } else {
796 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800797 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
798 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700799 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 }
801
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700802 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700803 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800805 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800806 if (c == nullptr) {
807 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700808 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700809 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 }
811
Ian Rogersbc939662013-08-15 10:26:54 -0700812 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700815 CHECK_NON_NULL_ARGUMENT(java_class);
816 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700817 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 va_end(args);
819 return result;
820 }
821
Elliott Hughes72025e52011-08-23 17:50:30 -0700822 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700823 CHECK_NON_NULL_ARGUMENT(java_class);
824 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800826 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800827 if (c == nullptr) {
828 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700829 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800830 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800831 if (result == nullptr) {
832 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700833 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700834 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700835 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800836 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800837 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700838 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800839 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 }
841
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700843 CHECK_NON_NULL_ARGUMENT(java_class);
844 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800846 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800847 if (c == nullptr) {
848 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700849 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800850 mirror::Object* result = c->AllocObject(soa.Self());
851 if (result == nullptr) {
852 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700853 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700855 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800856 if (soa.Self()->IsExceptionPending()) {
857 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700858 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800859 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 }
861
Ian Rogersbc939662013-08-15 10:26:54 -0700862 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700863 CHECK_NON_NULL_ARGUMENT(java_class);
864 CHECK_NON_NULL_ARGUMENT(name);
865 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700866 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700867 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 }
869
Ian Rogersbc939662013-08-15 10:26:54 -0700870 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
871 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700872 CHECK_NON_NULL_ARGUMENT(java_class);
873 CHECK_NON_NULL_ARGUMENT(name);
874 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700876 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 }
878
Elliott Hughes72025e52011-08-23 17:50:30 -0700879 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700880 va_list ap;
881 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700882 CHECK_NON_NULL_ARGUMENT(obj);
883 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700884 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700885 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700887 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 }
889
Elliott Hughes72025e52011-08-23 17:50:30 -0700890 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700891 CHECK_NON_NULL_ARGUMENT(obj);
892 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 ScopedObjectAccess soa(env);
894 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
895 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 }
897
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700899 CHECK_NON_NULL_ARGUMENT(obj);
900 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700901 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700902 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
903 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 va_list ap;
909 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700910 CHECK_NON_NULL_ARGUMENT(obj);
911 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700912 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700913 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700915 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 }
917
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700919 CHECK_NON_NULL_ARGUMENT(obj);
920 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 ScopedObjectAccess soa(env);
922 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 }
924
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700926 CHECK_NON_NULL_ARGUMENT(obj);
927 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700929 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
930 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 }
932
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 va_list ap;
935 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700936 CHECK_NON_NULL_ARGUMENT(obj);
937 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700938 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700941 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 }
943
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700945 CHECK_NON_NULL_ARGUMENT(obj);
946 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
948 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700952 CHECK_NON_NULL_ARGUMENT(obj);
953 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700955 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
956 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_list ap;
961 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700962 CHECK_NON_NULL_ARGUMENT(obj);
963 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700964 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700967 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700971 CHECK_NON_NULL_ARGUMENT(obj);
972 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 ScopedObjectAccess soa(env);
974 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700978 CHECK_NON_NULL_ARGUMENT(obj);
979 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700981 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
982 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 va_list ap;
987 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700988 CHECK_NON_NULL_ARGUMENT(obj);
989 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700990 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700993 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700997 CHECK_NON_NULL_ARGUMENT(obj);
998 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 ScopedObjectAccess soa(env);
1000 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001004 CHECK_NON_NULL_ARGUMENT(obj);
1005 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001007 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1008 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_list ap;
1013 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001014 CHECK_NON_NULL_ARGUMENT(obj);
1015 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001016 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001017 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001019 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001023 CHECK_NON_NULL_ARGUMENT(obj);
1024 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
1026 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001030 CHECK_NON_NULL_ARGUMENT(obj);
1031 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001033 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1034 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_list ap;
1039 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001040 CHECK_NON_NULL_ARGUMENT(obj);
1041 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001042 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001045 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001049 CHECK_NON_NULL_ARGUMENT(obj);
1050 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
1052 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001056 CHECK_NON_NULL_ARGUMENT(obj);
1057 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001058 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001059 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1060 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 va_list ap;
1065 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001066 CHECK_NON_NULL_ARGUMENT(obj);
1067 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001068 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001071 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001075 CHECK_NON_NULL_ARGUMENT(obj);
1076 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 ScopedObjectAccess soa(env);
1078 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001082 CHECK_NON_NULL_ARGUMENT(obj);
1083 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001085 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1086 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_list ap;
1091 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001092 CHECK_NON_NULL_ARGUMENT(obj);
1093 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001094 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001097 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001101 CHECK_NON_NULL_ARGUMENT(obj);
1102 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
1104 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001108 CHECK_NON_NULL_ARGUMENT(obj);
1109 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001111 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1112 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001118 CHECK_NON_NULL_ARGUMENT(obj);
1119 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001120 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001121 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001126 CHECK_NON_NULL_ARGUMENT(obj);
1127 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 ScopedObjectAccess soa(env);
1129 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001133 CHECK_NON_NULL_ARGUMENT(obj);
1134 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001136 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001139 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001142 CHECK_NON_NULL_ARGUMENT(obj);
1143 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001144 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1146 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 va_end(ap);
1148 return local_result;
1149 }
1150
Ian Rogersbc939662013-08-15 10:26:54 -07001151 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1152 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001153 CHECK_NON_NULL_ARGUMENT(obj);
1154 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001155 ScopedObjectAccess soa(env);
1156 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1157 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Ian Rogersbc939662013-08-15 10:26:54 -07001160 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1161 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001162 CHECK_NON_NULL_ARGUMENT(obj);
1163 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001164 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001165 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001166 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Ian Rogersbc939662013-08-15 10:26:54 -07001169 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1170 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001173 CHECK_NON_NULL_ARGUMENT(obj);
1174 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001175 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001178 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Ian Rogersbc939662013-08-15 10:26:54 -07001181 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1182 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001183 CHECK_NON_NULL_ARGUMENT(obj);
1184 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 ScopedObjectAccess soa(env);
1186 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Ian Rogersbc939662013-08-15 10:26:54 -07001189 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1190 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001191 CHECK_NON_NULL_ARGUMENT(obj);
1192 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001194 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001197 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001200 CHECK_NON_NULL_ARGUMENT(obj);
1201 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001202 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001205 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Ian Rogersbc939662013-08-15 10:26:54 -07001208 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1209 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001210 CHECK_NON_NULL_ARGUMENT(obj);
1211 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(env);
1213 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Ian Rogersbc939662013-08-15 10:26:54 -07001216 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1217 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001218 CHECK_NON_NULL_ARGUMENT(obj);
1219 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001221 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001224 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001227 CHECK_NON_NULL_ARGUMENT(obj);
1228 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001229 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001232 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
Ian Rogersbc939662013-08-15 10:26:54 -07001235 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1236 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001237 CHECK_NON_NULL_ARGUMENT(obj);
1238 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 ScopedObjectAccess soa(env);
1240 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
Ian Rogersbc939662013-08-15 10:26:54 -07001243 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1244 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001245 CHECK_NON_NULL_ARGUMENT(obj);
1246 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001248 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001251 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001254 CHECK_NON_NULL_ARGUMENT(obj);
1255 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001256 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001259 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Ian Rogersbc939662013-08-15 10:26:54 -07001262 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1263 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001264 CHECK_NON_NULL_ARGUMENT(obj);
1265 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
1267 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Ian Rogersbc939662013-08-15 10:26:54 -07001270 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1271 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001272 CHECK_NON_NULL_ARGUMENT(obj);
1273 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001274 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001275 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001278 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001281 CHECK_NON_NULL_ARGUMENT(obj);
1282 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001283 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001286 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Ian Rogersbc939662013-08-15 10:26:54 -07001289 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1290 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001291 CHECK_NON_NULL_ARGUMENT(obj);
1292 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
1294 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Ian Rogersbc939662013-08-15 10:26:54 -07001297 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1298 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001299 CHECK_NON_NULL_ARGUMENT(obj);
1300 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001301 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001302 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001305 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001308 CHECK_NON_NULL_ARGUMENT(obj);
1309 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001310 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001311 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001313 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Ian Rogersbc939662013-08-15 10:26:54 -07001316 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1317 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001318 CHECK_NON_NULL_ARGUMENT(obj);
1319 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(env);
1321 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Ian Rogersbc939662013-08-15 10:26:54 -07001324 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1325 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001326 CHECK_NON_NULL_ARGUMENT(obj);
1327 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001328 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001329 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001335 CHECK_NON_NULL_ARGUMENT(obj);
1336 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001337 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001338 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001340 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Ian Rogersbc939662013-08-15 10:26:54 -07001343 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1344 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001345 CHECK_NON_NULL_ARGUMENT(obj);
1346 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
1348 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Ian Rogersbc939662013-08-15 10:26:54 -07001351 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1352 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001353 CHECK_NON_NULL_ARGUMENT(obj);
1354 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001355 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001356 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001359 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001362 CHECK_NON_NULL_ARGUMENT(obj);
1363 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001364 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001365 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001367 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Ian Rogersbc939662013-08-15 10:26:54 -07001370 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1371 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001372 CHECK_NON_NULL_ARGUMENT(obj);
1373 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(env);
1375 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Ian Rogersbc939662013-08-15 10:26:54 -07001378 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1379 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001380 CHECK_NON_NULL_ARGUMENT(obj);
1381 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001382 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001383 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001386 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001389 CHECK_NON_NULL_ARGUMENT(obj);
1390 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001391 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001392 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 va_end(ap);
1394 }
1395
Brian Carlstromea46f952013-07-30 01:26:50 -07001396 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1397 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001398 CHECK_NON_NULL_ARGUMENT(obj);
1399 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001400 ScopedObjectAccess soa(env);
1401 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Ian Rogersbc939662013-08-15 10:26:54 -07001404 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1405 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001406 CHECK_NON_NULL_ARGUMENT(obj);
1407 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001409 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Ian Rogersbc939662013-08-15 10:26:54 -07001412 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001413 CHECK_NON_NULL_ARGUMENT(java_class);
1414 CHECK_NON_NULL_ARGUMENT(name);
1415 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001417 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001419
Ian Rogersbc939662013-08-15 10:26:54 -07001420 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1421 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001422 CHECK_NON_NULL_ARGUMENT(java_class);
1423 CHECK_NON_NULL_ARGUMENT(name);
1424 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001425 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001426 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001428
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001430 CHECK_NON_NULL_ARGUMENT(obj);
1431 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001433 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1434 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001439 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001440 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001441 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001442 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001446 CHECK_NON_NULL_ARGUMENT(java_object);
1447 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001449 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1450 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1451 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001452 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001456 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001458 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1459 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001460 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463#define GET_PRIMITIVE_FIELD(fn, instance) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001464 CHECK_NON_NULL_ARGUMENT(instance); \
1465 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001466 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001467 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1468 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001469 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001471#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001472 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001473 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001474 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001475 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001476
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001477#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001478 CHECK_NON_NULL_ARGUMENT(instance); \
1479 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001480 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001481 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1482 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001483 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001485#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001486 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001487 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001488 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001489 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001492 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001496 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001500 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001504 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001508 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001512 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001516 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001520 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001523 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001524 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001527 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001528 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001531 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001532 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001535 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001536 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001539 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001540 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001543 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001544 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001548 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549 }
1550
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001551 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001552 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 }
1554
1555 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001556 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 }
1558
1559 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001560 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001561 }
1562
1563 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001564 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 }
1566
1567 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001568 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569 }
1570
1571 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001572 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001573 }
1574
1575 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001576 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577 }
1578
1579 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001580 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 }
1582
1583 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001584 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 }
1586
1587 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001588 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 }
1590
1591 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001592 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 }
1594
1595 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001596 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 }
1598
1599 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001600 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 }
1602
1603 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001604 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 }
1606
1607 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001608 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 }
1610
1611 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001612 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001613 }
1614
1615 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001616 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 }
1618
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001619 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001622 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001623 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001624 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001625 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 va_end(ap);
1627 return local_result;
1628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001631 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001633 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001634 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001638 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001639 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001640 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001641 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001647 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001648 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001649 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001651 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001654 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001655 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001656 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001657 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001660 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001661 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001662 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001663 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001669 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001670 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001671 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001673 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001677 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001678 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001679 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001682 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001683 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001685 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001691 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001692 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001693 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001695 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001699 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001700 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001701 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001704 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001705 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001706 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001707 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001710 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001713 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001714 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001715 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001717 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001721 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001723 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001727 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001728 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001729 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 }
1731
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001732 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001735 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001736 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001737 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001739 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001743 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001744 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001745 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001748 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001749 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001750 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001751 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001754 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001756 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001757 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001758 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001759 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001761 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001764 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001765 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001767 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001770 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001771 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001772 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001773 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001776 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001778 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001779 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001780 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001781 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001783 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001786 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001787 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001789 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001792 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001793 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001795 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001798 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001800 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001801 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001802 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001803 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001805 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001808 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001809 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001810 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001811 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001814 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001815 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001816 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001817 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001820 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001822 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001823 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001824 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001825 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 va_end(ap);
1827 }
1828
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001829 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001830 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001831 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001832 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001835 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001836 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001838 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
Elliott Hughes814e4032011-08-23 12:07:56 -07001841 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001842 if (UNLIKELY(char_count < 0)) {
1843 JniAbortF("NewString", "char_count < 0: %d", char_count);
1844 return nullptr;
1845 }
1846 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1847 JniAbortF("NewString", "chars == null && char_count > 0");
1848 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001849 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001851 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
1855 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 if (utf == nullptr) {
1857 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001860 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001861 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
Elliott Hughes814e4032011-08-23 12:07:56 -07001864 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001865 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001867 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001868 }
1869
1870 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001871 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001873 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001874 }
1875
Ian Rogersbc939662013-08-15 10:26:54 -07001876 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1877 jchar* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001878 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001880 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001881 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001882 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001883 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001884 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001885 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1886 memcpy(buf, chars + start, length * sizeof(jchar));
1887 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001888 }
1889
Ian Rogersbc939662013-08-15 10:26:54 -07001890 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1891 char* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001892 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001894 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001895 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001897 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001898 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001899 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1900 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1901 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001902 }
1903
Elliott Hughes75770752011-08-24 17:52:38 -07001904 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001905 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001906 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001907 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1908 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001909 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001910 if (is_copy != nullptr) {
1911 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001912 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001913 int32_t char_count = s->GetLength();
1914 int32_t offset = s->GetOffset();
1915 jchar* bytes = new jchar[char_count + 1];
1916 for (int32_t i = 0; i < char_count; i++) {
1917 bytes[i] = chars->Get(i + offset);
1918 }
1919 bytes[char_count] = '\0';
1920 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001921 }
1922
Mathieu Chartier590fee92013-09-13 13:46:47 -07001923 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001924 CHECK_NON_NULL_ARGUMENT(java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001925 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001926 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001927 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001931 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
1933
Elliott Hughes75770752011-08-24 17:52:38 -07001934 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001935 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes75770752011-08-24 17:52:38 -07001938 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001939 if (java_string == nullptr) {
1940 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001941 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001942 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001943 *is_copy = JNI_TRUE;
1944 }
Ian Rogersef28b142012-11-30 14:22:18 -08001945 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001946 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001947 size_t byte_count = s->GetUtfLength();
1948 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001949 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001950 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1951 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1952 bytes[byte_count] = '\0';
1953 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001957 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001958 }
1959
Elliott Hughesbd935992011-08-22 11:59:34 -07001960 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001961 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001963 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001964 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001965 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1966 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001967 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001968 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes814e4032011-08-23 12:07:56 -07001971 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001972 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001974 mirror::ObjectArray<mirror::Object>* array =
1975 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001976 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Ian Rogersbc939662013-08-15 10:26:54 -07001979 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1980 jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001981 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001982 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001983 mirror::ObjectArray<mirror::Object>* array =
1984 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1985 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001986 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
1989 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001990 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001991 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
1994 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001996 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
1999 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002000 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002001 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
2004 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002006 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
2009 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002010 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002011 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
2014 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002016 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
2019 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002020 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002021 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Ian Rogers1d99e452014-01-02 17:36:41 -08002024 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2025 jobject initial_element) {
2026 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002027 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002028 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002029 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002030
2031 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002032 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002033 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002034 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002035 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002036 if (UNLIKELY(element_class->IsPrimitive())) {
2037 JniAbortF("NewObjectArray", "not an object type: %s",
2038 PrettyDescriptor(element_class).c_str());
2039 return nullptr;
2040 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002041 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002042 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002043 if (UNLIKELY(array_class == nullptr)) {
2044 return nullptr;
2045 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002049 mirror::ObjectArray<mirror::Object>* result =
2050 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002051 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002052 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002053 if (initial_object != nullptr) {
2054 mirror::Class* element_class = result->GetClass()->GetComponentType();
2055 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2056 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2057 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2058 PrettyDescriptor(element_class).c_str());
2059
2060 } else {
2061 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002062 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002063 }
2064 }
Elliott Hughes75770752011-08-24 17:52:38 -07002065 }
2066 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002067 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
2070 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002071 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002072 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Ian Rogersa15e67d2012-02-28 13:51:55 -08002075 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002076 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002078 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002079 gc::Heap* heap = Runtime::Current()->GetHeap();
2080 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002081 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002082 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002083 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002084 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002086 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002087 *is_copy = JNI_FALSE;
2088 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002089 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002090 }
2091
Mathieu Chartier590fee92013-09-13 13:46:47 -07002092 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002093 CHECK_NON_NULL_ARGUMENT(array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002094 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002095 }
2096
Elliott Hughes75770752011-08-24 17:52:38 -07002097 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002098 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002099 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002100 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes75770752011-08-24 17:52:38 -07002103 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002104 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002105 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002106 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes75770752011-08-24 17:52:38 -07002109 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002110 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002111 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002112 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes75770752011-08-24 17:52:38 -07002115 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002116 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002117 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002118 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes75770752011-08-24 17:52:38 -07002121 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002122 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002123 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002124 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes75770752011-08-24 17:52:38 -07002127 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002128 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002129 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002130 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes75770752011-08-24 17:52:38 -07002133 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002134 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002135 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002136 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes75770752011-08-24 17:52:38 -07002139 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002140 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002141 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002142 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Mathieu Chartier590fee92013-09-13 13:46:47 -07002145 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2146 jint mode) {
2147 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Mathieu Chartier590fee92013-09-13 13:46:47 -07002150 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2151 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Mathieu Chartier590fee92013-09-13 13:46:47 -07002154 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2155 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Mathieu Chartier590fee92013-09-13 13:46:47 -07002158 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2159 jint mode) {
2160 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Mathieu Chartier590fee92013-09-13 13:46:47 -07002163 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2164 jint mode) {
2165 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Mathieu Chartier590fee92013-09-13 13:46:47 -07002168 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2169 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Mathieu Chartier590fee92013-09-13 13:46:47 -07002172 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2173 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Mathieu Chartier590fee92013-09-13 13:46:47 -07002176 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2177 jint mode) {
2178 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Ian Rogersbc939662013-08-15 10:26:54 -07002181 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2182 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002183 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002184 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2185 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Ian Rogersbc939662013-08-15 10:26:54 -07002188 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2189 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002190 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002191 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Ian Rogersbc939662013-08-15 10:26:54 -07002194 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2195 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002197 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199
Ian Rogersbc939662013-08-15 10:26:54 -07002200 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2201 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002202 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002203 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2204 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Ian Rogersbc939662013-08-15 10:26:54 -07002207 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2208 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002209 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002210 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2211 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Ian Rogersbc939662013-08-15 10:26:54 -07002214 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2215 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002216 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002217 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Ian Rogersbc939662013-08-15 10:26:54 -07002220 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2221 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002222 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002223 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Ian Rogersbc939662013-08-15 10:26:54 -07002226 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2227 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002228 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002229 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2230 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Ian Rogersbc939662013-08-15 10:26:54 -07002233 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2234 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002235 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002236 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2237 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Ian Rogersbc939662013-08-15 10:26:54 -07002240 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2241 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002243 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Ian Rogersbc939662013-08-15 10:26:54 -07002246 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2247 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002248 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002249 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Ian Rogersbc939662013-08-15 10:26:54 -07002252 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2253 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002254 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002255 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2256 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Ian Rogersbc939662013-08-15 10:26:54 -07002259 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2260 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002261 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002262 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2263 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
2265
Ian Rogersbc939662013-08-15 10:26:54 -07002266 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2267 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002268 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002269 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002270 }
2271
Ian Rogersbc939662013-08-15 10:26:54 -07002272 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2273 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002274 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002275 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
2277
Ian Rogersbc939662013-08-15 10:26:54 -07002278 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2279 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002280 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002281 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2282 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284
Ian Rogersbc939662013-08-15 10:26:54 -07002285 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2286 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002287 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2288 }
2289
Ian Rogersbc939662013-08-15 10:26:54 -07002290 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2291 jint method_count, bool return_errors) {
2292 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002293 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002294 return JNI_ERR; // Not reached.
2295 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002296 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002297 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002298 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002299 if (UNLIKELY(method_count == 0)) {
2300 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2301 << PrettyDescriptor(c);
2302 return JNI_OK;
2303 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002304 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods);
Ian Rogersbc939662013-08-15 10:26:54 -07002305 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 const char* name = methods[i].name;
2307 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002308 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002309 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002310 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 ++sig;
2312 }
2313
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002314 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2315 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002316 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002318 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002319 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002320 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002321 << PrettyDescriptor(c) << "." << name << sig << " in "
2322 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002323 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002325 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002326 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002327 << PrettyDescriptor(c) << "." << name << sig
2328 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002329 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002330 return JNI_ERR;
2331 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002332
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002333 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002334
Ian Rogers1eb512d2013-10-18 15:42:20 -07002335 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002336 }
2337 return JNI_OK;
2338 }
2339
Elliott Hughes5174fe62011-08-23 15:12:35 -07002340 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002341 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002343 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002344
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002345 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002346
2347 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002348 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002349 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002350 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002351 }
2352 }
2353 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002354 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002355 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002356 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002357 }
2358 }
2359
2360 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002361 }
2362
Ian Rogers719d1a32014-03-06 12:13:39 -08002363 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002364 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002365 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002366 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2367 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002368 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002369 return JNI_ERR;
2370 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002371 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002372 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002373 }
2374
Ian Rogers719d1a32014-03-06 12:13:39 -08002375 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002376 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002377 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002378 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002379 o->MonitorExit(soa.Self());
2380 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002381 return JNI_ERR;
2382 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002383 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002384 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002385 }
2386
2387 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002388 CHECK_NON_NULL_ARGUMENT(vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002389 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002390 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002391 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002393 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002394 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002395 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002396 }
2397
Elliott Hughescdf53122011-08-19 15:46:09 -07002398 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002399 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002400 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002401 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002402 if (address == nullptr && capacity != 0) {
2403 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002404 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002405
Ian Rogers936b37f2014-02-14 00:52:24 -08002406 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002407 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002408 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002409 jint capacity_arg = static_cast<jint>(capacity);
2410
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002411 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2412 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002413 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002414 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002415 }
2416
Elliott Hughesb465ab02011-08-24 11:21:21 -07002417 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002418 return reinterpret_cast<void*>(env->GetLongField(
2419 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002420 }
2421
Elliott Hughesb465ab02011-08-24 11:21:21 -07002422 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002423 return static_cast<jlong>(env->GetIntField(
2424 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002425 }
2426
Elliott Hughesb465ab02011-08-24 11:21:21 -07002427 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002428 CHECK_NON_NULL_ARGUMENT(java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002429
2430 // Do we definitely know what kind of reference this is?
2431 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2432 IndirectRefKind kind = GetIndirectRefKind(ref);
2433 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002434 case kLocal: {
2435 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002436 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002437 return JNILocalRefType;
2438 }
2439 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002440 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002441 case kGlobal:
2442 return JNIGlobalRefType;
2443 case kWeakGlobal:
2444 return JNIWeakGlobalRefType;
2445 case kSirtOrInvalid:
2446 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002447 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002448 return JNILocalRefType;
2449 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002450 return JNIInvalidRefType;
2451 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002452 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2453 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002454 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002455
2456 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002457 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2458 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002459 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002460 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002461 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2462 return JNI_ERR;
2463 }
2464 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002465 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002466 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2467 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002468 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 soa.Self()->ThrowOutOfMemoryError(caller);
2470 }
2471 return okay ? JNI_OK : JNI_ERR;
2472 }
2473
2474 template<typename JniT, typename ArtT>
2475 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002477 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002478 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002479 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002480 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002481 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002482 return soa.AddLocalReference<JniT>(result);
2483 }
2484
2485 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2486 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2487 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002489 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2490 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002491 // Only make a copy if necessary.
2492 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2493 if (is_copy != nullptr) {
2494 *is_copy = JNI_TRUE;
2495 }
2496 static const size_t component_size = array->GetClass()->GetComponentSize();
2497 size_t size = array->GetLength() * component_size;
2498 void* data = new uint64_t[RoundUp(size, 8) / 8];
2499 memcpy(data, array->GetData(), size);
2500 return reinterpret_cast<CArrayT>(data);
2501 } else {
2502 if (is_copy != nullptr) {
2503 *is_copy = JNI_FALSE;
2504 }
2505 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002506 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002507 }
2508
Mathieu Chartier590fee92013-09-13 13:46:47 -07002509 template <typename ArrayT, typename ElementT>
2510 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2511 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002512 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002513 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002514 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002515 gc::Heap* heap = Runtime::Current()->GetHeap();
2516 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2517 size_t bytes = array->GetLength() * component_size;
2518 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2519 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002520 if (is_copy) {
2521 // Sanity check: If elements is not the same as the java array's data, it better not be a
2522 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2523 // copies we make?
2524 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2525 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2526 reinterpret_cast<void*>(elements), array_data);
2527 return;
2528 }
2529 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002530 // Don't need to copy if we had a direct pointer.
2531 if (mode != JNI_ABORT && is_copy) {
2532 memcpy(array_data, elements, bytes);
2533 }
2534 if (mode != JNI_COMMIT) {
2535 if (is_copy) {
2536 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002537 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002538 // Non copy to a movable object must means that we had disabled the moving GC.
2539 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002540 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002541 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002542 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002543 }
2544
2545 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2546 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2547 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002548 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002549 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002550 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2551 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2552 ThrowAIOOBE(soa, array, start, length, "src");
2553 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002554 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002555 JavaT* data = array->GetData();
2556 memcpy(buf, data + start, length * sizeof(JavaT));
2557 }
2558 }
2559
2560 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2561 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2562 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002564 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002565 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2566 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2567 ThrowAIOOBE(soa, array, start, length, "dst");
2568 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002569 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570 JavaT* data = array->GetData();
2571 memcpy(data + start, buf, length * sizeof(JavaT));
2572 }
2573 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002574};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002575
Elliott Hughes88c5c352012-03-15 18:49:48 -07002576const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002577 nullptr, // reserved0.
2578 nullptr, // reserved1.
2579 nullptr, // reserved2.
2580 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002581 JNI::GetVersion,
2582 JNI::DefineClass,
2583 JNI::FindClass,
2584 JNI::FromReflectedMethod,
2585 JNI::FromReflectedField,
2586 JNI::ToReflectedMethod,
2587 JNI::GetSuperclass,
2588 JNI::IsAssignableFrom,
2589 JNI::ToReflectedField,
2590 JNI::Throw,
2591 JNI::ThrowNew,
2592 JNI::ExceptionOccurred,
2593 JNI::ExceptionDescribe,
2594 JNI::ExceptionClear,
2595 JNI::FatalError,
2596 JNI::PushLocalFrame,
2597 JNI::PopLocalFrame,
2598 JNI::NewGlobalRef,
2599 JNI::DeleteGlobalRef,
2600 JNI::DeleteLocalRef,
2601 JNI::IsSameObject,
2602 JNI::NewLocalRef,
2603 JNI::EnsureLocalCapacity,
2604 JNI::AllocObject,
2605 JNI::NewObject,
2606 JNI::NewObjectV,
2607 JNI::NewObjectA,
2608 JNI::GetObjectClass,
2609 JNI::IsInstanceOf,
2610 JNI::GetMethodID,
2611 JNI::CallObjectMethod,
2612 JNI::CallObjectMethodV,
2613 JNI::CallObjectMethodA,
2614 JNI::CallBooleanMethod,
2615 JNI::CallBooleanMethodV,
2616 JNI::CallBooleanMethodA,
2617 JNI::CallByteMethod,
2618 JNI::CallByteMethodV,
2619 JNI::CallByteMethodA,
2620 JNI::CallCharMethod,
2621 JNI::CallCharMethodV,
2622 JNI::CallCharMethodA,
2623 JNI::CallShortMethod,
2624 JNI::CallShortMethodV,
2625 JNI::CallShortMethodA,
2626 JNI::CallIntMethod,
2627 JNI::CallIntMethodV,
2628 JNI::CallIntMethodA,
2629 JNI::CallLongMethod,
2630 JNI::CallLongMethodV,
2631 JNI::CallLongMethodA,
2632 JNI::CallFloatMethod,
2633 JNI::CallFloatMethodV,
2634 JNI::CallFloatMethodA,
2635 JNI::CallDoubleMethod,
2636 JNI::CallDoubleMethodV,
2637 JNI::CallDoubleMethodA,
2638 JNI::CallVoidMethod,
2639 JNI::CallVoidMethodV,
2640 JNI::CallVoidMethodA,
2641 JNI::CallNonvirtualObjectMethod,
2642 JNI::CallNonvirtualObjectMethodV,
2643 JNI::CallNonvirtualObjectMethodA,
2644 JNI::CallNonvirtualBooleanMethod,
2645 JNI::CallNonvirtualBooleanMethodV,
2646 JNI::CallNonvirtualBooleanMethodA,
2647 JNI::CallNonvirtualByteMethod,
2648 JNI::CallNonvirtualByteMethodV,
2649 JNI::CallNonvirtualByteMethodA,
2650 JNI::CallNonvirtualCharMethod,
2651 JNI::CallNonvirtualCharMethodV,
2652 JNI::CallNonvirtualCharMethodA,
2653 JNI::CallNonvirtualShortMethod,
2654 JNI::CallNonvirtualShortMethodV,
2655 JNI::CallNonvirtualShortMethodA,
2656 JNI::CallNonvirtualIntMethod,
2657 JNI::CallNonvirtualIntMethodV,
2658 JNI::CallNonvirtualIntMethodA,
2659 JNI::CallNonvirtualLongMethod,
2660 JNI::CallNonvirtualLongMethodV,
2661 JNI::CallNonvirtualLongMethodA,
2662 JNI::CallNonvirtualFloatMethod,
2663 JNI::CallNonvirtualFloatMethodV,
2664 JNI::CallNonvirtualFloatMethodA,
2665 JNI::CallNonvirtualDoubleMethod,
2666 JNI::CallNonvirtualDoubleMethodV,
2667 JNI::CallNonvirtualDoubleMethodA,
2668 JNI::CallNonvirtualVoidMethod,
2669 JNI::CallNonvirtualVoidMethodV,
2670 JNI::CallNonvirtualVoidMethodA,
2671 JNI::GetFieldID,
2672 JNI::GetObjectField,
2673 JNI::GetBooleanField,
2674 JNI::GetByteField,
2675 JNI::GetCharField,
2676 JNI::GetShortField,
2677 JNI::GetIntField,
2678 JNI::GetLongField,
2679 JNI::GetFloatField,
2680 JNI::GetDoubleField,
2681 JNI::SetObjectField,
2682 JNI::SetBooleanField,
2683 JNI::SetByteField,
2684 JNI::SetCharField,
2685 JNI::SetShortField,
2686 JNI::SetIntField,
2687 JNI::SetLongField,
2688 JNI::SetFloatField,
2689 JNI::SetDoubleField,
2690 JNI::GetStaticMethodID,
2691 JNI::CallStaticObjectMethod,
2692 JNI::CallStaticObjectMethodV,
2693 JNI::CallStaticObjectMethodA,
2694 JNI::CallStaticBooleanMethod,
2695 JNI::CallStaticBooleanMethodV,
2696 JNI::CallStaticBooleanMethodA,
2697 JNI::CallStaticByteMethod,
2698 JNI::CallStaticByteMethodV,
2699 JNI::CallStaticByteMethodA,
2700 JNI::CallStaticCharMethod,
2701 JNI::CallStaticCharMethodV,
2702 JNI::CallStaticCharMethodA,
2703 JNI::CallStaticShortMethod,
2704 JNI::CallStaticShortMethodV,
2705 JNI::CallStaticShortMethodA,
2706 JNI::CallStaticIntMethod,
2707 JNI::CallStaticIntMethodV,
2708 JNI::CallStaticIntMethodA,
2709 JNI::CallStaticLongMethod,
2710 JNI::CallStaticLongMethodV,
2711 JNI::CallStaticLongMethodA,
2712 JNI::CallStaticFloatMethod,
2713 JNI::CallStaticFloatMethodV,
2714 JNI::CallStaticFloatMethodA,
2715 JNI::CallStaticDoubleMethod,
2716 JNI::CallStaticDoubleMethodV,
2717 JNI::CallStaticDoubleMethodA,
2718 JNI::CallStaticVoidMethod,
2719 JNI::CallStaticVoidMethodV,
2720 JNI::CallStaticVoidMethodA,
2721 JNI::GetStaticFieldID,
2722 JNI::GetStaticObjectField,
2723 JNI::GetStaticBooleanField,
2724 JNI::GetStaticByteField,
2725 JNI::GetStaticCharField,
2726 JNI::GetStaticShortField,
2727 JNI::GetStaticIntField,
2728 JNI::GetStaticLongField,
2729 JNI::GetStaticFloatField,
2730 JNI::GetStaticDoubleField,
2731 JNI::SetStaticObjectField,
2732 JNI::SetStaticBooleanField,
2733 JNI::SetStaticByteField,
2734 JNI::SetStaticCharField,
2735 JNI::SetStaticShortField,
2736 JNI::SetStaticIntField,
2737 JNI::SetStaticLongField,
2738 JNI::SetStaticFloatField,
2739 JNI::SetStaticDoubleField,
2740 JNI::NewString,
2741 JNI::GetStringLength,
2742 JNI::GetStringChars,
2743 JNI::ReleaseStringChars,
2744 JNI::NewStringUTF,
2745 JNI::GetStringUTFLength,
2746 JNI::GetStringUTFChars,
2747 JNI::ReleaseStringUTFChars,
2748 JNI::GetArrayLength,
2749 JNI::NewObjectArray,
2750 JNI::GetObjectArrayElement,
2751 JNI::SetObjectArrayElement,
2752 JNI::NewBooleanArray,
2753 JNI::NewByteArray,
2754 JNI::NewCharArray,
2755 JNI::NewShortArray,
2756 JNI::NewIntArray,
2757 JNI::NewLongArray,
2758 JNI::NewFloatArray,
2759 JNI::NewDoubleArray,
2760 JNI::GetBooleanArrayElements,
2761 JNI::GetByteArrayElements,
2762 JNI::GetCharArrayElements,
2763 JNI::GetShortArrayElements,
2764 JNI::GetIntArrayElements,
2765 JNI::GetLongArrayElements,
2766 JNI::GetFloatArrayElements,
2767 JNI::GetDoubleArrayElements,
2768 JNI::ReleaseBooleanArrayElements,
2769 JNI::ReleaseByteArrayElements,
2770 JNI::ReleaseCharArrayElements,
2771 JNI::ReleaseShortArrayElements,
2772 JNI::ReleaseIntArrayElements,
2773 JNI::ReleaseLongArrayElements,
2774 JNI::ReleaseFloatArrayElements,
2775 JNI::ReleaseDoubleArrayElements,
2776 JNI::GetBooleanArrayRegion,
2777 JNI::GetByteArrayRegion,
2778 JNI::GetCharArrayRegion,
2779 JNI::GetShortArrayRegion,
2780 JNI::GetIntArrayRegion,
2781 JNI::GetLongArrayRegion,
2782 JNI::GetFloatArrayRegion,
2783 JNI::GetDoubleArrayRegion,
2784 JNI::SetBooleanArrayRegion,
2785 JNI::SetByteArrayRegion,
2786 JNI::SetCharArrayRegion,
2787 JNI::SetShortArrayRegion,
2788 JNI::SetIntArrayRegion,
2789 JNI::SetLongArrayRegion,
2790 JNI::SetFloatArrayRegion,
2791 JNI::SetDoubleArrayRegion,
2792 JNI::RegisterNatives,
2793 JNI::UnregisterNatives,
2794 JNI::MonitorEnter,
2795 JNI::MonitorExit,
2796 JNI::GetJavaVM,
2797 JNI::GetStringRegion,
2798 JNI::GetStringUTFRegion,
2799 JNI::GetPrimitiveArrayCritical,
2800 JNI::ReleasePrimitiveArrayCritical,
2801 JNI::GetStringCritical,
2802 JNI::ReleaseStringCritical,
2803 JNI::NewWeakGlobalRef,
2804 JNI::DeleteWeakGlobalRef,
2805 JNI::ExceptionCheck,
2806 JNI::NewDirectByteBuffer,
2807 JNI::GetDirectBufferAddress,
2808 JNI::GetDirectBufferCapacity,
2809 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002810};
2811
Elliott Hughes75770752011-08-24 17:52:38 -07002812JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002813 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002814 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002815 local_ref_cookie(IRT_FIRST_SEGMENT),
2816 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002817 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002818 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002819 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002820 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002821 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002822 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002823 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002824}
2825
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002826JNIEnvExt::~JNIEnvExt() {
2827}
2828
Mathieu Chartier590fee92013-09-13 13:46:47 -07002829jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2830 if (obj == nullptr) {
2831 return nullptr;
2832 }
2833 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2834}
2835
2836void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2837 if (obj != nullptr) {
2838 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2839 }
2840}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002841void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2842 check_jni = enabled;
2843 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002844}
2845
Elliott Hughes73e66f72012-05-09 09:34:45 -07002846void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2847 locals.Dump(os);
2848 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002849}
2850
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002851void JNIEnvExt::PushFrame(int /*capacity*/) {
2852 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002853 stacked_local_ref_cookies.push_back(local_ref_cookie);
2854 local_ref_cookie = locals.GetSegmentState();
2855}
2856
2857void JNIEnvExt::PopFrame() {
2858 locals.SetSegmentState(local_ref_cookie);
2859 local_ref_cookie = stacked_local_ref_cookies.back();
2860 stacked_local_ref_cookies.pop_back();
2861}
2862
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002863Offset JNIEnvExt::SegmentStateOffset() {
2864 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2865 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2866}
2867
Carl Shapiroea4dca82011-08-01 13:45:38 -07002868// JNI Invocation interface.
2869
Brian Carlstrombddf9762013-05-14 11:35:37 -07002870extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002871 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002872 if (IsBadJniVersion(args->version)) {
2873 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002874 return JNI_EVERSION;
2875 }
2876 Runtime::Options options;
2877 for (int i = 0; i < args->nOptions; ++i) {
2878 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002879 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002880 }
2881 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002882 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002883 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002884 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002885 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002886 bool started = runtime->Start();
2887 if (!started) {
2888 delete Thread::Current()->GetJniEnv();
2889 delete runtime->GetJavaVM();
2890 LOG(WARNING) << "CreateJavaVM failed";
2891 return JNI_ERR;
2892 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002893 *p_env = Thread::Current()->GetJniEnv();
2894 *p_vm = runtime->GetJavaVM();
2895 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002896}
2897
Elliott Hughesf2682d52011-08-15 16:37:04 -07002898extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002899 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002900 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002901 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002902 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002903 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002904 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002905 }
2906 return JNI_OK;
2907}
2908
2909// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002910extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002911 return JNI_ERR;
2912}
2913
Elliott Hughescdf53122011-08-19 15:46:09 -07002914class JII {
2915 public:
2916 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002917 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002918 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002919 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002920 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2921 delete raw_vm->runtime;
2922 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002923 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002924
Elliott Hughescdf53122011-08-19 15:46:09 -07002925 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002926 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002927 }
2928
2929 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002930 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002931 }
2932
2933 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002934 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002935 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002936 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002937 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2938 Runtime* runtime = raw_vm->runtime;
2939 runtime->DetachCurrentThread();
2940 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002941 }
2942
2943 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002944 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2945 // and unlike other calls that take a JNI version doesn't care if you supply
2946 // JNI_VERSION_1_1, which we don't otherwise support.
2947 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002948 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002949 return JNI_EVERSION;
2950 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002951 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002952 return JNI_ERR;
2953 }
2954 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002955 if (thread == nullptr) {
2956 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002957 return JNI_EDETACHED;
2958 }
2959 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002960 return JNI_OK;
2961 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002962};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002963
Elliott Hughes88c5c352012-03-15 18:49:48 -07002964const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002965 nullptr, // reserved0
2966 nullptr, // reserved1
2967 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002968 JII::DestroyJavaVM,
2969 JII::AttachCurrentThread,
2970 JII::DetachCurrentThread,
2971 JII::GetEnv,
2972 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002973};
2974
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002975JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002976 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002977 check_jni_abort_hook(nullptr),
2978 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002979 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002980 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002981 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08002982 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002983 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002984 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002985 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002986 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002987 libraries(new Libraries),
2988 weak_globals_lock_("JNI weak global reference table lock"),
2989 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2990 allow_new_weak_globals_(true),
2991 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002992 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002993 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002994 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002995 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002996}
2997
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002998JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002999 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003000}
3001
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003002jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3003 if (obj == nullptr) {
3004 return nullptr;
3005 }
3006 MutexLock mu(self, weak_globals_lock_);
3007 while (UNLIKELY(!allow_new_weak_globals_)) {
3008 weak_globals_add_condition_.WaitHoldingLocks(self);
3009 }
3010 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3011 return reinterpret_cast<jweak>(ref);
3012}
3013
3014void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3015 MutexLock mu(self, weak_globals_lock_);
3016 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3017 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3018 << "failed to find entry";
3019 }
3020}
3021
Elliott Hughes88c5c352012-03-15 18:49:48 -07003022void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3023 check_jni = enabled;
3024 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003025}
3026
Elliott Hughesae80b492012-04-24 10:43:17 -07003027void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3028 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3029 if (force_copy) {
3030 os << " (with forcecopy)";
3031 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003032 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003033 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003034 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003035 os << "; pins=" << pin_table.Size();
3036 }
3037 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003038 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003039 os << "; globals=" << globals.Capacity();
3040 }
3041 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003042 MutexLock mu(self, weak_globals_lock_);
3043 if (weak_globals_.Capacity() > 0) {
3044 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003045 }
3046 }
3047 os << '\n';
3048
3049 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003050 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003051 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3052 }
3053}
3054
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003055void JavaVMExt::DisallowNewWeakGlobals() {
3056 MutexLock mu(Thread::Current(), weak_globals_lock_);
3057 allow_new_weak_globals_ = false;
3058}
3059
3060void JavaVMExt::AllowNewWeakGlobals() {
3061 Thread* self = Thread::Current();
3062 MutexLock mu(self, weak_globals_lock_);
3063 allow_new_weak_globals_ = true;
3064 weak_globals_add_condition_.Broadcast(self);
3065}
3066
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003067mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3068 MutexLock mu(self, weak_globals_lock_);
3069 while (UNLIKELY(!allow_new_weak_globals_)) {
3070 weak_globals_add_condition_.WaitHoldingLocks(self);
3071 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003072 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003073}
3074
Elliott Hughes73e66f72012-05-09 09:34:45 -07003075void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003076 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003077 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003078 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003079 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003080 }
3081 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003082 MutexLock mu(self, weak_globals_lock_);
3083 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003084 }
3085 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003086 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003087 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003088 }
3089}
3090
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003091bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003092 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003093 std::string* detail) {
3094 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003095
3096 // See if we've already loaded this library. If we have, and the class loader
3097 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003098 // TODO: for better results we should canonicalize the pathname (or even compare
3099 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003100 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003101 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003102 {
3103 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003104 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003105 library = libraries->Get(path);
3106 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003107 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003108 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003109 // The library will be associated with class_loader. The JNI
3110 // spec says we can't load the same library into more than one
3111 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003112 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003113 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003114 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003115 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003116 return false;
3117 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003118 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003119 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003120 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003121 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003122 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003123 return false;
3124 }
3125 return true;
3126 }
3127
3128 // Open the shared library. Because we're using a full path, the system
3129 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3130 // resolve this library's dependencies though.)
3131
3132 // Failures here are expected when java.library.path has several entries
3133 // and we have to hunt for the lib.
3134
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003135 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3136 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3137 // dlopen) becomes zero from dlclose.
3138
Elliott Hughescdf53122011-08-19 15:46:09 -07003139 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003140 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003141 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003142 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003143 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003144
Elliott Hughes84b2f142012-09-27 09:16:28 -07003145 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003146
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003147 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003148 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003149 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003150 return false;
3151 }
3152
3153 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003154 // TODO: move the locking (and more of this logic) into Libraries.
3155 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003156 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003157 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003158 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003159 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003160 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003161 libraries->Put(path, library);
3162 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003163 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003164 }
3165 if (!created_library) {
3166 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003167 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003168 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003169 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003170
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003171 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3172 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003173
Elliott Hughes79353722013-08-02 16:52:18 -07003174 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003175 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003176 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003177 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003178 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003179 } else {
3180 // Call JNI_OnLoad. We have to override the current class
3181 // loader, which will always be "null" since the stuff at the
3182 // top of the stack is around Runtime.loadLibrary(). (See
3183 // the comments in the JNI FindClass function.)
3184 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3185 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003186 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003187 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003188
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003189 int version = 0;
3190 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003191 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003192 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003193 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003194 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003195
Mathieu Chartier590fee92013-09-13 13:46:47 -07003196 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003197
Elliott Hughes79353722013-08-02 16:52:18 -07003198 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003199 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003200 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003201 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003202 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003203 // It's unwise to call dlclose() here, but we can mark it
3204 // as bad and ensure that future load attempts will fail.
3205 // We don't know how far JNI_OnLoad got, so there could
3206 // be some partially-initialized stuff accessible through
3207 // newly-registered native method calls. We could try to
3208 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003209 } else {
3210 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003211 }
Elliott Hughes79353722013-08-02 16:52:18 -07003212 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003213 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003214 }
3215
Elliott Hughes79353722013-08-02 16:52:18 -07003216 library->SetResult(was_successful);
3217 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003218}
3219
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003220void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003221 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003222 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003223 // If this is a static method, it could be called before the class
3224 // has been initialized.
3225 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003226 c = EnsureInitialized(Thread::Current(), c);
3227 if (c == nullptr) {
3228 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003229 }
3230 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003231 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003232 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003233 std::string detail;
3234 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003235 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003236 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003237 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003238 native_method = libraries->FindNativeMethod(m, detail);
3239 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003240 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003241 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003242 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3243 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003244 }
3245 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003246}
3247
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003248void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003249 MutexLock mu(Thread::Current(), weak_globals_lock_);
3250 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003251 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003252 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003253 if (new_obj == nullptr) {
3254 new_obj = kClearedJniWeakGlobal;
3255 }
3256 *entry = new_obj;
3257 }
3258}
3259
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003260void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003261 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003262 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003263 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003264 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003265 }
3266 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003267 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003268 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003269 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003270 {
3271 MutexLock mu(self, libraries_lock);
3272 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003273 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003274 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003275 // The weak_globals table is visited by the GC itself (because it mutates the table).
3276}
3277
Elliott Hughesc8fece32013-01-02 11:27:23 -08003278void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003279 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003280 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003281 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003282 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3283 }
3284 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3285}
3286
Ian Rogersdf20fe02011-07-20 20:34:16 -07003287} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003288
3289std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3290 switch (rhs) {
3291 case JNIInvalidRefType:
3292 os << "JNIInvalidRefType";
3293 return os;
3294 case JNILocalRefType:
3295 os << "JNILocalRefType";
3296 return os;
3297 case JNIGlobalRefType:
3298 os << "JNIGlobalRefType";
3299 return os;
3300 case JNIWeakGlobalRefType:
3301 os << "JNIWeakGlobalRefType";
3302 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003303 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003304 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003305 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003306 }
3307}