blob: fc5d5905cbc0234ccedf9a8b9d401021d1d1c081 [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
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800362 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700363 return class_loader_;
364 }
365
366 std::string GetPath() {
367 return path_;
368 }
369
370 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700371 * Check the result of an earlier call to JNI_OnLoad on this library.
372 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700373 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 bool CheckOnLoadResult()
375 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700376 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700377 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
379 bool okay;
380 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700381 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700382
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700383 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
385 // caller can continue.
386 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
387 okay = true;
388 } else {
389 while (jni_on_load_result_ == kPending) {
390 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700391 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 okay = (jni_on_load_result_ == kOkay);
395 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
396 << (okay ? "succeeded" : "failed") << "]";
397 }
398 }
399 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700400 return okay;
401 }
402
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700404 Thread* self = Thread::Current();
405 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700406
Elliott Hughes79082e32011-08-25 12:07:32 -0700407 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700408 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700409
410 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700411 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700412 }
413
414 void* FindSymbol(const std::string& symbol_name) {
415 return dlsym(handle_, symbol_name.c_str());
416 }
417
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800418 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800419 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800420 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800421 }
422 }
423
Elliott Hughes79082e32011-08-25 12:07:32 -0700424 private:
425 enum JNI_OnLoadState {
426 kPending,
427 kFailed,
428 kOkay,
429 };
430
431 // Path to library "/system/lib/libjni.so".
432 std::string path_;
433
434 // The void* returned by dlopen(3).
435 void* handle_;
436
437 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800438 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700439
440 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700441 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700442 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700443 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700445 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700446 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700447 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700448};
449
Elliott Hughes79082e32011-08-25 12:07:32 -0700450// This exists mainly to keep implementation details out of the header file.
451class Libraries {
452 public:
453 Libraries() {
454 }
455
456 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700457 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 }
459
Elliott Hughesae80b492012-04-24 10:43:17 -0700460 void Dump(std::ostream& os) const {
461 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700462 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700463 if (!first) {
464 os << ' ';
465 }
466 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700467 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700468 }
469 }
470
471 size_t size() const {
472 return libraries_.size();
473 }
474
Elliott Hughes79082e32011-08-25 12:07:32 -0700475 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700476 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800477 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700478 }
479
480 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700481 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700482 }
483
484 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800485 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 std::string jni_short_name(JniShortName(m));
488 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800489 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700490 for (const auto& lib : libraries_) {
491 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 if (library->GetClassLoader() != declaring_class_loader) {
493 // We only search libraries loaded by the appropriate ClassLoader.
494 continue;
495 }
496 // Try the short name then the long name...
497 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800498 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 fn = library->FindSymbol(jni_long_name);
500 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800501 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800502 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
503 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 return fn;
505 }
506 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700507 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700508 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700509 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700510 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800511 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700512 }
513
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800514 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800515 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800516 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800517 }
518 }
519
Elliott Hughes79082e32011-08-25 12:07:32 -0700520 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700521 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700522};
523
Ian Rogers2d10b202014-05-12 19:15:18 -0700524#define CHECK_NON_NULL_ARGUMENT(value) \
525 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700526
Ian Rogers2d10b202014-05-12 19:15:18 -0700527#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
528 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
529
530#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
531 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
532
533#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
534 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
535
536#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800537 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700538 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700539 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700540 }
541
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700542#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800543 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700544 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700545 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700546 }
547
Elliott Hughescdf53122011-08-19 15:46:09 -0700548class JNI {
549 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700550 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700551 return JNI_VERSION_1_6;
552 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700553
Ian Rogers25e8b912012-09-07 11:31:36 -0700554 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700555 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800556 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700557 }
558
Elliott Hughescdf53122011-08-19 15:46:09 -0700559 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700560 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700561 Runtime* runtime = Runtime::Current();
562 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700563 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800565 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700566 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700567 StackHandleScope<1> hs(soa.Self());
568 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800569 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700570 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800571 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700572 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700574 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700575
Ian Rogers62f05122014-03-21 11:21:29 -0700576 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700577 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700578 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700579 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700580 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700581
Ian Rogers62f05122014-03-21 11:21:29 -0700582 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700583 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700584 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700585 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700587
Elliott Hughescdf53122011-08-19 15:46:09 -0700588 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700589 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 mirror::ArtMethod* m = soa.DecodeMethod(mid);
592 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 jobject art_method = soa.AddLocalReference<jobject>(m);
594 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
595 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800596 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700597 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800598 SetObjectField(env, reflect_method,
599 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700600 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700601 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700602
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700604 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800606 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700607 jobject art_field = soa.AddLocalReference<jobject>(f);
608 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
609 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800610 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 SetObjectField(env, reflect_field,
613 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700615 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Elliott Hughes37f7a402011-08-22 18:56:01 -0700617 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700618 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800620 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700622 }
623
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700624 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700625 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800627 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700630
Elliott Hughes37f7a402011-08-22 18:56:01 -0700631 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700632 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
633 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800635 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
636 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700637 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700639
Elliott Hughese84278b2012-03-22 10:06:53 -0700640 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700641 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700643 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700644 return JNI_TRUE;
645 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800647 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
648 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700649 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800655 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
656 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700657 return JNI_ERR;
658 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800659 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
660 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 return JNI_OK;
662 }
663
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700664 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700665 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800666 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 }
668
669 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700670 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 }
672
673 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700674 ScopedObjectAccess soa(env);
675 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 }
677
678 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700680
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700681 StackHandleScope<3> hs(soa.Self());
682 // TODO: Use nullptr instead of null handles?
683 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
684 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
685 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200687 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 {
689 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800690 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700691 old_throw_this_object.Assign(old_throw_location.GetThis());
692 old_throw_method.Assign(old_throw_location.GetMethod());
693 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800694 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200695 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 soa.Self()->ClearException();
697 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800698 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700699 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700700 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
701 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800702 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700703 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700704 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 } else {
706 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800707 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800708 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700709 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800710 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700711 }
712 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700713 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800714 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700715
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700716 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200717 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700719
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800722 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
725
Ian Rogers25e8b912012-09-07 11:31:36 -0700726 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 LOG(FATAL) << "JNI FatalError called: " << msg;
728 }
729
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700730 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700731 // TODO: SOA may not be necessary but I do it to please lock annotations.
732 ScopedObjectAccess soa(env);
733 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700734 return JNI_ERR;
735 }
Ian Rogersef28b142012-11-30 14:22:18 -0800736 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700737 return JNI_OK;
738 }
739
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700740 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800742 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 soa.Env()->PopFrame();
744 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
746
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700747 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700748 // TODO: SOA may not be necessary but I do it to please lock annotations.
749 ScopedObjectAccess soa(env);
750 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700751 }
752
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700754 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800755 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800756 // Check for null after decoding the object to handle cleared weak globals.
757 if (decoded_obj == nullptr) {
758 return nullptr;
759 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700762 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 return reinterpret_cast<jobject>(ref);
765 }
766
767 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800768 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 return;
770 }
Ian Rogersef28b142012-11-30 14:22:18 -0800771 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800773 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700774 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700775
776 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
777 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
778 << "failed to find entry";
779 }
780 }
781
782 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800784 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 }
786
787 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700788 if (obj != nullptr) {
789 ScopedObjectAccess soa(env);
790 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 }
792 }
793
794 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800796 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800797 // Check for null after decoding the object to handle cleared weak globals.
798 if (decoded_obj == nullptr) {
799 return nullptr;
800 }
801 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 }
803
804 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800805 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 return;
807 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700808 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800809 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700810
Ian Rogersef28b142012-11-30 14:22:18 -0800811 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 if (!locals.Remove(cookie, obj)) {
813 // Attempting to delete a local reference that is not in the
814 // topmost local reference frame is a no-op. DeleteLocalRef returns
815 // void and doesn't throw any exceptions, but we should probably
816 // complain about it so the user will notice that things aren't
817 // going quite the way they expect.
818 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
819 << "failed to find entry";
820 }
821 }
822
823 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700824 if (obj1 == obj2) {
825 return JNI_TRUE;
826 } else {
827 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800828 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
829 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700830 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 }
832
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700833 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700834 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800836 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800837 if (c == nullptr) {
838 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700839 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700840 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 }
842
Ian Rogersbc939662013-08-15 10:26:54 -0700843 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700846 CHECK_NON_NULL_ARGUMENT(java_class);
847 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700848 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 va_end(args);
850 return result;
851 }
852
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700854 CHECK_NON_NULL_ARGUMENT(java_class);
855 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800857 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800858 if (c == nullptr) {
859 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700860 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800861 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800862 if (result == nullptr) {
863 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700864 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700866 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800867 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800868 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700869 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800870 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 }
872
Elliott Hughes72025e52011-08-23 17:50:30 -0700873 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700874 CHECK_NON_NULL_ARGUMENT(java_class);
875 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700876 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800877 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800878 if (c == nullptr) {
879 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700880 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800881 mirror::Object* result = c->AllocObject(soa.Self());
882 if (result == nullptr) {
883 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700884 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700885 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800887 if (soa.Self()->IsExceptionPending()) {
888 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700889 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800890 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 }
892
Ian Rogersbc939662013-08-15 10:26:54 -0700893 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700894 CHECK_NON_NULL_ARGUMENT(java_class);
895 CHECK_NON_NULL_ARGUMENT(name);
896 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700897 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700898 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Ian Rogersbc939662013-08-15 10:26:54 -0700901 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
902 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700903 CHECK_NON_NULL_ARGUMENT(java_class);
904 CHECK_NON_NULL_ARGUMENT(name);
905 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700907 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 }
909
Elliott Hughes72025e52011-08-23 17:50:30 -0700910 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 va_list ap;
912 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700913 CHECK_NON_NULL_ARGUMENT(obj);
914 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700915 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700916 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700922 CHECK_NON_NULL_ARGUMENT(obj);
923 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 ScopedObjectAccess soa(env);
925 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
926 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 }
928
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700930 CHECK_NON_NULL_ARGUMENT(obj);
931 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700933 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
934 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 }
937
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_list ap;
940 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700941 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
942 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700943 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700946 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 }
948
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700950 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
951 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 ScopedObjectAccess soa(env);
953 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700957 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
958 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700960 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
961 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_list ap;
966 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700967 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
968 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700969 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700972 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700976 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
977 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 ScopedObjectAccess soa(env);
979 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700983 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700986 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
987 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700993 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700995 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700998 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001002 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1003 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001009 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001012 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1013 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_list ap;
1018 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001019 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1020 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001021 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001024 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001028 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1029 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 ScopedObjectAccess soa(env);
1031 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001035 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001038 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1039 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_list ap;
1044 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001045 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1046 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001047 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001050 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001054 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1055 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 ScopedObjectAccess soa(env);
1057 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001061 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001064 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1065 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_list ap;
1070 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001071 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1072 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001073 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001076 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001080 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1081 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 ScopedObjectAccess soa(env);
1083 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001087 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001090 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1091 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_list ap;
1096 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001097 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001099 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001102 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001106 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1107 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 ScopedObjectAccess soa(env);
1109 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001113 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001116 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1117 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_list ap;
1122 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001123 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1124 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001125 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001128 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001132 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1133 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 ScopedObjectAccess soa(env);
1135 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001139 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001142 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1143 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_list ap;
1148 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001149 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1150 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001151 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001152 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001157 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1158 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159 ScopedObjectAccess soa(env);
1160 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001164 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1165 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001166 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001167 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001170 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001173 CHECK_NON_NULL_ARGUMENT(obj);
1174 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001175 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1177 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 va_end(ap);
1179 return local_result;
1180 }
1181
Ian Rogersbc939662013-08-15 10:26:54 -07001182 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1183 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001184 CHECK_NON_NULL_ARGUMENT(obj);
1185 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 ScopedObjectAccess soa(env);
1187 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1188 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Ian Rogersbc939662013-08-15 10:26:54 -07001191 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1192 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001193 CHECK_NON_NULL_ARGUMENT(obj);
1194 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001196 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001197 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Ian Rogersbc939662013-08-15 10:26:54 -07001200 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1201 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001204 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1205 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001206 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001209 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Ian Rogersbc939662013-08-15 10:26:54 -07001212 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1213 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001214 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1215 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001216 ScopedObjectAccess soa(env);
1217 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
Ian Rogersbc939662013-08-15 10:26:54 -07001220 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1221 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001222 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1223 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001225 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001228 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001231 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1232 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001233 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001236 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
Ian Rogersbc939662013-08-15 10:26:54 -07001239 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1240 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001241 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1242 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 ScopedObjectAccess soa(env);
1244 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
Ian Rogersbc939662013-08-15 10:26:54 -07001247 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1248 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001249 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1250 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001252 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001255 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001258 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1259 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001260 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001263 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
Ian Rogersbc939662013-08-15 10:26:54 -07001266 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1267 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001268 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1269 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 ScopedObjectAccess soa(env);
1271 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
Ian Rogersbc939662013-08-15 10:26:54 -07001274 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1275 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001276 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1277 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001279 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001282 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001285 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1286 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001287 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001288 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001290 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Ian Rogersbc939662013-08-15 10:26:54 -07001293 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1294 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001295 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1296 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001297 ScopedObjectAccess soa(env);
1298 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Ian Rogersbc939662013-08-15 10:26:54 -07001301 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1302 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001303 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1304 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001306 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001309 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001312 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1313 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001314 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001315 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001317 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Ian Rogersbc939662013-08-15 10:26:54 -07001320 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1321 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001322 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1323 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001324 ScopedObjectAccess soa(env);
1325 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
Ian Rogersbc939662013-08-15 10:26:54 -07001328 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1329 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001330 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1331 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001333 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001336 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001339 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1340 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001341 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001342 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001344 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
Ian Rogersbc939662013-08-15 10:26:54 -07001347 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1348 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001349 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1350 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001351 ScopedObjectAccess soa(env);
1352 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
Ian Rogersbc939662013-08-15 10:26:54 -07001355 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1356 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001357 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1358 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001359 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001360 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001363 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001366 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1367 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001368 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001369 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001371 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Ian Rogersbc939662013-08-15 10:26:54 -07001374 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1375 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001376 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1377 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001378 ScopedObjectAccess soa(env);
1379 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 }
1381
Ian Rogersbc939662013-08-15 10:26:54 -07001382 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1383 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001384 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1385 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001386 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001387 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001390 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001392 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001393 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1394 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001395 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001396 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001398 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Ian Rogersbc939662013-08-15 10:26:54 -07001401 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1402 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001403 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1404 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001405 ScopedObjectAccess soa(env);
1406 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
Ian Rogersbc939662013-08-15 10:26:54 -07001409 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1410 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001411 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1412 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001414 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001417 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001420 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1421 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001422 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 va_end(ap);
1425 }
1426
Brian Carlstromea46f952013-07-30 01:26:50 -07001427 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1428 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001429 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1430 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001431 ScopedObjectAccess soa(env);
1432 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Ian Rogersbc939662013-08-15 10:26:54 -07001435 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1436 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001437 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1438 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001440 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Ian Rogersbc939662013-08-15 10:26:54 -07001443 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001444 CHECK_NON_NULL_ARGUMENT(java_class);
1445 CHECK_NON_NULL_ARGUMENT(name);
1446 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001448 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001450
Ian Rogersbc939662013-08-15 10:26:54 -07001451 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1452 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001453 CHECK_NON_NULL_ARGUMENT(java_class);
1454 CHECK_NON_NULL_ARGUMENT(name);
1455 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001457 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001461 CHECK_NON_NULL_ARGUMENT(obj);
1462 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001463 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001464 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1465 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001466 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001468
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001470 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001471 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001472 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001473 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001477 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1478 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001480 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1481 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1482 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001483 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001487 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001488 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001489 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1490 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001491 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001495 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1496 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001497 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001498 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1499 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001500 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001501
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001502#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001503 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001504 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001505 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001506 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001509 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1510 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001511 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001512 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1513 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001514 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001516#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001517 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001518 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001519 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001520 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001521
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001523 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001527 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001531 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001535 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001539 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001543 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001547 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001551 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001555 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001559 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001562 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001563 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001566 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001567 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001570 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001571 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 }
1573
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001574 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001575 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 }
1577
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001578 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001579 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001583 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 }
1585
1586 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001587 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 }
1589
1590 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001591 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 }
1593
1594 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001595 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 }
1597
1598 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001599 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 }
1601
1602 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001603 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 }
1605
1606 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001607 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 }
1609
1610 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001611 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 }
1613
1614 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001615 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 }
1617
1618 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001619 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001620 }
1621
1622 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001623 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001624 }
1625
1626 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001627 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001628 }
1629
1630 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001631 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001632 }
1633
1634 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001635 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001636 }
1637
1638 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001639 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001640 }
1641
1642 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001643 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001644 }
1645
1646 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001647 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001653 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001654 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001655 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001656 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 va_end(ap);
1658 return local_result;
1659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001662 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001664 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 }
1667
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001668 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001669 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001671 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001672 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001678 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001679 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001680 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001682 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001686 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001688 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001692 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001693 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001694 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001700 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001701 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001702 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001704 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001708 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001710 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001713 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001714 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001716 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001721 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001722 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001723 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001724 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001726 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001730 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001732 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001736 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001737 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001738 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001741 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001744 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001745 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001746 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001748 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001752 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001754 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001758 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001760 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001763 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001766 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001767 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001768 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001770 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001774 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001776 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001779 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001780 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001782 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 }
1784
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001785 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001787 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001788 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001789 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001790 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001792 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001796 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001798 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001801 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001802 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001804 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001807 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001809 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001811 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001812 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001814 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001820 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001823 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001824 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001826 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001829 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001831 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001832 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001833 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001834 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001836 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001840 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001842 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001845 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001846 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001848 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001851 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001853 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001854 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001855 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 va_end(ap);
1858 }
1859
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001860 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001861 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001863 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001866 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001867 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001869 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001873 if (UNLIKELY(char_count < 0)) {
1874 JniAbortF("NewString", "char_count < 0: %d", char_count);
1875 return nullptr;
1876 }
1877 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1878 JniAbortF("NewString", "chars == null && char_count > 0");
1879 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001880 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001882 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001883 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
1886 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001887 if (utf == nullptr) {
1888 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001891 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Elliott Hughes814e4032011-08-23 12:07:56 -07001895 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001896 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001898 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001899 }
1900
1901 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001902 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001904 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001905 }
1906
Ian Rogersbc939662013-08-15 10:26:54 -07001907 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1908 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001909 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001910 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001911 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001915 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1917 memcpy(buf, chars + start, length * sizeof(jchar));
1918 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001919 }
1920
Ian Rogersbc939662013-08-15 10:26:54 -07001921 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1922 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001923 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001925 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001926 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001928 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001929 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001930 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1931 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1932 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001936 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001938 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1939 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001940 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001941 gc::Heap* heap = Runtime::Current()->GetHeap();
1942 if (heap->IsMovableObject(chars)) {
1943 if (is_copy != nullptr) {
1944 *is_copy = JNI_TRUE;
1945 }
1946 int32_t char_count = s->GetLength();
1947 int32_t offset = s->GetOffset();
1948 jchar* bytes = new jchar[char_count];
1949 for (int32_t i = 0; i < char_count; i++) {
1950 bytes[i] = chars->Get(i + offset);
1951 }
1952 return bytes;
1953 } else {
1954 if (is_copy != nullptr) {
1955 *is_copy = JNI_FALSE;
1956 }
1957 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001958 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001959 }
1960
Mathieu Chartier590fee92013-09-13 13:46:47 -07001961 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001962 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001963 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001964 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1965 mirror::CharArray* s_chars = s->GetCharArray();
1966 if (chars != (s_chars->GetData() + s->GetOffset())) {
1967 delete[] chars;
1968 }
1969 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001973 CHECK_NON_NULL_ARGUMENT(java_string);
1974 ScopedObjectAccess soa(env);
1975 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1976 mirror::CharArray* chars = s->GetCharArray();
1977 int32_t offset = s->GetOffset();
1978 PinPrimitiveArray(soa, chars);
1979 gc::Heap* heap = Runtime::Current()->GetHeap();
1980 if (heap->IsMovableObject(chars)) {
1981 StackHandleScope<1> hs(soa.Self());
1982 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1983 heap->IncrementDisableMovingGC(soa.Self());
1984 }
1985 if (is_copy != nullptr) {
1986 *is_copy = JNI_FALSE;
1987 }
1988 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001992 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1993 ScopedObjectAccess soa(env);
1994 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
1995 gc::Heap* heap = Runtime::Current()->GetHeap();
1996 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1997 mirror::CharArray* s_chars = s->GetCharArray();
1998 if (heap->IsMovableObject(s_chars)) {
1999 heap->DecrementDisableMovingGC(soa.Self());
2000 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002004 if (java_string == nullptr) {
2005 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002006 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002007 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002008 *is_copy = JNI_TRUE;
2009 }
Ian Rogersef28b142012-11-30 14:22:18 -08002010 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002011 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002012 size_t byte_count = s->GetUtfLength();
2013 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002014 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002015 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2016 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2017 bytes[byte_count] = '\0';
2018 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002022 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002023 }
2024
Elliott Hughesbd935992011-08-22 11:59:34 -07002025 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002027 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002028 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002029 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002030 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2031 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002032 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002033 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002037 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002038 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002039 mirror::ObjectArray<mirror::Object>* array =
2040 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002041 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Ian Rogersbc939662013-08-15 10:26:54 -07002044 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2045 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002046 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002047 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002048 mirror::ObjectArray<mirror::Object>* array =
2049 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2050 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002051 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
2054 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002055 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
2058 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002059 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
2062 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002063 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
2066 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002067 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
2070 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002071 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
2074 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002075 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
2078 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002079 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Ian Rogers1d99e452014-01-02 17:36:41 -08002082 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2083 jobject initial_element) {
2084 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002085 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002086 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002087 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002088 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089
2090 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002091 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002092 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002093 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002094 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002095 if (UNLIKELY(element_class->IsPrimitive())) {
2096 JniAbortF("NewObjectArray", "not an object type: %s",
2097 PrettyDescriptor(element_class).c_str());
2098 return nullptr;
2099 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002100 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002101 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002102 if (UNLIKELY(array_class == nullptr)) {
2103 return nullptr;
2104 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes75770752011-08-24 17:52:38 -07002107 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002108 mirror::ObjectArray<mirror::Object>* result =
2109 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002110 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002111 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002112 if (initial_object != nullptr) {
2113 mirror::Class* element_class = result->GetClass()->GetComponentType();
2114 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2115 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2116 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2117 PrettyDescriptor(element_class).c_str());
2118
2119 } else {
2120 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002121 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002122 }
2123 }
Elliott Hughes75770752011-08-24 17:52:38 -07002124 }
2125 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002126 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
2129 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002130 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Ian Rogersa15e67d2012-02-28 13:51:55 -08002133 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002134 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002135 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002136 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002137 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2138 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2139 PrettyDescriptor(array->GetClass()).c_str());
2140 return nullptr;
2141 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002142 gc::Heap* heap = Runtime::Current()->GetHeap();
2143 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002144 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002145 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002146 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002147 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002148 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002149 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002150 *is_copy = JNI_FALSE;
2151 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002152 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002153 }
2154
Ian Rogers2d10b202014-05-12 19:15:18 -07002155 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2156 jint mode) {
2157 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2158 ScopedObjectAccess soa(env);
2159 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2160 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2161 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2162 PrettyDescriptor(array->GetClass()).c_str());
2163 return;
2164 }
2165 const size_t component_size = array->GetClass()->GetComponentSize();
2166 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002167 }
2168
Elliott Hughes75770752011-08-24 17:52:38 -07002169 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002170 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes75770752011-08-24 17:52:38 -07002173 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002174 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Elliott Hughes75770752011-08-24 17:52:38 -07002177 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002178 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Elliott Hughes75770752011-08-24 17:52:38 -07002181 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002182 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughes75770752011-08-24 17:52:38 -07002185 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002186 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughes75770752011-08-24 17:52:38 -07002189 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002190 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes75770752011-08-24 17:52:38 -07002193 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002194 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes75770752011-08-24 17:52:38 -07002197 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002198 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
Mathieu Chartier590fee92013-09-13 13:46:47 -07002201 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2202 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002203 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2204 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Mathieu Chartier590fee92013-09-13 13:46:47 -07002207 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002208 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Mathieu Chartier590fee92013-09-13 13:46:47 -07002211 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002212 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Mathieu Chartier590fee92013-09-13 13:46:47 -07002215 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2216 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002217 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Mathieu Chartier590fee92013-09-13 13:46:47 -07002220 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2221 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002222 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Mathieu Chartier590fee92013-09-13 13:46:47 -07002225 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002226 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 }
2228
Mathieu Chartier590fee92013-09-13 13:46:47 -07002229 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002230 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Mathieu Chartier590fee92013-09-13 13:46:47 -07002233 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2234 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002235 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Ian Rogersbc939662013-08-15 10:26:54 -07002238 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2239 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002240 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002241 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Ian Rogersbc939662013-08-15 10:26:54 -07002244 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2245 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002246 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Ian Rogersbc939662013-08-15 10:26:54 -07002249 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2250 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002251 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253
Ian Rogersbc939662013-08-15 10:26:54 -07002254 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2255 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002256 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002257 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259
Ian Rogersbc939662013-08-15 10:26:54 -07002260 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2261 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002262 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002263 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
2265
Ian Rogersbc939662013-08-15 10:26:54 -07002266 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2267 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002268 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Ian Rogersbc939662013-08-15 10:26:54 -07002271 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2272 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002273 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
Ian Rogersbc939662013-08-15 10:26:54 -07002276 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2277 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002278 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002279 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2283 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002284 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002285 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 }
2287
Ian Rogersbc939662013-08-15 10:26:54 -07002288 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2289 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002290 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 }
2292
Ian Rogersbc939662013-08-15 10:26:54 -07002293 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2294 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002295 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Ian Rogersbc939662013-08-15 10:26:54 -07002298 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2299 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002300 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002301 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Ian Rogersbc939662013-08-15 10:26:54 -07002304 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2305 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002306 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002307 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Ian Rogersbc939662013-08-15 10:26:54 -07002310 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2311 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002312 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Ian Rogersbc939662013-08-15 10:26:54 -07002315 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2316 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002317 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319
Ian Rogersbc939662013-08-15 10:26:54 -07002320 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2321 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002322 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002323 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
2325
Ian Rogersbc939662013-08-15 10:26:54 -07002326 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2327 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002328 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2329 }
2330
Ian Rogersbc939662013-08-15 10:26:54 -07002331 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2332 jint method_count, bool return_errors) {
2333 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002334 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002335 return JNI_ERR; // Not reached.
2336 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002337 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002338 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002339 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002340 if (UNLIKELY(method_count == 0)) {
2341 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2342 << PrettyDescriptor(c);
2343 return JNI_OK;
2344 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002345 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002346 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002347 const char* name = methods[i].name;
2348 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002349 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002351 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002352 ++sig;
2353 }
2354
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002355 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2356 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002357 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002358 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002359 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002360 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002361 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002362 << PrettyDescriptor(c) << "." << name << sig << " in "
2363 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002364 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002365 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002366 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002367 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002368 << PrettyDescriptor(c) << "." << name << sig
2369 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002370 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002371 return JNI_ERR;
2372 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002373
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002374 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002375
Ian Rogers1eb512d2013-10-18 15:42:20 -07002376 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002377 }
2378 return JNI_OK;
2379 }
2380
Elliott Hughes5174fe62011-08-23 15:12:35 -07002381 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002382 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002383 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002384 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002385
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002386 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002387
Ian Rogers2d10b202014-05-12 19:15:18 -07002388 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002389 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002390 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002391 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002392 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002393 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002394 }
2395 }
2396 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002397 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002398 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002399 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002400 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002401 }
2402 }
2403
Ian Rogers2d10b202014-05-12 19:15:18 -07002404 if (unregistered_count == 0) {
2405 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2406 << PrettyDescriptor(c) << "' that contains no native methods";
2407 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002408 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002409 }
2410
Ian Rogers719d1a32014-03-06 12:13:39 -08002411 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002412 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002413 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002414 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2415 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002416 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002417 return JNI_ERR;
2418 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002419 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002420 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002421 }
2422
Ian Rogers719d1a32014-03-06 12:13:39 -08002423 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002424 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002425 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002426 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002427 o->MonitorExit(soa.Self());
2428 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002429 return JNI_ERR;
2430 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002431 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002432 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002433 }
2434
2435 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002436 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002437 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002438 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002439 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002440 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002441 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002442 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002443 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002444 }
2445
Elliott Hughescdf53122011-08-19 15:46:09 -07002446 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002447 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002448 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002449 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002450 if (address == nullptr && capacity != 0) {
2451 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002452 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002453
Ian Rogers936b37f2014-02-14 00:52:24 -08002454 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002455 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002456 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002457 jint capacity_arg = static_cast<jint>(capacity);
2458
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002459 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2460 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002461 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002462 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002463 }
2464
Elliott Hughesb465ab02011-08-24 11:21:21 -07002465 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002466 return reinterpret_cast<void*>(env->GetLongField(
2467 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002468 }
2469
Elliott Hughesb465ab02011-08-24 11:21:21 -07002470 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002471 return static_cast<jlong>(env->GetIntField(
2472 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002473 }
2474
Elliott Hughesb465ab02011-08-24 11:21:21 -07002475 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002476 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002477
2478 // Do we definitely know what kind of reference this is?
2479 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2480 IndirectRefKind kind = GetIndirectRefKind(ref);
2481 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002482 case kLocal: {
2483 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002484 // The local refs don't need a read barrier.
2485 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2486 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002487 return JNILocalRefType;
2488 }
2489 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002490 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002491 case kGlobal:
2492 return JNIGlobalRefType;
2493 case kWeakGlobal:
2494 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002495 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002496 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002497 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002498 return JNILocalRefType;
2499 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002500 return JNIInvalidRefType;
2501 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002502 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2503 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002504 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002505
2506 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002507 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2508 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002509 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002510 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002511 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2512 return JNI_ERR;
2513 }
2514 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002515 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002516 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2517 if (!okay) {
2518 soa.Self()->ThrowOutOfMemoryError(caller);
2519 }
2520 return okay ? JNI_OK : JNI_ERR;
2521 }
2522
2523 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002524 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002525 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002526 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002527 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002528 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002529 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002530 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002531 return soa.AddLocalReference<JniT>(result);
2532 }
2533
Ian Rogers2d10b202014-05-12 19:15:18 -07002534 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2535 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2536 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002537 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002538 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002539 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2540 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2541 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2542 PrettyDescriptor(array->GetClass()).c_str());
2543 return nullptr;
2544 }
2545 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2546 return array;
2547 }
2548
2549 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2550 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2551 CHECK_NON_NULL_ARGUMENT(java_array);
2552 ScopedObjectAccess soa(env);
2553 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2554 "GetArrayElements",
2555 "get");
2556 if (UNLIKELY(array == nullptr)) {
2557 return nullptr;
2558 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002559 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002560 // Only make a copy if necessary.
2561 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2562 if (is_copy != nullptr) {
2563 *is_copy = JNI_TRUE;
2564 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002565 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002566 size_t size = array->GetLength() * component_size;
2567 void* data = new uint64_t[RoundUp(size, 8) / 8];
2568 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002569 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002570 } else {
2571 if (is_copy != nullptr) {
2572 *is_copy = JNI_FALSE;
2573 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002574 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002575 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576 }
2577
Ian Rogers2d10b202014-05-12 19:15:18 -07002578 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002579 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002580 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002581 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002582 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2583 "ReleaseArrayElements",
2584 "release");
2585 if (array == nullptr) {
2586 return;
2587 }
2588 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2589 }
2590
2591 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2592 size_t component_size, void* elements, jint mode)
2593 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002594 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002595 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002596 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002597 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002598 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2599 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002600 if (is_copy) {
2601 // Sanity check: If elements is not the same as the java array's data, it better not be a
2602 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2603 // copies we make?
2604 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2605 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2606 reinterpret_cast<void*>(elements), array_data);
2607 return;
2608 }
2609 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002610 // Don't need to copy if we had a direct pointer.
2611 if (mode != JNI_ABORT && is_copy) {
2612 memcpy(array_data, elements, bytes);
2613 }
2614 if (mode != JNI_COMMIT) {
2615 if (is_copy) {
2616 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002617 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002618 // Non copy to a movable object must means that we had disabled the moving GC.
2619 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002620 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002621 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002622 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002623 }
2624
Ian Rogers2d10b202014-05-12 19:15:18 -07002625 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2626 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2627 jsize start, jsize length, ElementT* buf) {
2628 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2629 ScopedObjectAccess soa(env);
2630 ArtArrayT* array =
2631 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2632 "GetPrimitiveArrayRegion",
2633 "get region of");
2634 if (array != nullptr) {
2635 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2636 ThrowAIOOBE(soa, array, start, length, "src");
2637 } else {
2638 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2639 ElementT* data = array->GetData();
2640 memcpy(buf, data + start, length * sizeof(ElementT));
2641 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002642 }
2643 }
2644
Ian Rogers2d10b202014-05-12 19:15:18 -07002645 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2646 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2647 jsize start, jsize length, const ElementT* buf) {
2648 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2649 ScopedObjectAccess soa(env);
2650 ArtArrayT* array =
2651 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2652 "SetPrimitiveArrayRegion",
2653 "set region of");
2654 if (array != nullptr) {
2655 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2656 ThrowAIOOBE(soa, array, start, length, "dst");
2657 } else {
2658 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2659 ElementT* data = array->GetData();
2660 memcpy(data + start, buf, length * sizeof(ElementT));
2661 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002662 }
2663 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002664};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002665
Elliott Hughes88c5c352012-03-15 18:49:48 -07002666const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002667 nullptr, // reserved0.
2668 nullptr, // reserved1.
2669 nullptr, // reserved2.
2670 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002671 JNI::GetVersion,
2672 JNI::DefineClass,
2673 JNI::FindClass,
2674 JNI::FromReflectedMethod,
2675 JNI::FromReflectedField,
2676 JNI::ToReflectedMethod,
2677 JNI::GetSuperclass,
2678 JNI::IsAssignableFrom,
2679 JNI::ToReflectedField,
2680 JNI::Throw,
2681 JNI::ThrowNew,
2682 JNI::ExceptionOccurred,
2683 JNI::ExceptionDescribe,
2684 JNI::ExceptionClear,
2685 JNI::FatalError,
2686 JNI::PushLocalFrame,
2687 JNI::PopLocalFrame,
2688 JNI::NewGlobalRef,
2689 JNI::DeleteGlobalRef,
2690 JNI::DeleteLocalRef,
2691 JNI::IsSameObject,
2692 JNI::NewLocalRef,
2693 JNI::EnsureLocalCapacity,
2694 JNI::AllocObject,
2695 JNI::NewObject,
2696 JNI::NewObjectV,
2697 JNI::NewObjectA,
2698 JNI::GetObjectClass,
2699 JNI::IsInstanceOf,
2700 JNI::GetMethodID,
2701 JNI::CallObjectMethod,
2702 JNI::CallObjectMethodV,
2703 JNI::CallObjectMethodA,
2704 JNI::CallBooleanMethod,
2705 JNI::CallBooleanMethodV,
2706 JNI::CallBooleanMethodA,
2707 JNI::CallByteMethod,
2708 JNI::CallByteMethodV,
2709 JNI::CallByteMethodA,
2710 JNI::CallCharMethod,
2711 JNI::CallCharMethodV,
2712 JNI::CallCharMethodA,
2713 JNI::CallShortMethod,
2714 JNI::CallShortMethodV,
2715 JNI::CallShortMethodA,
2716 JNI::CallIntMethod,
2717 JNI::CallIntMethodV,
2718 JNI::CallIntMethodA,
2719 JNI::CallLongMethod,
2720 JNI::CallLongMethodV,
2721 JNI::CallLongMethodA,
2722 JNI::CallFloatMethod,
2723 JNI::CallFloatMethodV,
2724 JNI::CallFloatMethodA,
2725 JNI::CallDoubleMethod,
2726 JNI::CallDoubleMethodV,
2727 JNI::CallDoubleMethodA,
2728 JNI::CallVoidMethod,
2729 JNI::CallVoidMethodV,
2730 JNI::CallVoidMethodA,
2731 JNI::CallNonvirtualObjectMethod,
2732 JNI::CallNonvirtualObjectMethodV,
2733 JNI::CallNonvirtualObjectMethodA,
2734 JNI::CallNonvirtualBooleanMethod,
2735 JNI::CallNonvirtualBooleanMethodV,
2736 JNI::CallNonvirtualBooleanMethodA,
2737 JNI::CallNonvirtualByteMethod,
2738 JNI::CallNonvirtualByteMethodV,
2739 JNI::CallNonvirtualByteMethodA,
2740 JNI::CallNonvirtualCharMethod,
2741 JNI::CallNonvirtualCharMethodV,
2742 JNI::CallNonvirtualCharMethodA,
2743 JNI::CallNonvirtualShortMethod,
2744 JNI::CallNonvirtualShortMethodV,
2745 JNI::CallNonvirtualShortMethodA,
2746 JNI::CallNonvirtualIntMethod,
2747 JNI::CallNonvirtualIntMethodV,
2748 JNI::CallNonvirtualIntMethodA,
2749 JNI::CallNonvirtualLongMethod,
2750 JNI::CallNonvirtualLongMethodV,
2751 JNI::CallNonvirtualLongMethodA,
2752 JNI::CallNonvirtualFloatMethod,
2753 JNI::CallNonvirtualFloatMethodV,
2754 JNI::CallNonvirtualFloatMethodA,
2755 JNI::CallNonvirtualDoubleMethod,
2756 JNI::CallNonvirtualDoubleMethodV,
2757 JNI::CallNonvirtualDoubleMethodA,
2758 JNI::CallNonvirtualVoidMethod,
2759 JNI::CallNonvirtualVoidMethodV,
2760 JNI::CallNonvirtualVoidMethodA,
2761 JNI::GetFieldID,
2762 JNI::GetObjectField,
2763 JNI::GetBooleanField,
2764 JNI::GetByteField,
2765 JNI::GetCharField,
2766 JNI::GetShortField,
2767 JNI::GetIntField,
2768 JNI::GetLongField,
2769 JNI::GetFloatField,
2770 JNI::GetDoubleField,
2771 JNI::SetObjectField,
2772 JNI::SetBooleanField,
2773 JNI::SetByteField,
2774 JNI::SetCharField,
2775 JNI::SetShortField,
2776 JNI::SetIntField,
2777 JNI::SetLongField,
2778 JNI::SetFloatField,
2779 JNI::SetDoubleField,
2780 JNI::GetStaticMethodID,
2781 JNI::CallStaticObjectMethod,
2782 JNI::CallStaticObjectMethodV,
2783 JNI::CallStaticObjectMethodA,
2784 JNI::CallStaticBooleanMethod,
2785 JNI::CallStaticBooleanMethodV,
2786 JNI::CallStaticBooleanMethodA,
2787 JNI::CallStaticByteMethod,
2788 JNI::CallStaticByteMethodV,
2789 JNI::CallStaticByteMethodA,
2790 JNI::CallStaticCharMethod,
2791 JNI::CallStaticCharMethodV,
2792 JNI::CallStaticCharMethodA,
2793 JNI::CallStaticShortMethod,
2794 JNI::CallStaticShortMethodV,
2795 JNI::CallStaticShortMethodA,
2796 JNI::CallStaticIntMethod,
2797 JNI::CallStaticIntMethodV,
2798 JNI::CallStaticIntMethodA,
2799 JNI::CallStaticLongMethod,
2800 JNI::CallStaticLongMethodV,
2801 JNI::CallStaticLongMethodA,
2802 JNI::CallStaticFloatMethod,
2803 JNI::CallStaticFloatMethodV,
2804 JNI::CallStaticFloatMethodA,
2805 JNI::CallStaticDoubleMethod,
2806 JNI::CallStaticDoubleMethodV,
2807 JNI::CallStaticDoubleMethodA,
2808 JNI::CallStaticVoidMethod,
2809 JNI::CallStaticVoidMethodV,
2810 JNI::CallStaticVoidMethodA,
2811 JNI::GetStaticFieldID,
2812 JNI::GetStaticObjectField,
2813 JNI::GetStaticBooleanField,
2814 JNI::GetStaticByteField,
2815 JNI::GetStaticCharField,
2816 JNI::GetStaticShortField,
2817 JNI::GetStaticIntField,
2818 JNI::GetStaticLongField,
2819 JNI::GetStaticFloatField,
2820 JNI::GetStaticDoubleField,
2821 JNI::SetStaticObjectField,
2822 JNI::SetStaticBooleanField,
2823 JNI::SetStaticByteField,
2824 JNI::SetStaticCharField,
2825 JNI::SetStaticShortField,
2826 JNI::SetStaticIntField,
2827 JNI::SetStaticLongField,
2828 JNI::SetStaticFloatField,
2829 JNI::SetStaticDoubleField,
2830 JNI::NewString,
2831 JNI::GetStringLength,
2832 JNI::GetStringChars,
2833 JNI::ReleaseStringChars,
2834 JNI::NewStringUTF,
2835 JNI::GetStringUTFLength,
2836 JNI::GetStringUTFChars,
2837 JNI::ReleaseStringUTFChars,
2838 JNI::GetArrayLength,
2839 JNI::NewObjectArray,
2840 JNI::GetObjectArrayElement,
2841 JNI::SetObjectArrayElement,
2842 JNI::NewBooleanArray,
2843 JNI::NewByteArray,
2844 JNI::NewCharArray,
2845 JNI::NewShortArray,
2846 JNI::NewIntArray,
2847 JNI::NewLongArray,
2848 JNI::NewFloatArray,
2849 JNI::NewDoubleArray,
2850 JNI::GetBooleanArrayElements,
2851 JNI::GetByteArrayElements,
2852 JNI::GetCharArrayElements,
2853 JNI::GetShortArrayElements,
2854 JNI::GetIntArrayElements,
2855 JNI::GetLongArrayElements,
2856 JNI::GetFloatArrayElements,
2857 JNI::GetDoubleArrayElements,
2858 JNI::ReleaseBooleanArrayElements,
2859 JNI::ReleaseByteArrayElements,
2860 JNI::ReleaseCharArrayElements,
2861 JNI::ReleaseShortArrayElements,
2862 JNI::ReleaseIntArrayElements,
2863 JNI::ReleaseLongArrayElements,
2864 JNI::ReleaseFloatArrayElements,
2865 JNI::ReleaseDoubleArrayElements,
2866 JNI::GetBooleanArrayRegion,
2867 JNI::GetByteArrayRegion,
2868 JNI::GetCharArrayRegion,
2869 JNI::GetShortArrayRegion,
2870 JNI::GetIntArrayRegion,
2871 JNI::GetLongArrayRegion,
2872 JNI::GetFloatArrayRegion,
2873 JNI::GetDoubleArrayRegion,
2874 JNI::SetBooleanArrayRegion,
2875 JNI::SetByteArrayRegion,
2876 JNI::SetCharArrayRegion,
2877 JNI::SetShortArrayRegion,
2878 JNI::SetIntArrayRegion,
2879 JNI::SetLongArrayRegion,
2880 JNI::SetFloatArrayRegion,
2881 JNI::SetDoubleArrayRegion,
2882 JNI::RegisterNatives,
2883 JNI::UnregisterNatives,
2884 JNI::MonitorEnter,
2885 JNI::MonitorExit,
2886 JNI::GetJavaVM,
2887 JNI::GetStringRegion,
2888 JNI::GetStringUTFRegion,
2889 JNI::GetPrimitiveArrayCritical,
2890 JNI::ReleasePrimitiveArrayCritical,
2891 JNI::GetStringCritical,
2892 JNI::ReleaseStringCritical,
2893 JNI::NewWeakGlobalRef,
2894 JNI::DeleteWeakGlobalRef,
2895 JNI::ExceptionCheck,
2896 JNI::NewDirectByteBuffer,
2897 JNI::GetDirectBufferAddress,
2898 JNI::GetDirectBufferCapacity,
2899 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002900};
2901
Elliott Hughes75770752011-08-24 17:52:38 -07002902JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002903 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002904 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002905 local_ref_cookie(IRT_FIRST_SEGMENT),
2906 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002907 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002908 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002909 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002910 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002911 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002912 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002913 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002914}
2915
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002916JNIEnvExt::~JNIEnvExt() {
2917}
2918
Mathieu Chartier590fee92013-09-13 13:46:47 -07002919jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2920 if (obj == nullptr) {
2921 return nullptr;
2922 }
2923 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2924}
2925
2926void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2927 if (obj != nullptr) {
2928 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2929 }
2930}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002931void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2932 check_jni = enabled;
2933 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002934}
2935
Elliott Hughes73e66f72012-05-09 09:34:45 -07002936void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2937 locals.Dump(os);
2938 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002939}
2940
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002941void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2942 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002943 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002944 stacked_local_ref_cookies.push_back(local_ref_cookie);
2945 local_ref_cookie = locals.GetSegmentState();
2946}
2947
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002948void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002949 locals.SetSegmentState(local_ref_cookie);
2950 local_ref_cookie = stacked_local_ref_cookies.back();
2951 stacked_local_ref_cookies.pop_back();
2952}
2953
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002954Offset JNIEnvExt::SegmentStateOffset() {
2955 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2956 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2957}
2958
Carl Shapiroea4dca82011-08-01 13:45:38 -07002959// JNI Invocation interface.
2960
Brian Carlstrombddf9762013-05-14 11:35:37 -07002961extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002962 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002963 if (IsBadJniVersion(args->version)) {
2964 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002965 return JNI_EVERSION;
2966 }
2967 Runtime::Options options;
2968 for (int i = 0; i < args->nOptions; ++i) {
2969 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002970 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002971 }
2972 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002973 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002974 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002975 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002976 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002977 bool started = runtime->Start();
2978 if (!started) {
2979 delete Thread::Current()->GetJniEnv();
2980 delete runtime->GetJavaVM();
2981 LOG(WARNING) << "CreateJavaVM failed";
2982 return JNI_ERR;
2983 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002984 *p_env = Thread::Current()->GetJniEnv();
2985 *p_vm = runtime->GetJavaVM();
2986 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002987}
2988
Elliott Hughesf2682d52011-08-15 16:37:04 -07002989extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002990 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002991 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002992 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002993 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002994 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002995 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002996 }
2997 return JNI_OK;
2998}
2999
3000// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003001extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003002 return JNI_ERR;
3003}
3004
Elliott Hughescdf53122011-08-19 15:46:09 -07003005class JII {
3006 public:
3007 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003008 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003009 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003010 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003011 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3012 delete raw_vm->runtime;
3013 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003014 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003015
Elliott Hughescdf53122011-08-19 15:46:09 -07003016 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003017 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003018 }
3019
3020 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003021 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003022 }
3023
3024 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003025 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003026 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003027 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003028 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3029 Runtime* runtime = raw_vm->runtime;
3030 runtime->DetachCurrentThread();
3031 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003032 }
3033
3034 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003035 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3036 // and unlike other calls that take a JNI version doesn't care if you supply
3037 // JNI_VERSION_1_1, which we don't otherwise support.
3038 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003039 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003040 return JNI_EVERSION;
3041 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003042 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003043 return JNI_ERR;
3044 }
3045 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003046 if (thread == nullptr) {
3047 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003048 return JNI_EDETACHED;
3049 }
3050 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003051 return JNI_OK;
3052 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003053};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003054
Elliott Hughes88c5c352012-03-15 18:49:48 -07003055const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003056 nullptr, // reserved0
3057 nullptr, // reserved1
3058 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003059 JII::DestroyJavaVM,
3060 JII::AttachCurrentThread,
3061 JII::DetachCurrentThread,
3062 JII::GetEnv,
3063 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003064};
3065
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003066JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003067 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003068 check_jni_abort_hook(nullptr),
3069 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003070 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003071 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003072 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003073 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003074 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003075 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003076 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003077 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003078 libraries(new Libraries),
3079 weak_globals_lock_("JNI weak global reference table lock"),
3080 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3081 allow_new_weak_globals_(true),
3082 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003083 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003084 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003085 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003086 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003087}
3088
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003089JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003090 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003091}
3092
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003093jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3094 if (obj == nullptr) {
3095 return nullptr;
3096 }
3097 MutexLock mu(self, weak_globals_lock_);
3098 while (UNLIKELY(!allow_new_weak_globals_)) {
3099 weak_globals_add_condition_.WaitHoldingLocks(self);
3100 }
3101 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3102 return reinterpret_cast<jweak>(ref);
3103}
3104
3105void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3106 MutexLock mu(self, weak_globals_lock_);
3107 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3108 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3109 << "failed to find entry";
3110 }
3111}
3112
Elliott Hughes88c5c352012-03-15 18:49:48 -07003113void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3114 check_jni = enabled;
3115 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003116}
3117
Elliott Hughesae80b492012-04-24 10:43:17 -07003118void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3119 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3120 if (force_copy) {
3121 os << " (with forcecopy)";
3122 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003123 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003124 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003125 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003126 os << "; pins=" << pin_table.Size();
3127 }
3128 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003129 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003130 os << "; globals=" << globals.Capacity();
3131 }
3132 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003133 MutexLock mu(self, weak_globals_lock_);
3134 if (weak_globals_.Capacity() > 0) {
3135 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003136 }
3137 }
3138 os << '\n';
3139
3140 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003141 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003142 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3143 }
3144}
3145
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003146void JavaVMExt::DisallowNewWeakGlobals() {
3147 MutexLock mu(Thread::Current(), weak_globals_lock_);
3148 allow_new_weak_globals_ = false;
3149}
3150
3151void JavaVMExt::AllowNewWeakGlobals() {
3152 Thread* self = Thread::Current();
3153 MutexLock mu(self, weak_globals_lock_);
3154 allow_new_weak_globals_ = true;
3155 weak_globals_add_condition_.Broadcast(self);
3156}
3157
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003158mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3159 MutexLock mu(self, weak_globals_lock_);
3160 while (UNLIKELY(!allow_new_weak_globals_)) {
3161 weak_globals_add_condition_.WaitHoldingLocks(self);
3162 }
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003163 // The weak globals do need a read barrier as they are weak roots.
3164 mirror::Object* obj = weak_globals_.Get<kWithReadBarrier>(ref);
3165 return obj;
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003166}
3167
Elliott Hughes73e66f72012-05-09 09:34:45 -07003168void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003169 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003170 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003171 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003172 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003173 }
3174 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003175 MutexLock mu(self, weak_globals_lock_);
3176 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003177 }
3178 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003179 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003180 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003181 }
3182}
3183
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003184bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003185 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003186 std::string* detail) {
3187 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003188
3189 // See if we've already loaded this library. If we have, and the class loader
3190 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003191 // TODO: for better results we should canonicalize the pathname (or even compare
3192 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003193 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003194 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003195 {
3196 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003197 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003198 library = libraries->Get(path);
3199 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003200 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003201 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003202 // The library will be associated with class_loader. The JNI
3203 // spec says we can't load the same library into more than one
3204 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003205 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003206 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003207 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003208 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003209 return false;
3210 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003211 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003212 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003213 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003214 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003215 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003216 return false;
3217 }
3218 return true;
3219 }
3220
3221 // Open the shared library. Because we're using a full path, the system
3222 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3223 // resolve this library's dependencies though.)
3224
3225 // Failures here are expected when java.library.path has several entries
3226 // and we have to hunt for the lib.
3227
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003228 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3229 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3230 // dlopen) becomes zero from dlclose.
3231
Elliott Hughescdf53122011-08-19 15:46:09 -07003232 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003233 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003234 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003235 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003236 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003237
Elliott Hughes84b2f142012-09-27 09:16:28 -07003238 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003239
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003240 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003241 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003242 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003243 return false;
3244 }
3245
3246 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003247 // TODO: move the locking (and more of this logic) into Libraries.
3248 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003249 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003250 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003251 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003252 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003253 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003254 libraries->Put(path, library);
3255 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003256 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003257 }
3258 if (!created_library) {
3259 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003260 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003261 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003262 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003263
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003264 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003265 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003266
Elliott Hughes79353722013-08-02 16:52:18 -07003267 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003268 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003269 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003270 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003271 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003272 } else {
3273 // Call JNI_OnLoad. We have to override the current class
3274 // loader, which will always be "null" since the stuff at the
3275 // top of the stack is around Runtime.loadLibrary(). (See
3276 // the comments in the JNI FindClass function.)
3277 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3278 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003279 StackHandleScope<1> hs(self);
3280 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3281 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003282
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003283 int version = 0;
3284 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003285 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003286 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003287 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003288 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003289
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003290 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003291
Elliott Hughes79353722013-08-02 16:52:18 -07003292 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003293 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003294 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003295 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003296 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003297 // It's unwise to call dlclose() here, but we can mark it
3298 // as bad and ensure that future load attempts will fail.
3299 // We don't know how far JNI_OnLoad got, so there could
3300 // be some partially-initialized stuff accessible through
3301 // newly-registered native method calls. We could try to
3302 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003303 } else {
3304 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003305 }
Elliott Hughes79353722013-08-02 16:52:18 -07003306 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003307 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003308 }
3309
Elliott Hughes79353722013-08-02 16:52:18 -07003310 library->SetResult(was_successful);
3311 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003312}
3313
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003314void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003315 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003316 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003317 // If this is a static method, it could be called before the class
3318 // has been initialized.
3319 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003320 c = EnsureInitialized(Thread::Current(), c);
3321 if (c == nullptr) {
3322 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003323 }
3324 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003325 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003326 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003327 std::string detail;
3328 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003329 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003330 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003331 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003332 native_method = libraries->FindNativeMethod(m, detail);
3333 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003334 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003335 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003336 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3337 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003338 }
3339 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003340}
3341
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003342void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003343 MutexLock mu(Thread::Current(), weak_globals_lock_);
3344 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003345 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003346 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003347 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003348 if (new_obj == nullptr) {
3349 new_obj = kClearedJniWeakGlobal;
3350 }
3351 *entry = new_obj;
3352 }
3353}
3354
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003355void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003356 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003357 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003358 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003359 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003360 }
3361 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003362 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003363 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003364 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003365 {
3366 MutexLock mu(self, libraries_lock);
3367 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003368 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003369 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003370 // The weak_globals table is visited by the GC itself (because it mutates the table).
3371}
3372
Elliott Hughesc8fece32013-01-02 11:27:23 -08003373void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003374 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003375 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003376 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003377 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3378 }
3379 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3380}
3381
Ian Rogersdf20fe02011-07-20 20:34:16 -07003382} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003383
3384std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3385 switch (rhs) {
3386 case JNIInvalidRefType:
3387 os << "JNIInvalidRefType";
3388 return os;
3389 case JNILocalRefType:
3390 os << "JNILocalRefType";
3391 return os;
3392 case JNIGlobalRefType:
3393 os << "JNIGlobalRefType";
3394 return os;
3395 case JNIWeakGlobalRefType:
3396 os << "JNIWeakGlobalRefType";
3397 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003398 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003399 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003400 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003401 }
3402}