blob: 46349712ac0024202013703ce5b7f9a35d5be26d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Ian Rogersef7d42f2014-01-06 12:55:46 -080025#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080029#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070032#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070033#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_field-inl.h"
36#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
38#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070041#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "object_utils.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080044#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070045#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070046#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070052#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070053#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070054
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070055namespace art {
56
Brian Carlstrom7934ac22013-07-26 10:54:15 -070057static const size_t kMonitorsInitial = 32; // Arbitrary.
58static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070059
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060static const size_t kLocalsInitial = 64; // Arbitrary.
61static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070062
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063static const size_t kPinTableInitial = 16; // Arbitrary.
64static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070065
Brian Carlstrom7934ac22013-07-26 10:54:15 -070066static size_t gGlobalsInitial = 512; // Arbitrary.
67static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070068
Brian Carlstrom7934ac22013-07-26 10:54:15 -070069static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
70static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070071
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080072static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070074 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070075}
76
Jeff Hao19c5d372013-03-15 14:33:43 -070077static bool IsBadJniVersion(int version) {
78 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
79 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
80}
81
Elliott Hughes6b436852011-08-12 10:16:44 -070082// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
83// separated with slashes but aren't wrapped with "L;" like regular descriptors
84// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
85// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
86// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070087static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070088 std::string result;
89 // Add the missing "L;" if necessary.
90 if (name[0] == '[') {
91 result = name;
92 } else {
93 result += 'L';
94 result += name;
95 result += ';';
96 }
97 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070098 if (result.find('.') != std::string::npos) {
99 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
100 << "\"" << name << "\"";
101 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700102 }
103 return result;
104}
105
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800106static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
110 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
111 "no %s method \"%s.%s%s\"",
112 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700113}
114
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800115static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 if (LIKELY(klass->IsInitialized())) {
118 return klass;
119 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 StackHandleScope<1> hs(self);
121 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
122 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800123 return nullptr;
124 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700125 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800126}
127
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
129 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800131 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800132 if (c == nullptr) {
133 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700134 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800135 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700136 if (is_static) {
137 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700138 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700139 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800140 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700141 // No virtual method matching the signature. Search declared
142 // private methods and constructors.
143 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700144 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700145 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800146 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800148 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700149 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700151}
152
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800153static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800155 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700156 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
157 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800158 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700159 }
Brian Carlstromce888532013-10-10 00:32:58 -0700160 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700162 return method->GetDeclaringClass()->GetClassLoader();
163 }
164 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800165 mirror::ClassLoader* class_loader =
166 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
167 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700168 return class_loader;
169 }
170 // See if the override ClassLoader is set for gtests.
171 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800172 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800173 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700174 CHECK(Runtime::Current()->UseCompileTimeClassPath());
175 return class_loader;
176 }
177 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800178 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700179}
180
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
182 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700184 StackHandleScope<2> hs(soa.Self());
185 Handle<mirror::Class> c(
186 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
187 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800188 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700189 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800190 mirror::ArtField* field = nullptr;
191 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
193 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800195 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700196 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700198 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800199 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 StackHandleScope<1> hs(soa.Self());
204 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800207 "no type \"%s\" found and so no field \"%s\" "
208 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 ClassHelper(c.Get()).GetDescriptor());
210 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800211 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 }
213 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800214 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800218 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
220 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
221 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700222 sig, name, ClassHelper(c.Get()).GetDescriptor());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800223 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700224 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700226}
227
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800228static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700231 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700232 vm->pin_table.Add(array);
233}
234
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800235static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700238 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700239 vm->pin_table.Remove(array);
240}
241
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800242static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700245 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800246 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
247 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
248 "%s offset=%d length=%d %s.length=%d",
249 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700250}
Ian Rogers0571d352011-11-03 19:51:38 -0700251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
253 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800255 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
256 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
257 "offset=%d length=%d string.length()=%d", start, length,
258 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700259}
Elliott Hughes814e4032011-08-23 12:07:56 -0700260
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263 // Turn the const char* into a java.lang.String.
264 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800265 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700267 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700268
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 // Choose an appropriate constructor and set up the arguments.
270 jvalue args[2];
271 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800272 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800274 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 signature = "(Ljava/lang/String;)V";
276 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 signature = "(Ljava/lang/Throwable;)V";
279 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700280 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
282 args[0].l = s.get();
283 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700284 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800286 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800287 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800289 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 return JNI_ERR;
291 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700292
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800293 ScopedLocalRef<jthrowable> exception(
294 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
295 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 return JNI_ERR;
297 }
Ian Rogersef28b142012-11-30 14:22:18 -0800298 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800300 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700302}
303
Elliott Hughes462c9442012-03-23 18:47:50 -0700304static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800305 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700306 return JNI_ERR;
307 }
308
Elliott Hughes462c9442012-03-23 18:47:50 -0700309 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700310 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800311 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700312 *p_env = self->GetJniEnv();
313 return JNI_OK;
314 }
315
316 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
317
318 // No threads allowed in zygote mode.
319 if (runtime->IsZygote()) {
320 LOG(ERROR) << "Attempt to attach a thread in the zygote";
321 return JNI_ERR;
322 }
323
Elliott Hughes462c9442012-03-23 18:47:50 -0700324 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800325 const char* thread_name = nullptr;
326 jobject thread_group = nullptr;
327 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700328 if (IsBadJniVersion(args->version)) {
329 LOG(ERROR) << "Bad JNI version passed to "
330 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
331 << args->version;
332 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700333 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700334 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700335 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700336 }
Elliott Hughes75770752011-08-24 17:52:38 -0700337
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800338 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800339 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700340 return JNI_ERR;
341 } else {
342 *p_env = Thread::Current()->GetJniEnv();
343 return JNI_OK;
344 }
Elliott Hughes75770752011-08-24 17:52:38 -0700345}
346
Elliott Hughes79082e32011-08-25 12:07:32 -0700347class SharedLibrary {
348 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800349 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700350 : path_(path),
351 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700352 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700353 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700354 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700355 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700356 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700357 }
358
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800359 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700360 return class_loader_;
361 }
362
363 std::string GetPath() {
364 return path_;
365 }
366
367 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700368 * Check the result of an earlier call to JNI_OnLoad on this library.
369 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700370 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 bool CheckOnLoadResult()
372 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700374 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
376 bool okay;
377 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700378 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700379
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700380 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
382 // caller can continue.
383 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
384 okay = true;
385 } else {
386 while (jni_on_load_result_ == kPending) {
387 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700388 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700390
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 okay = (jni_on_load_result_ == kOkay);
392 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
393 << (okay ? "succeeded" : "failed") << "]";
394 }
395 }
396 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700397 return okay;
398 }
399
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700401 Thread* self = Thread::Current();
402 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700403
Elliott Hughes79082e32011-08-25 12:07:32 -0700404 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700405 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700406
407 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700408 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700409 }
410
411 void* FindSymbol(const std::string& symbol_name) {
412 return dlsym(handle_, symbol_name.c_str());
413 }
414
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800415 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800416 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800417 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800418 }
419 }
420
Elliott Hughes79082e32011-08-25 12:07:32 -0700421 private:
422 enum JNI_OnLoadState {
423 kPending,
424 kFailed,
425 kOkay,
426 };
427
428 // Path to library "/system/lib/libjni.so".
429 std::string path_;
430
431 // The void* returned by dlopen(3).
432 void* handle_;
433
434 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800435 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700436
437 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700439 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700440 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700441 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700442 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700443 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700444 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700445};
446
Elliott Hughes79082e32011-08-25 12:07:32 -0700447// This exists mainly to keep implementation details out of the header file.
448class Libraries {
449 public:
450 Libraries() {
451 }
452
453 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700454 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700455 }
456
Elliott Hughesae80b492012-04-24 10:43:17 -0700457 void Dump(std::ostream& os) const {
458 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700459 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700460 if (!first) {
461 os << ' ';
462 }
463 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700464 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700465 }
466 }
467
468 size_t size() const {
469 return libraries_.size();
470 }
471
Elliott Hughes79082e32011-08-25 12:07:32 -0700472 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700473 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800474 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700475 }
476
477 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700478 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700479 }
480
481 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800482 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700483 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 std::string jni_short_name(JniShortName(m));
485 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800486 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700487 for (const auto& lib : libraries_) {
488 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700489 if (library->GetClassLoader() != declaring_class_loader) {
490 // We only search libraries loaded by the appropriate ClassLoader.
491 continue;
492 }
493 // Try the short name then the long name...
494 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800495 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700496 fn = library->FindSymbol(jni_long_name);
497 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800498 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800499 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
500 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 return fn;
502 }
503 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700505 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700506 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700507 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800508 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700509 }
510
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800511 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800512 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800513 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800514 }
515 }
516
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700518 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700519};
520
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700521#define CHECK_NON_NULL_ARGUMENT(value) CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value)
522
523#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800524 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700525 JniAbortF(name, #value " == null"); \
Ian Rogersbc939662013-08-15 10:26:54 -0700526 }
527
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700528#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800529 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700530 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700531 }
532
Elliott Hughescdf53122011-08-19 15:46:09 -0700533class JNI {
534 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700535 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700536 return JNI_VERSION_1_6;
537 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700538
Ian Rogers25e8b912012-09-07 11:31:36 -0700539 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700540 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800541 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700542 }
543
Elliott Hughescdf53122011-08-19 15:46:09 -0700544 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700545 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700546 Runtime* runtime = Runtime::Current();
547 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700548 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700549 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800550 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700551 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700552 StackHandleScope<1> hs(soa.Self());
553 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800554 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700555 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800556 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700557 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700559 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700560
Ian Rogers62f05122014-03-21 11:21:29 -0700561 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700562 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700563 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700564 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700565 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700566
Ian Rogers62f05122014-03-21 11:21:29 -0700567 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700568 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700569 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700570 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700571 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700572
Elliott Hughescdf53122011-08-19 15:46:09 -0700573 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700574 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800576 mirror::ArtMethod* m = soa.DecodeMethod(mid);
577 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700578 jobject art_method = soa.AddLocalReference<jobject>(m);
579 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
580 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800581 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700582 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800583 SetObjectField(env, reflect_method,
584 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700585 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700587
Elliott Hughescdf53122011-08-19 15:46:09 -0700588 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700589 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 jobject art_field = soa.AddLocalReference<jobject>(f);
593 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
594 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800595 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700596 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800597 SetObjectField(env, reflect_field,
598 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700600 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700601
Elliott Hughes37f7a402011-08-22 18:56:01 -0700602 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700603 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800605 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700607 }
608
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700609 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700610 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700615
Elliott Hughes37f7a402011-08-22 18:56:01 -0700616 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700617 CHECK_NON_NULL_ARGUMENT(java_class1);
618 CHECK_NON_NULL_ARGUMENT(java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800620 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
621 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700622 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughese84278b2012-03-22 10:06:53 -0700625 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700626 CHECK_NON_NULL_ARGUMENT(java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800627 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700628 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700629 return JNI_TRUE;
630 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
633 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700634 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700635 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700637
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800640 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
641 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700642 return JNI_ERR;
643 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800644 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
645 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700646 return JNI_OK;
647 }
648
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700649 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700650 CHECK_NON_NULL_ARGUMENT(c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800651 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700652 }
653
654 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700655 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700656 }
657
658 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700659 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700660 }
661
662 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700664
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700665 StackHandleScope<3> hs(soa.Self());
666 // TODO: Use nullptr instead of null handles?
667 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
668 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
669 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800670 uint32_t old_throw_dex_pc;
671 {
672 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800673 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700674 old_throw_this_object.Assign(old_throw_location.GetThis());
675 old_throw_method.Assign(old_throw_location.GetMethod());
676 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800677 old_throw_dex_pc = old_throw_location.GetDexPc();
678 soa.Self()->ClearException();
679 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800680 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700681 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700682 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
683 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800684 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700685 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700686 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 } else {
688 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800690 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700691 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800692 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 }
694 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700695 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700697
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700698 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700700
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800703 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
706
Ian Rogers25e8b912012-09-07 11:31:36 -0700707 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 LOG(FATAL) << "JNI FatalError called: " << msg;
709 }
710
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700711 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800712 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700713 return JNI_ERR;
714 }
Ian Rogersef28b142012-11-30 14:22:18 -0800715 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 return JNI_OK;
717 }
718
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700719 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800721 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 soa.Env()->PopFrame();
723 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
725
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700726 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800727 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 }
729
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700731 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800732 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800733 // Check for null after decoding the object to handle cleared weak globals.
734 if (decoded_obj == nullptr) {
735 return nullptr;
736 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700739 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700740 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 return reinterpret_cast<jobject>(ref);
742 }
743
744 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800745 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 return;
747 }
Ian Rogersef28b142012-11-30 14:22:18 -0800748 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800750 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700751 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700752
753 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
754 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
755 << "failed to find entry";
756 }
757 }
758
759 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800761 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 }
763
764 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700765 if (obj != nullptr) {
766 ScopedObjectAccess soa(env);
767 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 }
769 }
770
771 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700772 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800773 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800774 // Check for null after decoding the object to handle cleared weak globals.
775 if (decoded_obj == nullptr) {
776 return nullptr;
777 }
778 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 }
780
781 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800782 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 return;
784 }
Ian Rogersef28b142012-11-30 14:22:18 -0800785 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700786
Ian Rogersef28b142012-11-30 14:22:18 -0800787 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 if (!locals.Remove(cookie, obj)) {
789 // Attempting to delete a local reference that is not in the
790 // topmost local reference frame is a no-op. DeleteLocalRef returns
791 // void and doesn't throw any exceptions, but we should probably
792 // complain about it so the user will notice that things aren't
793 // going quite the way they expect.
794 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
795 << "failed to find entry";
796 }
797 }
798
799 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700800 if (obj1 == obj2) {
801 return JNI_TRUE;
802 } else {
803 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800804 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
805 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700806 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 }
808
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700809 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700810 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800812 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800813 if (c == nullptr) {
814 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700815 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700816 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 }
818
Ian Rogersbc939662013-08-15 10:26:54 -0700819 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700822 CHECK_NON_NULL_ARGUMENT(java_class);
823 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700824 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 va_end(args);
826 return result;
827 }
828
Elliott Hughes72025e52011-08-23 17:50:30 -0700829 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700830 CHECK_NON_NULL_ARGUMENT(java_class);
831 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800833 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800834 if (c == nullptr) {
835 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700836 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800837 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800838 if (result == nullptr) {
839 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700840 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800843 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800844 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700845 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800846 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 }
848
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700850 CHECK_NON_NULL_ARGUMENT(java_class);
851 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800853 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800854 if (c == nullptr) {
855 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700856 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800857 mirror::Object* result = c->AllocObject(soa.Self());
858 if (result == nullptr) {
859 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700860 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700862 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800863 if (soa.Self()->IsExceptionPending()) {
864 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700865 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800866 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 }
868
Ian Rogersbc939662013-08-15 10:26:54 -0700869 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700870 CHECK_NON_NULL_ARGUMENT(java_class);
871 CHECK_NON_NULL_ARGUMENT(name);
872 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700874 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
Ian Rogersbc939662013-08-15 10:26:54 -0700877 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
878 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700879 CHECK_NON_NULL_ARGUMENT(java_class);
880 CHECK_NON_NULL_ARGUMENT(name);
881 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700883 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 va_list ap;
888 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700889 CHECK_NON_NULL_ARGUMENT(obj);
890 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700891 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700898 CHECK_NON_NULL_ARGUMENT(obj);
899 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700900 ScopedObjectAccess soa(env);
901 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
902 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 }
904
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700906 CHECK_NON_NULL_ARGUMENT(obj);
907 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700909 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
910 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 }
913
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 va_list ap;
916 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700917 CHECK_NON_NULL_ARGUMENT(obj);
918 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700919 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700922 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 }
924
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
929 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700933 CHECK_NON_NULL_ARGUMENT(obj);
934 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700936 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
937 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 va_list ap;
942 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700943 CHECK_NON_NULL_ARGUMENT(obj);
944 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700945 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700948 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
955 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700959 CHECK_NON_NULL_ARGUMENT(obj);
960 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700962 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
963 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 }
965
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 va_list ap;
968 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700969 CHECK_NON_NULL_ARGUMENT(obj);
970 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700971 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700974 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
981 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700985 CHECK_NON_NULL_ARGUMENT(obj);
986 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700987 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700988 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
989 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_list ap;
994 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700995 CHECK_NON_NULL_ARGUMENT(obj);
996 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700997 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001000 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
1007 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001011 CHECK_NON_NULL_ARGUMENT(obj);
1012 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001014 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1015 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_list ap;
1020 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001021 CHECK_NON_NULL_ARGUMENT(obj);
1022 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001023 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001026 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
1033 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001037 CHECK_NON_NULL_ARGUMENT(obj);
1038 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001040 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1041 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_list ap;
1046 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001047 CHECK_NON_NULL_ARGUMENT(obj);
1048 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001049 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001052 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
1059 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001063 CHECK_NON_NULL_ARGUMENT(obj);
1064 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001066 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1067 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_list ap;
1072 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001073 CHECK_NON_NULL_ARGUMENT(obj);
1074 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001075 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001078 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
1085 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001089 CHECK_NON_NULL_ARGUMENT(obj);
1090 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001092 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1093 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001099 CHECK_NON_NULL_ARGUMENT(obj);
1100 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001101 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
1111 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001115 CHECK_NON_NULL_ARGUMENT(obj);
1116 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001118 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1119 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_list ap;
1124 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001125 CHECK_NON_NULL_ARGUMENT(obj);
1126 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001127 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001128 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
1136 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001140 CHECK_NON_NULL_ARGUMENT(obj);
1141 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001143 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001146 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001149 CHECK_NON_NULL_ARGUMENT(obj);
1150 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001151 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001152 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1153 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 va_end(ap);
1155 return local_result;
1156 }
1157
Ian Rogersbc939662013-08-15 10:26:54 -07001158 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1159 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001160 CHECK_NON_NULL_ARGUMENT(obj);
1161 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1164 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Ian Rogersbc939662013-08-15 10:26:54 -07001167 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1168 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001169 CHECK_NON_NULL_ARGUMENT(obj);
1170 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001171 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001172 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
Ian Rogersbc939662013-08-15 10:26:54 -07001176 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1177 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001180 CHECK_NON_NULL_ARGUMENT(obj);
1181 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001182 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001185 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
Ian Rogersbc939662013-08-15 10:26:54 -07001188 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1189 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001190 CHECK_NON_NULL_ARGUMENT(obj);
1191 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001192 ScopedObjectAccess soa(env);
1193 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Ian Rogersbc939662013-08-15 10:26:54 -07001196 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1197 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001198 CHECK_NON_NULL_ARGUMENT(obj);
1199 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001200 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001201 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001204 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001207 CHECK_NON_NULL_ARGUMENT(obj);
1208 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001209 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001212 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Ian Rogersbc939662013-08-15 10:26:54 -07001215 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1216 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001217 CHECK_NON_NULL_ARGUMENT(obj);
1218 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 ScopedObjectAccess soa(env);
1220 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Ian Rogersbc939662013-08-15 10:26:54 -07001223 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1224 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001225 CHECK_NON_NULL_ARGUMENT(obj);
1226 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001228 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001231 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001234 CHECK_NON_NULL_ARGUMENT(obj);
1235 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001236 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001239 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Ian Rogersbc939662013-08-15 10:26:54 -07001242 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1243 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001244 CHECK_NON_NULL_ARGUMENT(obj);
1245 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
1247 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Ian Rogersbc939662013-08-15 10:26:54 -07001250 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1251 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001252 CHECK_NON_NULL_ARGUMENT(obj);
1253 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001255 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001258 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001261 CHECK_NON_NULL_ARGUMENT(obj);
1262 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001263 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001266 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Ian Rogersbc939662013-08-15 10:26:54 -07001269 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1270 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001271 CHECK_NON_NULL_ARGUMENT(obj);
1272 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env);
1274 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Ian Rogersbc939662013-08-15 10:26:54 -07001277 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1278 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001279 CHECK_NON_NULL_ARGUMENT(obj);
1280 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001281 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001282 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001285 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001288 CHECK_NON_NULL_ARGUMENT(obj);
1289 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001290 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001293 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Ian Rogersbc939662013-08-15 10:26:54 -07001296 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1297 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001298 CHECK_NON_NULL_ARGUMENT(obj);
1299 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 ScopedObjectAccess soa(env);
1301 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Ian Rogersbc939662013-08-15 10:26:54 -07001304 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1305 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001306 CHECK_NON_NULL_ARGUMENT(obj);
1307 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001309 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001315 CHECK_NON_NULL_ARGUMENT(obj);
1316 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001317 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001320 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Ian Rogersbc939662013-08-15 10:26:54 -07001323 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1324 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001325 CHECK_NON_NULL_ARGUMENT(obj);
1326 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327 ScopedObjectAccess soa(env);
1328 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Ian Rogersbc939662013-08-15 10:26:54 -07001331 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1332 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001333 CHECK_NON_NULL_ARGUMENT(obj);
1334 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001335 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001336 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001339 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001342 CHECK_NON_NULL_ARGUMENT(obj);
1343 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001344 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001345 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001347 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Ian Rogersbc939662013-08-15 10:26:54 -07001350 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1351 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001352 CHECK_NON_NULL_ARGUMENT(obj);
1353 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 ScopedObjectAccess soa(env);
1355 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Ian Rogersbc939662013-08-15 10:26:54 -07001358 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1359 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001360 CHECK_NON_NULL_ARGUMENT(obj);
1361 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001362 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001363 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001366 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001369 CHECK_NON_NULL_ARGUMENT(obj);
1370 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001371 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001374 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Ian Rogersbc939662013-08-15 10:26:54 -07001377 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1378 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001379 CHECK_NON_NULL_ARGUMENT(obj);
1380 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001381 ScopedObjectAccess soa(env);
1382 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Ian Rogersbc939662013-08-15 10:26:54 -07001385 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1386 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001387 CHECK_NON_NULL_ARGUMENT(obj);
1388 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001389 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001390 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001393 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001395 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001396 CHECK_NON_NULL_ARGUMENT(obj);
1397 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001398 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 va_end(ap);
1401 }
1402
Brian Carlstromea46f952013-07-30 01:26:50 -07001403 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1404 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001405 CHECK_NON_NULL_ARGUMENT(obj);
1406 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001407 ScopedObjectAccess soa(env);
1408 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Ian Rogersbc939662013-08-15 10:26:54 -07001411 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1412 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001413 CHECK_NON_NULL_ARGUMENT(obj);
1414 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001415 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001416 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
Ian Rogersbc939662013-08-15 10:26:54 -07001419 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001420 CHECK_NON_NULL_ARGUMENT(java_class);
1421 CHECK_NON_NULL_ARGUMENT(name);
1422 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001424 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001426
Ian Rogersbc939662013-08-15 10:26:54 -07001427 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1428 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001429 CHECK_NON_NULL_ARGUMENT(java_class);
1430 CHECK_NON_NULL_ARGUMENT(name);
1431 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001433 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001437 CHECK_NON_NULL_ARGUMENT(obj);
1438 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001440 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1441 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001442 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001444
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001446 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001448 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001449 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001453 CHECK_NON_NULL_ARGUMENT(java_object);
1454 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001456 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1457 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1458 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001459 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001463 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001465 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1466 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001467 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470#define GET_PRIMITIVE_FIELD(fn, instance) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001471 CHECK_NON_NULL_ARGUMENT(instance); \
1472 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001474 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1475 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001476 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001477
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001478#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001479 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001480 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001481 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001482 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001485 CHECK_NON_NULL_ARGUMENT(instance); \
1486 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001487 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001488 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1489 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001490 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001492#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001493 CHECK_NON_NULL_ARGUMENT(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001494 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001495 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001496 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001499 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001503 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001507 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001511 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001515 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001519 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001523 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001527 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001531 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001535 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001538 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001539 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001543 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001547 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001550 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001551 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001555 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001559 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560 }
1561
1562 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001563 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 }
1565
1566 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001567 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 }
1569
1570 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001571 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 }
1573
1574 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001575 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 }
1577
1578 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001579 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 }
1581
1582 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001583 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 }
1585
1586 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001587 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 }
1589
1590 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001591 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 }
1593
1594 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001595 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 }
1597
1598 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001599 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 }
1601
1602 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001603 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 }
1605
1606 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001607 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 }
1609
1610 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001611 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 }
1613
1614 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001615 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 }
1617
1618 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001619 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001620 }
1621
1622 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001623 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001629 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001630 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001631 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
1634 return local_result;
1635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list 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(InvokeWithVarArgs(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 jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001645 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001647 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001648 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001654 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001656 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001658 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001662 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001664 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001667 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001668 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001669 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001670 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001673 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001676 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001677 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001678 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001680 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001684 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001686 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001690 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001692 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001698 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001699 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001700 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001702 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001705 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001706 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001708 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001712 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001714 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001720 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001722 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001724 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001728 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001730 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001734 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001736 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001742 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001743 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001744 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001746 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001750 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001752 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001756 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001764 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001765 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001766 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001768 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001772 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001774 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001778 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001780 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001786 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001787 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001788 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001790 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001793 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001794 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001800 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001802 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001807 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001808 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001809 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001810 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001812 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001816 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001817 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001818 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001822 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001824 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001829 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001830 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001831 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001832 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 va_end(ap);
1834 }
1835
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001836 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001837 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001838 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001839 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001842 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001843 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001845 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes814e4032011-08-23 12:07:56 -07001848 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001849 if (UNLIKELY(char_count < 0)) {
1850 JniAbortF("NewString", "char_count < 0: %d", char_count);
1851 return nullptr;
1852 }
1853 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1854 JniAbortF("NewString", "chars == null && char_count > 0");
1855 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001856 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001857 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001858 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
1862 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001863 if (utf == nullptr) {
1864 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001867 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001872 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001874 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001875 }
1876
1877 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
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 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001881 }
1882
Ian Rogersbc939662013-08-15 10:26:54 -07001883 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1884 jchar* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001885 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001887 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001888 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001890 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001891 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001892 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1893 memcpy(buf, chars + start, length * sizeof(jchar));
1894 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001895 }
1896
Ian Rogersbc939662013-08-15 10:26:54 -07001897 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1898 char* buf) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001899 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001901 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001902 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001904 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001905 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001906 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1907 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1908 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001909 }
1910
Elliott Hughes75770752011-08-24 17:52:38 -07001911 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001912 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001914 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1915 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001917 if (is_copy != nullptr) {
1918 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001919 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001920 int32_t char_count = s->GetLength();
1921 int32_t offset = s->GetOffset();
1922 jchar* bytes = new jchar[char_count + 1];
1923 for (int32_t i = 0; i < char_count; i++) {
1924 bytes[i] = chars->Get(i + offset);
1925 }
1926 bytes[char_count] = '\0';
1927 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001928 }
1929
Mathieu Chartier590fee92013-09-13 13:46:47 -07001930 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001931 CHECK_NON_NULL_ARGUMENT(java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001932 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001934 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001938 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001942 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes75770752011-08-24 17:52:38 -07001945 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001946 if (java_string == nullptr) {
1947 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001948 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001949 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001950 *is_copy = JNI_TRUE;
1951 }
Ian Rogersef28b142012-11-30 14:22:18 -08001952 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001953 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 size_t byte_count = s->GetUtfLength();
1955 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001956 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001957 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1958 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1959 bytes[byte_count] = '\0';
1960 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001964 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001965 }
1966
Elliott Hughesbd935992011-08-22 11:59:34 -07001967 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001968 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001969 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001970 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001971 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001972 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1973 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001974 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001975 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes814e4032011-08-23 12:07:56 -07001978 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001979 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001980 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001981 mirror::ObjectArray<mirror::Object>* array =
1982 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Ian Rogersbc939662013-08-15 10:26:54 -07001986 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1987 jobject java_value) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001988 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001989 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001990 mirror::ObjectArray<mirror::Object>* array =
1991 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1992 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001993 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
1996 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001998 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
2001 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002003 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
2006 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002008 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
2011 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002013 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
2016 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002018 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
2021 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002022 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002023 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
2026 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002027 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002028 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Ian Rogers1d99e452014-01-02 17:36:41 -08002031 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2032 jobject initial_element) {
2033 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002034 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002035 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002036 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002037
2038 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002039 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002041 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002042 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002043 if (UNLIKELY(element_class->IsPrimitive())) {
2044 JniAbortF("NewObjectArray", "not an object type: %s",
2045 PrettyDescriptor(element_class).c_str());
2046 return nullptr;
2047 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002048 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002049 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002050 if (UNLIKELY(array_class == nullptr)) {
2051 return nullptr;
2052 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes75770752011-08-24 17:52:38 -07002055 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002056 mirror::ObjectArray<mirror::Object>* result =
2057 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002058 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002059 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002060 if (initial_object != nullptr) {
2061 mirror::Class* element_class = result->GetClass()->GetComponentType();
2062 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2063 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2064 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2065 PrettyDescriptor(element_class).c_str());
2066
2067 } else {
2068 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002069 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002070 }
2071 }
Elliott Hughes75770752011-08-24 17:52:38 -07002072 }
2073 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002074 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
2077 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002078 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002079 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Ian Rogersa15e67d2012-02-28 13:51:55 -08002082 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002083 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002085 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002086 gc::Heap* heap = Runtime::Current()->GetHeap();
2087 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002088 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002089 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002090 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002091 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002093 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002094 *is_copy = JNI_FALSE;
2095 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002096 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002097 }
2098
Mathieu Chartier590fee92013-09-13 13:46:47 -07002099 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002100 CHECK_NON_NULL_ARGUMENT(array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002101 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002102 }
2103
Elliott Hughes75770752011-08-24 17:52:38 -07002104 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002105 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002107 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes75770752011-08-24 17:52:38 -07002110 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002111 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002112 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002113 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes75770752011-08-24 17:52:38 -07002116 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002117 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002118 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002119 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes75770752011-08-24 17:52:38 -07002122 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002123 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002124 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002125 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes75770752011-08-24 17:52:38 -07002128 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002129 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002130 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002131 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes75770752011-08-24 17:52:38 -07002134 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002135 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002136 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002137 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughes75770752011-08-24 17:52:38 -07002140 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002141 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002142 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002143 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes75770752011-08-24 17:52:38 -07002146 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002147 CHECK_NON_NULL_ARGUMENT(array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002148 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002149 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Mathieu Chartier590fee92013-09-13 13:46:47 -07002152 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2153 jint mode) {
2154 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Mathieu Chartier590fee92013-09-13 13:46:47 -07002157 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2158 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Mathieu Chartier590fee92013-09-13 13:46:47 -07002161 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2162 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Mathieu Chartier590fee92013-09-13 13:46:47 -07002165 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2166 jint mode) {
2167 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169
Mathieu Chartier590fee92013-09-13 13:46:47 -07002170 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2171 jint mode) {
2172 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Mathieu Chartier590fee92013-09-13 13:46:47 -07002175 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2176 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Mathieu Chartier590fee92013-09-13 13:46:47 -07002179 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2180 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Mathieu Chartier590fee92013-09-13 13:46:47 -07002183 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2184 jint mode) {
2185 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Ian Rogersbc939662013-08-15 10:26:54 -07002188 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2189 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002190 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002191 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2192 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Ian Rogersbc939662013-08-15 10:26:54 -07002195 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2196 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002198 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
Ian Rogersbc939662013-08-15 10:26:54 -07002201 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2202 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002203 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002204 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Ian Rogersbc939662013-08-15 10:26:54 -07002207 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2208 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002209 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002210 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(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 GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2215 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002216 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002217 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2218 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Ian Rogersbc939662013-08-15 10:26:54 -07002221 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2222 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002223 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002224 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Ian Rogersbc939662013-08-15 10:26:54 -07002227 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2228 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002229 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002230 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Ian Rogersbc939662013-08-15 10:26:54 -07002233 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2234 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002235 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002236 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2237 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Ian Rogersbc939662013-08-15 10:26:54 -07002240 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2241 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002243 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2244 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Ian Rogersbc939662013-08-15 10:26:54 -07002247 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2248 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002250 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Ian Rogersbc939662013-08-15 10:26:54 -07002253 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2254 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002255 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002256 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Ian Rogersbc939662013-08-15 10:26:54 -07002259 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2260 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002261 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002262 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(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 SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2267 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002268 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002269 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2270 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272
Ian Rogersbc939662013-08-15 10:26:54 -07002273 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2274 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002275 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002276 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Ian Rogersbc939662013-08-15 10:26:54 -07002279 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2280 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002282 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284
Ian Rogersbc939662013-08-15 10:26:54 -07002285 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2286 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002287 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002288 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2289 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Ian Rogersbc939662013-08-15 10:26:54 -07002292 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2293 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002294 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2295 }
2296
Ian Rogersbc939662013-08-15 10:26:54 -07002297 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2298 jint method_count, bool return_errors) {
2299 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002300 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002301 return JNI_ERR; // Not reached.
2302 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002303 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002304 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002305 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002306 if (UNLIKELY(method_count == 0)) {
2307 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2308 << PrettyDescriptor(c);
2309 return JNI_OK;
2310 }
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002311 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods);
Ian Rogersbc939662013-08-15 10:26:54 -07002312 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 const char* name = methods[i].name;
2314 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002315 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002317 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 ++sig;
2319 }
2320
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002321 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2322 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002323 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002325 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002326 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002327 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002328 << PrettyDescriptor(c) << "." << name << sig << " in "
2329 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002332 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002333 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002334 << PrettyDescriptor(c) << "." << name << sig
2335 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002336 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002337 return JNI_ERR;
2338 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002339
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002340 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002341
Ian Rogers1eb512d2013-10-18 15:42:20 -07002342 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002343 }
2344 return JNI_OK;
2345 }
2346
Elliott Hughes5174fe62011-08-23 15:12:35 -07002347 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002348 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002350 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002351
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002352 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002353
2354 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002355 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002356 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002357 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002358 }
2359 }
2360 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002361 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002362 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002363 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002364 }
2365 }
2366
2367 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 }
2369
Ian Rogers719d1a32014-03-06 12:13:39 -08002370 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002371 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002373 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2374 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002375 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002376 return JNI_ERR;
2377 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002379 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002380 }
2381
Ian Rogers719d1a32014-03-06 12:13:39 -08002382 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002383 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002385 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002386 o->MonitorExit(soa.Self());
2387 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002388 return JNI_ERR;
2389 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002390 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002391 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 }
2393
2394 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002395 CHECK_NON_NULL_ARGUMENT(vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002396 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002397 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002398 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002399 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002400 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002401 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002402 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002403 }
2404
Elliott Hughescdf53122011-08-19 15:46:09 -07002405 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002406 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002407 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002408 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002409 if (address == nullptr && capacity != 0) {
2410 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002411 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002412
Ian Rogers936b37f2014-02-14 00:52:24 -08002413 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002414 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002415 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002416 jint capacity_arg = static_cast<jint>(capacity);
2417
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002418 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2419 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002420 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002421 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 }
2423
Elliott Hughesb465ab02011-08-24 11:21:21 -07002424 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002425 return reinterpret_cast<void*>(env->GetLongField(
2426 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002427 }
2428
Elliott Hughesb465ab02011-08-24 11:21:21 -07002429 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002430 return static_cast<jlong>(env->GetIntField(
2431 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002432 }
2433
Elliott Hughesb465ab02011-08-24 11:21:21 -07002434 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002435 CHECK_NON_NULL_ARGUMENT(java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002436
2437 // Do we definitely know what kind of reference this is?
2438 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2439 IndirectRefKind kind = GetIndirectRefKind(ref);
2440 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002441 case kLocal: {
2442 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002443 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002444 return JNILocalRefType;
2445 }
2446 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002447 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002448 case kGlobal:
2449 return JNIGlobalRefType;
2450 case kWeakGlobal:
2451 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002452 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002453 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002454 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002455 return JNILocalRefType;
2456 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002457 return JNIInvalidRefType;
2458 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002459 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2460 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002461 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002462
2463 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002464 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2465 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002466 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002467 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002468 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2469 return JNI_ERR;
2470 }
2471 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002472 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002473 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2474 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002475 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002476 soa.Self()->ThrowOutOfMemoryError(caller);
2477 }
2478 return okay ? JNI_OK : JNI_ERR;
2479 }
2480
2481 template<typename JniT, typename ArtT>
2482 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002483 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002484 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002485 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002486 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002487 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002488 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002489 return soa.AddLocalReference<JniT>(result);
2490 }
2491
2492 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2493 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2494 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002496 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2497 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002498 // Only make a copy if necessary.
2499 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2500 if (is_copy != nullptr) {
2501 *is_copy = JNI_TRUE;
2502 }
2503 static const size_t component_size = array->GetClass()->GetComponentSize();
2504 size_t size = array->GetLength() * component_size;
2505 void* data = new uint64_t[RoundUp(size, 8) / 8];
2506 memcpy(data, array->GetData(), size);
2507 return reinterpret_cast<CArrayT>(data);
2508 } else {
2509 if (is_copy != nullptr) {
2510 *is_copy = JNI_FALSE;
2511 }
2512 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002513 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002514 }
2515
Mathieu Chartier590fee92013-09-13 13:46:47 -07002516 template <typename ArrayT, typename ElementT>
2517 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2518 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002519 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002520 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002521 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002522 gc::Heap* heap = Runtime::Current()->GetHeap();
2523 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2524 size_t bytes = array->GetLength() * component_size;
2525 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2526 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002527 if (is_copy) {
2528 // Sanity check: If elements is not the same as the java array's data, it better not be a
2529 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2530 // copies we make?
2531 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2532 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2533 reinterpret_cast<void*>(elements), array_data);
2534 return;
2535 }
2536 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002537 // Don't need to copy if we had a direct pointer.
2538 if (mode != JNI_ABORT && is_copy) {
2539 memcpy(array_data, elements, bytes);
2540 }
2541 if (mode != JNI_COMMIT) {
2542 if (is_copy) {
2543 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002544 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002545 // Non copy to a movable object must means that we had disabled the moving GC.
2546 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002547 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002548 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002549 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002550 }
2551
2552 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2553 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2554 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002555 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002556 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002557 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2558 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2559 ThrowAIOOBE(soa, array, start, length, "src");
2560 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002561 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002562 JavaT* data = array->GetData();
2563 memcpy(buf, data + start, length * sizeof(JavaT));
2564 }
2565 }
2566
2567 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2568 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2569 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002571 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002572 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2573 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2574 ThrowAIOOBE(soa, array, start, length, "dst");
2575 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002576 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002577 JavaT* data = array->GetData();
2578 memcpy(data + start, buf, length * sizeof(JavaT));
2579 }
2580 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002581};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002582
Elliott Hughes88c5c352012-03-15 18:49:48 -07002583const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002584 nullptr, // reserved0.
2585 nullptr, // reserved1.
2586 nullptr, // reserved2.
2587 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002588 JNI::GetVersion,
2589 JNI::DefineClass,
2590 JNI::FindClass,
2591 JNI::FromReflectedMethod,
2592 JNI::FromReflectedField,
2593 JNI::ToReflectedMethod,
2594 JNI::GetSuperclass,
2595 JNI::IsAssignableFrom,
2596 JNI::ToReflectedField,
2597 JNI::Throw,
2598 JNI::ThrowNew,
2599 JNI::ExceptionOccurred,
2600 JNI::ExceptionDescribe,
2601 JNI::ExceptionClear,
2602 JNI::FatalError,
2603 JNI::PushLocalFrame,
2604 JNI::PopLocalFrame,
2605 JNI::NewGlobalRef,
2606 JNI::DeleteGlobalRef,
2607 JNI::DeleteLocalRef,
2608 JNI::IsSameObject,
2609 JNI::NewLocalRef,
2610 JNI::EnsureLocalCapacity,
2611 JNI::AllocObject,
2612 JNI::NewObject,
2613 JNI::NewObjectV,
2614 JNI::NewObjectA,
2615 JNI::GetObjectClass,
2616 JNI::IsInstanceOf,
2617 JNI::GetMethodID,
2618 JNI::CallObjectMethod,
2619 JNI::CallObjectMethodV,
2620 JNI::CallObjectMethodA,
2621 JNI::CallBooleanMethod,
2622 JNI::CallBooleanMethodV,
2623 JNI::CallBooleanMethodA,
2624 JNI::CallByteMethod,
2625 JNI::CallByteMethodV,
2626 JNI::CallByteMethodA,
2627 JNI::CallCharMethod,
2628 JNI::CallCharMethodV,
2629 JNI::CallCharMethodA,
2630 JNI::CallShortMethod,
2631 JNI::CallShortMethodV,
2632 JNI::CallShortMethodA,
2633 JNI::CallIntMethod,
2634 JNI::CallIntMethodV,
2635 JNI::CallIntMethodA,
2636 JNI::CallLongMethod,
2637 JNI::CallLongMethodV,
2638 JNI::CallLongMethodA,
2639 JNI::CallFloatMethod,
2640 JNI::CallFloatMethodV,
2641 JNI::CallFloatMethodA,
2642 JNI::CallDoubleMethod,
2643 JNI::CallDoubleMethodV,
2644 JNI::CallDoubleMethodA,
2645 JNI::CallVoidMethod,
2646 JNI::CallVoidMethodV,
2647 JNI::CallVoidMethodA,
2648 JNI::CallNonvirtualObjectMethod,
2649 JNI::CallNonvirtualObjectMethodV,
2650 JNI::CallNonvirtualObjectMethodA,
2651 JNI::CallNonvirtualBooleanMethod,
2652 JNI::CallNonvirtualBooleanMethodV,
2653 JNI::CallNonvirtualBooleanMethodA,
2654 JNI::CallNonvirtualByteMethod,
2655 JNI::CallNonvirtualByteMethodV,
2656 JNI::CallNonvirtualByteMethodA,
2657 JNI::CallNonvirtualCharMethod,
2658 JNI::CallNonvirtualCharMethodV,
2659 JNI::CallNonvirtualCharMethodA,
2660 JNI::CallNonvirtualShortMethod,
2661 JNI::CallNonvirtualShortMethodV,
2662 JNI::CallNonvirtualShortMethodA,
2663 JNI::CallNonvirtualIntMethod,
2664 JNI::CallNonvirtualIntMethodV,
2665 JNI::CallNonvirtualIntMethodA,
2666 JNI::CallNonvirtualLongMethod,
2667 JNI::CallNonvirtualLongMethodV,
2668 JNI::CallNonvirtualLongMethodA,
2669 JNI::CallNonvirtualFloatMethod,
2670 JNI::CallNonvirtualFloatMethodV,
2671 JNI::CallNonvirtualFloatMethodA,
2672 JNI::CallNonvirtualDoubleMethod,
2673 JNI::CallNonvirtualDoubleMethodV,
2674 JNI::CallNonvirtualDoubleMethodA,
2675 JNI::CallNonvirtualVoidMethod,
2676 JNI::CallNonvirtualVoidMethodV,
2677 JNI::CallNonvirtualVoidMethodA,
2678 JNI::GetFieldID,
2679 JNI::GetObjectField,
2680 JNI::GetBooleanField,
2681 JNI::GetByteField,
2682 JNI::GetCharField,
2683 JNI::GetShortField,
2684 JNI::GetIntField,
2685 JNI::GetLongField,
2686 JNI::GetFloatField,
2687 JNI::GetDoubleField,
2688 JNI::SetObjectField,
2689 JNI::SetBooleanField,
2690 JNI::SetByteField,
2691 JNI::SetCharField,
2692 JNI::SetShortField,
2693 JNI::SetIntField,
2694 JNI::SetLongField,
2695 JNI::SetFloatField,
2696 JNI::SetDoubleField,
2697 JNI::GetStaticMethodID,
2698 JNI::CallStaticObjectMethod,
2699 JNI::CallStaticObjectMethodV,
2700 JNI::CallStaticObjectMethodA,
2701 JNI::CallStaticBooleanMethod,
2702 JNI::CallStaticBooleanMethodV,
2703 JNI::CallStaticBooleanMethodA,
2704 JNI::CallStaticByteMethod,
2705 JNI::CallStaticByteMethodV,
2706 JNI::CallStaticByteMethodA,
2707 JNI::CallStaticCharMethod,
2708 JNI::CallStaticCharMethodV,
2709 JNI::CallStaticCharMethodA,
2710 JNI::CallStaticShortMethod,
2711 JNI::CallStaticShortMethodV,
2712 JNI::CallStaticShortMethodA,
2713 JNI::CallStaticIntMethod,
2714 JNI::CallStaticIntMethodV,
2715 JNI::CallStaticIntMethodA,
2716 JNI::CallStaticLongMethod,
2717 JNI::CallStaticLongMethodV,
2718 JNI::CallStaticLongMethodA,
2719 JNI::CallStaticFloatMethod,
2720 JNI::CallStaticFloatMethodV,
2721 JNI::CallStaticFloatMethodA,
2722 JNI::CallStaticDoubleMethod,
2723 JNI::CallStaticDoubleMethodV,
2724 JNI::CallStaticDoubleMethodA,
2725 JNI::CallStaticVoidMethod,
2726 JNI::CallStaticVoidMethodV,
2727 JNI::CallStaticVoidMethodA,
2728 JNI::GetStaticFieldID,
2729 JNI::GetStaticObjectField,
2730 JNI::GetStaticBooleanField,
2731 JNI::GetStaticByteField,
2732 JNI::GetStaticCharField,
2733 JNI::GetStaticShortField,
2734 JNI::GetStaticIntField,
2735 JNI::GetStaticLongField,
2736 JNI::GetStaticFloatField,
2737 JNI::GetStaticDoubleField,
2738 JNI::SetStaticObjectField,
2739 JNI::SetStaticBooleanField,
2740 JNI::SetStaticByteField,
2741 JNI::SetStaticCharField,
2742 JNI::SetStaticShortField,
2743 JNI::SetStaticIntField,
2744 JNI::SetStaticLongField,
2745 JNI::SetStaticFloatField,
2746 JNI::SetStaticDoubleField,
2747 JNI::NewString,
2748 JNI::GetStringLength,
2749 JNI::GetStringChars,
2750 JNI::ReleaseStringChars,
2751 JNI::NewStringUTF,
2752 JNI::GetStringUTFLength,
2753 JNI::GetStringUTFChars,
2754 JNI::ReleaseStringUTFChars,
2755 JNI::GetArrayLength,
2756 JNI::NewObjectArray,
2757 JNI::GetObjectArrayElement,
2758 JNI::SetObjectArrayElement,
2759 JNI::NewBooleanArray,
2760 JNI::NewByteArray,
2761 JNI::NewCharArray,
2762 JNI::NewShortArray,
2763 JNI::NewIntArray,
2764 JNI::NewLongArray,
2765 JNI::NewFloatArray,
2766 JNI::NewDoubleArray,
2767 JNI::GetBooleanArrayElements,
2768 JNI::GetByteArrayElements,
2769 JNI::GetCharArrayElements,
2770 JNI::GetShortArrayElements,
2771 JNI::GetIntArrayElements,
2772 JNI::GetLongArrayElements,
2773 JNI::GetFloatArrayElements,
2774 JNI::GetDoubleArrayElements,
2775 JNI::ReleaseBooleanArrayElements,
2776 JNI::ReleaseByteArrayElements,
2777 JNI::ReleaseCharArrayElements,
2778 JNI::ReleaseShortArrayElements,
2779 JNI::ReleaseIntArrayElements,
2780 JNI::ReleaseLongArrayElements,
2781 JNI::ReleaseFloatArrayElements,
2782 JNI::ReleaseDoubleArrayElements,
2783 JNI::GetBooleanArrayRegion,
2784 JNI::GetByteArrayRegion,
2785 JNI::GetCharArrayRegion,
2786 JNI::GetShortArrayRegion,
2787 JNI::GetIntArrayRegion,
2788 JNI::GetLongArrayRegion,
2789 JNI::GetFloatArrayRegion,
2790 JNI::GetDoubleArrayRegion,
2791 JNI::SetBooleanArrayRegion,
2792 JNI::SetByteArrayRegion,
2793 JNI::SetCharArrayRegion,
2794 JNI::SetShortArrayRegion,
2795 JNI::SetIntArrayRegion,
2796 JNI::SetLongArrayRegion,
2797 JNI::SetFloatArrayRegion,
2798 JNI::SetDoubleArrayRegion,
2799 JNI::RegisterNatives,
2800 JNI::UnregisterNatives,
2801 JNI::MonitorEnter,
2802 JNI::MonitorExit,
2803 JNI::GetJavaVM,
2804 JNI::GetStringRegion,
2805 JNI::GetStringUTFRegion,
2806 JNI::GetPrimitiveArrayCritical,
2807 JNI::ReleasePrimitiveArrayCritical,
2808 JNI::GetStringCritical,
2809 JNI::ReleaseStringCritical,
2810 JNI::NewWeakGlobalRef,
2811 JNI::DeleteWeakGlobalRef,
2812 JNI::ExceptionCheck,
2813 JNI::NewDirectByteBuffer,
2814 JNI::GetDirectBufferAddress,
2815 JNI::GetDirectBufferCapacity,
2816 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002817};
2818
Elliott Hughes75770752011-08-24 17:52:38 -07002819JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002820 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002821 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002822 local_ref_cookie(IRT_FIRST_SEGMENT),
2823 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002824 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002825 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002826 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002827 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002828 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002829 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002830 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002831}
2832
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002833JNIEnvExt::~JNIEnvExt() {
2834}
2835
Mathieu Chartier590fee92013-09-13 13:46:47 -07002836jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2837 if (obj == nullptr) {
2838 return nullptr;
2839 }
2840 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2841}
2842
2843void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2844 if (obj != nullptr) {
2845 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2846 }
2847}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002848void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2849 check_jni = enabled;
2850 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002851}
2852
Elliott Hughes73e66f72012-05-09 09:34:45 -07002853void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2854 locals.Dump(os);
2855 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002856}
2857
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002858void JNIEnvExt::PushFrame(int /*capacity*/) {
2859 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002860 stacked_local_ref_cookies.push_back(local_ref_cookie);
2861 local_ref_cookie = locals.GetSegmentState();
2862}
2863
2864void JNIEnvExt::PopFrame() {
2865 locals.SetSegmentState(local_ref_cookie);
2866 local_ref_cookie = stacked_local_ref_cookies.back();
2867 stacked_local_ref_cookies.pop_back();
2868}
2869
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002870Offset JNIEnvExt::SegmentStateOffset() {
2871 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2872 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2873}
2874
Carl Shapiroea4dca82011-08-01 13:45:38 -07002875// JNI Invocation interface.
2876
Brian Carlstrombddf9762013-05-14 11:35:37 -07002877extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002878 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002879 if (IsBadJniVersion(args->version)) {
2880 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002881 return JNI_EVERSION;
2882 }
2883 Runtime::Options options;
2884 for (int i = 0; i < args->nOptions; ++i) {
2885 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002886 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002887 }
2888 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002889 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002890 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002891 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002892 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002893 bool started = runtime->Start();
2894 if (!started) {
2895 delete Thread::Current()->GetJniEnv();
2896 delete runtime->GetJavaVM();
2897 LOG(WARNING) << "CreateJavaVM failed";
2898 return JNI_ERR;
2899 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002900 *p_env = Thread::Current()->GetJniEnv();
2901 *p_vm = runtime->GetJavaVM();
2902 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002903}
2904
Elliott Hughesf2682d52011-08-15 16:37:04 -07002905extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002906 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002907 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002908 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002909 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002910 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002911 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002912 }
2913 return JNI_OK;
2914}
2915
2916// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002917extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002918 return JNI_ERR;
2919}
2920
Elliott Hughescdf53122011-08-19 15:46:09 -07002921class JII {
2922 public:
2923 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002924 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002925 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002926 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002927 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2928 delete raw_vm->runtime;
2929 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002930 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002931
Elliott Hughescdf53122011-08-19 15:46:09 -07002932 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002933 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002934 }
2935
2936 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002937 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002938 }
2939
2940 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002941 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002942 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002943 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002944 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2945 Runtime* runtime = raw_vm->runtime;
2946 runtime->DetachCurrentThread();
2947 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002948 }
2949
2950 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002951 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2952 // and unlike other calls that take a JNI version doesn't care if you supply
2953 // JNI_VERSION_1_1, which we don't otherwise support.
2954 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002955 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002956 return JNI_EVERSION;
2957 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002958 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002959 return JNI_ERR;
2960 }
2961 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002962 if (thread == nullptr) {
2963 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002964 return JNI_EDETACHED;
2965 }
2966 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002967 return JNI_OK;
2968 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002969};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002970
Elliott Hughes88c5c352012-03-15 18:49:48 -07002971const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002972 nullptr, // reserved0
2973 nullptr, // reserved1
2974 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002975 JII::DestroyJavaVM,
2976 JII::AttachCurrentThread,
2977 JII::DetachCurrentThread,
2978 JII::GetEnv,
2979 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002980};
2981
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002982JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002983 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002984 check_jni_abort_hook(nullptr),
2985 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002986 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002987 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002988 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08002989 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002990 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002991 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002992 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002993 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002994 libraries(new Libraries),
2995 weak_globals_lock_("JNI weak global reference table lock"),
2996 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2997 allow_new_weak_globals_(true),
2998 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002999 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003000 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003001 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003002 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003003}
3004
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003005JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003006 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003007}
3008
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003009jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3010 if (obj == nullptr) {
3011 return nullptr;
3012 }
3013 MutexLock mu(self, weak_globals_lock_);
3014 while (UNLIKELY(!allow_new_weak_globals_)) {
3015 weak_globals_add_condition_.WaitHoldingLocks(self);
3016 }
3017 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3018 return reinterpret_cast<jweak>(ref);
3019}
3020
3021void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3022 MutexLock mu(self, weak_globals_lock_);
3023 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3024 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3025 << "failed to find entry";
3026 }
3027}
3028
Elliott Hughes88c5c352012-03-15 18:49:48 -07003029void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3030 check_jni = enabled;
3031 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003032}
3033
Elliott Hughesae80b492012-04-24 10:43:17 -07003034void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3035 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3036 if (force_copy) {
3037 os << " (with forcecopy)";
3038 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003039 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003040 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003041 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003042 os << "; pins=" << pin_table.Size();
3043 }
3044 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003045 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003046 os << "; globals=" << globals.Capacity();
3047 }
3048 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003049 MutexLock mu(self, weak_globals_lock_);
3050 if (weak_globals_.Capacity() > 0) {
3051 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003052 }
3053 }
3054 os << '\n';
3055
3056 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003057 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003058 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3059 }
3060}
3061
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003062void JavaVMExt::DisallowNewWeakGlobals() {
3063 MutexLock mu(Thread::Current(), weak_globals_lock_);
3064 allow_new_weak_globals_ = false;
3065}
3066
3067void JavaVMExt::AllowNewWeakGlobals() {
3068 Thread* self = Thread::Current();
3069 MutexLock mu(self, weak_globals_lock_);
3070 allow_new_weak_globals_ = true;
3071 weak_globals_add_condition_.Broadcast(self);
3072}
3073
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003074mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3075 MutexLock mu(self, weak_globals_lock_);
3076 while (UNLIKELY(!allow_new_weak_globals_)) {
3077 weak_globals_add_condition_.WaitHoldingLocks(self);
3078 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003079 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003080}
3081
Elliott Hughes73e66f72012-05-09 09:34:45 -07003082void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003083 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003084 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003085 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003086 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003087 }
3088 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003089 MutexLock mu(self, weak_globals_lock_);
3090 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003091 }
3092 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003093 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003094 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003095 }
3096}
3097
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003098bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003099 const Handle<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003100 std::string* detail) {
3101 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003102
3103 // See if we've already loaded this library. If we have, and the class loader
3104 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003105 // TODO: for better results we should canonicalize the pathname (or even compare
3106 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003107 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003108 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003109 {
3110 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003111 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003112 library = libraries->Get(path);
3113 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003114 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003115 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003116 // The library will be associated with class_loader. The JNI
3117 // spec says we can't load the same library into more than one
3118 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003119 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003120 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003121 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003122 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003123 return false;
3124 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003125 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003126 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003127 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003128 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003129 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003130 return false;
3131 }
3132 return true;
3133 }
3134
3135 // Open the shared library. Because we're using a full path, the system
3136 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3137 // resolve this library's dependencies though.)
3138
3139 // Failures here are expected when java.library.path has several entries
3140 // and we have to hunt for the lib.
3141
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003142 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3143 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3144 // dlopen) becomes zero from dlclose.
3145
Elliott Hughescdf53122011-08-19 15:46:09 -07003146 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003147 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003148 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003149 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003150 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003151
Elliott Hughes84b2f142012-09-27 09:16:28 -07003152 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003153
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003154 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003155 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003156 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003157 return false;
3158 }
3159
3160 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003161 // TODO: move the locking (and more of this logic) into Libraries.
3162 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003163 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003164 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003165 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003166 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003167 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003168 libraries->Put(path, library);
3169 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003170 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003171 }
3172 if (!created_library) {
3173 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003174 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003175 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003176 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003177
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003178 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003179 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003180
Elliott Hughes79353722013-08-02 16:52:18 -07003181 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003182 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003183 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003184 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003185 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003186 } else {
3187 // Call JNI_OnLoad. We have to override the current class
3188 // loader, which will always be "null" since the stuff at the
3189 // top of the stack is around Runtime.loadLibrary(). (See
3190 // the comments in the JNI FindClass function.)
3191 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3192 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003193 StackHandleScope<1> hs(self);
3194 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3195 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003196
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003197 int version = 0;
3198 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003199 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003200 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003201 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003202 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003203
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003204 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003205
Elliott Hughes79353722013-08-02 16:52:18 -07003206 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003207 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003208 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003209 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003210 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003211 // It's unwise to call dlclose() here, but we can mark it
3212 // as bad and ensure that future load attempts will fail.
3213 // We don't know how far JNI_OnLoad got, so there could
3214 // be some partially-initialized stuff accessible through
3215 // newly-registered native method calls. We could try to
3216 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003217 } else {
3218 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003219 }
Elliott Hughes79353722013-08-02 16:52:18 -07003220 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003221 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003222 }
3223
Elliott Hughes79353722013-08-02 16:52:18 -07003224 library->SetResult(was_successful);
3225 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003226}
3227
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003228void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003229 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003230 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003231 // If this is a static method, it could be called before the class
3232 // has been initialized.
3233 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003234 c = EnsureInitialized(Thread::Current(), c);
3235 if (c == nullptr) {
3236 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003237 }
3238 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003239 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003240 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003241 std::string detail;
3242 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003243 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003244 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003245 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003246 native_method = libraries->FindNativeMethod(m, detail);
3247 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003248 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003249 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003250 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3251 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003252 }
3253 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003254}
3255
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003256void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003257 MutexLock mu(Thread::Current(), weak_globals_lock_);
3258 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003259 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003260 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003261 if (new_obj == nullptr) {
3262 new_obj = kClearedJniWeakGlobal;
3263 }
3264 *entry = new_obj;
3265 }
3266}
3267
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003268void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003269 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003270 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003271 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003272 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003273 }
3274 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003275 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003276 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003277 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003278 {
3279 MutexLock mu(self, libraries_lock);
3280 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003281 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003282 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003283 // The weak_globals table is visited by the GC itself (because it mutates the table).
3284}
3285
Elliott Hughesc8fece32013-01-02 11:27:23 -08003286void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003287 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003288 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003289 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003290 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3291 }
3292 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3293}
3294
Ian Rogersdf20fe02011-07-20 20:34:16 -07003295} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003296
3297std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3298 switch (rhs) {
3299 case JNIInvalidRefType:
3300 os << "JNIInvalidRefType";
3301 return os;
3302 case JNILocalRefType:
3303 os << "JNILocalRefType";
3304 return os;
3305 case JNIGlobalRefType:
3306 os << "JNIGlobalRefType";
3307 return os;
3308 case JNIWeakGlobalRefType:
3309 os << "JNIWeakGlobalRefType";
3310 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003311 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003312 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003313 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003314 }
3315}