blob: 0624281c202c7eb3a5f92c69ff576da07025918e [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>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Elliott Hughes0af55432011-08-17 18:37:28 -070023#include <utility>
24#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Ian Rogersef7d42f2014-01-06 12:55:46 -080026#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080028#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080029#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080030#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070033#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070034#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070035#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070036#include "mirror/art_field-inl.h"
37#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class-inl.h"
39#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070042#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080044#include "object_utils.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080045#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070046#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070048#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070051#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "utf.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\"",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700112 kind, c->GetDescriptor().c_str(), 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);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700138 } else if (c->IsInterface()) {
139 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700140 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700141 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800142 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700143 // No virtual method matching the signature. Search declared
144 // private methods and constructors.
145 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700146 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700147 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800148 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800150 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700151 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700153}
154
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800155static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800157 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700158 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
159 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800160 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700161 }
Brian Carlstromce888532013-10-10 00:32:58 -0700162 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800163 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700164 return method->GetDeclaringClass()->GetClassLoader();
165 }
166 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800167 mirror::ClassLoader* class_loader =
168 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
169 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700170 return class_loader;
171 }
172 // See if the override ClassLoader is set for gtests.
173 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800174 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800175 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700176 CHECK(Runtime::Current()->UseCompileTimeClassPath());
177 return class_loader;
178 }
179 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800180 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700181}
182
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700183static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
184 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700186 StackHandleScope<2> hs(soa.Self());
187 Handle<mirror::Class> c(
188 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
189 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800190 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700191 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800192 mirror::ArtField* field = nullptr;
193 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
195 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700196 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800197 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700198 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700200 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800201 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800204 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700205 StackHandleScope<1> hs(soa.Self());
206 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800208 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800209 "no type \"%s\" found and so no field \"%s\" "
210 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700211 c->GetDescriptor().c_str());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700212 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800213 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 }
215 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700216 field = mirror::Class::FindStaticField(soa.Self(), c, name,
217 field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 } else {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700219 field = c->FindInstanceField(name, field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700220 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800221 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800222 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
223 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
224 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700225 sig, name, c->GetDescriptor().c_str());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800226 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700227 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700229}
230
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800231static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700234 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700235 vm->pin_table.Add(array);
236}
237
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800238static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700241 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700242 vm->pin_table.Remove(array);
243}
244
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800245static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700248 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
250 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
251 "%s offset=%d length=%d %s.length=%d",
252 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700253}
Ian Rogers0571d352011-11-03 19:51:38 -0700254
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
256 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
259 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
260 "offset=%d length=%d string.length()=%d", start, length,
261 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700262}
Elliott Hughes814e4032011-08-23 12:07:56 -0700263
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 // Turn the const char* into a java.lang.String.
267 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800268 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700270 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 // Choose an appropriate constructor and set up the arguments.
273 jvalue args[2];
274 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800275 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 signature = "()V";
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/String;)V";
279 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800280 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 signature = "(Ljava/lang/Throwable;)V";
282 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700283 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
285 args[0].l = s.get();
286 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700287 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800289 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800290 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800292 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 return JNI_ERR;
294 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700295
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800296 ScopedLocalRef<jthrowable> exception(
297 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
298 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299 return JNI_ERR;
300 }
Ian Rogersef28b142012-11-30 14:22:18 -0800301 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800303 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700305}
306
Elliott Hughes462c9442012-03-23 18:47:50 -0700307static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800308 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700309 return JNI_ERR;
310 }
311
Elliott Hughes462c9442012-03-23 18:47:50 -0700312 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700313 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800314 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700315 *p_env = self->GetJniEnv();
316 return JNI_OK;
317 }
318
319 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
320
321 // No threads allowed in zygote mode.
322 if (runtime->IsZygote()) {
323 LOG(ERROR) << "Attempt to attach a thread in the zygote";
324 return JNI_ERR;
325 }
326
Elliott Hughes462c9442012-03-23 18:47:50 -0700327 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800328 const char* thread_name = nullptr;
329 jobject thread_group = nullptr;
330 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700331 if (IsBadJniVersion(args->version)) {
332 LOG(ERROR) << "Bad JNI version passed to "
333 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
334 << args->version;
335 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700336 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700337 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700338 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700339 }
Elliott Hughes75770752011-08-24 17:52:38 -0700340
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800341 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800342 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700343 return JNI_ERR;
344 } else {
345 *p_env = Thread::Current()->GetJniEnv();
346 return JNI_OK;
347 }
Elliott Hughes75770752011-08-24 17:52:38 -0700348}
349
Elliott Hughes79082e32011-08-25 12:07:32 -0700350class SharedLibrary {
351 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800352 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700353 : path_(path),
354 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700355 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700356 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700357 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700358 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700359 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700360 }
361
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700362 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
363 mirror::Object** root = &class_loader_;
364 return ReadBarrier::BarrierForRoot<mirror::Object, kWithReadBarrier>(root);
Elliott Hughes79082e32011-08-25 12:07:32 -0700365 }
366
367 std::string GetPath() {
368 return path_;
369 }
370
371 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700372 * Check the result of an earlier call to JNI_OnLoad on this library.
373 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700374 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 bool CheckOnLoadResult()
376 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700378 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
380 bool okay;
381 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700382 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700383
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700384 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
386 // caller can continue.
387 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
388 okay = true;
389 } else {
390 while (jni_on_load_result_ == kPending) {
391 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700392 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700394
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 okay = (jni_on_load_result_ == kOkay);
396 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
397 << (okay ? "succeeded" : "failed") << "]";
398 }
399 }
400 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700401 return okay;
402 }
403
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700405 Thread* self = Thread::Current();
406 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700407
Elliott Hughes79082e32011-08-25 12:07:32 -0700408 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700409 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700410
411 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700412 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700413 }
414
415 void* FindSymbol(const std::string& symbol_name) {
416 return dlsym(handle_, symbol_name.c_str());
417 }
418
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800419 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800420 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800421 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800422 }
423 }
424
Elliott Hughes79082e32011-08-25 12:07:32 -0700425 private:
426 enum JNI_OnLoadState {
427 kPending,
428 kFailed,
429 kOkay,
430 };
431
432 // Path to library "/system/lib/libjni.so".
433 std::string path_;
434
435 // The void* returned by dlopen(3).
436 void* handle_;
437
438 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800439 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700440
441 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700442 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700443 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700444 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700446 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700448 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700449};
450
Elliott Hughes79082e32011-08-25 12:07:32 -0700451// This exists mainly to keep implementation details out of the header file.
452class Libraries {
453 public:
454 Libraries() {
455 }
456
457 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700458 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700459 }
460
Elliott Hughesae80b492012-04-24 10:43:17 -0700461 void Dump(std::ostream& os) const {
462 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700463 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700464 if (!first) {
465 os << ' ';
466 }
467 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700468 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700469 }
470 }
471
472 size_t size() const {
473 return libraries_.size();
474 }
475
Elliott Hughes79082e32011-08-25 12:07:32 -0700476 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700477 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800478 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700479 }
480
481 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700482 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700483 }
484
485 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800486 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700488 std::string jni_short_name(JniShortName(m));
489 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800490 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700491 for (const auto& lib : libraries_) {
492 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700493 if (library->GetClassLoader() != declaring_class_loader) {
494 // We only search libraries loaded by the appropriate ClassLoader.
495 continue;
496 }
497 // Try the short name then the long name...
498 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800499 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 fn = library->FindSymbol(jni_long_name);
501 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800502 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800503 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
504 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 return fn;
506 }
507 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700508 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700509 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700510 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700511 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800512 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 }
514
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800515 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800516 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800517 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800518 }
519 }
520
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700522 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700523};
524
Ian Rogers2d10b202014-05-12 19:15:18 -0700525#define CHECK_NON_NULL_ARGUMENT(value) \
526 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700527
Ian Rogers2d10b202014-05-12 19:15:18 -0700528#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
529 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
530
531#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
532 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
533
534#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
535 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
536
537#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800538 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700539 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700540 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700541 }
542
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700543#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800544 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700545 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700546 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700547 }
548
Elliott Hughescdf53122011-08-19 15:46:09 -0700549class JNI {
550 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700551 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700552 return JNI_VERSION_1_6;
553 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700554
Ian Rogers25e8b912012-09-07 11:31:36 -0700555 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700556 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800557 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700558 }
559
Elliott Hughescdf53122011-08-19 15:46:09 -0700560 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700561 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700562 Runtime* runtime = Runtime::Current();
563 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700564 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700565 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800566 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700567 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700568 StackHandleScope<1> hs(soa.Self());
569 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800570 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700571 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800572 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700573 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700574 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700575 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700576
Ian Rogers62f05122014-03-21 11:21:29 -0700577 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700578 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700579 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700580 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700581 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700582
Ian Rogers62f05122014-03-21 11:21:29 -0700583 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700584 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700586 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700587 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700588
Elliott Hughescdf53122011-08-19 15:46:09 -0700589 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700590 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700591 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800592 mirror::ArtMethod* m = soa.DecodeMethod(mid);
593 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200595 jobject reflect_method;
596 if (m->IsConstructor()) {
597 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
598 } else {
599 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
600 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700601 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800602 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700603 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800604 SetObjectField(env, reflect_method,
605 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700606 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700610 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700613 jobject art_field = soa.AddLocalReference<jobject>(f);
614 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
615 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800616 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700617 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800618 SetObjectField(env, reflect_field,
619 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700620 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700621 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700622
Elliott Hughes37f7a402011-08-22 18:56:01 -0700623 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700624 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800626 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700628 }
629
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700630 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700631 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800633 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughes37f7a402011-08-22 18:56:01 -0700637 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700638 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
639 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800641 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
642 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700643 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughese84278b2012-03-22 10:06:53 -0700646 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700647 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700649 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 return JNI_TRUE;
651 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700652 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800653 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
654 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700655 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700656 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700658
Elliott Hughes37f7a402011-08-22 18:56:01 -0700659 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700660 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800661 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
662 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700663 return JNI_ERR;
664 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
666 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 return JNI_OK;
668 }
669
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700670 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700671 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800672 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 }
674
675 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700676 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 }
678
679 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700680 ScopedObjectAccess soa(env);
681 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 }
683
684 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700686
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700687 // If we have no exception to describe, pass through.
688 if (!soa.Self()->GetException(nullptr)) {
689 return;
690 }
691
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700692 StackHandleScope<3> hs(soa.Self());
693 // TODO: Use nullptr instead of null handles?
694 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
695 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
696 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800697 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200698 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800699 {
700 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800701 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700702 old_throw_this_object.Assign(old_throw_location.GetThis());
703 old_throw_method.Assign(old_throw_location.GetMethod());
704 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800705 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200706 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800707 soa.Self()->ClearException();
708 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800709 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700710 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700711 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
712 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800713 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700715 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700716 } else {
717 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800718 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800719 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800721 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700722 }
723 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700724 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800725 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700726
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700727 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200728 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700730
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800733 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 }
736
Ian Rogers25e8b912012-09-07 11:31:36 -0700737 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 LOG(FATAL) << "JNI FatalError called: " << msg;
739 }
740
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700741 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700742 // TODO: SOA may not be necessary but I do it to please lock annotations.
743 ScopedObjectAccess soa(env);
744 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700745 return JNI_ERR;
746 }
Ian Rogersef28b142012-11-30 14:22:18 -0800747 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 return JNI_OK;
749 }
750
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700751 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800753 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 soa.Env()->PopFrame();
755 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 }
757
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700758 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700759 // TODO: SOA may not be necessary but I do it to please lock annotations.
760 ScopedObjectAccess soa(env);
761 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 }
763
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700765 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800766 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800767 // Check for null after decoding the object to handle cleared weak globals.
768 if (decoded_obj == nullptr) {
769 return nullptr;
770 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700773 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 return reinterpret_cast<jobject>(ref);
776 }
777
778 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800779 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 return;
781 }
Ian Rogersef28b142012-11-30 14:22:18 -0800782 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800784 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700785 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700786
787 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
788 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
789 << "failed to find entry";
790 }
791 }
792
793 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800795 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 }
797
798 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700799 if (obj != nullptr) {
800 ScopedObjectAccess soa(env);
801 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 }
803 }
804
805 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700806 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800807 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800808 // Check for null after decoding the object to handle cleared weak globals.
809 if (decoded_obj == nullptr) {
810 return nullptr;
811 }
812 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
814
815 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800816 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 return;
818 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700819 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800820 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700821
Ian Rogersef28b142012-11-30 14:22:18 -0800822 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 if (!locals.Remove(cookie, obj)) {
824 // Attempting to delete a local reference that is not in the
825 // topmost local reference frame is a no-op. DeleteLocalRef returns
826 // void and doesn't throw any exceptions, but we should probably
827 // complain about it so the user will notice that things aren't
828 // going quite the way they expect.
829 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
830 << "failed to find entry";
831 }
832 }
833
834 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700835 if (obj1 == obj2) {
836 return JNI_TRUE;
837 } else {
838 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800839 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
840 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700841 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 }
843
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700844 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700845 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800847 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800848 if (c == nullptr) {
849 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700850 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700851 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 }
853
Ian Rogersbc939662013-08-15 10:26:54 -0700854 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700857 CHECK_NON_NULL_ARGUMENT(java_class);
858 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700859 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 va_end(args);
861 return result;
862 }
863
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700865 CHECK_NON_NULL_ARGUMENT(java_class);
866 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800868 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800869 if (c == nullptr) {
870 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700871 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800872 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800873 if (result == nullptr) {
874 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700875 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700876 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700877 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800878 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800879 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700880 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800881 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 }
883
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700885 CHECK_NON_NULL_ARGUMENT(java_class);
886 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700887 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800888 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800889 if (c == nullptr) {
890 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700891 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800892 mirror::Object* result = c->AllocObject(soa.Self());
893 if (result == nullptr) {
894 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700895 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800898 if (soa.Self()->IsExceptionPending()) {
899 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700900 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800901 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 }
903
Ian Rogersbc939662013-08-15 10:26:54 -0700904 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700905 CHECK_NON_NULL_ARGUMENT(java_class);
906 CHECK_NON_NULL_ARGUMENT(name);
907 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700909 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Ian Rogersbc939662013-08-15 10:26:54 -0700912 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
913 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700914 CHECK_NON_NULL_ARGUMENT(java_class);
915 CHECK_NON_NULL_ARGUMENT(name);
916 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700918 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 va_list ap;
923 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700924 CHECK_NON_NULL_ARGUMENT(obj);
925 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700926 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700933 CHECK_NON_NULL_ARGUMENT(obj);
934 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 ScopedObjectAccess soa(env);
936 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
937 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700941 CHECK_NON_NULL_ARGUMENT(obj);
942 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700944 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
945 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 }
948
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 va_list ap;
951 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700952 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
953 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700954 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700957 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700961 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
962 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 ScopedObjectAccess soa(env);
964 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700968 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
969 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700971 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
972 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 va_list ap;
977 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700978 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
979 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700980 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700981 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700983 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700987 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
988 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
995 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700997 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
998 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 va_list ap;
1003 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1005 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001006 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001009 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001013 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1014 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001015 ScopedObjectAccess soa(env);
1016 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001020 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1021 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001023 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1024 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_list ap;
1029 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001030 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1031 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001032 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001035 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001039 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1040 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041 ScopedObjectAccess soa(env);
1042 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001046 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1047 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001049 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1050 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_list ap;
1055 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001056 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1057 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001058 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001059 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001061 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001065 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1066 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 ScopedObjectAccess soa(env);
1068 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001072 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1073 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001075 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1076 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 va_list ap;
1081 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001082 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1083 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001084 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001087 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001091 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1092 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 ScopedObjectAccess soa(env);
1094 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1099 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001101 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1102 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001108 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1109 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001110 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001113 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001117 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1118 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001124 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1125 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001127 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1128 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_list ap;
1133 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1135 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001136 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001139 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001143 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1144 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 ScopedObjectAccess soa(env);
1146 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001150 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1151 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001152 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001153 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1154 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 va_list ap;
1159 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001160 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1161 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001162 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001163 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001168 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1169 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170 ScopedObjectAccess soa(env);
1171 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001175 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1176 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001178 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001181 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001184 CHECK_NON_NULL_ARGUMENT(obj);
1185 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001186 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1188 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 va_end(ap);
1190 return local_result;
1191 }
1192
Ian Rogersbc939662013-08-15 10:26:54 -07001193 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1194 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001195 CHECK_NON_NULL_ARGUMENT(obj);
1196 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001197 ScopedObjectAccess soa(env);
1198 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1199 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Ian Rogersbc939662013-08-15 10:26:54 -07001202 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1203 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001204 CHECK_NON_NULL_ARGUMENT(obj);
1205 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001207 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Ian Rogersbc939662013-08-15 10:26:54 -07001211 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1212 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001215 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1216 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001217 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001220 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Ian Rogersbc939662013-08-15 10:26:54 -07001223 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1224 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001225 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1226 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(env);
1228 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Ian Rogersbc939662013-08-15 10:26:54 -07001231 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1232 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001233 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1234 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001236 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001239 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001242 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1243 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001244 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001247 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Ian Rogersbc939662013-08-15 10:26:54 -07001250 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1251 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001252 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1253 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 ScopedObjectAccess soa(env);
1255 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Ian Rogersbc939662013-08-15 10:26:54 -07001258 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1259 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001260 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1261 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001263 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001266 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001269 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1270 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001271 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001274 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Ian Rogersbc939662013-08-15 10:26:54 -07001277 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1278 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001279 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1280 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001281 ScopedObjectAccess soa(env);
1282 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
Ian Rogersbc939662013-08-15 10:26:54 -07001285 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1286 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001287 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1288 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001290 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001293 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001296 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1297 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001298 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001301 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Ian Rogersbc939662013-08-15 10:26:54 -07001304 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1305 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001306 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1307 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308 ScopedObjectAccess soa(env);
1309 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Ian Rogersbc939662013-08-15 10:26:54 -07001312 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1313 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001314 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1315 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001316 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001317 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001323 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1324 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001325 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001328 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Ian Rogersbc939662013-08-15 10:26:54 -07001331 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1332 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001333 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1334 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001335 ScopedObjectAccess soa(env);
1336 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
Ian Rogersbc939662013-08-15 10:26:54 -07001339 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1340 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001341 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1342 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001343 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001344 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001347 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001350 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1351 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001352 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001355 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Ian Rogersbc939662013-08-15 10:26:54 -07001358 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1359 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001360 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1361 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001362 ScopedObjectAccess soa(env);
1363 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
Ian Rogersbc939662013-08-15 10:26:54 -07001366 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1367 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001368 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1369 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001370 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001371 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001374 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001377 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1378 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001379 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001380 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001382 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Ian Rogersbc939662013-08-15 10:26:54 -07001385 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1386 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001387 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1388 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001389 ScopedObjectAccess soa(env);
1390 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
Ian Rogersbc939662013-08-15 10:26:54 -07001393 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1394 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001395 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1396 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001397 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001398 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001401 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001404 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1405 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001406 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001407 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001409 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Ian Rogersbc939662013-08-15 10:26:54 -07001412 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1413 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001414 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1415 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416 ScopedObjectAccess soa(env);
1417 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Ian Rogersbc939662013-08-15 10:26:54 -07001420 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1421 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001422 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1423 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001424 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001425 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001428 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001430 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001431 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1432 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001433 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001434 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 va_end(ap);
1436 }
1437
Brian Carlstromea46f952013-07-30 01:26:50 -07001438 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1439 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001440 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1441 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001442 ScopedObjectAccess soa(env);
1443 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Ian Rogersbc939662013-08-15 10:26:54 -07001446 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1447 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001448 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1449 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001450 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001451 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Ian Rogersbc939662013-08-15 10:26:54 -07001454 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001455 CHECK_NON_NULL_ARGUMENT(java_class);
1456 CHECK_NON_NULL_ARGUMENT(name);
1457 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001458 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001459 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001461
Ian Rogersbc939662013-08-15 10:26:54 -07001462 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1463 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001464 CHECK_NON_NULL_ARGUMENT(java_class);
1465 CHECK_NON_NULL_ARGUMENT(name);
1466 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001468 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001472 CHECK_NON_NULL_ARGUMENT(obj);
1473 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001475 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1476 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001481 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001483 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001484 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001488 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1489 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001490 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001491 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1492 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1493 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001494 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001497 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001498 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* v = soa.Decode<mirror::Object*>(java_value);
1501 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001502 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001506 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1507 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001508 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001509 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1510 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001511 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001513#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001514 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001515 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001516 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001517 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001520 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1521 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001523 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1524 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001525 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001527#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001528 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001529 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001530 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001531 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001532
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001534 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001538 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001541 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001542 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001546 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001550 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001554 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001558 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001561 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001562 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001566 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001569 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001570 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001574 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001578 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001582 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001586 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001587 }
1588
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001589 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001590 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001594 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001595 }
1596
1597 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001598 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001599 }
1600
1601 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001602 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001603 }
1604
1605 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001606 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001607 }
1608
1609 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001610 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001611 }
1612
1613 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001614 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001615 }
1616
1617 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001618 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001619 }
1620
1621 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001622 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001623 }
1624
1625 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001626 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001627 }
1628
1629 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001630 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001631 }
1632
1633 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001634 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001635 }
1636
1637 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001638 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001639 }
1640
1641 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001642 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001643 }
1644
1645 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001646 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001647 }
1648
1649 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001650 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001651 }
1652
1653 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001654 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001655 }
1656
1657 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001664 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001665 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001666 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 va_end(ap);
1669 return local_result;
1670 }
1671
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001672 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001673 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001674 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001675 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001676 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001680 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001682 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001686 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001689 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001690 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001691 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001693 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001696 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001697 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001698 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001699 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001702 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001703 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001704 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001705 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001708 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001711 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001712 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001713 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001715 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001718 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001719 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001720 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001721 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001724 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001725 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001727 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001733 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001734 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001735 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001737 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001740 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001741 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001742 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001743 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001746 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001747 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001748 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001749 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001754 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001755 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001756 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001757 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001759 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001763 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001765 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001768 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001769 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001771 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001774 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001776 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001777 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001778 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001779 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001781 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001784 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001785 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001787 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001791 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001792 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001793 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001798 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001799 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001800 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001801 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001803 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001807 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001809 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001812 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001813 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001815 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001818 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001820 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001821 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001822 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001823 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001825 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001828 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001829 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001830 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001831 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001835 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001837 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001840 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001842 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001843 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001844 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001845 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001847 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001850 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001851 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001853 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001856 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001857 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001859 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001862 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001864 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001865 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001866 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001867 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 va_end(ap);
1869 }
1870
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001871 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001872 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001874 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001877 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001878 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001880 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
Elliott Hughes814e4032011-08-23 12:07:56 -07001883 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001884 if (UNLIKELY(char_count < 0)) {
1885 JniAbortF("NewString", "char_count < 0: %d", char_count);
1886 return nullptr;
1887 }
1888 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1889 JniAbortF("NewString", "chars == null && char_count > 0");
1890 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001891 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001893 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001894 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
1897 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001898 if (utf == nullptr) {
1899 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001902 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
Elliott Hughes814e4032011-08-23 12:07:56 -07001906 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001907 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001909 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001910 }
1911
1912 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001913 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001914 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001915 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001916 }
1917
Ian Rogersbc939662013-08-15 10:26:54 -07001918 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1919 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001920 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001922 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001923 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001925 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001926 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001927 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1928 memcpy(buf, chars + start, length * sizeof(jchar));
1929 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001930 }
1931
Ian Rogersbc939662013-08-15 10:26:54 -07001932 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1933 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001934 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001935 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001936 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001937 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001939 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001940 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001941 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1942 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1943 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001944 }
1945
Elliott Hughes75770752011-08-24 17:52:38 -07001946 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001947 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001949 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1950 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001952 gc::Heap* heap = Runtime::Current()->GetHeap();
1953 if (heap->IsMovableObject(chars)) {
1954 if (is_copy != nullptr) {
1955 *is_copy = JNI_TRUE;
1956 }
1957 int32_t char_count = s->GetLength();
1958 int32_t offset = s->GetOffset();
1959 jchar* bytes = new jchar[char_count];
1960 for (int32_t i = 0; i < char_count; i++) {
1961 bytes[i] = chars->Get(i + offset);
1962 }
1963 return bytes;
1964 } else {
1965 if (is_copy != nullptr) {
1966 *is_copy = JNI_FALSE;
1967 }
1968 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001969 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 }
1971
Mathieu Chartier590fee92013-09-13 13:46:47 -07001972 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001973 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001975 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1976 mirror::CharArray* s_chars = s->GetCharArray();
1977 if (chars != (s_chars->GetData() + s->GetOffset())) {
1978 delete[] chars;
1979 }
1980 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001984 CHECK_NON_NULL_ARGUMENT(java_string);
1985 ScopedObjectAccess soa(env);
1986 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1987 mirror::CharArray* chars = s->GetCharArray();
1988 int32_t offset = s->GetOffset();
1989 PinPrimitiveArray(soa, chars);
1990 gc::Heap* heap = Runtime::Current()->GetHeap();
1991 if (heap->IsMovableObject(chars)) {
1992 StackHandleScope<1> hs(soa.Self());
1993 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1994 heap->IncrementDisableMovingGC(soa.Self());
1995 }
1996 if (is_copy != nullptr) {
1997 *is_copy = JNI_FALSE;
1998 }
1999 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes75770752011-08-24 17:52:38 -07002002 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002003 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2004 ScopedObjectAccess soa(env);
2005 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
2006 gc::Heap* heap = Runtime::Current()->GetHeap();
2007 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2008 mirror::CharArray* s_chars = s->GetCharArray();
2009 if (heap->IsMovableObject(s_chars)) {
2010 heap->DecrementDisableMovingGC(soa.Self());
2011 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002015 if (java_string == nullptr) {
2016 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002017 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002018 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002019 *is_copy = JNI_TRUE;
2020 }
Ian Rogersef28b142012-11-30 14:22:18 -08002021 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002022 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 size_t byte_count = s->GetUtfLength();
2024 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002025 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002026 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2027 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2028 bytes[byte_count] = '\0';
2029 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002033 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002034 }
2035
Elliott Hughesbd935992011-08-22 11:59:34 -07002036 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002038 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002039 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002040 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002041 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2042 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002043 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002044 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002048 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002049 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002050 mirror::ObjectArray<mirror::Object>* array =
2051 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002052 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Ian Rogersbc939662013-08-15 10:26:54 -07002055 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2056 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002057 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002058 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002059 mirror::ObjectArray<mirror::Object>* array =
2060 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2061 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002062 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
2065 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002066 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
2069 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002070 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
2073 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002074 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
2077 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002078 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
2081 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002082 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
2085 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002086 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
2089 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002090 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Ian Rogers1d99e452014-01-02 17:36:41 -08002093 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2094 jobject initial_element) {
2095 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002096 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002097 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002098 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002099 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100
2101 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002102 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002103 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002104 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002105 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002106 if (UNLIKELY(element_class->IsPrimitive())) {
2107 JniAbortF("NewObjectArray", "not an object type: %s",
2108 PrettyDescriptor(element_class).c_str());
2109 return nullptr;
2110 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002111 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002112 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002113 if (UNLIKELY(array_class == nullptr)) {
2114 return nullptr;
2115 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes75770752011-08-24 17:52:38 -07002118 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002119 mirror::ObjectArray<mirror::Object>* result =
2120 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002121 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002122 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002123 if (initial_object != nullptr) {
2124 mirror::Class* element_class = result->GetClass()->GetComponentType();
2125 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2126 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2127 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2128 PrettyDescriptor(element_class).c_str());
2129
2130 } else {
2131 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002132 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002133 }
2134 }
Elliott Hughes75770752011-08-24 17:52:38 -07002135 }
2136 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
2140 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002141 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Ian Rogersa15e67d2012-02-28 13:51:55 -08002144 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002145 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002147 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002148 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2149 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2150 PrettyDescriptor(array->GetClass()).c_str());
2151 return nullptr;
2152 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002153 gc::Heap* heap = Runtime::Current()->GetHeap();
2154 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002155 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002156 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002157 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002158 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002159 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002160 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002161 *is_copy = JNI_FALSE;
2162 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002163 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002164 }
2165
Ian Rogers2d10b202014-05-12 19:15:18 -07002166 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2167 jint mode) {
2168 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2169 ScopedObjectAccess soa(env);
2170 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2171 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2172 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2173 PrettyDescriptor(array->GetClass()).c_str());
2174 return;
2175 }
2176 const size_t component_size = array->GetClass()->GetComponentSize();
2177 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002178 }
2179
Elliott Hughes75770752011-08-24 17:52:38 -07002180 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002181 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Elliott Hughes75770752011-08-24 17:52:38 -07002184 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002185 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes75770752011-08-24 17:52:38 -07002188 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002189 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Elliott Hughes75770752011-08-24 17:52:38 -07002192 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002193 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes75770752011-08-24 17:52:38 -07002196 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002197 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199
Elliott Hughes75770752011-08-24 17:52:38 -07002200 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002201 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Elliott Hughes75770752011-08-24 17:52:38 -07002204 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002205 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Elliott Hughes75770752011-08-24 17:52:38 -07002208 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002209 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Mathieu Chartier590fee92013-09-13 13:46:47 -07002212 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2213 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002214 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2215 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Mathieu Chartier590fee92013-09-13 13:46:47 -07002218 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002219 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Mathieu Chartier590fee92013-09-13 13:46:47 -07002222 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002223 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Mathieu Chartier590fee92013-09-13 13:46:47 -07002226 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2227 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002228 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Mathieu Chartier590fee92013-09-13 13:46:47 -07002231 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2232 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002233 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Mathieu Chartier590fee92013-09-13 13:46:47 -07002236 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002237 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Mathieu Chartier590fee92013-09-13 13:46:47 -07002240 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002241 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Mathieu Chartier590fee92013-09-13 13:46:47 -07002244 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2245 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002246 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Ian Rogersbc939662013-08-15 10:26:54 -07002249 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2250 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002251 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002252 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Ian Rogersbc939662013-08-15 10:26:54 -07002255 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2256 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002257 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259
Ian Rogersbc939662013-08-15 10:26:54 -07002260 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2261 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002262 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Ian Rogersbc939662013-08-15 10:26:54 -07002265 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2266 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002267 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002268 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Ian Rogersbc939662013-08-15 10:26:54 -07002271 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2272 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002273 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002274 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Ian Rogersbc939662013-08-15 10:26:54 -07002277 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2278 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002279 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2283 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002284 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Ian Rogersbc939662013-08-15 10:26:54 -07002287 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2288 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002289 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002290 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 }
2292
Ian Rogersbc939662013-08-15 10:26:54 -07002293 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2294 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002295 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002296 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 }
2298
Ian Rogersbc939662013-08-15 10:26:54 -07002299 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2300 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002301 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Ian Rogersbc939662013-08-15 10:26:54 -07002304 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2305 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002306 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Ian Rogersbc939662013-08-15 10:26:54 -07002309 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2310 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002311 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002312 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Ian Rogersbc939662013-08-15 10:26:54 -07002315 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2316 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002317 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002318 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320
Ian Rogersbc939662013-08-15 10:26:54 -07002321 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2322 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002323 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
2325
Ian Rogersbc939662013-08-15 10:26:54 -07002326 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2327 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002328 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 }
2330
Ian Rogersbc939662013-08-15 10:26:54 -07002331 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2332 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002333 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002334 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002335 }
2336
Ian Rogersbc939662013-08-15 10:26:54 -07002337 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2338 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002339 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2340 }
2341
Ian Rogersbc939662013-08-15 10:26:54 -07002342 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2343 jint method_count, bool return_errors) {
2344 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002345 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002346 return JNI_ERR; // Not reached.
2347 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002348 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002350 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002351 if (UNLIKELY(method_count == 0)) {
2352 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2353 << PrettyDescriptor(c);
2354 return JNI_OK;
2355 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002356 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002357 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002358 const char* name = methods[i].name;
2359 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002360 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002361 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002362 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002363 ++sig;
2364 }
2365
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002366 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2367 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002368 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002370 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002371 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002372 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002373 << PrettyDescriptor(c) << "." << name << sig << " in "
2374 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002375 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002376 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002377 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002378 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002379 << PrettyDescriptor(c) << "." << name << sig
2380 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002381 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002382 return JNI_ERR;
2383 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002384
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002385 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002386
Ian Rogers1eb512d2013-10-18 15:42:20 -07002387 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002388 }
2389 return JNI_OK;
2390 }
2391
Elliott Hughes5174fe62011-08-23 15:12:35 -07002392 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002393 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002394 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002395 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002396
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002397 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002398
Ian Rogers2d10b202014-05-12 19:15:18 -07002399 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002400 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002401 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002402 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002403 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002404 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002405 }
2406 }
2407 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002408 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002409 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002410 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002411 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002412 }
2413 }
2414
Ian Rogers2d10b202014-05-12 19:15:18 -07002415 if (unregistered_count == 0) {
2416 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2417 << PrettyDescriptor(c) << "' that contains no native methods";
2418 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002419 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002420 }
2421
Ian Rogers719d1a32014-03-06 12:13:39 -08002422 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002423 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002424 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002425 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2426 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002427 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002428 return JNI_ERR;
2429 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002430 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002431 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002432 }
2433
Ian Rogers719d1a32014-03-06 12:13:39 -08002434 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002435 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002436 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002437 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002438 o->MonitorExit(soa.Self());
2439 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002440 return JNI_ERR;
2441 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002442 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002443 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002444 }
2445
2446 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002447 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002448 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002449 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002450 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002451 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002452 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002453 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002454 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002455 }
2456
Elliott Hughescdf53122011-08-19 15:46:09 -07002457 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002458 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002459 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002460 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002461 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002462 if (address == nullptr && capacity != 0) {
2463 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002464 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002465 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002466
Brian Carlstrom85a93362014-06-25 09:30:52 -07002467 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002468 if (capacity > INT_MAX) {
2469 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2470 return nullptr;
2471 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002472 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002473 jint capacity_arg = static_cast<jint>(capacity);
2474
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002475 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2476 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002477 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002478 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002479 }
2480
Elliott Hughesb465ab02011-08-24 11:21:21 -07002481 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002482 return reinterpret_cast<void*>(env->GetLongField(
2483 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002484 }
2485
Elliott Hughesb465ab02011-08-24 11:21:21 -07002486 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002487 return static_cast<jlong>(env->GetIntField(
2488 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002489 }
2490
Elliott Hughesb465ab02011-08-24 11:21:21 -07002491 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002492 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002493
2494 // Do we definitely know what kind of reference this is?
2495 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2496 IndirectRefKind kind = GetIndirectRefKind(ref);
2497 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002498 case kLocal: {
2499 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002500 // The local refs don't need a read barrier.
2501 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2502 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002503 return JNILocalRefType;
2504 }
2505 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002506 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002507 case kGlobal:
2508 return JNIGlobalRefType;
2509 case kWeakGlobal:
2510 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002511 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002512 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002513 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002514 return JNILocalRefType;
2515 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002516 return JNIInvalidRefType;
2517 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002518 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2519 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002520 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002521
2522 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002523 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2524 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002525 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002526 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002527 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2528 return JNI_ERR;
2529 }
2530 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002531 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002532 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2533 if (!okay) {
2534 soa.Self()->ThrowOutOfMemoryError(caller);
2535 }
2536 return okay ? JNI_OK : JNI_ERR;
2537 }
2538
2539 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002540 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002541 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002542 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002543 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002544 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002545 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002546 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002547 return soa.AddLocalReference<JniT>(result);
2548 }
2549
Ian Rogers2d10b202014-05-12 19:15:18 -07002550 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2551 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2552 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002553 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002554 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002555 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2556 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2557 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2558 PrettyDescriptor(array->GetClass()).c_str());
2559 return nullptr;
2560 }
2561 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2562 return array;
2563 }
2564
2565 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2566 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2567 CHECK_NON_NULL_ARGUMENT(java_array);
2568 ScopedObjectAccess soa(env);
2569 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2570 "GetArrayElements",
2571 "get");
2572 if (UNLIKELY(array == nullptr)) {
2573 return nullptr;
2574 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002575 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002576 // Only make a copy if necessary.
2577 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2578 if (is_copy != nullptr) {
2579 *is_copy = JNI_TRUE;
2580 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002581 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002582 size_t size = array->GetLength() * component_size;
2583 void* data = new uint64_t[RoundUp(size, 8) / 8];
2584 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002585 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002586 } else {
2587 if (is_copy != nullptr) {
2588 *is_copy = JNI_FALSE;
2589 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002590 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002591 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002592 }
2593
Ian Rogers2d10b202014-05-12 19:15:18 -07002594 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002595 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002596 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002597 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002598 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2599 "ReleaseArrayElements",
2600 "release");
2601 if (array == nullptr) {
2602 return;
2603 }
2604 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2605 }
2606
2607 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2608 size_t component_size, void* elements, jint mode)
2609 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002610 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002611 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002612 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002613 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002614 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2615 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002616 if (is_copy) {
2617 // Sanity check: If elements is not the same as the java array's data, it better not be a
2618 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2619 // copies we make?
2620 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2621 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2622 reinterpret_cast<void*>(elements), array_data);
2623 return;
2624 }
2625 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002626 // Don't need to copy if we had a direct pointer.
2627 if (mode != JNI_ABORT && is_copy) {
2628 memcpy(array_data, elements, bytes);
2629 }
2630 if (mode != JNI_COMMIT) {
2631 if (is_copy) {
2632 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002633 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002634 // Non copy to a movable object must means that we had disabled the moving GC.
2635 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002636 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002637 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002638 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002639 }
2640
Ian Rogers2d10b202014-05-12 19:15:18 -07002641 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2642 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2643 jsize start, jsize length, ElementT* buf) {
2644 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2645 ScopedObjectAccess soa(env);
2646 ArtArrayT* array =
2647 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2648 "GetPrimitiveArrayRegion",
2649 "get region of");
2650 if (array != nullptr) {
2651 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2652 ThrowAIOOBE(soa, array, start, length, "src");
2653 } else {
2654 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2655 ElementT* data = array->GetData();
2656 memcpy(buf, data + start, length * sizeof(ElementT));
2657 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002658 }
2659 }
2660
Ian Rogers2d10b202014-05-12 19:15:18 -07002661 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2662 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2663 jsize start, jsize length, const ElementT* buf) {
2664 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2665 ScopedObjectAccess soa(env);
2666 ArtArrayT* array =
2667 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2668 "SetPrimitiveArrayRegion",
2669 "set region of");
2670 if (array != nullptr) {
2671 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2672 ThrowAIOOBE(soa, array, start, length, "dst");
2673 } else {
2674 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2675 ElementT* data = array->GetData();
2676 memcpy(data + start, buf, length * sizeof(ElementT));
2677 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002678 }
2679 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002680};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002681
Elliott Hughes88c5c352012-03-15 18:49:48 -07002682const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002683 nullptr, // reserved0.
2684 nullptr, // reserved1.
2685 nullptr, // reserved2.
2686 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002687 JNI::GetVersion,
2688 JNI::DefineClass,
2689 JNI::FindClass,
2690 JNI::FromReflectedMethod,
2691 JNI::FromReflectedField,
2692 JNI::ToReflectedMethod,
2693 JNI::GetSuperclass,
2694 JNI::IsAssignableFrom,
2695 JNI::ToReflectedField,
2696 JNI::Throw,
2697 JNI::ThrowNew,
2698 JNI::ExceptionOccurred,
2699 JNI::ExceptionDescribe,
2700 JNI::ExceptionClear,
2701 JNI::FatalError,
2702 JNI::PushLocalFrame,
2703 JNI::PopLocalFrame,
2704 JNI::NewGlobalRef,
2705 JNI::DeleteGlobalRef,
2706 JNI::DeleteLocalRef,
2707 JNI::IsSameObject,
2708 JNI::NewLocalRef,
2709 JNI::EnsureLocalCapacity,
2710 JNI::AllocObject,
2711 JNI::NewObject,
2712 JNI::NewObjectV,
2713 JNI::NewObjectA,
2714 JNI::GetObjectClass,
2715 JNI::IsInstanceOf,
2716 JNI::GetMethodID,
2717 JNI::CallObjectMethod,
2718 JNI::CallObjectMethodV,
2719 JNI::CallObjectMethodA,
2720 JNI::CallBooleanMethod,
2721 JNI::CallBooleanMethodV,
2722 JNI::CallBooleanMethodA,
2723 JNI::CallByteMethod,
2724 JNI::CallByteMethodV,
2725 JNI::CallByteMethodA,
2726 JNI::CallCharMethod,
2727 JNI::CallCharMethodV,
2728 JNI::CallCharMethodA,
2729 JNI::CallShortMethod,
2730 JNI::CallShortMethodV,
2731 JNI::CallShortMethodA,
2732 JNI::CallIntMethod,
2733 JNI::CallIntMethodV,
2734 JNI::CallIntMethodA,
2735 JNI::CallLongMethod,
2736 JNI::CallLongMethodV,
2737 JNI::CallLongMethodA,
2738 JNI::CallFloatMethod,
2739 JNI::CallFloatMethodV,
2740 JNI::CallFloatMethodA,
2741 JNI::CallDoubleMethod,
2742 JNI::CallDoubleMethodV,
2743 JNI::CallDoubleMethodA,
2744 JNI::CallVoidMethod,
2745 JNI::CallVoidMethodV,
2746 JNI::CallVoidMethodA,
2747 JNI::CallNonvirtualObjectMethod,
2748 JNI::CallNonvirtualObjectMethodV,
2749 JNI::CallNonvirtualObjectMethodA,
2750 JNI::CallNonvirtualBooleanMethod,
2751 JNI::CallNonvirtualBooleanMethodV,
2752 JNI::CallNonvirtualBooleanMethodA,
2753 JNI::CallNonvirtualByteMethod,
2754 JNI::CallNonvirtualByteMethodV,
2755 JNI::CallNonvirtualByteMethodA,
2756 JNI::CallNonvirtualCharMethod,
2757 JNI::CallNonvirtualCharMethodV,
2758 JNI::CallNonvirtualCharMethodA,
2759 JNI::CallNonvirtualShortMethod,
2760 JNI::CallNonvirtualShortMethodV,
2761 JNI::CallNonvirtualShortMethodA,
2762 JNI::CallNonvirtualIntMethod,
2763 JNI::CallNonvirtualIntMethodV,
2764 JNI::CallNonvirtualIntMethodA,
2765 JNI::CallNonvirtualLongMethod,
2766 JNI::CallNonvirtualLongMethodV,
2767 JNI::CallNonvirtualLongMethodA,
2768 JNI::CallNonvirtualFloatMethod,
2769 JNI::CallNonvirtualFloatMethodV,
2770 JNI::CallNonvirtualFloatMethodA,
2771 JNI::CallNonvirtualDoubleMethod,
2772 JNI::CallNonvirtualDoubleMethodV,
2773 JNI::CallNonvirtualDoubleMethodA,
2774 JNI::CallNonvirtualVoidMethod,
2775 JNI::CallNonvirtualVoidMethodV,
2776 JNI::CallNonvirtualVoidMethodA,
2777 JNI::GetFieldID,
2778 JNI::GetObjectField,
2779 JNI::GetBooleanField,
2780 JNI::GetByteField,
2781 JNI::GetCharField,
2782 JNI::GetShortField,
2783 JNI::GetIntField,
2784 JNI::GetLongField,
2785 JNI::GetFloatField,
2786 JNI::GetDoubleField,
2787 JNI::SetObjectField,
2788 JNI::SetBooleanField,
2789 JNI::SetByteField,
2790 JNI::SetCharField,
2791 JNI::SetShortField,
2792 JNI::SetIntField,
2793 JNI::SetLongField,
2794 JNI::SetFloatField,
2795 JNI::SetDoubleField,
2796 JNI::GetStaticMethodID,
2797 JNI::CallStaticObjectMethod,
2798 JNI::CallStaticObjectMethodV,
2799 JNI::CallStaticObjectMethodA,
2800 JNI::CallStaticBooleanMethod,
2801 JNI::CallStaticBooleanMethodV,
2802 JNI::CallStaticBooleanMethodA,
2803 JNI::CallStaticByteMethod,
2804 JNI::CallStaticByteMethodV,
2805 JNI::CallStaticByteMethodA,
2806 JNI::CallStaticCharMethod,
2807 JNI::CallStaticCharMethodV,
2808 JNI::CallStaticCharMethodA,
2809 JNI::CallStaticShortMethod,
2810 JNI::CallStaticShortMethodV,
2811 JNI::CallStaticShortMethodA,
2812 JNI::CallStaticIntMethod,
2813 JNI::CallStaticIntMethodV,
2814 JNI::CallStaticIntMethodA,
2815 JNI::CallStaticLongMethod,
2816 JNI::CallStaticLongMethodV,
2817 JNI::CallStaticLongMethodA,
2818 JNI::CallStaticFloatMethod,
2819 JNI::CallStaticFloatMethodV,
2820 JNI::CallStaticFloatMethodA,
2821 JNI::CallStaticDoubleMethod,
2822 JNI::CallStaticDoubleMethodV,
2823 JNI::CallStaticDoubleMethodA,
2824 JNI::CallStaticVoidMethod,
2825 JNI::CallStaticVoidMethodV,
2826 JNI::CallStaticVoidMethodA,
2827 JNI::GetStaticFieldID,
2828 JNI::GetStaticObjectField,
2829 JNI::GetStaticBooleanField,
2830 JNI::GetStaticByteField,
2831 JNI::GetStaticCharField,
2832 JNI::GetStaticShortField,
2833 JNI::GetStaticIntField,
2834 JNI::GetStaticLongField,
2835 JNI::GetStaticFloatField,
2836 JNI::GetStaticDoubleField,
2837 JNI::SetStaticObjectField,
2838 JNI::SetStaticBooleanField,
2839 JNI::SetStaticByteField,
2840 JNI::SetStaticCharField,
2841 JNI::SetStaticShortField,
2842 JNI::SetStaticIntField,
2843 JNI::SetStaticLongField,
2844 JNI::SetStaticFloatField,
2845 JNI::SetStaticDoubleField,
2846 JNI::NewString,
2847 JNI::GetStringLength,
2848 JNI::GetStringChars,
2849 JNI::ReleaseStringChars,
2850 JNI::NewStringUTF,
2851 JNI::GetStringUTFLength,
2852 JNI::GetStringUTFChars,
2853 JNI::ReleaseStringUTFChars,
2854 JNI::GetArrayLength,
2855 JNI::NewObjectArray,
2856 JNI::GetObjectArrayElement,
2857 JNI::SetObjectArrayElement,
2858 JNI::NewBooleanArray,
2859 JNI::NewByteArray,
2860 JNI::NewCharArray,
2861 JNI::NewShortArray,
2862 JNI::NewIntArray,
2863 JNI::NewLongArray,
2864 JNI::NewFloatArray,
2865 JNI::NewDoubleArray,
2866 JNI::GetBooleanArrayElements,
2867 JNI::GetByteArrayElements,
2868 JNI::GetCharArrayElements,
2869 JNI::GetShortArrayElements,
2870 JNI::GetIntArrayElements,
2871 JNI::GetLongArrayElements,
2872 JNI::GetFloatArrayElements,
2873 JNI::GetDoubleArrayElements,
2874 JNI::ReleaseBooleanArrayElements,
2875 JNI::ReleaseByteArrayElements,
2876 JNI::ReleaseCharArrayElements,
2877 JNI::ReleaseShortArrayElements,
2878 JNI::ReleaseIntArrayElements,
2879 JNI::ReleaseLongArrayElements,
2880 JNI::ReleaseFloatArrayElements,
2881 JNI::ReleaseDoubleArrayElements,
2882 JNI::GetBooleanArrayRegion,
2883 JNI::GetByteArrayRegion,
2884 JNI::GetCharArrayRegion,
2885 JNI::GetShortArrayRegion,
2886 JNI::GetIntArrayRegion,
2887 JNI::GetLongArrayRegion,
2888 JNI::GetFloatArrayRegion,
2889 JNI::GetDoubleArrayRegion,
2890 JNI::SetBooleanArrayRegion,
2891 JNI::SetByteArrayRegion,
2892 JNI::SetCharArrayRegion,
2893 JNI::SetShortArrayRegion,
2894 JNI::SetIntArrayRegion,
2895 JNI::SetLongArrayRegion,
2896 JNI::SetFloatArrayRegion,
2897 JNI::SetDoubleArrayRegion,
2898 JNI::RegisterNatives,
2899 JNI::UnregisterNatives,
2900 JNI::MonitorEnter,
2901 JNI::MonitorExit,
2902 JNI::GetJavaVM,
2903 JNI::GetStringRegion,
2904 JNI::GetStringUTFRegion,
2905 JNI::GetPrimitiveArrayCritical,
2906 JNI::ReleasePrimitiveArrayCritical,
2907 JNI::GetStringCritical,
2908 JNI::ReleaseStringCritical,
2909 JNI::NewWeakGlobalRef,
2910 JNI::DeleteWeakGlobalRef,
2911 JNI::ExceptionCheck,
2912 JNI::NewDirectByteBuffer,
2913 JNI::GetDirectBufferAddress,
2914 JNI::GetDirectBufferCapacity,
2915 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002916};
2917
Elliott Hughes75770752011-08-24 17:52:38 -07002918JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002919 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002920 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002921 local_ref_cookie(IRT_FIRST_SEGMENT),
2922 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002923 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002924 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002925 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002926 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002927 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002928 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002929 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002930}
2931
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002932JNIEnvExt::~JNIEnvExt() {
2933}
2934
Mathieu Chartier590fee92013-09-13 13:46:47 -07002935jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2936 if (obj == nullptr) {
2937 return nullptr;
2938 }
2939 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2940}
2941
2942void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2943 if (obj != nullptr) {
2944 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2945 }
2946}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002947void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2948 check_jni = enabled;
2949 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002950}
2951
Elliott Hughes73e66f72012-05-09 09:34:45 -07002952void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2953 locals.Dump(os);
2954 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002955}
2956
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002957void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2958 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002959 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002960 stacked_local_ref_cookies.push_back(local_ref_cookie);
2961 local_ref_cookie = locals.GetSegmentState();
2962}
2963
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002964void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002965 locals.SetSegmentState(local_ref_cookie);
2966 local_ref_cookie = stacked_local_ref_cookies.back();
2967 stacked_local_ref_cookies.pop_back();
2968}
2969
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002970Offset JNIEnvExt::SegmentStateOffset() {
2971 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2972 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2973}
2974
Carl Shapiroea4dca82011-08-01 13:45:38 -07002975// JNI Invocation interface.
2976
Brian Carlstrombddf9762013-05-14 11:35:37 -07002977extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002978 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002979 if (IsBadJniVersion(args->version)) {
2980 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002981 return JNI_EVERSION;
2982 }
2983 Runtime::Options options;
2984 for (int i = 0; i < args->nOptions; ++i) {
2985 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002986 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002987 }
2988 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002989 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002990 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002991 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002992 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002993 bool started = runtime->Start();
2994 if (!started) {
2995 delete Thread::Current()->GetJniEnv();
2996 delete runtime->GetJavaVM();
2997 LOG(WARNING) << "CreateJavaVM failed";
2998 return JNI_ERR;
2999 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003000 *p_env = Thread::Current()->GetJniEnv();
3001 *p_vm = runtime->GetJavaVM();
3002 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003003}
3004
Elliott Hughesf2682d52011-08-15 16:37:04 -07003005extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003006 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003007 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003008 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003009 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003010 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003011 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003012 }
3013 return JNI_OK;
3014}
3015
3016// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003017extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003018 return JNI_ERR;
3019}
3020
Elliott Hughescdf53122011-08-19 15:46:09 -07003021class JII {
3022 public:
3023 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003024 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003025 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003026 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003027 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3028 delete raw_vm->runtime;
3029 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003030 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003031
Elliott Hughescdf53122011-08-19 15:46:09 -07003032 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003033 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003034 }
3035
3036 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003037 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003038 }
3039
3040 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003041 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003042 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003043 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003044 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3045 Runtime* runtime = raw_vm->runtime;
3046 runtime->DetachCurrentThread();
3047 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003048 }
3049
3050 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003051 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3052 // and unlike other calls that take a JNI version doesn't care if you supply
3053 // JNI_VERSION_1_1, which we don't otherwise support.
3054 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003055 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003056 return JNI_EVERSION;
3057 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003058 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003059 return JNI_ERR;
3060 }
3061 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003062 if (thread == nullptr) {
3063 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003064 return JNI_EDETACHED;
3065 }
3066 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003067 return JNI_OK;
3068 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003069};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003070
Elliott Hughes88c5c352012-03-15 18:49:48 -07003071const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003072 nullptr, // reserved0
3073 nullptr, // reserved1
3074 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003075 JII::DestroyJavaVM,
3076 JII::AttachCurrentThread,
3077 JII::DetachCurrentThread,
3078 JII::GetEnv,
3079 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003080};
3081
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003082JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003083 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003084 check_jni_abort_hook(nullptr),
3085 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003086 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003087 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003088 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003089 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003090 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003091 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003092 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003093 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003094 libraries(new Libraries),
3095 weak_globals_lock_("JNI weak global reference table lock"),
3096 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3097 allow_new_weak_globals_(true),
3098 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003099 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003100 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003101 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003102 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003103}
3104
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003105JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003106 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003107}
3108
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003109jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3110 if (obj == nullptr) {
3111 return nullptr;
3112 }
3113 MutexLock mu(self, weak_globals_lock_);
3114 while (UNLIKELY(!allow_new_weak_globals_)) {
3115 weak_globals_add_condition_.WaitHoldingLocks(self);
3116 }
3117 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3118 return reinterpret_cast<jweak>(ref);
3119}
3120
3121void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3122 MutexLock mu(self, weak_globals_lock_);
3123 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3124 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3125 << "failed to find entry";
3126 }
3127}
3128
Elliott Hughes88c5c352012-03-15 18:49:48 -07003129void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3130 check_jni = enabled;
3131 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003132}
3133
Elliott Hughesae80b492012-04-24 10:43:17 -07003134void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3135 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3136 if (force_copy) {
3137 os << " (with forcecopy)";
3138 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003139 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003140 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003141 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003142 os << "; pins=" << pin_table.Size();
3143 }
3144 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003145 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003146 os << "; globals=" << globals.Capacity();
3147 }
3148 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003149 MutexLock mu(self, weak_globals_lock_);
3150 if (weak_globals_.Capacity() > 0) {
3151 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003152 }
3153 }
3154 os << '\n';
3155
3156 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003157 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003158 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3159 }
3160}
3161
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003162void JavaVMExt::DisallowNewWeakGlobals() {
3163 MutexLock mu(Thread::Current(), weak_globals_lock_);
3164 allow_new_weak_globals_ = false;
3165}
3166
3167void JavaVMExt::AllowNewWeakGlobals() {
3168 Thread* self = Thread::Current();
3169 MutexLock mu(self, weak_globals_lock_);
3170 allow_new_weak_globals_ = true;
3171 weak_globals_add_condition_.Broadcast(self);
3172}
3173
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003174mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3175 MutexLock mu(self, weak_globals_lock_);
3176 while (UNLIKELY(!allow_new_weak_globals_)) {
3177 weak_globals_add_condition_.WaitHoldingLocks(self);
3178 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003179 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003180}
3181
Elliott Hughes73e66f72012-05-09 09:34:45 -07003182void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003183 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003184 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003185 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003186 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003187 }
3188 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003189 MutexLock mu(self, weak_globals_lock_);
3190 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003191 }
3192 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003193 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003194 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003195 }
3196}
3197
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003198bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003199 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003200 std::string* detail) {
3201 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003202
3203 // See if we've already loaded this library. If we have, and the class loader
3204 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003205 // TODO: for better results we should canonicalize the pathname (or even compare
3206 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003207 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003208 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003209 {
3210 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003211 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003212 library = libraries->Get(path);
3213 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003214 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003215 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003216 // The library will be associated with class_loader. The JNI
3217 // spec says we can't load the same library into more than one
3218 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003219 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003220 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003221 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003222 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003223 return false;
3224 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003225 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003226 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003227 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003228 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003229 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003230 return false;
3231 }
3232 return true;
3233 }
3234
3235 // Open the shared library. Because we're using a full path, the system
3236 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3237 // resolve this library's dependencies though.)
3238
3239 // Failures here are expected when java.library.path has several entries
3240 // and we have to hunt for the lib.
3241
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003242 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3243 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3244 // dlopen) becomes zero from dlclose.
3245
Elliott Hughescdf53122011-08-19 15:46:09 -07003246 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003247 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003248 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003249 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003250 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003251
Elliott Hughes84b2f142012-09-27 09:16:28 -07003252 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003253
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003254 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003255 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003256 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003257 return false;
3258 }
3259
3260 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003261 // TODO: move the locking (and more of this logic) into Libraries.
3262 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003263 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003264 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003265 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003266 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003267 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003268 libraries->Put(path, library);
3269 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003270 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003271 }
3272 if (!created_library) {
3273 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003274 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003275 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003276 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003277
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003278 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003279 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003280
Elliott Hughes79353722013-08-02 16:52:18 -07003281 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003282 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003283 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003284 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003285 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003286 } else {
3287 // Call JNI_OnLoad. We have to override the current class
3288 // loader, which will always be "null" since the stuff at the
3289 // top of the stack is around Runtime.loadLibrary(). (See
3290 // the comments in the JNI FindClass function.)
3291 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3292 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003293 StackHandleScope<1> hs(self);
3294 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3295 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003296
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003297 int version = 0;
3298 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003299 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003300 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003301 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003302 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003303
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003304 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003305
Elliott Hughes79353722013-08-02 16:52:18 -07003306 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003307 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003308 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003309 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003310 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003311 // It's unwise to call dlclose() here, but we can mark it
3312 // as bad and ensure that future load attempts will fail.
3313 // We don't know how far JNI_OnLoad got, so there could
3314 // be some partially-initialized stuff accessible through
3315 // newly-registered native method calls. We could try to
3316 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003317 } else {
3318 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003319 }
Elliott Hughes79353722013-08-02 16:52:18 -07003320 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003321 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003322 }
3323
Elliott Hughes79353722013-08-02 16:52:18 -07003324 library->SetResult(was_successful);
3325 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003326}
3327
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003328void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003329 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003330 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003331 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003332 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003333 c = EnsureInitialized(Thread::Current(), c);
3334 if (c == nullptr) {
3335 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003336 }
3337 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003338 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003339 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003340 std::string detail;
3341 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003342 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003343 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003344 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003345 native_method = libraries->FindNativeMethod(m, detail);
3346 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003347 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003348 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003349 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3350 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003351 }
3352 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003353}
3354
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003355void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003356 MutexLock mu(Thread::Current(), weak_globals_lock_);
3357 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003358 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003359 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003360 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003361 if (new_obj == nullptr) {
3362 new_obj = kClearedJniWeakGlobal;
3363 }
3364 *entry = new_obj;
3365 }
3366}
3367
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003368void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003369 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003370 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003371 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003372 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003373 }
3374 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003375 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003376 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003377 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003378 {
3379 MutexLock mu(self, libraries_lock);
3380 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003381 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003382 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003383 // The weak_globals table is visited by the GC itself (because it mutates the table).
3384}
3385
Elliott Hughesc8fece32013-01-02 11:27:23 -08003386void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003387 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003388 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003389 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003390 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3391 }
3392 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3393}
3394
Ian Rogersdf20fe02011-07-20 20:34:16 -07003395} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003396
3397std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3398 switch (rhs) {
3399 case JNIInvalidRefType:
3400 os << "JNIInvalidRefType";
3401 return os;
3402 case JNILocalRefType:
3403 os << "JNILocalRefType";
3404 return os;
3405 case JNIGlobalRefType:
3406 os << "JNIGlobalRefType";
3407 return os;
3408 case JNIWeakGlobalRefType:
3409 os << "JNIWeakGlobalRefType";
3410 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003411 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003412 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003413 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003414 }
3415}