blob: 3afb149fa3c572353583199162920c867642afd2 [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"
Ian Rogers507dfdd2014-05-15 16:42:40 -070052#include "UniquePtrCompat.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
Ian Rogers2d10b202014-05-12 19:15:18 -0700521#define CHECK_NON_NULL_ARGUMENT(value) \
522 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700523
Ian Rogers2d10b202014-05-12 19:15:18 -0700524#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
525 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
526
527#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
528 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
529
530#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
531 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
532
533#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800534 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700535 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700536 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700537 }
538
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700539#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800540 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700541 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700542 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700543 }
544
Elliott Hughescdf53122011-08-19 15:46:09 -0700545class JNI {
546 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700547 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700548 return JNI_VERSION_1_6;
549 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700550
Ian Rogers25e8b912012-09-07 11:31:36 -0700551 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700552 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800553 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700554 }
555
Elliott Hughescdf53122011-08-19 15:46:09 -0700556 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700557 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700558 Runtime* runtime = Runtime::Current();
559 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700560 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700561 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800562 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700563 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700564 StackHandleScope<1> hs(soa.Self());
565 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800566 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700567 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800568 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700569 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700570 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700571 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700572
Ian Rogers62f05122014-03-21 11:21:29 -0700573 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700574 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700576 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700577 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700578
Ian Rogers62f05122014-03-21 11:21:29 -0700579 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700580 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700581 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700582 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700583 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700584
Elliott Hughescdf53122011-08-19 15:46:09 -0700585 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700586 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800588 mirror::ArtMethod* m = soa.DecodeMethod(mid);
589 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700590 jobject art_method = soa.AddLocalReference<jobject>(m);
591 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
592 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800593 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800595 SetObjectField(env, reflect_method,
596 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700597 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700598 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700599
Elliott Hughescdf53122011-08-19 15:46:09 -0700600 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700601 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800603 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700604 jobject art_field = soa.AddLocalReference<jobject>(f);
605 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
606 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800607 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700608 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800609 SetObjectField(env, reflect_field,
610 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700613
Elliott Hughes37f7a402011-08-22 18:56:01 -0700614 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700615 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700616 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800617 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700619 }
620
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700621 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700622 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800624 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700627
Elliott Hughes37f7a402011-08-22 18:56:01 -0700628 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700629 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
630 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
633 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700634 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughese84278b2012-03-22 10:06:53 -0700637 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700638 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800639 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700640 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700641 return JNI_TRUE;
642 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700643 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800644 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
645 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700646 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700647 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700649
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800652 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
653 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 return JNI_ERR;
655 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
657 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700658 return JNI_OK;
659 }
660
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700661 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700662 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 }
665
666 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700667 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 }
669
670 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700671 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700672 }
673
674 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700676
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700677 StackHandleScope<3> hs(soa.Self());
678 // TODO: Use nullptr instead of null handles?
679 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
680 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
681 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 uint32_t old_throw_dex_pc;
683 {
684 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800685 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700686 old_throw_this_object.Assign(old_throw_location.GetThis());
687 old_throw_method.Assign(old_throw_location.GetMethod());
688 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 old_throw_dex_pc = old_throw_location.GetDexPc();
690 soa.Self()->ClearException();
691 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800692 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700693 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700694 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
695 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800696 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700698 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700699 } else {
700 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800702 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700703 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800704 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 }
706 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700707 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700709
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700710 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700712
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800715 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 }
718
Ian Rogers25e8b912012-09-07 11:31:36 -0700719 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 LOG(FATAL) << "JNI FatalError called: " << msg;
721 }
722
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700723 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800724 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700725 return JNI_ERR;
726 }
Ian Rogersef28b142012-11-30 14:22:18 -0800727 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 return JNI_OK;
729 }
730
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700731 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800733 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 soa.Env()->PopFrame();
735 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 }
737
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700738 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800739 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 }
741
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700743 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800744 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800745 // Check for null after decoding the object to handle cleared weak globals.
746 if (decoded_obj == nullptr) {
747 return nullptr;
748 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700749 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700751 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 return reinterpret_cast<jobject>(ref);
754 }
755
756 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800757 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 return;
759 }
Ian Rogersef28b142012-11-30 14:22:18 -0800760 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800762 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700763 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700764
765 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
766 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
767 << "failed to find entry";
768 }
769 }
770
771 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800773 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 }
775
776 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700777 if (obj != nullptr) {
778 ScopedObjectAccess soa(env);
779 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 }
781 }
782
783 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700784 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800785 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800786 // Check for null after decoding the object to handle cleared weak globals.
787 if (decoded_obj == nullptr) {
788 return nullptr;
789 }
790 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 }
792
793 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800794 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 return;
796 }
Ian Rogersef28b142012-11-30 14:22:18 -0800797 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700798
Ian Rogersef28b142012-11-30 14:22:18 -0800799 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 if (!locals.Remove(cookie, obj)) {
801 // Attempting to delete a local reference that is not in the
802 // topmost local reference frame is a no-op. DeleteLocalRef returns
803 // void and doesn't throw any exceptions, but we should probably
804 // complain about it so the user will notice that things aren't
805 // going quite the way they expect.
806 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
807 << "failed to find entry";
808 }
809 }
810
811 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700812 if (obj1 == obj2) {
813 return JNI_TRUE;
814 } else {
815 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800816 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
817 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700818 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 }
820
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700821 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700822 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800824 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800825 if (c == nullptr) {
826 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700827 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700828 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Ian Rogersbc939662013-08-15 10:26:54 -0700831 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700834 CHECK_NON_NULL_ARGUMENT(java_class);
835 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700836 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 va_end(args);
838 return result;
839 }
840
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700842 CHECK_NON_NULL_ARGUMENT(java_class);
843 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800845 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800846 if (c == nullptr) {
847 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700848 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800849 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800850 if (result == nullptr) {
851 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700852 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700853 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700854 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800855 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800856 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700857 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800858 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700859 }
860
Elliott Hughes72025e52011-08-23 17:50:30 -0700861 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700862 CHECK_NON_NULL_ARGUMENT(java_class);
863 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800865 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800866 if (c == nullptr) {
867 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700868 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800869 mirror::Object* result = c->AllocObject(soa.Self());
870 if (result == nullptr) {
871 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700872 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800875 if (soa.Self()->IsExceptionPending()) {
876 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700877 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800878 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
Ian Rogersbc939662013-08-15 10:26:54 -0700881 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700882 CHECK_NON_NULL_ARGUMENT(java_class);
883 CHECK_NON_NULL_ARGUMENT(name);
884 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700885 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700886 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 }
888
Ian Rogersbc939662013-08-15 10:26:54 -0700889 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
890 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700891 CHECK_NON_NULL_ARGUMENT(java_class);
892 CHECK_NON_NULL_ARGUMENT(name);
893 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700895 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 }
897
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_list ap;
900 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700901 CHECK_NON_NULL_ARGUMENT(obj);
902 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700903 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700910 CHECK_NON_NULL_ARGUMENT(obj);
911 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 ScopedObjectAccess soa(env);
913 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
914 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700918 CHECK_NON_NULL_ARGUMENT(obj);
919 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700921 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
922 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 va_list ap;
928 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700929 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
930 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700931 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700934 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 }
936
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700938 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
939 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 ScopedObjectAccess soa(env);
941 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 }
943
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700945 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
946 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700948 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
949 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_list ap;
954 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700955 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
956 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700957 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700960 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700964 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
965 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 ScopedObjectAccess soa(env);
967 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700971 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
972 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700974 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
975 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_list ap;
980 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700981 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
982 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700983 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700986 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700990 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
991 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 ScopedObjectAccess soa(env);
993 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700997 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
998 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001000 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1001 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_list ap;
1006 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001007 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1008 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001009 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001012 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001016 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 ScopedObjectAccess soa(env);
1019 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1024 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001026 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1027 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_list ap;
1032 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001033 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1034 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001035 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001038 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001042 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1043 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 ScopedObjectAccess soa(env);
1045 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001049 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1050 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001052 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1053 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_list ap;
1058 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001059 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1060 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001061 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001064 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1069 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 ScopedObjectAccess soa(env);
1071 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001075 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1076 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001078 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1079 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 va_list ap;
1084 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001085 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1086 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001087 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001090 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001094 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 ScopedObjectAccess soa(env);
1097 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001101 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1102 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001104 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1105 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_list ap;
1110 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001111 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1112 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001113 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001116 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001120 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1121 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 ScopedObjectAccess soa(env);
1123 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001127 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1128 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001130 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1131 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_list ap;
1136 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001137 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1138 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001139 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001140 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001145 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1146 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 ScopedObjectAccess soa(env);
1148 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001152 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1153 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001155 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001158 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001161 CHECK_NON_NULL_ARGUMENT(obj);
1162 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001163 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001164 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1165 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 va_end(ap);
1167 return local_result;
1168 }
1169
Ian Rogersbc939662013-08-15 10:26:54 -07001170 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1171 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001172 CHECK_NON_NULL_ARGUMENT(obj);
1173 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 ScopedObjectAccess soa(env);
1175 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1176 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Ian Rogersbc939662013-08-15 10:26:54 -07001179 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1180 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001181 CHECK_NON_NULL_ARGUMENT(obj);
1182 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001184 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
Ian Rogersbc939662013-08-15 10:26:54 -07001188 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1189 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001192 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1193 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001194 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001197 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Ian Rogersbc939662013-08-15 10:26:54 -07001200 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1201 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001202 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1203 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204 ScopedObjectAccess soa(env);
1205 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Ian Rogersbc939662013-08-15 10:26:54 -07001208 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1209 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001210 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1211 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001213 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001216 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001219 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1220 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001221 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001222 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001224 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
Ian Rogersbc939662013-08-15 10:26:54 -07001227 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1228 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001229 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1230 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 ScopedObjectAccess soa(env);
1232 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
Ian Rogersbc939662013-08-15 10:26:54 -07001235 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1236 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001237 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1238 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001240 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001243 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001246 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1247 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001248 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001251 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Ian Rogersbc939662013-08-15 10:26:54 -07001254 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1255 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001256 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1257 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001258 ScopedObjectAccess soa(env);
1259 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Ian Rogersbc939662013-08-15 10:26:54 -07001262 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1263 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001264 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1265 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001267 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001270 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001273 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1274 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001275 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001278 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
Ian Rogersbc939662013-08-15 10:26:54 -07001281 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1282 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001283 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1284 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 ScopedObjectAccess soa(env);
1286 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Ian Rogersbc939662013-08-15 10:26:54 -07001289 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1290 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001291 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1292 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001294 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001297 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001300 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1301 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001302 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001305 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Ian Rogersbc939662013-08-15 10:26:54 -07001308 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1309 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001310 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1311 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(env);
1313 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Ian Rogersbc939662013-08-15 10:26:54 -07001316 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1317 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001318 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1319 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001321 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001327 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1328 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001329 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001332 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Ian Rogersbc939662013-08-15 10:26:54 -07001335 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1336 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001337 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1338 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env);
1340 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Ian Rogersbc939662013-08-15 10:26:54 -07001343 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1344 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001345 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1346 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001348 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001354 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1355 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001356 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001359 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Ian Rogersbc939662013-08-15 10:26:54 -07001362 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1363 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001364 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1365 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Ian Rogersbc939662013-08-15 10:26:54 -07001370 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1371 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001372 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1373 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001375 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001381 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1382 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001383 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001386 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Ian Rogersbc939662013-08-15 10:26:54 -07001389 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1390 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001391 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1392 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 ScopedObjectAccess soa(env);
1394 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Ian Rogersbc939662013-08-15 10:26:54 -07001397 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1398 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001399 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1400 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001401 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001402 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001408 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1409 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001410 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_end(ap);
1413 }
1414
Brian Carlstromea46f952013-07-30 01:26:50 -07001415 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1416 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001417 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1418 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001419 ScopedObjectAccess soa(env);
1420 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Ian Rogersbc939662013-08-15 10:26:54 -07001423 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1424 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001425 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1426 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001427 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001428 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Ian Rogersbc939662013-08-15 10:26:54 -07001431 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001432 CHECK_NON_NULL_ARGUMENT(java_class);
1433 CHECK_NON_NULL_ARGUMENT(name);
1434 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001436 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001438
Ian Rogersbc939662013-08-15 10:26:54 -07001439 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1440 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001441 CHECK_NON_NULL_ARGUMENT(java_class);
1442 CHECK_NON_NULL_ARGUMENT(name);
1443 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001444 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001445 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001449 CHECK_NON_NULL_ARGUMENT(obj);
1450 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001451 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001452 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1453 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001454 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001456
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001458 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001459 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001460 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001461 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001465 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1466 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001468 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1469 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1470 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001471 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001475 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001476 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001477 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1478 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001479 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001483 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1484 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001485 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001486 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1487 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001488 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001489
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001490#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001491 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001492 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001493 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001494 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001497 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1498 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001499 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001500 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1501 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001502 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001504#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001505 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001506 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001507 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001508 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001511 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001515 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001519 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001523 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001527 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001531 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001535 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001539 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001543 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001547 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001550 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001551 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001555 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001559 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001562 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001563 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 }
1565
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001566 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001567 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 }
1569
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001570 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001571 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 }
1573
1574 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001575 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 }
1577
1578 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001579 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 }
1581
1582 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001583 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 }
1585
1586 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001587 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 }
1589
1590 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001591 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 }
1593
1594 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001595 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 }
1597
1598 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001599 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 }
1601
1602 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001603 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 }
1605
1606 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001607 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 }
1609
1610 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001611 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 }
1613
1614 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001615 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 }
1617
1618 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001619 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001620 }
1621
1622 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001623 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001624 }
1625
1626 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001627 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001628 }
1629
1630 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001631 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001632 }
1633
1634 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001635 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001638 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001641 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001642 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001643 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_end(ap);
1646 return local_result;
1647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001650 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001651 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001652 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001653 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001656 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001657 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001658 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001659 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001660 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001663 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001666 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001667 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001668 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001670 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001673 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001674 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001676 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001680 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001682 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001688 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001689 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001690 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001692 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001696 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001698 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001702 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001704 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001710 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001711 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001712 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001714 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001718 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001719 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001720 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001723 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001724 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001725 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001726 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001732 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001733 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001734 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001736 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001740 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001742 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001746 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001748 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001754 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001755 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001756 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001758 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001762 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001764 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001768 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001769 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001775 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001776 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001777 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001778 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001780 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001784 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001786 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001790 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001792 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001797 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001798 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001799 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001800 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001802 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001806 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001812 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001814 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001819 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001820 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001821 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001822 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001824 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001828 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001830 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001836 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001841 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001842 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001843 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001844 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 va_end(ap);
1846 }
1847
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001848 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001849 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001851 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001854 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001855 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
Elliott Hughes814e4032011-08-23 12:07:56 -07001860 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001861 if (UNLIKELY(char_count < 0)) {
1862 JniAbortF("NewString", "char_count < 0: %d", char_count);
1863 return nullptr;
1864 }
1865 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1866 JniAbortF("NewString", "chars == null && char_count > 0");
1867 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001868 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001869 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001870 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
1874 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001875 if (utf == nullptr) {
1876 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001879 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
Elliott Hughes814e4032011-08-23 12:07:56 -07001883 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001884 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001885 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001886 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001887 }
1888
1889 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001890 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001892 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001893 }
1894
Ian Rogersbc939662013-08-15 10:26:54 -07001895 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1896 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001897 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001899 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001900 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001902 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001903 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001904 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1905 memcpy(buf, chars + start, length * sizeof(jchar));
1906 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001907 }
1908
Ian Rogersbc939662013-08-15 10:26:54 -07001909 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1910 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001911 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001913 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001915 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001917 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001918 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1919 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1920 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001921 }
1922
Elliott Hughes75770752011-08-24 17:52:38 -07001923 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001924 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001926 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1927 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001928 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001929 if (is_copy != nullptr) {
1930 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001931 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001932 int32_t char_count = s->GetLength();
1933 int32_t offset = s->GetOffset();
1934 jchar* bytes = new jchar[char_count + 1];
1935 for (int32_t i = 0; i < char_count; i++) {
1936 bytes[i] = chars->Get(i + offset);
1937 }
1938 bytes[char_count] = '\0';
1939 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001940 }
1941
Mathieu Chartier590fee92013-09-13 13:46:47 -07001942 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001943 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001944 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001945 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001946 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001950 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001954 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001958 if (java_string == nullptr) {
1959 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001960 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001961 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001962 *is_copy = JNI_TRUE;
1963 }
Ian Rogersef28b142012-11-30 14:22:18 -08001964 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001965 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001966 size_t byte_count = s->GetUtfLength();
1967 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001968 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001969 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1970 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1971 bytes[byte_count] = '\0';
1972 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001976 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001977 }
1978
Elliott Hughesbd935992011-08-22 11:59:34 -07001979 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001980 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001982 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001983 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001984 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1985 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001986 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001987 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001991 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001992 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001993 mirror::ObjectArray<mirror::Object>* array =
1994 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Ian Rogersbc939662013-08-15 10:26:54 -07001998 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1999 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002000 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002002 mirror::ObjectArray<mirror::Object>* array =
2003 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2004 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002005 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
2008 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002009 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
2012 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002013 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
2016 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002017 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
2020 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002021 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
2024 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002025 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
2028 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002029 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
2032 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002033 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Ian Rogers1d99e452014-01-02 17:36:41 -08002036 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2037 jobject initial_element) {
2038 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002039 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002040 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002041 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002042 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043
2044 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002045 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002046 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002047 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002048 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002049 if (UNLIKELY(element_class->IsPrimitive())) {
2050 JniAbortF("NewObjectArray", "not an object type: %s",
2051 PrettyDescriptor(element_class).c_str());
2052 return nullptr;
2053 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002054 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002055 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002056 if (UNLIKELY(array_class == nullptr)) {
2057 return nullptr;
2058 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes75770752011-08-24 17:52:38 -07002061 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002062 mirror::ObjectArray<mirror::Object>* result =
2063 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002064 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002065 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002066 if (initial_object != nullptr) {
2067 mirror::Class* element_class = result->GetClass()->GetComponentType();
2068 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2069 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2070 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2071 PrettyDescriptor(element_class).c_str());
2072
2073 } else {
2074 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002075 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002076 }
2077 }
Elliott Hughes75770752011-08-24 17:52:38 -07002078 }
2079 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002080 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
2083 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002084 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Ian Rogersa15e67d2012-02-28 13:51:55 -08002087 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002088 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002089 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002090 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002091 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2092 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2093 PrettyDescriptor(array->GetClass()).c_str());
2094 return nullptr;
2095 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002096 gc::Heap* heap = Runtime::Current()->GetHeap();
2097 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002098 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002099 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002100 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002101 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002103 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002104 *is_copy = JNI_FALSE;
2105 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002106 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002107 }
2108
Ian Rogers2d10b202014-05-12 19:15:18 -07002109 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2110 jint mode) {
2111 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2112 ScopedObjectAccess soa(env);
2113 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2114 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2115 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2116 PrettyDescriptor(array->GetClass()).c_str());
2117 return;
2118 }
2119 const size_t component_size = array->GetClass()->GetComponentSize();
2120 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002121 }
2122
Elliott Hughes75770752011-08-24 17:52:38 -07002123 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002124 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes75770752011-08-24 17:52:38 -07002127 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002128 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes75770752011-08-24 17:52:38 -07002131 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002132 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Elliott Hughes75770752011-08-24 17:52:38 -07002135 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002136 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes75770752011-08-24 17:52:38 -07002139 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002140 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes75770752011-08-24 17:52:38 -07002143 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002144 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes75770752011-08-24 17:52:38 -07002147 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002148 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Elliott Hughes75770752011-08-24 17:52:38 -07002151 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002152 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Mathieu Chartier590fee92013-09-13 13:46:47 -07002155 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2156 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002157 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2158 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Mathieu Chartier590fee92013-09-13 13:46:47 -07002161 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002162 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Mathieu Chartier590fee92013-09-13 13:46:47 -07002165 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002166 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Mathieu Chartier590fee92013-09-13 13:46:47 -07002169 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2170 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002171 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 }
2173
Mathieu Chartier590fee92013-09-13 13:46:47 -07002174 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2175 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002176 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Mathieu Chartier590fee92013-09-13 13:46:47 -07002179 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002180 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Mathieu Chartier590fee92013-09-13 13:46:47 -07002183 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002184 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Mathieu Chartier590fee92013-09-13 13:46:47 -07002187 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2188 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002189 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Ian Rogersbc939662013-08-15 10:26:54 -07002192 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2193 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002194 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002195 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Ian Rogersbc939662013-08-15 10:26:54 -07002198 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2199 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002200 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Ian Rogersbc939662013-08-15 10:26:54 -07002203 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2204 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002205 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Ian Rogersbc939662013-08-15 10:26:54 -07002208 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2209 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002210 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002211 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Ian Rogersbc939662013-08-15 10:26:54 -07002214 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2215 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002216 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002217 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Ian Rogersbc939662013-08-15 10:26:54 -07002220 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2221 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002222 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Ian Rogersbc939662013-08-15 10:26:54 -07002225 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2226 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002227 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Ian Rogersbc939662013-08-15 10:26:54 -07002230 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2231 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002232 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002233 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Ian Rogersbc939662013-08-15 10:26:54 -07002236 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2237 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002238 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002239 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Ian Rogersbc939662013-08-15 10:26:54 -07002242 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2243 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002244 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Ian Rogersbc939662013-08-15 10:26:54 -07002247 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2248 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002249 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Ian Rogersbc939662013-08-15 10:26:54 -07002252 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2253 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002254 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002255 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2259 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002260 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002261 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Ian Rogersbc939662013-08-15 10:26:54 -07002264 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2265 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002266 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Ian Rogersbc939662013-08-15 10:26:54 -07002269 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2270 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002271 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Ian Rogersbc939662013-08-15 10:26:54 -07002274 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2275 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002276 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002277 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Ian Rogersbc939662013-08-15 10:26:54 -07002280 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2281 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002282 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2283 }
2284
Ian Rogersbc939662013-08-15 10:26:54 -07002285 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2286 jint method_count, bool return_errors) {
2287 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002288 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002289 return JNI_ERR; // Not reached.
2290 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002291 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002292 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002293 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002294 if (UNLIKELY(method_count == 0)) {
2295 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2296 << PrettyDescriptor(c);
2297 return JNI_OK;
2298 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002299 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002300 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002301 const char* name = methods[i].name;
2302 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002303 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002305 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 ++sig;
2307 }
2308
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002309 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2310 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002311 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002313 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002314 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002315 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002316 << PrettyDescriptor(c) << "." << name << sig << " in "
2317 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002318 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002320 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002321 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002322 << PrettyDescriptor(c) << "." << name << sig
2323 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 return JNI_ERR;
2326 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002327
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002328 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002329
Ian Rogers1eb512d2013-10-18 15:42:20 -07002330 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 }
2332 return JNI_OK;
2333 }
2334
Elliott Hughes5174fe62011-08-23 15:12:35 -07002335 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002336 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002337 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002338 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002339
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002340 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002341
Ian Rogers2d10b202014-05-12 19:15:18 -07002342 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002343 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002344 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002345 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002346 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002347 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002348 }
2349 }
2350 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002351 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002352 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002353 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002355 }
2356 }
2357
Ian Rogers2d10b202014-05-12 19:15:18 -07002358 if (unregistered_count == 0) {
2359 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2360 << PrettyDescriptor(c) << "' that contains no native methods";
2361 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002362 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002363 }
2364
Ian Rogers719d1a32014-03-06 12:13:39 -08002365 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002366 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002367 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002368 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2369 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002370 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002371 return JNI_ERR;
2372 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002373 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002374 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002375 }
2376
Ian Rogers719d1a32014-03-06 12:13:39 -08002377 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002378 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002379 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002380 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002381 o->MonitorExit(soa.Self());
2382 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002383 return JNI_ERR;
2384 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002385 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002386 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002387 }
2388
2389 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002390 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002391 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002392 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002393 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002394 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002395 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002396 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002397 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002398 }
2399
Elliott Hughescdf53122011-08-19 15:46:09 -07002400 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002401 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002402 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002403 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002404 if (address == nullptr && capacity != 0) {
2405 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002406 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002407
Ian Rogers936b37f2014-02-14 00:52:24 -08002408 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002409 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002410 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002411 jint capacity_arg = static_cast<jint>(capacity);
2412
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002413 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2414 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002415 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002416 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002417 }
2418
Elliott Hughesb465ab02011-08-24 11:21:21 -07002419 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002420 return reinterpret_cast<void*>(env->GetLongField(
2421 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 }
2423
Elliott Hughesb465ab02011-08-24 11:21:21 -07002424 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002425 return static_cast<jlong>(env->GetIntField(
2426 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002427 }
2428
Elliott Hughesb465ab02011-08-24 11:21:21 -07002429 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002430 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002431
2432 // Do we definitely know what kind of reference this is?
2433 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2434 IndirectRefKind kind = GetIndirectRefKind(ref);
2435 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002436 case kLocal: {
2437 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002438 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002439 return JNILocalRefType;
2440 }
2441 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002442 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002443 case kGlobal:
2444 return JNIGlobalRefType;
2445 case kWeakGlobal:
2446 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002447 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002448 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002449 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002450 return JNILocalRefType;
2451 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002452 return JNIInvalidRefType;
2453 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002454 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2455 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002456 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002457
2458 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002459 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2460 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002461 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002462 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002463 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2464 return JNI_ERR;
2465 }
2466 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002467 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002468 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2469 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002470 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002471 soa.Self()->ThrowOutOfMemoryError(caller);
2472 }
2473 return okay ? JNI_OK : JNI_ERR;
2474 }
2475
2476 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002477 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002478 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002479 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002480 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002481 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002482 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002483 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002484 return soa.AddLocalReference<JniT>(result);
2485 }
2486
Ian Rogers2d10b202014-05-12 19:15:18 -07002487 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2488 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2489 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002490 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002491 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002492 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2493 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2494 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2495 PrettyDescriptor(array->GetClass()).c_str());
2496 return nullptr;
2497 }
2498 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2499 return array;
2500 }
2501
2502 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2503 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2504 CHECK_NON_NULL_ARGUMENT(java_array);
2505 ScopedObjectAccess soa(env);
2506 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2507 "GetArrayElements",
2508 "get");
2509 if (UNLIKELY(array == nullptr)) {
2510 return nullptr;
2511 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002512 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002513 // Only make a copy if necessary.
2514 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2515 if (is_copy != nullptr) {
2516 *is_copy = JNI_TRUE;
2517 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002518 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002519 size_t size = array->GetLength() * component_size;
2520 void* data = new uint64_t[RoundUp(size, 8) / 8];
2521 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002522 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002523 } else {
2524 if (is_copy != nullptr) {
2525 *is_copy = JNI_FALSE;
2526 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002527 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002528 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002529 }
2530
Ian Rogers2d10b202014-05-12 19:15:18 -07002531 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002532 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002533 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002534 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002535 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2536 "ReleaseArrayElements",
2537 "release");
2538 if (array == nullptr) {
2539 return;
2540 }
2541 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2542 }
2543
2544 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2545 size_t component_size, void* elements, jint mode)
2546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002547 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002548 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002549 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002550 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002551 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2552 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002553 if (is_copy) {
2554 // Sanity check: If elements is not the same as the java array's data, it better not be a
2555 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2556 // copies we make?
2557 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2558 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2559 reinterpret_cast<void*>(elements), array_data);
2560 return;
2561 }
2562 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002563 // Don't need to copy if we had a direct pointer.
2564 if (mode != JNI_ABORT && is_copy) {
2565 memcpy(array_data, elements, bytes);
2566 }
2567 if (mode != JNI_COMMIT) {
2568 if (is_copy) {
2569 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002570 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002571 // Non copy to a movable object must means that we had disabled the moving GC.
2572 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002573 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002574 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002575 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576 }
2577
Ian Rogers2d10b202014-05-12 19:15:18 -07002578 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2579 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2580 jsize start, jsize length, ElementT* buf) {
2581 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2582 ScopedObjectAccess soa(env);
2583 ArtArrayT* array =
2584 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2585 "GetPrimitiveArrayRegion",
2586 "get region of");
2587 if (array != nullptr) {
2588 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2589 ThrowAIOOBE(soa, array, start, length, "src");
2590 } else {
2591 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2592 ElementT* data = array->GetData();
2593 memcpy(buf, data + start, length * sizeof(ElementT));
2594 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002595 }
2596 }
2597
Ian Rogers2d10b202014-05-12 19:15:18 -07002598 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2599 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2600 jsize start, jsize length, const ElementT* buf) {
2601 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2602 ScopedObjectAccess soa(env);
2603 ArtArrayT* array =
2604 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2605 "SetPrimitiveArrayRegion",
2606 "set region of");
2607 if (array != nullptr) {
2608 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2609 ThrowAIOOBE(soa, array, start, length, "dst");
2610 } else {
2611 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2612 ElementT* data = array->GetData();
2613 memcpy(data + start, buf, length * sizeof(ElementT));
2614 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002615 }
2616 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002617};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002618
Elliott Hughes88c5c352012-03-15 18:49:48 -07002619const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002620 nullptr, // reserved0.
2621 nullptr, // reserved1.
2622 nullptr, // reserved2.
2623 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002624 JNI::GetVersion,
2625 JNI::DefineClass,
2626 JNI::FindClass,
2627 JNI::FromReflectedMethod,
2628 JNI::FromReflectedField,
2629 JNI::ToReflectedMethod,
2630 JNI::GetSuperclass,
2631 JNI::IsAssignableFrom,
2632 JNI::ToReflectedField,
2633 JNI::Throw,
2634 JNI::ThrowNew,
2635 JNI::ExceptionOccurred,
2636 JNI::ExceptionDescribe,
2637 JNI::ExceptionClear,
2638 JNI::FatalError,
2639 JNI::PushLocalFrame,
2640 JNI::PopLocalFrame,
2641 JNI::NewGlobalRef,
2642 JNI::DeleteGlobalRef,
2643 JNI::DeleteLocalRef,
2644 JNI::IsSameObject,
2645 JNI::NewLocalRef,
2646 JNI::EnsureLocalCapacity,
2647 JNI::AllocObject,
2648 JNI::NewObject,
2649 JNI::NewObjectV,
2650 JNI::NewObjectA,
2651 JNI::GetObjectClass,
2652 JNI::IsInstanceOf,
2653 JNI::GetMethodID,
2654 JNI::CallObjectMethod,
2655 JNI::CallObjectMethodV,
2656 JNI::CallObjectMethodA,
2657 JNI::CallBooleanMethod,
2658 JNI::CallBooleanMethodV,
2659 JNI::CallBooleanMethodA,
2660 JNI::CallByteMethod,
2661 JNI::CallByteMethodV,
2662 JNI::CallByteMethodA,
2663 JNI::CallCharMethod,
2664 JNI::CallCharMethodV,
2665 JNI::CallCharMethodA,
2666 JNI::CallShortMethod,
2667 JNI::CallShortMethodV,
2668 JNI::CallShortMethodA,
2669 JNI::CallIntMethod,
2670 JNI::CallIntMethodV,
2671 JNI::CallIntMethodA,
2672 JNI::CallLongMethod,
2673 JNI::CallLongMethodV,
2674 JNI::CallLongMethodA,
2675 JNI::CallFloatMethod,
2676 JNI::CallFloatMethodV,
2677 JNI::CallFloatMethodA,
2678 JNI::CallDoubleMethod,
2679 JNI::CallDoubleMethodV,
2680 JNI::CallDoubleMethodA,
2681 JNI::CallVoidMethod,
2682 JNI::CallVoidMethodV,
2683 JNI::CallVoidMethodA,
2684 JNI::CallNonvirtualObjectMethod,
2685 JNI::CallNonvirtualObjectMethodV,
2686 JNI::CallNonvirtualObjectMethodA,
2687 JNI::CallNonvirtualBooleanMethod,
2688 JNI::CallNonvirtualBooleanMethodV,
2689 JNI::CallNonvirtualBooleanMethodA,
2690 JNI::CallNonvirtualByteMethod,
2691 JNI::CallNonvirtualByteMethodV,
2692 JNI::CallNonvirtualByteMethodA,
2693 JNI::CallNonvirtualCharMethod,
2694 JNI::CallNonvirtualCharMethodV,
2695 JNI::CallNonvirtualCharMethodA,
2696 JNI::CallNonvirtualShortMethod,
2697 JNI::CallNonvirtualShortMethodV,
2698 JNI::CallNonvirtualShortMethodA,
2699 JNI::CallNonvirtualIntMethod,
2700 JNI::CallNonvirtualIntMethodV,
2701 JNI::CallNonvirtualIntMethodA,
2702 JNI::CallNonvirtualLongMethod,
2703 JNI::CallNonvirtualLongMethodV,
2704 JNI::CallNonvirtualLongMethodA,
2705 JNI::CallNonvirtualFloatMethod,
2706 JNI::CallNonvirtualFloatMethodV,
2707 JNI::CallNonvirtualFloatMethodA,
2708 JNI::CallNonvirtualDoubleMethod,
2709 JNI::CallNonvirtualDoubleMethodV,
2710 JNI::CallNonvirtualDoubleMethodA,
2711 JNI::CallNonvirtualVoidMethod,
2712 JNI::CallNonvirtualVoidMethodV,
2713 JNI::CallNonvirtualVoidMethodA,
2714 JNI::GetFieldID,
2715 JNI::GetObjectField,
2716 JNI::GetBooleanField,
2717 JNI::GetByteField,
2718 JNI::GetCharField,
2719 JNI::GetShortField,
2720 JNI::GetIntField,
2721 JNI::GetLongField,
2722 JNI::GetFloatField,
2723 JNI::GetDoubleField,
2724 JNI::SetObjectField,
2725 JNI::SetBooleanField,
2726 JNI::SetByteField,
2727 JNI::SetCharField,
2728 JNI::SetShortField,
2729 JNI::SetIntField,
2730 JNI::SetLongField,
2731 JNI::SetFloatField,
2732 JNI::SetDoubleField,
2733 JNI::GetStaticMethodID,
2734 JNI::CallStaticObjectMethod,
2735 JNI::CallStaticObjectMethodV,
2736 JNI::CallStaticObjectMethodA,
2737 JNI::CallStaticBooleanMethod,
2738 JNI::CallStaticBooleanMethodV,
2739 JNI::CallStaticBooleanMethodA,
2740 JNI::CallStaticByteMethod,
2741 JNI::CallStaticByteMethodV,
2742 JNI::CallStaticByteMethodA,
2743 JNI::CallStaticCharMethod,
2744 JNI::CallStaticCharMethodV,
2745 JNI::CallStaticCharMethodA,
2746 JNI::CallStaticShortMethod,
2747 JNI::CallStaticShortMethodV,
2748 JNI::CallStaticShortMethodA,
2749 JNI::CallStaticIntMethod,
2750 JNI::CallStaticIntMethodV,
2751 JNI::CallStaticIntMethodA,
2752 JNI::CallStaticLongMethod,
2753 JNI::CallStaticLongMethodV,
2754 JNI::CallStaticLongMethodA,
2755 JNI::CallStaticFloatMethod,
2756 JNI::CallStaticFloatMethodV,
2757 JNI::CallStaticFloatMethodA,
2758 JNI::CallStaticDoubleMethod,
2759 JNI::CallStaticDoubleMethodV,
2760 JNI::CallStaticDoubleMethodA,
2761 JNI::CallStaticVoidMethod,
2762 JNI::CallStaticVoidMethodV,
2763 JNI::CallStaticVoidMethodA,
2764 JNI::GetStaticFieldID,
2765 JNI::GetStaticObjectField,
2766 JNI::GetStaticBooleanField,
2767 JNI::GetStaticByteField,
2768 JNI::GetStaticCharField,
2769 JNI::GetStaticShortField,
2770 JNI::GetStaticIntField,
2771 JNI::GetStaticLongField,
2772 JNI::GetStaticFloatField,
2773 JNI::GetStaticDoubleField,
2774 JNI::SetStaticObjectField,
2775 JNI::SetStaticBooleanField,
2776 JNI::SetStaticByteField,
2777 JNI::SetStaticCharField,
2778 JNI::SetStaticShortField,
2779 JNI::SetStaticIntField,
2780 JNI::SetStaticLongField,
2781 JNI::SetStaticFloatField,
2782 JNI::SetStaticDoubleField,
2783 JNI::NewString,
2784 JNI::GetStringLength,
2785 JNI::GetStringChars,
2786 JNI::ReleaseStringChars,
2787 JNI::NewStringUTF,
2788 JNI::GetStringUTFLength,
2789 JNI::GetStringUTFChars,
2790 JNI::ReleaseStringUTFChars,
2791 JNI::GetArrayLength,
2792 JNI::NewObjectArray,
2793 JNI::GetObjectArrayElement,
2794 JNI::SetObjectArrayElement,
2795 JNI::NewBooleanArray,
2796 JNI::NewByteArray,
2797 JNI::NewCharArray,
2798 JNI::NewShortArray,
2799 JNI::NewIntArray,
2800 JNI::NewLongArray,
2801 JNI::NewFloatArray,
2802 JNI::NewDoubleArray,
2803 JNI::GetBooleanArrayElements,
2804 JNI::GetByteArrayElements,
2805 JNI::GetCharArrayElements,
2806 JNI::GetShortArrayElements,
2807 JNI::GetIntArrayElements,
2808 JNI::GetLongArrayElements,
2809 JNI::GetFloatArrayElements,
2810 JNI::GetDoubleArrayElements,
2811 JNI::ReleaseBooleanArrayElements,
2812 JNI::ReleaseByteArrayElements,
2813 JNI::ReleaseCharArrayElements,
2814 JNI::ReleaseShortArrayElements,
2815 JNI::ReleaseIntArrayElements,
2816 JNI::ReleaseLongArrayElements,
2817 JNI::ReleaseFloatArrayElements,
2818 JNI::ReleaseDoubleArrayElements,
2819 JNI::GetBooleanArrayRegion,
2820 JNI::GetByteArrayRegion,
2821 JNI::GetCharArrayRegion,
2822 JNI::GetShortArrayRegion,
2823 JNI::GetIntArrayRegion,
2824 JNI::GetLongArrayRegion,
2825 JNI::GetFloatArrayRegion,
2826 JNI::GetDoubleArrayRegion,
2827 JNI::SetBooleanArrayRegion,
2828 JNI::SetByteArrayRegion,
2829 JNI::SetCharArrayRegion,
2830 JNI::SetShortArrayRegion,
2831 JNI::SetIntArrayRegion,
2832 JNI::SetLongArrayRegion,
2833 JNI::SetFloatArrayRegion,
2834 JNI::SetDoubleArrayRegion,
2835 JNI::RegisterNatives,
2836 JNI::UnregisterNatives,
2837 JNI::MonitorEnter,
2838 JNI::MonitorExit,
2839 JNI::GetJavaVM,
2840 JNI::GetStringRegion,
2841 JNI::GetStringUTFRegion,
2842 JNI::GetPrimitiveArrayCritical,
2843 JNI::ReleasePrimitiveArrayCritical,
2844 JNI::GetStringCritical,
2845 JNI::ReleaseStringCritical,
2846 JNI::NewWeakGlobalRef,
2847 JNI::DeleteWeakGlobalRef,
2848 JNI::ExceptionCheck,
2849 JNI::NewDirectByteBuffer,
2850 JNI::GetDirectBufferAddress,
2851 JNI::GetDirectBufferCapacity,
2852 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002853};
2854
Elliott Hughes75770752011-08-24 17:52:38 -07002855JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002856 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002857 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002858 local_ref_cookie(IRT_FIRST_SEGMENT),
2859 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002860 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002861 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002862 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002863 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002864 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002865 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002866 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002867}
2868
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002869JNIEnvExt::~JNIEnvExt() {
2870}
2871
Mathieu Chartier590fee92013-09-13 13:46:47 -07002872jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2873 if (obj == nullptr) {
2874 return nullptr;
2875 }
2876 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2877}
2878
2879void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2880 if (obj != nullptr) {
2881 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2882 }
2883}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002884void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2885 check_jni = enabled;
2886 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002887}
2888
Elliott Hughes73e66f72012-05-09 09:34:45 -07002889void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2890 locals.Dump(os);
2891 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002892}
2893
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002894void JNIEnvExt::PushFrame(int /*capacity*/) {
2895 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002896 stacked_local_ref_cookies.push_back(local_ref_cookie);
2897 local_ref_cookie = locals.GetSegmentState();
2898}
2899
2900void JNIEnvExt::PopFrame() {
2901 locals.SetSegmentState(local_ref_cookie);
2902 local_ref_cookie = stacked_local_ref_cookies.back();
2903 stacked_local_ref_cookies.pop_back();
2904}
2905
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002906Offset JNIEnvExt::SegmentStateOffset() {
2907 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2908 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2909}
2910
Carl Shapiroea4dca82011-08-01 13:45:38 -07002911// JNI Invocation interface.
2912
Brian Carlstrombddf9762013-05-14 11:35:37 -07002913extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002914 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002915 if (IsBadJniVersion(args->version)) {
2916 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002917 return JNI_EVERSION;
2918 }
2919 Runtime::Options options;
2920 for (int i = 0; i < args->nOptions; ++i) {
2921 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002922 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002923 }
2924 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002925 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002926 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002927 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002928 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002929 bool started = runtime->Start();
2930 if (!started) {
2931 delete Thread::Current()->GetJniEnv();
2932 delete runtime->GetJavaVM();
2933 LOG(WARNING) << "CreateJavaVM failed";
2934 return JNI_ERR;
2935 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002936 *p_env = Thread::Current()->GetJniEnv();
2937 *p_vm = runtime->GetJavaVM();
2938 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002939}
2940
Elliott Hughesf2682d52011-08-15 16:37:04 -07002941extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002942 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002943 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002944 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002945 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002946 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002947 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002948 }
2949 return JNI_OK;
2950}
2951
2952// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002953extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002954 return JNI_ERR;
2955}
2956
Elliott Hughescdf53122011-08-19 15:46:09 -07002957class JII {
2958 public:
2959 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002960 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002961 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002962 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002963 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2964 delete raw_vm->runtime;
2965 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002966 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002967
Elliott Hughescdf53122011-08-19 15:46:09 -07002968 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002969 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002970 }
2971
2972 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002973 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002974 }
2975
2976 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002977 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002978 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002979 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002980 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2981 Runtime* runtime = raw_vm->runtime;
2982 runtime->DetachCurrentThread();
2983 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002984 }
2985
2986 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002987 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2988 // and unlike other calls that take a JNI version doesn't care if you supply
2989 // JNI_VERSION_1_1, which we don't otherwise support.
2990 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002991 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002992 return JNI_EVERSION;
2993 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002994 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002995 return JNI_ERR;
2996 }
2997 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002998 if (thread == nullptr) {
2999 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003000 return JNI_EDETACHED;
3001 }
3002 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003003 return JNI_OK;
3004 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003005};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003006
Elliott Hughes88c5c352012-03-15 18:49:48 -07003007const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003008 nullptr, // reserved0
3009 nullptr, // reserved1
3010 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003011 JII::DestroyJavaVM,
3012 JII::AttachCurrentThread,
3013 JII::DetachCurrentThread,
3014 JII::GetEnv,
3015 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003016};
3017
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003018JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003019 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003020 check_jni_abort_hook(nullptr),
3021 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003022 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003023 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003024 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003025 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003026 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003027 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003028 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003029 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003030 libraries(new Libraries),
3031 weak_globals_lock_("JNI weak global reference table lock"),
3032 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3033 allow_new_weak_globals_(true),
3034 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003035 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003036 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003037 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003038 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003039}
3040
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003041JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003042 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003043}
3044
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003045jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3046 if (obj == nullptr) {
3047 return nullptr;
3048 }
3049 MutexLock mu(self, weak_globals_lock_);
3050 while (UNLIKELY(!allow_new_weak_globals_)) {
3051 weak_globals_add_condition_.WaitHoldingLocks(self);
3052 }
3053 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3054 return reinterpret_cast<jweak>(ref);
3055}
3056
3057void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3058 MutexLock mu(self, weak_globals_lock_);
3059 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3060 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3061 << "failed to find entry";
3062 }
3063}
3064
Elliott Hughes88c5c352012-03-15 18:49:48 -07003065void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3066 check_jni = enabled;
3067 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003068}
3069
Elliott Hughesae80b492012-04-24 10:43:17 -07003070void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3071 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3072 if (force_copy) {
3073 os << " (with forcecopy)";
3074 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003075 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003076 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003077 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003078 os << "; pins=" << pin_table.Size();
3079 }
3080 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003081 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003082 os << "; globals=" << globals.Capacity();
3083 }
3084 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003085 MutexLock mu(self, weak_globals_lock_);
3086 if (weak_globals_.Capacity() > 0) {
3087 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003088 }
3089 }
3090 os << '\n';
3091
3092 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003093 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003094 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3095 }
3096}
3097
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003098void JavaVMExt::DisallowNewWeakGlobals() {
3099 MutexLock mu(Thread::Current(), weak_globals_lock_);
3100 allow_new_weak_globals_ = false;
3101}
3102
3103void JavaVMExt::AllowNewWeakGlobals() {
3104 Thread* self = Thread::Current();
3105 MutexLock mu(self, weak_globals_lock_);
3106 allow_new_weak_globals_ = true;
3107 weak_globals_add_condition_.Broadcast(self);
3108}
3109
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003110mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3111 MutexLock mu(self, weak_globals_lock_);
3112 while (UNLIKELY(!allow_new_weak_globals_)) {
3113 weak_globals_add_condition_.WaitHoldingLocks(self);
3114 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003115 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003116}
3117
Elliott Hughes73e66f72012-05-09 09:34:45 -07003118void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003119 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003120 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003121 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003122 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003123 }
3124 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003125 MutexLock mu(self, weak_globals_lock_);
3126 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003127 }
3128 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003129 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003130 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003131 }
3132}
3133
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003134bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003135 const Handle<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003136 std::string* detail) {
3137 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003138
3139 // See if we've already loaded this library. If we have, and the class loader
3140 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003141 // TODO: for better results we should canonicalize the pathname (or even compare
3142 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003143 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003144 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003145 {
3146 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003147 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003148 library = libraries->Get(path);
3149 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003150 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003151 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003152 // The library will be associated with class_loader. The JNI
3153 // spec says we can't load the same library into more than one
3154 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003155 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003156 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003157 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003158 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003159 return false;
3160 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003161 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003162 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003163 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003164 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003165 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003166 return false;
3167 }
3168 return true;
3169 }
3170
3171 // Open the shared library. Because we're using a full path, the system
3172 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3173 // resolve this library's dependencies though.)
3174
3175 // Failures here are expected when java.library.path has several entries
3176 // and we have to hunt for the lib.
3177
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003178 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3179 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3180 // dlopen) becomes zero from dlclose.
3181
Elliott Hughescdf53122011-08-19 15:46:09 -07003182 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003183 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003184 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003185 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003186 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003187
Elliott Hughes84b2f142012-09-27 09:16:28 -07003188 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003189
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003190 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003191 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003192 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003193 return false;
3194 }
3195
3196 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003197 // TODO: move the locking (and more of this logic) into Libraries.
3198 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003199 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003200 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003201 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003202 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003203 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003204 libraries->Put(path, library);
3205 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003206 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003207 }
3208 if (!created_library) {
3209 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003210 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003211 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003212 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003213
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003214 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003215 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003216
Elliott Hughes79353722013-08-02 16:52:18 -07003217 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003218 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003219 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003220 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003221 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003222 } else {
3223 // Call JNI_OnLoad. We have to override the current class
3224 // loader, which will always be "null" since the stuff at the
3225 // top of the stack is around Runtime.loadLibrary(). (See
3226 // the comments in the JNI FindClass function.)
3227 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3228 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003229 StackHandleScope<1> hs(self);
3230 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3231 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003232
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003233 int version = 0;
3234 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003235 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003236 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003237 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003238 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003239
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003240 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003241
Elliott Hughes79353722013-08-02 16:52:18 -07003242 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003243 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003244 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003245 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003246 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003247 // It's unwise to call dlclose() here, but we can mark it
3248 // as bad and ensure that future load attempts will fail.
3249 // We don't know how far JNI_OnLoad got, so there could
3250 // be some partially-initialized stuff accessible through
3251 // newly-registered native method calls. We could try to
3252 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003253 } else {
3254 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003255 }
Elliott Hughes79353722013-08-02 16:52:18 -07003256 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003257 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003258 }
3259
Elliott Hughes79353722013-08-02 16:52:18 -07003260 library->SetResult(was_successful);
3261 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003262}
3263
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003264void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003265 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003266 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003267 // If this is a static method, it could be called before the class
3268 // has been initialized.
3269 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003270 c = EnsureInitialized(Thread::Current(), c);
3271 if (c == nullptr) {
3272 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003273 }
3274 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003275 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003276 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003277 std::string detail;
3278 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003279 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003280 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003281 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003282 native_method = libraries->FindNativeMethod(m, detail);
3283 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003284 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003285 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003286 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3287 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003288 }
3289 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003290}
3291
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003292void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003293 MutexLock mu(Thread::Current(), weak_globals_lock_);
3294 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003295 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003296 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003297 if (new_obj == nullptr) {
3298 new_obj = kClearedJniWeakGlobal;
3299 }
3300 *entry = new_obj;
3301 }
3302}
3303
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003304void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003305 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003306 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003307 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003308 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003309 }
3310 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003311 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003312 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003313 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003314 {
3315 MutexLock mu(self, libraries_lock);
3316 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003317 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003318 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003319 // The weak_globals table is visited by the GC itself (because it mutates the table).
3320}
3321
Elliott Hughesc8fece32013-01-02 11:27:23 -08003322void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003323 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003324 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003325 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003326 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3327 }
3328 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3329}
3330
Ian Rogersdf20fe02011-07-20 20:34:16 -07003331} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003332
3333std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3334 switch (rhs) {
3335 case JNIInvalidRefType:
3336 os << "JNIInvalidRefType";
3337 return os;
3338 case JNILocalRefType:
3339 os << "JNILocalRefType";
3340 return os;
3341 case JNIGlobalRefType:
3342 os << "JNIGlobalRefType";
3343 return os;
3344 case JNIWeakGlobalRefType:
3345 os << "JNIWeakGlobalRefType";
3346 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003347 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003348 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003349 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003350 }
3351}