blob: 17a5592e97ab415b97d6d57f606380aff2b869ae [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);
595 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
596 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800597 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700598 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800599 SetObjectField(env, reflect_method,
600 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700601 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700602 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700603
Elliott Hughescdf53122011-08-19 15:46:09 -0700604 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700605 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800607 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700608 jobject art_field = soa.AddLocalReference<jobject>(f);
609 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
610 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800611 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700612 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800613 SetObjectField(env, reflect_field,
614 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700615 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700617
Elliott Hughes37f7a402011-08-22 18:56:01 -0700618 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700619 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800621 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700623 }
624
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700625 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700626 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800628 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughes37f7a402011-08-22 18:56:01 -0700632 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700633 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
634 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800636 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
637 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughese84278b2012-03-22 10:06:53 -0700641 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700642 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800643 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700644 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700645 return JNI_TRUE;
646 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700647 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
649 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700650 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700651 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700653
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800656 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
657 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700658 return JNI_ERR;
659 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800660 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
661 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700662 return JNI_OK;
663 }
664
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700665 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700666 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800667 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 }
669
670 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700671 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700672 }
673
674 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700675 ScopedObjectAccess soa(env);
676 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 }
678
679 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700681
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700682 StackHandleScope<3> hs(soa.Self());
683 // TODO: Use nullptr instead of null handles?
684 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
685 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
686 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200688 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 {
690 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800691 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700692 old_throw_this_object.Assign(old_throw_location.GetThis());
693 old_throw_method.Assign(old_throw_location.GetMethod());
694 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800695 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200696 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800697 soa.Self()->ClearException();
698 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800699 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700700 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700701 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
702 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800703 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700704 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700705 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700706 } else {
707 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800709 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700710 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800711 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700712 }
713 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700714 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800715 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700716
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700717 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200718 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700720
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800723 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 }
726
Ian Rogers25e8b912012-09-07 11:31:36 -0700727 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 LOG(FATAL) << "JNI FatalError called: " << msg;
729 }
730
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700731 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700732 // TODO: SOA may not be necessary but I do it to please lock annotations.
733 ScopedObjectAccess soa(env);
734 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700735 return JNI_ERR;
736 }
Ian Rogersef28b142012-11-30 14:22:18 -0800737 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 return JNI_OK;
739 }
740
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700741 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800743 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700744 soa.Env()->PopFrame();
745 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 }
747
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700748 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700749 // TODO: SOA may not be necessary but I do it to please lock annotations.
750 ScopedObjectAccess soa(env);
751 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700752 }
753
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700755 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800756 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800757 // Check for null after decoding the object to handle cleared weak globals.
758 if (decoded_obj == nullptr) {
759 return nullptr;
760 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700761 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700763 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 return reinterpret_cast<jobject>(ref);
766 }
767
768 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800769 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 return;
771 }
Ian Rogersef28b142012-11-30 14:22:18 -0800772 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800774 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700775 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700776
777 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
778 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
779 << "failed to find entry";
780 }
781 }
782
783 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700784 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800785 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 }
787
788 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700789 if (obj != nullptr) {
790 ScopedObjectAccess soa(env);
791 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 }
793 }
794
795 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700796 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800797 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800798 // Check for null after decoding the object to handle cleared weak globals.
799 if (decoded_obj == nullptr) {
800 return nullptr;
801 }
802 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 }
804
805 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800806 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 return;
808 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700809 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800810 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700811
Ian Rogersef28b142012-11-30 14:22:18 -0800812 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 if (!locals.Remove(cookie, obj)) {
814 // Attempting to delete a local reference that is not in the
815 // topmost local reference frame is a no-op. DeleteLocalRef returns
816 // void and doesn't throw any exceptions, but we should probably
817 // complain about it so the user will notice that things aren't
818 // going quite the way they expect.
819 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
820 << "failed to find entry";
821 }
822 }
823
824 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700825 if (obj1 == obj2) {
826 return JNI_TRUE;
827 } else {
828 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800829 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
830 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700831 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 }
833
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700834 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700835 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800837 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800838 if (c == nullptr) {
839 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700840 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700841 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 }
843
Ian Rogersbc939662013-08-15 10:26:54 -0700844 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700846 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700847 CHECK_NON_NULL_ARGUMENT(java_class);
848 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700849 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 va_end(args);
851 return result;
852 }
853
Elliott Hughes72025e52011-08-23 17:50:30 -0700854 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700855 CHECK_NON_NULL_ARGUMENT(java_class);
856 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700857 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800858 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800859 if (c == nullptr) {
860 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700861 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800862 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800863 if (result == nullptr) {
864 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700865 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700866 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800868 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800869 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700870 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800871 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700875 CHECK_NON_NULL_ARGUMENT(java_class);
876 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800878 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800879 if (c == nullptr) {
880 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700881 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800882 mirror::Object* result = c->AllocObject(soa.Self());
883 if (result == nullptr) {
884 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700885 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800888 if (soa.Self()->IsExceptionPending()) {
889 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700890 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800891 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 }
893
Ian Rogersbc939662013-08-15 10:26:54 -0700894 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700895 CHECK_NON_NULL_ARGUMENT(java_class);
896 CHECK_NON_NULL_ARGUMENT(name);
897 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700899 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Ian Rogersbc939662013-08-15 10:26:54 -0700902 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
903 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700904 CHECK_NON_NULL_ARGUMENT(java_class);
905 CHECK_NON_NULL_ARGUMENT(name);
906 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700907 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700908 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 va_list ap;
913 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700914 CHECK_NON_NULL_ARGUMENT(obj);
915 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700916 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700923 CHECK_NON_NULL_ARGUMENT(obj);
924 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(env);
926 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
927 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700931 CHECK_NON_NULL_ARGUMENT(obj);
932 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700934 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
935 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 }
938
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 va_list ap;
941 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700942 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
943 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700944 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700947 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700951 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
952 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
954 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700958 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
959 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700961 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
962 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_list ap;
967 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700968 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
969 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700970 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700973 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700977 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
978 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 ScopedObjectAccess soa(env);
980 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
985 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700987 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
988 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_list ap;
993 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
995 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700996 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700999 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001003 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 ScopedObjectAccess soa(env);
1006 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1011 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001013 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1014 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_list ap;
1019 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001020 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1021 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001022 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001025 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001029 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1030 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 ScopedObjectAccess soa(env);
1032 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001039 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1040 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_list ap;
1045 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001046 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1047 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001048 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001051 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001055 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1056 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 ScopedObjectAccess soa(env);
1058 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1063 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001065 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1066 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_list ap;
1071 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001072 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1073 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001074 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001075 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001077 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001081 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1082 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083 ScopedObjectAccess soa(env);
1084 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001090 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001091 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1092 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_list ap;
1097 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1099 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001100 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001101 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001103 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001107 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1108 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
1110 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001117 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1118 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_list ap;
1123 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001124 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1125 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001126 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001129 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001133 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ScopedObjectAccess soa(env);
1136 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1141 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001143 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1144 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_list ap;
1149 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001150 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1151 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001152 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001153 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001158 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1159 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(env);
1161 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001165 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1166 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001168 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001171 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001174 CHECK_NON_NULL_ARGUMENT(obj);
1175 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001176 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1178 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 va_end(ap);
1180 return local_result;
1181 }
1182
Ian Rogersbc939662013-08-15 10:26:54 -07001183 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1184 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001185 CHECK_NON_NULL_ARGUMENT(obj);
1186 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 ScopedObjectAccess soa(env);
1188 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1189 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Ian Rogersbc939662013-08-15 10:26:54 -07001192 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1193 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001194 CHECK_NON_NULL_ARGUMENT(obj);
1195 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001197 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Ian Rogersbc939662013-08-15 10:26:54 -07001201 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1202 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001205 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1206 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001207 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001210 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Ian Rogersbc939662013-08-15 10:26:54 -07001213 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1214 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001215 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1216 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
1218 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Ian Rogersbc939662013-08-15 10:26:54 -07001221 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1222 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001223 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1224 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001226 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001232 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1233 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001234 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001237 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Ian Rogersbc939662013-08-15 10:26:54 -07001240 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1241 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001242 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1243 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 ScopedObjectAccess soa(env);
1245 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Ian Rogersbc939662013-08-15 10:26:54 -07001248 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1249 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001250 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1251 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001253 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001256 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001259 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1260 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001261 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001264 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
Ian Rogersbc939662013-08-15 10:26:54 -07001267 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1268 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001269 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1270 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001271 ScopedObjectAccess soa(env);
1272 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
Ian Rogersbc939662013-08-15 10:26:54 -07001275 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1276 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001277 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1278 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001279 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001280 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001283 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001286 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1287 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001288 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001291 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Ian Rogersbc939662013-08-15 10:26:54 -07001294 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1295 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001296 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1297 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001298 ScopedObjectAccess soa(env);
1299 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Ian Rogersbc939662013-08-15 10:26:54 -07001302 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1303 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001304 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1305 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001306 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001307 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001313 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1314 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001315 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001316 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001318 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Ian Rogersbc939662013-08-15 10:26:54 -07001321 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1322 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001323 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1324 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001325 ScopedObjectAccess soa(env);
1326 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Ian Rogersbc939662013-08-15 10:26:54 -07001329 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1330 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001331 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1332 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001333 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001334 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001340 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1341 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001342 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001343 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001345 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
Ian Rogersbc939662013-08-15 10:26:54 -07001348 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1349 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001350 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1351 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001352 ScopedObjectAccess soa(env);
1353 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Ian Rogersbc939662013-08-15 10:26:54 -07001356 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1357 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001358 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1359 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001360 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001361 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001364 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001367 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1368 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001369 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001370 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001372 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Ian Rogersbc939662013-08-15 10:26:54 -07001375 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1376 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001377 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1378 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001379 ScopedObjectAccess soa(env);
1380 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Ian Rogersbc939662013-08-15 10:26:54 -07001383 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1384 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001385 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1386 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001387 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001388 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001393 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001394 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1395 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001396 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001397 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001399 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Ian Rogersbc939662013-08-15 10:26:54 -07001402 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1403 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001404 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1405 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001406 ScopedObjectAccess soa(env);
1407 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
Ian Rogersbc939662013-08-15 10:26:54 -07001410 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1411 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001412 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1413 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001414 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001415 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001418 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001420 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001421 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1422 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001423 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001424 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 va_end(ap);
1426 }
1427
Brian Carlstromea46f952013-07-30 01:26:50 -07001428 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1429 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001430 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1431 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 ScopedObjectAccess soa(env);
1433 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Ian Rogersbc939662013-08-15 10:26:54 -07001436 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1437 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001438 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1439 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001440 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001441 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Ian Rogersbc939662013-08-15 10:26:54 -07001444 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001445 CHECK_NON_NULL_ARGUMENT(java_class);
1446 CHECK_NON_NULL_ARGUMENT(name);
1447 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001449 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001451
Ian Rogersbc939662013-08-15 10:26:54 -07001452 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1453 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001454 CHECK_NON_NULL_ARGUMENT(java_class);
1455 CHECK_NON_NULL_ARGUMENT(name);
1456 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001458 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001460
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001461 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001462 CHECK_NON_NULL_ARGUMENT(obj);
1463 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001465 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1466 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001471 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001472 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001473 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001474 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001477 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001478 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1479 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001480 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001481 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1482 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1483 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001484 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001488 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001490 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1491 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001492 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001496 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1497 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001499 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1500 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001501 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001503#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001504 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001505 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001506 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001507 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001508
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001510 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1511 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001512 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001513 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1514 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001515 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001517#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001518 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001519 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001520 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001521 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001522
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001524 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001528 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001532 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001536 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001540 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001544 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001548 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001552 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001555 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001556 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001560 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001564 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001567 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001568 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001571 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001572 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001576 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577 }
1578
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001579 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001580 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 }
1582
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001583 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001584 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 }
1586
1587 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001588 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 }
1590
1591 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001592 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 }
1594
1595 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001596 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 }
1598
1599 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001600 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 }
1602
1603 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001604 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 }
1606
1607 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001608 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 }
1610
1611 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001612 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001613 }
1614
1615 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001616 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001617 }
1618
1619 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001620 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001621 }
1622
1623 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001624 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001625 }
1626
1627 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001628 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001629 }
1630
1631 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001632 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001633 }
1634
1635 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001636 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001637 }
1638
1639 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001640 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001641 }
1642
1643 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001644 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001645 }
1646
1647 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001648 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001654 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001656 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 va_end(ap);
1659 return local_result;
1660 }
1661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001662 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001663 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001665 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001666 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001670 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001671 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001672 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001679 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001680 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001681 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001683 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001686 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001687 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001689 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001692 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001693 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001695 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001701 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001702 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001703 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001705 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001708 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001709 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001711 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001714 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001715 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001716 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001717 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001723 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001724 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001725 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001727 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001731 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001733 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001737 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001739 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001745 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001746 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001747 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001749 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001753 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001755 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001759 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001760 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001761 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001764 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001767 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001768 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001769 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001771 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001774 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001775 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001776 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001777 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 }
1779
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001780 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001781 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001782 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001783 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001786 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001788 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001789 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001790 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001791 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001793 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001797 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001799 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001802 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001803 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001805 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001808 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001810 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001811 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001812 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001813 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001815 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001818 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001819 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001820 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001821 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001824 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001825 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001826 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001827 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001830 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001832 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001833 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001834 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001835 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001837 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001840 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001841 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001843 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001846 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001847 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001848 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001849 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001852 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001854 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001855 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 va_end(ap);
1859 }
1860
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001861 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001862 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001863 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001864 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001867 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001868 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001869 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001870 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001874 if (UNLIKELY(char_count < 0)) {
1875 JniAbortF("NewString", "char_count < 0: %d", char_count);
1876 return nullptr;
1877 }
1878 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1879 JniAbortF("NewString", "chars == null && char_count > 0");
1880 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001881 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001882 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001883 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
1887 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001888 if (utf == nullptr) {
1889 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001890 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001892 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
Elliott Hughes814e4032011-08-23 12:07:56 -07001896 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001897 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001899 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001900 }
1901
1902 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001903 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001904 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001905 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001906 }
1907
Ian Rogersbc939662013-08-15 10:26:54 -07001908 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1909 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001910 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001912 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001913 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001914 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001915 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001916 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001917 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1918 memcpy(buf, chars + start, length * sizeof(jchar));
1919 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001920 }
1921
Ian Rogersbc939662013-08-15 10:26:54 -07001922 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1923 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001924 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001926 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001927 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001928 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001929 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001930 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001931 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1932 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1933 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001937 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001939 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1940 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001941 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001942 gc::Heap* heap = Runtime::Current()->GetHeap();
1943 if (heap->IsMovableObject(chars)) {
1944 if (is_copy != nullptr) {
1945 *is_copy = JNI_TRUE;
1946 }
1947 int32_t char_count = s->GetLength();
1948 int32_t offset = s->GetOffset();
1949 jchar* bytes = new jchar[char_count];
1950 for (int32_t i = 0; i < char_count; i++) {
1951 bytes[i] = chars->Get(i + offset);
1952 }
1953 return bytes;
1954 } else {
1955 if (is_copy != nullptr) {
1956 *is_copy = JNI_FALSE;
1957 }
1958 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001959 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001960 }
1961
Mathieu Chartier590fee92013-09-13 13:46:47 -07001962 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001963 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001964 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001965 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1966 mirror::CharArray* s_chars = s->GetCharArray();
1967 if (chars != (s_chars->GetData() + s->GetOffset())) {
1968 delete[] chars;
1969 }
1970 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001974 CHECK_NON_NULL_ARGUMENT(java_string);
1975 ScopedObjectAccess soa(env);
1976 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1977 mirror::CharArray* chars = s->GetCharArray();
1978 int32_t offset = s->GetOffset();
1979 PinPrimitiveArray(soa, chars);
1980 gc::Heap* heap = Runtime::Current()->GetHeap();
1981 if (heap->IsMovableObject(chars)) {
1982 StackHandleScope<1> hs(soa.Self());
1983 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1984 heap->IncrementDisableMovingGC(soa.Self());
1985 }
1986 if (is_copy != nullptr) {
1987 *is_copy = JNI_FALSE;
1988 }
1989 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes75770752011-08-24 17:52:38 -07001992 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001993 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1994 ScopedObjectAccess soa(env);
1995 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
1996 gc::Heap* heap = Runtime::Current()->GetHeap();
1997 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1998 mirror::CharArray* s_chars = s->GetCharArray();
1999 if (heap->IsMovableObject(s_chars)) {
2000 heap->DecrementDisableMovingGC(soa.Self());
2001 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002005 if (java_string == nullptr) {
2006 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002007 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002008 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002009 *is_copy = JNI_TRUE;
2010 }
Ian Rogersef28b142012-11-30 14:22:18 -08002011 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002012 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 size_t byte_count = s->GetUtfLength();
2014 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002015 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002016 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2017 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2018 bytes[byte_count] = '\0';
2019 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002020 }
2021
Elliott Hughes75770752011-08-24 17:52:38 -07002022 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002023 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002024 }
2025
Elliott Hughesbd935992011-08-22 11:59:34 -07002026 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002027 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002028 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002029 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002030 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002031 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2032 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002033 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002034 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes814e4032011-08-23 12:07:56 -07002037 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002038 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002039 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 mirror::ObjectArray<mirror::Object>* array =
2041 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002042 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Ian Rogersbc939662013-08-15 10:26:54 -07002045 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2046 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002047 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002048 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002049 mirror::ObjectArray<mirror::Object>* array =
2050 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2051 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002052 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
2055 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002056 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
2059 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002060 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
2063 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002064 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
2067 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002068 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
2071 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002072 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
2075 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002076 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
2079 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002080 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Ian Rogers1d99e452014-01-02 17:36:41 -08002083 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2084 jobject initial_element) {
2085 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002086 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002087 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002088 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002089 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090
2091 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002092 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002093 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002094 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002095 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002096 if (UNLIKELY(element_class->IsPrimitive())) {
2097 JniAbortF("NewObjectArray", "not an object type: %s",
2098 PrettyDescriptor(element_class).c_str());
2099 return nullptr;
2100 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002101 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002102 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002103 if (UNLIKELY(array_class == nullptr)) {
2104 return nullptr;
2105 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes75770752011-08-24 17:52:38 -07002108 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002109 mirror::ObjectArray<mirror::Object>* result =
2110 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002111 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002112 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002113 if (initial_object != nullptr) {
2114 mirror::Class* element_class = result->GetClass()->GetComponentType();
2115 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2116 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2117 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2118 PrettyDescriptor(element_class).c_str());
2119
2120 } else {
2121 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002122 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002123 }
2124 }
Elliott Hughes75770752011-08-24 17:52:38 -07002125 }
2126 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002127 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
2130 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002131 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Ian Rogersa15e67d2012-02-28 13:51:55 -08002134 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002135 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002136 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002137 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002138 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2139 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2140 PrettyDescriptor(array->GetClass()).c_str());
2141 return nullptr;
2142 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002143 gc::Heap* heap = Runtime::Current()->GetHeap();
2144 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002145 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002146 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002147 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002148 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002149 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002150 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002151 *is_copy = JNI_FALSE;
2152 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002153 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002154 }
2155
Ian Rogers2d10b202014-05-12 19:15:18 -07002156 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2157 jint mode) {
2158 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2159 ScopedObjectAccess soa(env);
2160 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2161 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2162 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2163 PrettyDescriptor(array->GetClass()).c_str());
2164 return;
2165 }
2166 const size_t component_size = array->GetClass()->GetComponentSize();
2167 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002168 }
2169
Elliott Hughes75770752011-08-24 17:52:38 -07002170 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002171 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 }
2173
Elliott Hughes75770752011-08-24 17:52:38 -07002174 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002175 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes75770752011-08-24 17:52:38 -07002178 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002179 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes75770752011-08-24 17:52:38 -07002182 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002183 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
2185
Elliott Hughes75770752011-08-24 17:52:38 -07002186 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002187 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
Elliott Hughes75770752011-08-24 17:52:38 -07002190 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002191 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughes75770752011-08-24 17:52:38 -07002194 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002195 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes75770752011-08-24 17:52:38 -07002198 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002199 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Mathieu Chartier590fee92013-09-13 13:46:47 -07002202 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2203 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002204 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2205 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Mathieu Chartier590fee92013-09-13 13:46:47 -07002208 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002209 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Mathieu Chartier590fee92013-09-13 13:46:47 -07002212 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002213 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Mathieu Chartier590fee92013-09-13 13:46:47 -07002216 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2217 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002218 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Mathieu Chartier590fee92013-09-13 13:46:47 -07002221 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2222 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002223 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Mathieu Chartier590fee92013-09-13 13:46:47 -07002226 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002227 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Mathieu Chartier590fee92013-09-13 13:46:47 -07002230 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002231 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Mathieu Chartier590fee92013-09-13 13:46:47 -07002234 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2235 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002236 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Ian Rogersbc939662013-08-15 10:26:54 -07002239 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2240 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002241 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002242 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
Ian Rogersbc939662013-08-15 10:26:54 -07002245 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2246 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002247 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Ian Rogersbc939662013-08-15 10:26:54 -07002250 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2251 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002252 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Ian Rogersbc939662013-08-15 10:26:54 -07002255 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2256 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002257 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002258 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Ian Rogersbc939662013-08-15 10:26:54 -07002261 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2262 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002263 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002264 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
Ian Rogersbc939662013-08-15 10:26:54 -07002267 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2268 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002269 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002270 }
2271
Ian Rogersbc939662013-08-15 10:26:54 -07002272 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2273 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002274 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Ian Rogersbc939662013-08-15 10:26:54 -07002277 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2278 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002279 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002280 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Ian Rogersbc939662013-08-15 10:26:54 -07002283 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2284 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002285 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002286 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 }
2288
Ian Rogersbc939662013-08-15 10:26:54 -07002289 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2290 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002291 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 }
2293
Ian Rogersbc939662013-08-15 10:26:54 -07002294 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2295 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002296 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 }
2298
Ian Rogersbc939662013-08-15 10:26:54 -07002299 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2300 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002301 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002302 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 }
2304
Ian Rogersbc939662013-08-15 10:26:54 -07002305 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2306 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002307 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002308 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002309 }
2310
Ian Rogersbc939662013-08-15 10:26:54 -07002311 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2312 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002313 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315
Ian Rogersbc939662013-08-15 10:26:54 -07002316 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2317 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002318 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320
Ian Rogersbc939662013-08-15 10:26:54 -07002321 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2322 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002323 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002324 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 }
2326
Ian Rogersbc939662013-08-15 10:26:54 -07002327 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2328 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002329 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2330 }
2331
Ian Rogersbc939662013-08-15 10:26:54 -07002332 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2333 jint method_count, bool return_errors) {
2334 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002335 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002336 return JNI_ERR; // Not reached.
2337 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002338 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002339 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002340 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002341 if (UNLIKELY(method_count == 0)) {
2342 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2343 << PrettyDescriptor(c);
2344 return JNI_OK;
2345 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002346 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002347 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002348 const char* name = methods[i].name;
2349 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002350 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002352 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002353 ++sig;
2354 }
2355
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002356 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2357 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002358 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002359 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002360 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002361 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002362 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002363 << PrettyDescriptor(c) << "." << name << sig << " in "
2364 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002365 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002366 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002367 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002368 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002369 << PrettyDescriptor(c) << "." << name << sig
2370 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002371 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002372 return JNI_ERR;
2373 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002374
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002375 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002376
Ian Rogers1eb512d2013-10-18 15:42:20 -07002377 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002378 }
2379 return JNI_OK;
2380 }
2381
Elliott Hughes5174fe62011-08-23 15:12:35 -07002382 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002383 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002385 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002386
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002387 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002388
Ian Rogers2d10b202014-05-12 19:15:18 -07002389 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002390 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002391 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002392 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002393 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002394 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002395 }
2396 }
2397 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002398 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002399 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002400 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002401 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002402 }
2403 }
2404
Ian Rogers2d10b202014-05-12 19:15:18 -07002405 if (unregistered_count == 0) {
2406 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2407 << PrettyDescriptor(c) << "' that contains no native methods";
2408 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002409 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002410 }
2411
Ian Rogers719d1a32014-03-06 12:13:39 -08002412 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002413 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002414 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002415 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2416 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002417 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002418 return JNI_ERR;
2419 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002420 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002421 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 }
2423
Ian Rogers719d1a32014-03-06 12:13:39 -08002424 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002425 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002426 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002427 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002428 o->MonitorExit(soa.Self());
2429 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002430 return JNI_ERR;
2431 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002432 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002433 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002434 }
2435
2436 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002437 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002438 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002439 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002440 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002441 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002442 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002443 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002444 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002445 }
2446
Elliott Hughescdf53122011-08-19 15:46:09 -07002447 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002448 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002449 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002450 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002451 if (address == nullptr && capacity != 0) {
2452 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002453 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002454
Ian Rogers936b37f2014-02-14 00:52:24 -08002455 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002456 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002457 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002458 jint capacity_arg = static_cast<jint>(capacity);
2459
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002460 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2461 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002462 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002463 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002464 }
2465
Elliott Hughesb465ab02011-08-24 11:21:21 -07002466 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002467 return reinterpret_cast<void*>(env->GetLongField(
2468 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002469 }
2470
Elliott Hughesb465ab02011-08-24 11:21:21 -07002471 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002472 return static_cast<jlong>(env->GetIntField(
2473 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002474 }
2475
Elliott Hughesb465ab02011-08-24 11:21:21 -07002476 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002477 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002478
2479 // Do we definitely know what kind of reference this is?
2480 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2481 IndirectRefKind kind = GetIndirectRefKind(ref);
2482 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002483 case kLocal: {
2484 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002485 // The local refs don't need a read barrier.
2486 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2487 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002488 return JNILocalRefType;
2489 }
2490 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002491 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002492 case kGlobal:
2493 return JNIGlobalRefType;
2494 case kWeakGlobal:
2495 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002496 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002497 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002498 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002499 return JNILocalRefType;
2500 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002501 return JNIInvalidRefType;
2502 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002503 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2504 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002505 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002506
2507 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002508 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2509 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002510 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002511 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002512 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2513 return JNI_ERR;
2514 }
2515 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002516 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002517 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2518 if (!okay) {
2519 soa.Self()->ThrowOutOfMemoryError(caller);
2520 }
2521 return okay ? JNI_OK : JNI_ERR;
2522 }
2523
2524 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002525 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002526 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002527 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002528 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002529 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002530 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002531 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002532 return soa.AddLocalReference<JniT>(result);
2533 }
2534
Ian Rogers2d10b202014-05-12 19:15:18 -07002535 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2536 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2537 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002538 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002539 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002540 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2541 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2542 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2543 PrettyDescriptor(array->GetClass()).c_str());
2544 return nullptr;
2545 }
2546 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2547 return array;
2548 }
2549
2550 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2551 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2552 CHECK_NON_NULL_ARGUMENT(java_array);
2553 ScopedObjectAccess soa(env);
2554 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2555 "GetArrayElements",
2556 "get");
2557 if (UNLIKELY(array == nullptr)) {
2558 return nullptr;
2559 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002560 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002561 // Only make a copy if necessary.
2562 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2563 if (is_copy != nullptr) {
2564 *is_copy = JNI_TRUE;
2565 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002566 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002567 size_t size = array->GetLength() * component_size;
2568 void* data = new uint64_t[RoundUp(size, 8) / 8];
2569 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002570 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002571 } else {
2572 if (is_copy != nullptr) {
2573 *is_copy = JNI_FALSE;
2574 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002575 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002577 }
2578
Ian Rogers2d10b202014-05-12 19:15:18 -07002579 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002580 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002581 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002582 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002583 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2584 "ReleaseArrayElements",
2585 "release");
2586 if (array == nullptr) {
2587 return;
2588 }
2589 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2590 }
2591
2592 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2593 size_t component_size, void* elements, jint mode)
2594 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002595 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002596 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002597 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002598 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002599 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2600 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002601 if (is_copy) {
2602 // Sanity check: If elements is not the same as the java array's data, it better not be a
2603 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2604 // copies we make?
2605 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2606 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2607 reinterpret_cast<void*>(elements), array_data);
2608 return;
2609 }
2610 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002611 // Don't need to copy if we had a direct pointer.
2612 if (mode != JNI_ABORT && is_copy) {
2613 memcpy(array_data, elements, bytes);
2614 }
2615 if (mode != JNI_COMMIT) {
2616 if (is_copy) {
2617 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002618 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002619 // Non copy to a movable object must means that we had disabled the moving GC.
2620 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002621 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002622 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002623 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002624 }
2625
Ian Rogers2d10b202014-05-12 19:15:18 -07002626 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2627 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2628 jsize start, jsize length, ElementT* buf) {
2629 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2630 ScopedObjectAccess soa(env);
2631 ArtArrayT* array =
2632 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2633 "GetPrimitiveArrayRegion",
2634 "get region of");
2635 if (array != nullptr) {
2636 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2637 ThrowAIOOBE(soa, array, start, length, "src");
2638 } else {
2639 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2640 ElementT* data = array->GetData();
2641 memcpy(buf, data + start, length * sizeof(ElementT));
2642 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002643 }
2644 }
2645
Ian Rogers2d10b202014-05-12 19:15:18 -07002646 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2647 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2648 jsize start, jsize length, const ElementT* buf) {
2649 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2650 ScopedObjectAccess soa(env);
2651 ArtArrayT* array =
2652 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2653 "SetPrimitiveArrayRegion",
2654 "set region of");
2655 if (array != nullptr) {
2656 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2657 ThrowAIOOBE(soa, array, start, length, "dst");
2658 } else {
2659 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2660 ElementT* data = array->GetData();
2661 memcpy(data + start, buf, length * sizeof(ElementT));
2662 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002663 }
2664 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002665};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002666
Elliott Hughes88c5c352012-03-15 18:49:48 -07002667const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002668 nullptr, // reserved0.
2669 nullptr, // reserved1.
2670 nullptr, // reserved2.
2671 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002672 JNI::GetVersion,
2673 JNI::DefineClass,
2674 JNI::FindClass,
2675 JNI::FromReflectedMethod,
2676 JNI::FromReflectedField,
2677 JNI::ToReflectedMethod,
2678 JNI::GetSuperclass,
2679 JNI::IsAssignableFrom,
2680 JNI::ToReflectedField,
2681 JNI::Throw,
2682 JNI::ThrowNew,
2683 JNI::ExceptionOccurred,
2684 JNI::ExceptionDescribe,
2685 JNI::ExceptionClear,
2686 JNI::FatalError,
2687 JNI::PushLocalFrame,
2688 JNI::PopLocalFrame,
2689 JNI::NewGlobalRef,
2690 JNI::DeleteGlobalRef,
2691 JNI::DeleteLocalRef,
2692 JNI::IsSameObject,
2693 JNI::NewLocalRef,
2694 JNI::EnsureLocalCapacity,
2695 JNI::AllocObject,
2696 JNI::NewObject,
2697 JNI::NewObjectV,
2698 JNI::NewObjectA,
2699 JNI::GetObjectClass,
2700 JNI::IsInstanceOf,
2701 JNI::GetMethodID,
2702 JNI::CallObjectMethod,
2703 JNI::CallObjectMethodV,
2704 JNI::CallObjectMethodA,
2705 JNI::CallBooleanMethod,
2706 JNI::CallBooleanMethodV,
2707 JNI::CallBooleanMethodA,
2708 JNI::CallByteMethod,
2709 JNI::CallByteMethodV,
2710 JNI::CallByteMethodA,
2711 JNI::CallCharMethod,
2712 JNI::CallCharMethodV,
2713 JNI::CallCharMethodA,
2714 JNI::CallShortMethod,
2715 JNI::CallShortMethodV,
2716 JNI::CallShortMethodA,
2717 JNI::CallIntMethod,
2718 JNI::CallIntMethodV,
2719 JNI::CallIntMethodA,
2720 JNI::CallLongMethod,
2721 JNI::CallLongMethodV,
2722 JNI::CallLongMethodA,
2723 JNI::CallFloatMethod,
2724 JNI::CallFloatMethodV,
2725 JNI::CallFloatMethodA,
2726 JNI::CallDoubleMethod,
2727 JNI::CallDoubleMethodV,
2728 JNI::CallDoubleMethodA,
2729 JNI::CallVoidMethod,
2730 JNI::CallVoidMethodV,
2731 JNI::CallVoidMethodA,
2732 JNI::CallNonvirtualObjectMethod,
2733 JNI::CallNonvirtualObjectMethodV,
2734 JNI::CallNonvirtualObjectMethodA,
2735 JNI::CallNonvirtualBooleanMethod,
2736 JNI::CallNonvirtualBooleanMethodV,
2737 JNI::CallNonvirtualBooleanMethodA,
2738 JNI::CallNonvirtualByteMethod,
2739 JNI::CallNonvirtualByteMethodV,
2740 JNI::CallNonvirtualByteMethodA,
2741 JNI::CallNonvirtualCharMethod,
2742 JNI::CallNonvirtualCharMethodV,
2743 JNI::CallNonvirtualCharMethodA,
2744 JNI::CallNonvirtualShortMethod,
2745 JNI::CallNonvirtualShortMethodV,
2746 JNI::CallNonvirtualShortMethodA,
2747 JNI::CallNonvirtualIntMethod,
2748 JNI::CallNonvirtualIntMethodV,
2749 JNI::CallNonvirtualIntMethodA,
2750 JNI::CallNonvirtualLongMethod,
2751 JNI::CallNonvirtualLongMethodV,
2752 JNI::CallNonvirtualLongMethodA,
2753 JNI::CallNonvirtualFloatMethod,
2754 JNI::CallNonvirtualFloatMethodV,
2755 JNI::CallNonvirtualFloatMethodA,
2756 JNI::CallNonvirtualDoubleMethod,
2757 JNI::CallNonvirtualDoubleMethodV,
2758 JNI::CallNonvirtualDoubleMethodA,
2759 JNI::CallNonvirtualVoidMethod,
2760 JNI::CallNonvirtualVoidMethodV,
2761 JNI::CallNonvirtualVoidMethodA,
2762 JNI::GetFieldID,
2763 JNI::GetObjectField,
2764 JNI::GetBooleanField,
2765 JNI::GetByteField,
2766 JNI::GetCharField,
2767 JNI::GetShortField,
2768 JNI::GetIntField,
2769 JNI::GetLongField,
2770 JNI::GetFloatField,
2771 JNI::GetDoubleField,
2772 JNI::SetObjectField,
2773 JNI::SetBooleanField,
2774 JNI::SetByteField,
2775 JNI::SetCharField,
2776 JNI::SetShortField,
2777 JNI::SetIntField,
2778 JNI::SetLongField,
2779 JNI::SetFloatField,
2780 JNI::SetDoubleField,
2781 JNI::GetStaticMethodID,
2782 JNI::CallStaticObjectMethod,
2783 JNI::CallStaticObjectMethodV,
2784 JNI::CallStaticObjectMethodA,
2785 JNI::CallStaticBooleanMethod,
2786 JNI::CallStaticBooleanMethodV,
2787 JNI::CallStaticBooleanMethodA,
2788 JNI::CallStaticByteMethod,
2789 JNI::CallStaticByteMethodV,
2790 JNI::CallStaticByteMethodA,
2791 JNI::CallStaticCharMethod,
2792 JNI::CallStaticCharMethodV,
2793 JNI::CallStaticCharMethodA,
2794 JNI::CallStaticShortMethod,
2795 JNI::CallStaticShortMethodV,
2796 JNI::CallStaticShortMethodA,
2797 JNI::CallStaticIntMethod,
2798 JNI::CallStaticIntMethodV,
2799 JNI::CallStaticIntMethodA,
2800 JNI::CallStaticLongMethod,
2801 JNI::CallStaticLongMethodV,
2802 JNI::CallStaticLongMethodA,
2803 JNI::CallStaticFloatMethod,
2804 JNI::CallStaticFloatMethodV,
2805 JNI::CallStaticFloatMethodA,
2806 JNI::CallStaticDoubleMethod,
2807 JNI::CallStaticDoubleMethodV,
2808 JNI::CallStaticDoubleMethodA,
2809 JNI::CallStaticVoidMethod,
2810 JNI::CallStaticVoidMethodV,
2811 JNI::CallStaticVoidMethodA,
2812 JNI::GetStaticFieldID,
2813 JNI::GetStaticObjectField,
2814 JNI::GetStaticBooleanField,
2815 JNI::GetStaticByteField,
2816 JNI::GetStaticCharField,
2817 JNI::GetStaticShortField,
2818 JNI::GetStaticIntField,
2819 JNI::GetStaticLongField,
2820 JNI::GetStaticFloatField,
2821 JNI::GetStaticDoubleField,
2822 JNI::SetStaticObjectField,
2823 JNI::SetStaticBooleanField,
2824 JNI::SetStaticByteField,
2825 JNI::SetStaticCharField,
2826 JNI::SetStaticShortField,
2827 JNI::SetStaticIntField,
2828 JNI::SetStaticLongField,
2829 JNI::SetStaticFloatField,
2830 JNI::SetStaticDoubleField,
2831 JNI::NewString,
2832 JNI::GetStringLength,
2833 JNI::GetStringChars,
2834 JNI::ReleaseStringChars,
2835 JNI::NewStringUTF,
2836 JNI::GetStringUTFLength,
2837 JNI::GetStringUTFChars,
2838 JNI::ReleaseStringUTFChars,
2839 JNI::GetArrayLength,
2840 JNI::NewObjectArray,
2841 JNI::GetObjectArrayElement,
2842 JNI::SetObjectArrayElement,
2843 JNI::NewBooleanArray,
2844 JNI::NewByteArray,
2845 JNI::NewCharArray,
2846 JNI::NewShortArray,
2847 JNI::NewIntArray,
2848 JNI::NewLongArray,
2849 JNI::NewFloatArray,
2850 JNI::NewDoubleArray,
2851 JNI::GetBooleanArrayElements,
2852 JNI::GetByteArrayElements,
2853 JNI::GetCharArrayElements,
2854 JNI::GetShortArrayElements,
2855 JNI::GetIntArrayElements,
2856 JNI::GetLongArrayElements,
2857 JNI::GetFloatArrayElements,
2858 JNI::GetDoubleArrayElements,
2859 JNI::ReleaseBooleanArrayElements,
2860 JNI::ReleaseByteArrayElements,
2861 JNI::ReleaseCharArrayElements,
2862 JNI::ReleaseShortArrayElements,
2863 JNI::ReleaseIntArrayElements,
2864 JNI::ReleaseLongArrayElements,
2865 JNI::ReleaseFloatArrayElements,
2866 JNI::ReleaseDoubleArrayElements,
2867 JNI::GetBooleanArrayRegion,
2868 JNI::GetByteArrayRegion,
2869 JNI::GetCharArrayRegion,
2870 JNI::GetShortArrayRegion,
2871 JNI::GetIntArrayRegion,
2872 JNI::GetLongArrayRegion,
2873 JNI::GetFloatArrayRegion,
2874 JNI::GetDoubleArrayRegion,
2875 JNI::SetBooleanArrayRegion,
2876 JNI::SetByteArrayRegion,
2877 JNI::SetCharArrayRegion,
2878 JNI::SetShortArrayRegion,
2879 JNI::SetIntArrayRegion,
2880 JNI::SetLongArrayRegion,
2881 JNI::SetFloatArrayRegion,
2882 JNI::SetDoubleArrayRegion,
2883 JNI::RegisterNatives,
2884 JNI::UnregisterNatives,
2885 JNI::MonitorEnter,
2886 JNI::MonitorExit,
2887 JNI::GetJavaVM,
2888 JNI::GetStringRegion,
2889 JNI::GetStringUTFRegion,
2890 JNI::GetPrimitiveArrayCritical,
2891 JNI::ReleasePrimitiveArrayCritical,
2892 JNI::GetStringCritical,
2893 JNI::ReleaseStringCritical,
2894 JNI::NewWeakGlobalRef,
2895 JNI::DeleteWeakGlobalRef,
2896 JNI::ExceptionCheck,
2897 JNI::NewDirectByteBuffer,
2898 JNI::GetDirectBufferAddress,
2899 JNI::GetDirectBufferCapacity,
2900 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002901};
2902
Elliott Hughes75770752011-08-24 17:52:38 -07002903JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002904 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002905 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002906 local_ref_cookie(IRT_FIRST_SEGMENT),
2907 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002908 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002909 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002910 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002911 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002912 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002913 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002914 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002915}
2916
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002917JNIEnvExt::~JNIEnvExt() {
2918}
2919
Mathieu Chartier590fee92013-09-13 13:46:47 -07002920jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2921 if (obj == nullptr) {
2922 return nullptr;
2923 }
2924 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2925}
2926
2927void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2928 if (obj != nullptr) {
2929 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2930 }
2931}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002932void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2933 check_jni = enabled;
2934 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002935}
2936
Elliott Hughes73e66f72012-05-09 09:34:45 -07002937void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2938 locals.Dump(os);
2939 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002940}
2941
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002942void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2943 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002944 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002945 stacked_local_ref_cookies.push_back(local_ref_cookie);
2946 local_ref_cookie = locals.GetSegmentState();
2947}
2948
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002949void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002950 locals.SetSegmentState(local_ref_cookie);
2951 local_ref_cookie = stacked_local_ref_cookies.back();
2952 stacked_local_ref_cookies.pop_back();
2953}
2954
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002955Offset JNIEnvExt::SegmentStateOffset() {
2956 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2957 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2958}
2959
Carl Shapiroea4dca82011-08-01 13:45:38 -07002960// JNI Invocation interface.
2961
Brian Carlstrombddf9762013-05-14 11:35:37 -07002962extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002963 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002964 if (IsBadJniVersion(args->version)) {
2965 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002966 return JNI_EVERSION;
2967 }
2968 Runtime::Options options;
2969 for (int i = 0; i < args->nOptions; ++i) {
2970 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002971 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002972 }
2973 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002974 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002975 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002976 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002977 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002978 bool started = runtime->Start();
2979 if (!started) {
2980 delete Thread::Current()->GetJniEnv();
2981 delete runtime->GetJavaVM();
2982 LOG(WARNING) << "CreateJavaVM failed";
2983 return JNI_ERR;
2984 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002985 *p_env = Thread::Current()->GetJniEnv();
2986 *p_vm = runtime->GetJavaVM();
2987 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002988}
2989
Elliott Hughesf2682d52011-08-15 16:37:04 -07002990extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002991 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002992 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002993 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002994 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002995 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002996 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002997 }
2998 return JNI_OK;
2999}
3000
3001// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003002extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003003 return JNI_ERR;
3004}
3005
Elliott Hughescdf53122011-08-19 15:46:09 -07003006class JII {
3007 public:
3008 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003009 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003010 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003011 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003012 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3013 delete raw_vm->runtime;
3014 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003015 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003016
Elliott Hughescdf53122011-08-19 15:46:09 -07003017 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003018 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003019 }
3020
3021 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003022 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003023 }
3024
3025 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003026 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003027 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003028 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003029 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3030 Runtime* runtime = raw_vm->runtime;
3031 runtime->DetachCurrentThread();
3032 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003033 }
3034
3035 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003036 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3037 // and unlike other calls that take a JNI version doesn't care if you supply
3038 // JNI_VERSION_1_1, which we don't otherwise support.
3039 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003040 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003041 return JNI_EVERSION;
3042 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003043 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003044 return JNI_ERR;
3045 }
3046 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003047 if (thread == nullptr) {
3048 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003049 return JNI_EDETACHED;
3050 }
3051 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003052 return JNI_OK;
3053 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003054};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003055
Elliott Hughes88c5c352012-03-15 18:49:48 -07003056const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003057 nullptr, // reserved0
3058 nullptr, // reserved1
3059 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003060 JII::DestroyJavaVM,
3061 JII::AttachCurrentThread,
3062 JII::DetachCurrentThread,
3063 JII::GetEnv,
3064 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003065};
3066
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003067JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003068 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003069 check_jni_abort_hook(nullptr),
3070 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003071 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003072 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003073 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003074 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003075 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003076 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003077 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003078 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003079 libraries(new Libraries),
3080 weak_globals_lock_("JNI weak global reference table lock"),
3081 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3082 allow_new_weak_globals_(true),
3083 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003084 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003085 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003086 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003087 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003088}
3089
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003090JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003091 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003092}
3093
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003094jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3095 if (obj == nullptr) {
3096 return nullptr;
3097 }
3098 MutexLock mu(self, weak_globals_lock_);
3099 while (UNLIKELY(!allow_new_weak_globals_)) {
3100 weak_globals_add_condition_.WaitHoldingLocks(self);
3101 }
3102 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3103 return reinterpret_cast<jweak>(ref);
3104}
3105
3106void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3107 MutexLock mu(self, weak_globals_lock_);
3108 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3109 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3110 << "failed to find entry";
3111 }
3112}
3113
Elliott Hughes88c5c352012-03-15 18:49:48 -07003114void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3115 check_jni = enabled;
3116 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003117}
3118
Elliott Hughesae80b492012-04-24 10:43:17 -07003119void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3120 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3121 if (force_copy) {
3122 os << " (with forcecopy)";
3123 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003124 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003125 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003126 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003127 os << "; pins=" << pin_table.Size();
3128 }
3129 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003130 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003131 os << "; globals=" << globals.Capacity();
3132 }
3133 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003134 MutexLock mu(self, weak_globals_lock_);
3135 if (weak_globals_.Capacity() > 0) {
3136 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003137 }
3138 }
3139 os << '\n';
3140
3141 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003142 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003143 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3144 }
3145}
3146
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003147void JavaVMExt::DisallowNewWeakGlobals() {
3148 MutexLock mu(Thread::Current(), weak_globals_lock_);
3149 allow_new_weak_globals_ = false;
3150}
3151
3152void JavaVMExt::AllowNewWeakGlobals() {
3153 Thread* self = Thread::Current();
3154 MutexLock mu(self, weak_globals_lock_);
3155 allow_new_weak_globals_ = true;
3156 weak_globals_add_condition_.Broadcast(self);
3157}
3158
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003159mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3160 MutexLock mu(self, weak_globals_lock_);
3161 while (UNLIKELY(!allow_new_weak_globals_)) {
3162 weak_globals_add_condition_.WaitHoldingLocks(self);
3163 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003164 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003165}
3166
Elliott Hughes73e66f72012-05-09 09:34:45 -07003167void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003168 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003169 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003170 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003171 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003172 }
3173 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003174 MutexLock mu(self, weak_globals_lock_);
3175 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003176 }
3177 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003178 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003179 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003180 }
3181}
3182
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003183bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003184 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003185 std::string* detail) {
3186 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003187
3188 // See if we've already loaded this library. If we have, and the class loader
3189 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003190 // TODO: for better results we should canonicalize the pathname (or even compare
3191 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003192 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003193 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003194 {
3195 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003196 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003197 library = libraries->Get(path);
3198 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003199 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003200 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003201 // The library will be associated with class_loader. The JNI
3202 // spec says we can't load the same library into more than one
3203 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003204 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003205 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003206 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003207 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003208 return false;
3209 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003210 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003211 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003212 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003213 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003214 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003215 return false;
3216 }
3217 return true;
3218 }
3219
3220 // Open the shared library. Because we're using a full path, the system
3221 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3222 // resolve this library's dependencies though.)
3223
3224 // Failures here are expected when java.library.path has several entries
3225 // and we have to hunt for the lib.
3226
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003227 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3228 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3229 // dlopen) becomes zero from dlclose.
3230
Elliott Hughescdf53122011-08-19 15:46:09 -07003231 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003232 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003233 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003234 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003235 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003236
Elliott Hughes84b2f142012-09-27 09:16:28 -07003237 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003238
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003239 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003240 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003241 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003242 return false;
3243 }
3244
3245 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003246 // TODO: move the locking (and more of this logic) into Libraries.
3247 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003248 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003249 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003250 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003251 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003252 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003253 libraries->Put(path, library);
3254 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003255 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003256 }
3257 if (!created_library) {
3258 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003259 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003260 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003261 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003262
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003263 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003264 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003265
Elliott Hughes79353722013-08-02 16:52:18 -07003266 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003267 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003268 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003269 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003270 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003271 } else {
3272 // Call JNI_OnLoad. We have to override the current class
3273 // loader, which will always be "null" since the stuff at the
3274 // top of the stack is around Runtime.loadLibrary(). (See
3275 // the comments in the JNI FindClass function.)
3276 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3277 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003278 StackHandleScope<1> hs(self);
3279 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3280 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003281
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003282 int version = 0;
3283 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003284 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003285 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003286 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003287 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003288
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003289 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003290
Elliott Hughes79353722013-08-02 16:52:18 -07003291 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003292 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003293 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003294 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003295 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003296 // It's unwise to call dlclose() here, but we can mark it
3297 // as bad and ensure that future load attempts will fail.
3298 // We don't know how far JNI_OnLoad got, so there could
3299 // be some partially-initialized stuff accessible through
3300 // newly-registered native method calls. We could try to
3301 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003302 } else {
3303 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003304 }
Elliott Hughes79353722013-08-02 16:52:18 -07003305 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003306 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003307 }
3308
Elliott Hughes79353722013-08-02 16:52:18 -07003309 library->SetResult(was_successful);
3310 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003311}
3312
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003313void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003314 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003315 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003316 // If this is a static method, it could be called before the class
3317 // has been initialized.
3318 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003319 c = EnsureInitialized(Thread::Current(), c);
3320 if (c == nullptr) {
3321 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003322 }
3323 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003324 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003325 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003326 std::string detail;
3327 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003328 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003329 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003330 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003331 native_method = libraries->FindNativeMethod(m, detail);
3332 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003333 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003334 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003335 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3336 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003337 }
3338 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003339}
3340
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003341void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003342 MutexLock mu(Thread::Current(), weak_globals_lock_);
3343 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003344 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003345 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003346 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003347 if (new_obj == nullptr) {
3348 new_obj = kClearedJniWeakGlobal;
3349 }
3350 *entry = new_obj;
3351 }
3352}
3353
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003354void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003355 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003356 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003357 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003358 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003359 }
3360 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003361 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003362 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003363 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003364 {
3365 MutexLock mu(self, libraries_lock);
3366 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003367 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003368 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003369 // The weak_globals table is visited by the GC itself (because it mutates the table).
3370}
3371
Elliott Hughesc8fece32013-01-02 11:27:23 -08003372void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003373 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003374 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003375 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003376 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3377 }
3378 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3379}
3380
Ian Rogersdf20fe02011-07-20 20:34:16 -07003381} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003382
3383std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3384 switch (rhs) {
3385 case JNIInvalidRefType:
3386 os << "JNIInvalidRefType";
3387 return os;
3388 case JNILocalRefType:
3389 os << "JNILocalRefType";
3390 return os;
3391 case JNIGlobalRefType:
3392 os << "JNIGlobalRefType";
3393 return os;
3394 case JNIWeakGlobalRefType:
3395 os << "JNIWeakGlobalRefType";
3396 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003397 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003398 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003399 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003400 }
3401}