blob: 66406bfa73fa920591ec2e3e9077c730a7f2668b [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);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700138 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700139 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800140 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700141 // No virtual method matching the signature. Search declared
142 // private methods and constructors.
143 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700144 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700145 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800146 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800148 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700149 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700151}
152
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800153static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800155 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700156 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
157 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800158 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700159 }
Brian Carlstromce888532013-10-10 00:32:58 -0700160 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700162 return method->GetDeclaringClass()->GetClassLoader();
163 }
164 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800165 mirror::ClassLoader* class_loader =
166 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
167 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700168 return class_loader;
169 }
170 // See if the override ClassLoader is set for gtests.
171 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800172 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800173 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700174 CHECK(Runtime::Current()->UseCompileTimeClassPath());
175 return class_loader;
176 }
177 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800178 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700179}
180
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
182 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700184 StackHandleScope<2> hs(soa.Self());
185 Handle<mirror::Class> c(
186 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
187 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800188 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700189 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800190 mirror::ArtField* field = nullptr;
191 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
193 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800195 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700196 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700198 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800199 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 StackHandleScope<1> hs(soa.Self());
204 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800207 "no type \"%s\" found and so no field \"%s\" "
208 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700209 c->GetDescriptor().c_str());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700210 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800211 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 }
213 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214 field = mirror::Class::FindStaticField(soa.Self(), c, name,
215 field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 } else {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700217 field = c->FindInstanceField(name, field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800219 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
221 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
222 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700223 sig, name, c->GetDescriptor().c_str());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800224 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700225 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700227}
228
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800229static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700232 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700233 vm->pin_table.Add(array);
234}
235
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800236static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700239 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700240 vm->pin_table.Remove(array);
241}
242
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800243static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700246 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800247 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
248 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
249 "%s offset=%d length=%d %s.length=%d",
250 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700251}
Ian Rogers0571d352011-11-03 19:51:38 -0700252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
254 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800256 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
257 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
258 "offset=%d length=%d string.length()=%d", start, length,
259 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700260}
Elliott Hughes814e4032011-08-23 12:07:56 -0700261
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 // Turn the const char* into a java.lang.String.
265 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800266 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700268 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700269
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 // Choose an appropriate constructor and set up the arguments.
271 jvalue args[2];
272 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800273 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800275 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 signature = "(Ljava/lang/String;)V";
277 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800278 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 signature = "(Ljava/lang/Throwable;)V";
280 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700281 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
283 args[0].l = s.get();
284 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700285 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800287 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800288 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800290 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 return JNI_ERR;
292 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700293
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800294 ScopedLocalRef<jthrowable> exception(
295 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
296 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 return JNI_ERR;
298 }
Ian Rogersef28b142012-11-30 14:22:18 -0800299 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800301 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700303}
304
Elliott Hughes462c9442012-03-23 18:47:50 -0700305static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800306 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700307 return JNI_ERR;
308 }
309
Elliott Hughes462c9442012-03-23 18:47:50 -0700310 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700311 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800312 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700313 *p_env = self->GetJniEnv();
314 return JNI_OK;
315 }
316
317 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
318
319 // No threads allowed in zygote mode.
320 if (runtime->IsZygote()) {
321 LOG(ERROR) << "Attempt to attach a thread in the zygote";
322 return JNI_ERR;
323 }
324
Elliott Hughes462c9442012-03-23 18:47:50 -0700325 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800326 const char* thread_name = nullptr;
327 jobject thread_group = nullptr;
328 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700329 if (IsBadJniVersion(args->version)) {
330 LOG(ERROR) << "Bad JNI version passed to "
331 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
332 << args->version;
333 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700334 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700335 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700336 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700337 }
Elliott Hughes75770752011-08-24 17:52:38 -0700338
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800339 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800340 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700341 return JNI_ERR;
342 } else {
343 *p_env = Thread::Current()->GetJniEnv();
344 return JNI_OK;
345 }
Elliott Hughes75770752011-08-24 17:52:38 -0700346}
347
Elliott Hughes79082e32011-08-25 12:07:32 -0700348class SharedLibrary {
349 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800350 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700351 : path_(path),
352 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700353 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700354 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700355 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700356 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700357 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700358 }
359
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800360 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700361 return class_loader_;
362 }
363
364 std::string GetPath() {
365 return path_;
366 }
367
368 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700369 * Check the result of an earlier call to JNI_OnLoad on this library.
370 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700371 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 bool CheckOnLoadResult()
373 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700374 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700375 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
377 bool okay;
378 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700379 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700380
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700381 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
383 // caller can continue.
384 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
385 okay = true;
386 } else {
387 while (jni_on_load_result_ == kPending) {
388 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700389 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700391
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 okay = (jni_on_load_result_ == kOkay);
393 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
394 << (okay ? "succeeded" : "failed") << "]";
395 }
396 }
397 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700398 return okay;
399 }
400
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700402 Thread* self = Thread::Current();
403 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700404
Elliott Hughes79082e32011-08-25 12:07:32 -0700405 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700406 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700407
408 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700409 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700410 }
411
412 void* FindSymbol(const std::string& symbol_name) {
413 return dlsym(handle_, symbol_name.c_str());
414 }
415
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800416 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800417 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800418 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800419 }
420 }
421
Elliott Hughes79082e32011-08-25 12:07:32 -0700422 private:
423 enum JNI_OnLoadState {
424 kPending,
425 kFailed,
426 kOkay,
427 };
428
429 // Path to library "/system/lib/libjni.so".
430 std::string path_;
431
432 // The void* returned by dlopen(3).
433 void* handle_;
434
435 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800436 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700437
438 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700440 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700441 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700442 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700443 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700445 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700446};
447
Elliott Hughes79082e32011-08-25 12:07:32 -0700448// This exists mainly to keep implementation details out of the header file.
449class Libraries {
450 public:
451 Libraries() {
452 }
453
454 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700455 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 }
457
Elliott Hughesae80b492012-04-24 10:43:17 -0700458 void Dump(std::ostream& os) const {
459 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700460 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700461 if (!first) {
462 os << ' ';
463 }
464 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700465 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700466 }
467 }
468
469 size_t size() const {
470 return libraries_.size();
471 }
472
Elliott Hughes79082e32011-08-25 12:07:32 -0700473 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700474 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800475 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700476 }
477
478 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700479 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700480 }
481
482 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800483 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 std::string jni_short_name(JniShortName(m));
486 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800487 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700488 for (const auto& lib : libraries_) {
489 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700490 if (library->GetClassLoader() != declaring_class_loader) {
491 // We only search libraries loaded by the appropriate ClassLoader.
492 continue;
493 }
494 // Try the short name then the long name...
495 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800496 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 fn = library->FindSymbol(jni_long_name);
498 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800499 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800500 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
501 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 return fn;
503 }
504 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700506 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700507 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700508 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800509 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700510 }
511
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800512 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800513 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800514 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800515 }
516 }
517
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700519 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700520};
521
Ian Rogers2d10b202014-05-12 19:15:18 -0700522#define CHECK_NON_NULL_ARGUMENT(value) \
523 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700524
Ian Rogers2d10b202014-05-12 19:15:18 -0700525#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
526 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
527
528#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
529 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
530
531#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
532 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
533
534#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800535 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700536 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700537 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700538 }
539
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700540#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800541 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700542 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700543 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700544 }
545
Elliott Hughescdf53122011-08-19 15:46:09 -0700546class JNI {
547 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700548 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700549 return JNI_VERSION_1_6;
550 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700551
Ian Rogers25e8b912012-09-07 11:31:36 -0700552 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700553 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800554 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700555 }
556
Elliott Hughescdf53122011-08-19 15:46:09 -0700557 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700558 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700559 Runtime* runtime = Runtime::Current();
560 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700561 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700562 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800563 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700564 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700565 StackHandleScope<1> hs(soa.Self());
566 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800567 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700568 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800569 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700570 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700572 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700573
Ian Rogers62f05122014-03-21 11:21:29 -0700574 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700575 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700576 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700577 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700578 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700579
Ian Rogers62f05122014-03-21 11:21:29 -0700580 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700581 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700582 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700583 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700584 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700585
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700587 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 mirror::ArtMethod* m = soa.DecodeMethod(mid);
590 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700591 jobject art_method = soa.AddLocalReference<jobject>(m);
592 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
593 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800594 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700595 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800596 SetObjectField(env, reflect_method,
597 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700598 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700600
Elliott Hughescdf53122011-08-19 15:46:09 -0700601 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700602 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800604 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700605 jobject art_field = soa.AddLocalReference<jobject>(f);
606 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
607 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800608 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700609 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800610 SetObjectField(env, reflect_field,
611 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700612 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700614
Elliott Hughes37f7a402011-08-22 18:56:01 -0700615 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700616 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800618 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700620 }
621
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700622 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700623 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800625 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700628
Elliott Hughes37f7a402011-08-22 18:56:01 -0700629 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700630 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
631 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800633 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
634 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700635 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700637
Elliott Hughese84278b2012-03-22 10:06:53 -0700638 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700639 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800640 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700641 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700642 return JNI_TRUE;
643 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700644 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800645 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
646 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700647 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700648 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700650
Elliott Hughes37f7a402011-08-22 18:56:01 -0700651 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800653 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
654 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700655 return JNI_ERR;
656 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800657 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
658 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700659 return JNI_OK;
660 }
661
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700662 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700663 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800664 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 }
666
667 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700668 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 }
670
671 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700672 ScopedObjectAccess soa(env);
673 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 }
675
676 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700678
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700679 StackHandleScope<3> hs(soa.Self());
680 // TODO: Use nullptr instead of null handles?
681 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
682 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
683 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800684 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200685 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 {
687 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800688 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700689 old_throw_this_object.Assign(old_throw_location.GetThis());
690 old_throw_method.Assign(old_throw_location.GetMethod());
691 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800692 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200693 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800694 soa.Self()->ClearException();
695 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800696 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700697 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700698 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
699 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800700 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700701 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700702 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700703 } else {
704 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800705 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800706 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700707 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700709 }
710 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700711 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800712 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700713
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700714 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200715 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700717
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800720 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
723
Ian Rogers25e8b912012-09-07 11:31:36 -0700724 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 LOG(FATAL) << "JNI FatalError called: " << msg;
726 }
727
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700728 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700729 // TODO: SOA may not be necessary but I do it to please lock annotations.
730 ScopedObjectAccess soa(env);
731 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700732 return JNI_ERR;
733 }
Ian Rogersef28b142012-11-30 14:22:18 -0800734 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 return JNI_OK;
736 }
737
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700738 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700739 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800740 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 soa.Env()->PopFrame();
742 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
744
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700745 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700746 // TODO: SOA may not be necessary but I do it to please lock annotations.
747 ScopedObjectAccess soa(env);
748 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 }
750
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700752 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800753 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800754 // Check for null after decoding the object to handle cleared weak globals.
755 if (decoded_obj == nullptr) {
756 return nullptr;
757 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700760 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700761 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 return reinterpret_cast<jobject>(ref);
763 }
764
765 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800766 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 return;
768 }
Ian Rogersef28b142012-11-30 14:22:18 -0800769 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800771 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700772 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700773
774 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
775 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
776 << "failed to find entry";
777 }
778 }
779
780 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700781 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800782 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 }
784
785 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700786 if (obj != nullptr) {
787 ScopedObjectAccess soa(env);
788 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 }
790 }
791
792 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700793 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800794 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800795 // Check for null after decoding the object to handle cleared weak globals.
796 if (decoded_obj == nullptr) {
797 return nullptr;
798 }
799 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 }
801
802 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800803 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 return;
805 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700806 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800807 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700808
Ian Rogersef28b142012-11-30 14:22:18 -0800809 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 if (!locals.Remove(cookie, obj)) {
811 // Attempting to delete a local reference that is not in the
812 // topmost local reference frame is a no-op. DeleteLocalRef returns
813 // void and doesn't throw any exceptions, but we should probably
814 // complain about it so the user will notice that things aren't
815 // going quite the way they expect.
816 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
817 << "failed to find entry";
818 }
819 }
820
821 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700822 if (obj1 == obj2) {
823 return JNI_TRUE;
824 } else {
825 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800826 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
827 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700828 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700831 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700832 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700833 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800834 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800835 if (c == nullptr) {
836 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700837 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700838 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
840
Ian Rogersbc939662013-08-15 10:26:54 -0700841 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700843 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700844 CHECK_NON_NULL_ARGUMENT(java_class);
845 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700846 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 va_end(args);
848 return result;
849 }
850
Elliott Hughes72025e52011-08-23 17:50:30 -0700851 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700852 CHECK_NON_NULL_ARGUMENT(java_class);
853 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800855 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800856 if (c == nullptr) {
857 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700858 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800859 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800860 if (result == nullptr) {
861 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700862 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800865 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800866 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700867 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800868 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 }
870
Elliott Hughes72025e52011-08-23 17:50:30 -0700871 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700872 CHECK_NON_NULL_ARGUMENT(java_class);
873 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700874 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800875 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800876 if (c == nullptr) {
877 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700878 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800879 mirror::Object* result = c->AllocObject(soa.Self());
880 if (result == nullptr) {
881 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700882 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800885 if (soa.Self()->IsExceptionPending()) {
886 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700887 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800888 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 }
890
Ian Rogersbc939662013-08-15 10:26:54 -0700891 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700892 CHECK_NON_NULL_ARGUMENT(java_class);
893 CHECK_NON_NULL_ARGUMENT(name);
894 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700896 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Ian Rogersbc939662013-08-15 10:26:54 -0700899 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
900 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700901 CHECK_NON_NULL_ARGUMENT(java_class);
902 CHECK_NON_NULL_ARGUMENT(name);
903 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700905 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 va_list ap;
910 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700911 CHECK_NON_NULL_ARGUMENT(obj);
912 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700913 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700916 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 }
918
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700920 CHECK_NON_NULL_ARGUMENT(obj);
921 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700922 ScopedObjectAccess soa(env);
923 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
924 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700928 CHECK_NON_NULL_ARGUMENT(obj);
929 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700930 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700931 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
932 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 va_list ap;
938 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700939 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
940 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700941 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700944 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 }
946
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700948 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
949 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700950 ScopedObjectAccess soa(env);
951 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700955 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
956 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700958 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
959 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_list ap;
964 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700965 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
966 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700967 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700970 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700974 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
975 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 ScopedObjectAccess soa(env);
977 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700981 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
982 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700984 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
985 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 va_list ap;
990 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700991 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
992 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700993 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700996 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001000 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1001 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(env);
1003 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001007 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1008 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001009 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001010 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1011 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_list ap;
1016 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1018 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001019 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001020 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001022 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1027 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 ScopedObjectAccess soa(env);
1029 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001033 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1034 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001036 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1037 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_list ap;
1042 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001043 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1044 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001045 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001048 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001052 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1053 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 ScopedObjectAccess soa(env);
1055 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001059 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1060 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001062 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1063 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_list ap;
1068 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001069 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1070 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001071 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001074 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001078 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1079 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 ScopedObjectAccess soa(env);
1081 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001085 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1086 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001088 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1089 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_list ap;
1094 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1096 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001097 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001100 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1105 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001111 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1112 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001114 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1115 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_list ap;
1120 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001121 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1122 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001123 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001126 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1131 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
1133 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001137 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1138 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001139 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001140 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1141 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_list ap;
1146 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001147 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1148 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001149 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001150 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001155 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1156 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
1158 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001162 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1163 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001164 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001165 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001168 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001171 CHECK_NON_NULL_ARGUMENT(obj);
1172 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001173 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1175 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
1177 return local_result;
1178 }
1179
Ian Rogersbc939662013-08-15 10:26:54 -07001180 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1181 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001182 CHECK_NON_NULL_ARGUMENT(obj);
1183 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 ScopedObjectAccess soa(env);
1185 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1186 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Ian Rogersbc939662013-08-15 10:26:54 -07001189 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1190 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001191 CHECK_NON_NULL_ARGUMENT(obj);
1192 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001194 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Ian Rogersbc939662013-08-15 10:26:54 -07001198 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1199 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001202 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1203 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001204 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001207 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Ian Rogersbc939662013-08-15 10:26:54 -07001210 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1211 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001212 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1213 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001214 ScopedObjectAccess soa(env);
1215 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
Ian Rogersbc939662013-08-15 10:26:54 -07001218 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1219 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001220 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1221 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001222 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001223 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001226 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001229 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1230 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001231 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001232 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001234 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
Ian Rogersbc939662013-08-15 10:26:54 -07001237 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1238 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001239 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1240 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241 ScopedObjectAccess soa(env);
1242 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
Ian Rogersbc939662013-08-15 10:26:54 -07001245 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1246 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001247 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1248 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001250 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001253 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001256 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1257 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001258 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001261 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
Ian Rogersbc939662013-08-15 10:26:54 -07001264 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1265 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001266 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1267 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001268 ScopedObjectAccess soa(env);
1269 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
Ian Rogersbc939662013-08-15 10:26:54 -07001272 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1273 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001274 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1275 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001277 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001280 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001283 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1284 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001285 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001288 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Ian Rogersbc939662013-08-15 10:26:54 -07001291 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1292 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001293 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1294 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 ScopedObjectAccess soa(env);
1296 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Ian Rogersbc939662013-08-15 10:26:54 -07001299 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1300 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001301 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1302 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001304 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001307 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001310 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1311 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001312 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001315 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Ian Rogersbc939662013-08-15 10:26:54 -07001318 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1319 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001320 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1321 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 ScopedObjectAccess soa(env);
1323 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Ian Rogersbc939662013-08-15 10:26:54 -07001326 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1327 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001328 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1329 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001331 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001337 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1338 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001339 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001340 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001342 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
Ian Rogersbc939662013-08-15 10:26:54 -07001345 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1346 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001347 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1348 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001349 ScopedObjectAccess soa(env);
1350 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
Ian Rogersbc939662013-08-15 10:26:54 -07001353 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1354 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001355 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1356 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001358 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001361 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001364 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1365 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001366 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001367 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001369 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Ian Rogersbc939662013-08-15 10:26:54 -07001372 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1373 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001374 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1375 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376 ScopedObjectAccess soa(env);
1377 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Ian Rogersbc939662013-08-15 10:26:54 -07001380 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1381 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001382 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1383 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001385 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001388 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001390 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001391 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1392 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001393 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001394 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001396 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Ian Rogersbc939662013-08-15 10:26:54 -07001399 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1400 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001401 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1402 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001403 ScopedObjectAccess soa(env);
1404 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Ian Rogersbc939662013-08-15 10:26:54 -07001407 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1408 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001409 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1410 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001412 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001415 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001417 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001418 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1419 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001420 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 va_end(ap);
1423 }
1424
Brian Carlstromea46f952013-07-30 01:26:50 -07001425 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1426 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001427 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1428 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
1430 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Ian Rogersbc939662013-08-15 10:26:54 -07001433 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1434 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001435 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1436 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001438 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
Ian Rogersbc939662013-08-15 10:26:54 -07001441 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001442 CHECK_NON_NULL_ARGUMENT(java_class);
1443 CHECK_NON_NULL_ARGUMENT(name);
1444 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001446 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001448
Ian Rogersbc939662013-08-15 10:26:54 -07001449 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1450 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001451 CHECK_NON_NULL_ARGUMENT(java_class);
1452 CHECK_NON_NULL_ARGUMENT(name);
1453 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001454 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001455 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001459 CHECK_NON_NULL_ARGUMENT(obj);
1460 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001461 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001462 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1463 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001468 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001469 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001470 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001471 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001475 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1476 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001478 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1479 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1480 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001481 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001485 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001486 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001487 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1488 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001489 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001493 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1494 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001495 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001496 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1497 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001498 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001500#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001501 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001502 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001503 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001504 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001507 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1508 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001510 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1511 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001512 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001513
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001514#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001515 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001516 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001517 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001518 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001521 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001525 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001533 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001537 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001549 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001560 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 }
1575
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001576 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 }
1579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001580 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 }
1583
1584 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 }
1587
1588 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 }
1591
1592 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 }
1595
1596 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 }
1599
1600 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 }
1603
1604 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 }
1607
1608 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 }
1611
1612 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 }
1615
1616 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 }
1619
1620 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 }
1623
1624 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001626 }
1627
1628 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001630 }
1631
1632 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 }
1635
1636 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001638 }
1639
1640 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001642 }
1643
1644 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001651 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001652 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001653 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_end(ap);
1656 return local_result;
1657 }
1658
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001659 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001660 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001662 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001667 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001669 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001673 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001676 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001677 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001678 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001680 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001684 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001686 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001690 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001692 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001698 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001699 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001700 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001702 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001705 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001706 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001708 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001712 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001714 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001720 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001722 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001724 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001728 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001730 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001734 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001736 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001742 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001743 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001744 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001746 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001750 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001752 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001756 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001764 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001765 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001766 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001768 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001772 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001774 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001778 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001780 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001786 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001787 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001788 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001790 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001793 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001794 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001802 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001807 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001808 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001809 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001810 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001812 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001816 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001817 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001818 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001822 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001824 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001829 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001830 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001831 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001832 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001834 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001837 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001838 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001839 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001840 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001845 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001846 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001849 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001851 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001852 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001853 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001854 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 va_end(ap);
1856 }
1857
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001858 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001859 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001861 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001864 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001865 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001867 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes814e4032011-08-23 12:07:56 -07001870 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001871 if (UNLIKELY(char_count < 0)) {
1872 JniAbortF("NewString", "char_count < 0: %d", char_count);
1873 return nullptr;
1874 }
1875 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1876 JniAbortF("NewString", "chars == null && char_count > 0");
1877 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001878 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001880 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
1884 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001885 if (utf == nullptr) {
1886 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001888 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001889 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Elliott Hughes814e4032011-08-23 12:07:56 -07001893 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001894 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001896 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001897 }
1898
1899 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001900 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001902 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001903 }
1904
Ian Rogersbc939662013-08-15 10:26:54 -07001905 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1906 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001907 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001909 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001910 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001913 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1915 memcpy(buf, chars + start, length * sizeof(jchar));
1916 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001917 }
1918
Ian Rogersbc939662013-08-15 10:26:54 -07001919 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1920 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001921 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001923 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001924 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001926 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001927 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001928 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1929 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1930 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001934 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001935 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001936 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1937 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001939 gc::Heap* heap = Runtime::Current()->GetHeap();
1940 if (heap->IsMovableObject(chars)) {
1941 if (is_copy != nullptr) {
1942 *is_copy = JNI_TRUE;
1943 }
1944 int32_t char_count = s->GetLength();
1945 int32_t offset = s->GetOffset();
1946 jchar* bytes = new jchar[char_count];
1947 for (int32_t i = 0; i < char_count; i++) {
1948 bytes[i] = chars->Get(i + offset);
1949 }
1950 return bytes;
1951 } else {
1952 if (is_copy != nullptr) {
1953 *is_copy = JNI_FALSE;
1954 }
1955 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001956 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001957 }
1958
Mathieu Chartier590fee92013-09-13 13:46:47 -07001959 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001960 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001962 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1963 mirror::CharArray* s_chars = s->GetCharArray();
1964 if (chars != (s_chars->GetData() + s->GetOffset())) {
1965 delete[] chars;
1966 }
1967 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001971 CHECK_NON_NULL_ARGUMENT(java_string);
1972 ScopedObjectAccess soa(env);
1973 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1974 mirror::CharArray* chars = s->GetCharArray();
1975 int32_t offset = s->GetOffset();
1976 PinPrimitiveArray(soa, chars);
1977 gc::Heap* heap = Runtime::Current()->GetHeap();
1978 if (heap->IsMovableObject(chars)) {
1979 StackHandleScope<1> hs(soa.Self());
1980 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1981 heap->IncrementDisableMovingGC(soa.Self());
1982 }
1983 if (is_copy != nullptr) {
1984 *is_copy = JNI_FALSE;
1985 }
1986 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001990 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1991 ScopedObjectAccess soa(env);
1992 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
1993 gc::Heap* heap = Runtime::Current()->GetHeap();
1994 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1995 mirror::CharArray* s_chars = s->GetCharArray();
1996 if (heap->IsMovableObject(s_chars)) {
1997 heap->DecrementDisableMovingGC(soa.Self());
1998 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002002 if (java_string == nullptr) {
2003 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002004 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002005 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002006 *is_copy = JNI_TRUE;
2007 }
Ian Rogersef28b142012-11-30 14:22:18 -08002008 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002009 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 size_t byte_count = s->GetUtfLength();
2011 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002012 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002013 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2014 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2015 bytes[byte_count] = '\0';
2016 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002020 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002021 }
2022
Elliott Hughesbd935992011-08-22 11:59:34 -07002023 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002024 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002026 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002027 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002028 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2029 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002030 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002031 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002035 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002036 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002037 mirror::ObjectArray<mirror::Object>* array =
2038 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002039 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Ian Rogersbc939662013-08-15 10:26:54 -07002042 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2043 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002044 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002046 mirror::ObjectArray<mirror::Object>* array =
2047 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2048 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002049 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
2052 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002053 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
2056 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002057 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
2060 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002061 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
2064 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002065 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
2068 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002069 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
2072 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002073 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
2076 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002077 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Ian Rogers1d99e452014-01-02 17:36:41 -08002080 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2081 jobject initial_element) {
2082 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002083 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002084 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002085 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002086 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087
2088 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002089 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002090 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002091 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002092 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002093 if (UNLIKELY(element_class->IsPrimitive())) {
2094 JniAbortF("NewObjectArray", "not an object type: %s",
2095 PrettyDescriptor(element_class).c_str());
2096 return nullptr;
2097 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002098 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002099 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002100 if (UNLIKELY(array_class == nullptr)) {
2101 return nullptr;
2102 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes75770752011-08-24 17:52:38 -07002105 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002106 mirror::ObjectArray<mirror::Object>* result =
2107 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002108 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002109 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002110 if (initial_object != nullptr) {
2111 mirror::Class* element_class = result->GetClass()->GetComponentType();
2112 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2113 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2114 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2115 PrettyDescriptor(element_class).c_str());
2116
2117 } else {
2118 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002119 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002120 }
2121 }
Elliott Hughes75770752011-08-24 17:52:38 -07002122 }
2123 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002124 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
2127 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002128 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Ian Rogersa15e67d2012-02-28 13:51:55 -08002131 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002132 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002133 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002134 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002135 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2136 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2137 PrettyDescriptor(array->GetClass()).c_str());
2138 return nullptr;
2139 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002140 gc::Heap* heap = Runtime::Current()->GetHeap();
2141 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002142 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002143 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002144 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002145 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002147 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002148 *is_copy = JNI_FALSE;
2149 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002150 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002151 }
2152
Ian Rogers2d10b202014-05-12 19:15:18 -07002153 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2154 jint mode) {
2155 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2156 ScopedObjectAccess soa(env);
2157 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2158 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2159 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2160 PrettyDescriptor(array->GetClass()).c_str());
2161 return;
2162 }
2163 const size_t component_size = array->GetClass()->GetComponentSize();
2164 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002165 }
2166
Elliott Hughes75770752011-08-24 17:52:38 -07002167 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002168 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Elliott Hughes75770752011-08-24 17:52:38 -07002171 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002172 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Elliott Hughes75770752011-08-24 17:52:38 -07002175 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002176 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Elliott Hughes75770752011-08-24 17:52:38 -07002179 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002180 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes75770752011-08-24 17:52:38 -07002183 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002184 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes75770752011-08-24 17:52:38 -07002187 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002188 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes75770752011-08-24 17:52:38 -07002191 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002192 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes75770752011-08-24 17:52:38 -07002195 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002196 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Mathieu Chartier590fee92013-09-13 13:46:47 -07002199 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2200 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002201 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2202 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 }
2204
Mathieu Chartier590fee92013-09-13 13:46:47 -07002205 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002206 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Mathieu Chartier590fee92013-09-13 13:46:47 -07002209 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002210 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Mathieu Chartier590fee92013-09-13 13:46:47 -07002213 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2214 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002215 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Mathieu Chartier590fee92013-09-13 13:46:47 -07002218 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2219 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002220 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Mathieu Chartier590fee92013-09-13 13:46:47 -07002223 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002224 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Mathieu Chartier590fee92013-09-13 13:46:47 -07002227 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002228 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Mathieu Chartier590fee92013-09-13 13:46:47 -07002231 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2232 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002233 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Ian Rogersbc939662013-08-15 10:26:54 -07002236 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2237 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002238 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002239 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Ian Rogersbc939662013-08-15 10:26:54 -07002242 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2243 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002244 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Ian Rogersbc939662013-08-15 10:26:54 -07002247 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2248 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002249 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Ian Rogersbc939662013-08-15 10:26:54 -07002252 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2253 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002254 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002255 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2259 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002260 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002261 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Ian Rogersbc939662013-08-15 10:26:54 -07002264 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2265 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002266 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Ian Rogersbc939662013-08-15 10:26:54 -07002269 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2270 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002271 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Ian Rogersbc939662013-08-15 10:26:54 -07002274 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2275 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002276 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002277 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Ian Rogersbc939662013-08-15 10:26:54 -07002280 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2281 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002282 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002283 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285
Ian Rogersbc939662013-08-15 10:26:54 -07002286 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2287 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002288 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
2290
Ian Rogersbc939662013-08-15 10:26:54 -07002291 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2292 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002293 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Ian Rogersbc939662013-08-15 10:26:54 -07002296 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2297 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002298 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002299 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301
Ian Rogersbc939662013-08-15 10:26:54 -07002302 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2303 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002304 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002305 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Ian Rogersbc939662013-08-15 10:26:54 -07002308 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2309 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002310 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312
Ian Rogersbc939662013-08-15 10:26:54 -07002313 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2314 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002315 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 }
2317
Ian Rogersbc939662013-08-15 10:26:54 -07002318 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2319 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002320 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002321 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002322 }
2323
Ian Rogersbc939662013-08-15 10:26:54 -07002324 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2325 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002326 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2327 }
2328
Ian Rogersbc939662013-08-15 10:26:54 -07002329 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2330 jint method_count, bool return_errors) {
2331 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002332 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002333 return JNI_ERR; // Not reached.
2334 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002335 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002336 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002337 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002338 if (UNLIKELY(method_count == 0)) {
2339 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2340 << PrettyDescriptor(c);
2341 return JNI_OK;
2342 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002343 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002344 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002345 const char* name = methods[i].name;
2346 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002347 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002348 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002349 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 ++sig;
2351 }
2352
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002353 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2354 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002355 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002357 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002358 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002359 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002360 << PrettyDescriptor(c) << "." << name << sig << " in "
2361 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002362 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002363 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002364 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002365 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002366 << PrettyDescriptor(c) << "." << name << sig
2367 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002368 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 return JNI_ERR;
2370 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002371
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002372 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002373
Ian Rogers1eb512d2013-10-18 15:42:20 -07002374 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002375 }
2376 return JNI_OK;
2377 }
2378
Elliott Hughes5174fe62011-08-23 15:12:35 -07002379 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002380 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002381 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002382 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002383
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002384 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002385
Ian Rogers2d10b202014-05-12 19:15:18 -07002386 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002387 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002388 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002389 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002390 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002391 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002392 }
2393 }
2394 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002395 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002396 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002397 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002398 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002399 }
2400 }
2401
Ian Rogers2d10b202014-05-12 19:15:18 -07002402 if (unregistered_count == 0) {
2403 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2404 << PrettyDescriptor(c) << "' that contains no native methods";
2405 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002406 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002407 }
2408
Ian Rogers719d1a32014-03-06 12:13:39 -08002409 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002410 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002411 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002412 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2413 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002414 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002415 return JNI_ERR;
2416 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002417 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002418 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002419 }
2420
Ian Rogers719d1a32014-03-06 12:13:39 -08002421 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002422 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002423 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002424 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002425 o->MonitorExit(soa.Self());
2426 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002427 return JNI_ERR;
2428 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002429 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002430 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002431 }
2432
2433 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002434 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002435 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002436 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002437 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002438 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002439 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002440 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002441 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002442 }
2443
Elliott Hughescdf53122011-08-19 15:46:09 -07002444 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002445 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002446 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002447 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002448 if (address == nullptr && capacity != 0) {
2449 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002450 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002451
Ian Rogers936b37f2014-02-14 00:52:24 -08002452 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002453 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002454 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002455 jint capacity_arg = static_cast<jint>(capacity);
2456
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002457 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2458 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002459 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002460 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002461 }
2462
Elliott Hughesb465ab02011-08-24 11:21:21 -07002463 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002464 return reinterpret_cast<void*>(env->GetLongField(
2465 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002466 }
2467
Elliott Hughesb465ab02011-08-24 11:21:21 -07002468 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002469 return static_cast<jlong>(env->GetIntField(
2470 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002471 }
2472
Elliott Hughesb465ab02011-08-24 11:21:21 -07002473 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002474 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002475
2476 // Do we definitely know what kind of reference this is?
2477 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2478 IndirectRefKind kind = GetIndirectRefKind(ref);
2479 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002480 case kLocal: {
2481 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002482 // The local refs don't need a read barrier.
2483 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2484 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002485 return JNILocalRefType;
2486 }
2487 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002488 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002489 case kGlobal:
2490 return JNIGlobalRefType;
2491 case kWeakGlobal:
2492 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002493 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002494 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002495 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002496 return JNILocalRefType;
2497 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002498 return JNIInvalidRefType;
2499 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002500 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2501 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002502 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002503
2504 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002505 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2506 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002507 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002508 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002509 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2510 return JNI_ERR;
2511 }
2512 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002513 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002514 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2515 if (!okay) {
2516 soa.Self()->ThrowOutOfMemoryError(caller);
2517 }
2518 return okay ? JNI_OK : JNI_ERR;
2519 }
2520
2521 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002522 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002523 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002524 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002525 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002526 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002527 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002528 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002529 return soa.AddLocalReference<JniT>(result);
2530 }
2531
Ian Rogers2d10b202014-05-12 19:15:18 -07002532 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2533 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2534 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002535 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002536 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002537 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2538 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2539 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2540 PrettyDescriptor(array->GetClass()).c_str());
2541 return nullptr;
2542 }
2543 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2544 return array;
2545 }
2546
2547 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2548 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2549 CHECK_NON_NULL_ARGUMENT(java_array);
2550 ScopedObjectAccess soa(env);
2551 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2552 "GetArrayElements",
2553 "get");
2554 if (UNLIKELY(array == nullptr)) {
2555 return nullptr;
2556 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002557 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002558 // Only make a copy if necessary.
2559 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2560 if (is_copy != nullptr) {
2561 *is_copy = JNI_TRUE;
2562 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002563 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002564 size_t size = array->GetLength() * component_size;
2565 void* data = new uint64_t[RoundUp(size, 8) / 8];
2566 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002567 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002568 } else {
2569 if (is_copy != nullptr) {
2570 *is_copy = JNI_FALSE;
2571 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002572 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002573 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002574 }
2575
Ian Rogers2d10b202014-05-12 19:15:18 -07002576 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002577 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002578 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002579 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002580 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2581 "ReleaseArrayElements",
2582 "release");
2583 if (array == nullptr) {
2584 return;
2585 }
2586 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2587 }
2588
2589 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2590 size_t component_size, void* elements, jint mode)
2591 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002592 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002593 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002594 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002595 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002596 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2597 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002598 if (is_copy) {
2599 // Sanity check: If elements is not the same as the java array's data, it better not be a
2600 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2601 // copies we make?
2602 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2603 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2604 reinterpret_cast<void*>(elements), array_data);
2605 return;
2606 }
2607 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002608 // Don't need to copy if we had a direct pointer.
2609 if (mode != JNI_ABORT && is_copy) {
2610 memcpy(array_data, elements, bytes);
2611 }
2612 if (mode != JNI_COMMIT) {
2613 if (is_copy) {
2614 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002615 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002616 // Non copy to a movable object must means that we had disabled the moving GC.
2617 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002618 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002619 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002620 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002621 }
2622
Ian Rogers2d10b202014-05-12 19:15:18 -07002623 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2624 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2625 jsize start, jsize length, ElementT* buf) {
2626 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2627 ScopedObjectAccess soa(env);
2628 ArtArrayT* array =
2629 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2630 "GetPrimitiveArrayRegion",
2631 "get region of");
2632 if (array != nullptr) {
2633 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2634 ThrowAIOOBE(soa, array, start, length, "src");
2635 } else {
2636 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2637 ElementT* data = array->GetData();
2638 memcpy(buf, data + start, length * sizeof(ElementT));
2639 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002640 }
2641 }
2642
Ian Rogers2d10b202014-05-12 19:15:18 -07002643 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2644 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2645 jsize start, jsize length, const ElementT* buf) {
2646 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2647 ScopedObjectAccess soa(env);
2648 ArtArrayT* array =
2649 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2650 "SetPrimitiveArrayRegion",
2651 "set region of");
2652 if (array != nullptr) {
2653 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2654 ThrowAIOOBE(soa, array, start, length, "dst");
2655 } else {
2656 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2657 ElementT* data = array->GetData();
2658 memcpy(data + start, buf, length * sizeof(ElementT));
2659 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002660 }
2661 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002662};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002663
Elliott Hughes88c5c352012-03-15 18:49:48 -07002664const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002665 nullptr, // reserved0.
2666 nullptr, // reserved1.
2667 nullptr, // reserved2.
2668 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002669 JNI::GetVersion,
2670 JNI::DefineClass,
2671 JNI::FindClass,
2672 JNI::FromReflectedMethod,
2673 JNI::FromReflectedField,
2674 JNI::ToReflectedMethod,
2675 JNI::GetSuperclass,
2676 JNI::IsAssignableFrom,
2677 JNI::ToReflectedField,
2678 JNI::Throw,
2679 JNI::ThrowNew,
2680 JNI::ExceptionOccurred,
2681 JNI::ExceptionDescribe,
2682 JNI::ExceptionClear,
2683 JNI::FatalError,
2684 JNI::PushLocalFrame,
2685 JNI::PopLocalFrame,
2686 JNI::NewGlobalRef,
2687 JNI::DeleteGlobalRef,
2688 JNI::DeleteLocalRef,
2689 JNI::IsSameObject,
2690 JNI::NewLocalRef,
2691 JNI::EnsureLocalCapacity,
2692 JNI::AllocObject,
2693 JNI::NewObject,
2694 JNI::NewObjectV,
2695 JNI::NewObjectA,
2696 JNI::GetObjectClass,
2697 JNI::IsInstanceOf,
2698 JNI::GetMethodID,
2699 JNI::CallObjectMethod,
2700 JNI::CallObjectMethodV,
2701 JNI::CallObjectMethodA,
2702 JNI::CallBooleanMethod,
2703 JNI::CallBooleanMethodV,
2704 JNI::CallBooleanMethodA,
2705 JNI::CallByteMethod,
2706 JNI::CallByteMethodV,
2707 JNI::CallByteMethodA,
2708 JNI::CallCharMethod,
2709 JNI::CallCharMethodV,
2710 JNI::CallCharMethodA,
2711 JNI::CallShortMethod,
2712 JNI::CallShortMethodV,
2713 JNI::CallShortMethodA,
2714 JNI::CallIntMethod,
2715 JNI::CallIntMethodV,
2716 JNI::CallIntMethodA,
2717 JNI::CallLongMethod,
2718 JNI::CallLongMethodV,
2719 JNI::CallLongMethodA,
2720 JNI::CallFloatMethod,
2721 JNI::CallFloatMethodV,
2722 JNI::CallFloatMethodA,
2723 JNI::CallDoubleMethod,
2724 JNI::CallDoubleMethodV,
2725 JNI::CallDoubleMethodA,
2726 JNI::CallVoidMethod,
2727 JNI::CallVoidMethodV,
2728 JNI::CallVoidMethodA,
2729 JNI::CallNonvirtualObjectMethod,
2730 JNI::CallNonvirtualObjectMethodV,
2731 JNI::CallNonvirtualObjectMethodA,
2732 JNI::CallNonvirtualBooleanMethod,
2733 JNI::CallNonvirtualBooleanMethodV,
2734 JNI::CallNonvirtualBooleanMethodA,
2735 JNI::CallNonvirtualByteMethod,
2736 JNI::CallNonvirtualByteMethodV,
2737 JNI::CallNonvirtualByteMethodA,
2738 JNI::CallNonvirtualCharMethod,
2739 JNI::CallNonvirtualCharMethodV,
2740 JNI::CallNonvirtualCharMethodA,
2741 JNI::CallNonvirtualShortMethod,
2742 JNI::CallNonvirtualShortMethodV,
2743 JNI::CallNonvirtualShortMethodA,
2744 JNI::CallNonvirtualIntMethod,
2745 JNI::CallNonvirtualIntMethodV,
2746 JNI::CallNonvirtualIntMethodA,
2747 JNI::CallNonvirtualLongMethod,
2748 JNI::CallNonvirtualLongMethodV,
2749 JNI::CallNonvirtualLongMethodA,
2750 JNI::CallNonvirtualFloatMethod,
2751 JNI::CallNonvirtualFloatMethodV,
2752 JNI::CallNonvirtualFloatMethodA,
2753 JNI::CallNonvirtualDoubleMethod,
2754 JNI::CallNonvirtualDoubleMethodV,
2755 JNI::CallNonvirtualDoubleMethodA,
2756 JNI::CallNonvirtualVoidMethod,
2757 JNI::CallNonvirtualVoidMethodV,
2758 JNI::CallNonvirtualVoidMethodA,
2759 JNI::GetFieldID,
2760 JNI::GetObjectField,
2761 JNI::GetBooleanField,
2762 JNI::GetByteField,
2763 JNI::GetCharField,
2764 JNI::GetShortField,
2765 JNI::GetIntField,
2766 JNI::GetLongField,
2767 JNI::GetFloatField,
2768 JNI::GetDoubleField,
2769 JNI::SetObjectField,
2770 JNI::SetBooleanField,
2771 JNI::SetByteField,
2772 JNI::SetCharField,
2773 JNI::SetShortField,
2774 JNI::SetIntField,
2775 JNI::SetLongField,
2776 JNI::SetFloatField,
2777 JNI::SetDoubleField,
2778 JNI::GetStaticMethodID,
2779 JNI::CallStaticObjectMethod,
2780 JNI::CallStaticObjectMethodV,
2781 JNI::CallStaticObjectMethodA,
2782 JNI::CallStaticBooleanMethod,
2783 JNI::CallStaticBooleanMethodV,
2784 JNI::CallStaticBooleanMethodA,
2785 JNI::CallStaticByteMethod,
2786 JNI::CallStaticByteMethodV,
2787 JNI::CallStaticByteMethodA,
2788 JNI::CallStaticCharMethod,
2789 JNI::CallStaticCharMethodV,
2790 JNI::CallStaticCharMethodA,
2791 JNI::CallStaticShortMethod,
2792 JNI::CallStaticShortMethodV,
2793 JNI::CallStaticShortMethodA,
2794 JNI::CallStaticIntMethod,
2795 JNI::CallStaticIntMethodV,
2796 JNI::CallStaticIntMethodA,
2797 JNI::CallStaticLongMethod,
2798 JNI::CallStaticLongMethodV,
2799 JNI::CallStaticLongMethodA,
2800 JNI::CallStaticFloatMethod,
2801 JNI::CallStaticFloatMethodV,
2802 JNI::CallStaticFloatMethodA,
2803 JNI::CallStaticDoubleMethod,
2804 JNI::CallStaticDoubleMethodV,
2805 JNI::CallStaticDoubleMethodA,
2806 JNI::CallStaticVoidMethod,
2807 JNI::CallStaticVoidMethodV,
2808 JNI::CallStaticVoidMethodA,
2809 JNI::GetStaticFieldID,
2810 JNI::GetStaticObjectField,
2811 JNI::GetStaticBooleanField,
2812 JNI::GetStaticByteField,
2813 JNI::GetStaticCharField,
2814 JNI::GetStaticShortField,
2815 JNI::GetStaticIntField,
2816 JNI::GetStaticLongField,
2817 JNI::GetStaticFloatField,
2818 JNI::GetStaticDoubleField,
2819 JNI::SetStaticObjectField,
2820 JNI::SetStaticBooleanField,
2821 JNI::SetStaticByteField,
2822 JNI::SetStaticCharField,
2823 JNI::SetStaticShortField,
2824 JNI::SetStaticIntField,
2825 JNI::SetStaticLongField,
2826 JNI::SetStaticFloatField,
2827 JNI::SetStaticDoubleField,
2828 JNI::NewString,
2829 JNI::GetStringLength,
2830 JNI::GetStringChars,
2831 JNI::ReleaseStringChars,
2832 JNI::NewStringUTF,
2833 JNI::GetStringUTFLength,
2834 JNI::GetStringUTFChars,
2835 JNI::ReleaseStringUTFChars,
2836 JNI::GetArrayLength,
2837 JNI::NewObjectArray,
2838 JNI::GetObjectArrayElement,
2839 JNI::SetObjectArrayElement,
2840 JNI::NewBooleanArray,
2841 JNI::NewByteArray,
2842 JNI::NewCharArray,
2843 JNI::NewShortArray,
2844 JNI::NewIntArray,
2845 JNI::NewLongArray,
2846 JNI::NewFloatArray,
2847 JNI::NewDoubleArray,
2848 JNI::GetBooleanArrayElements,
2849 JNI::GetByteArrayElements,
2850 JNI::GetCharArrayElements,
2851 JNI::GetShortArrayElements,
2852 JNI::GetIntArrayElements,
2853 JNI::GetLongArrayElements,
2854 JNI::GetFloatArrayElements,
2855 JNI::GetDoubleArrayElements,
2856 JNI::ReleaseBooleanArrayElements,
2857 JNI::ReleaseByteArrayElements,
2858 JNI::ReleaseCharArrayElements,
2859 JNI::ReleaseShortArrayElements,
2860 JNI::ReleaseIntArrayElements,
2861 JNI::ReleaseLongArrayElements,
2862 JNI::ReleaseFloatArrayElements,
2863 JNI::ReleaseDoubleArrayElements,
2864 JNI::GetBooleanArrayRegion,
2865 JNI::GetByteArrayRegion,
2866 JNI::GetCharArrayRegion,
2867 JNI::GetShortArrayRegion,
2868 JNI::GetIntArrayRegion,
2869 JNI::GetLongArrayRegion,
2870 JNI::GetFloatArrayRegion,
2871 JNI::GetDoubleArrayRegion,
2872 JNI::SetBooleanArrayRegion,
2873 JNI::SetByteArrayRegion,
2874 JNI::SetCharArrayRegion,
2875 JNI::SetShortArrayRegion,
2876 JNI::SetIntArrayRegion,
2877 JNI::SetLongArrayRegion,
2878 JNI::SetFloatArrayRegion,
2879 JNI::SetDoubleArrayRegion,
2880 JNI::RegisterNatives,
2881 JNI::UnregisterNatives,
2882 JNI::MonitorEnter,
2883 JNI::MonitorExit,
2884 JNI::GetJavaVM,
2885 JNI::GetStringRegion,
2886 JNI::GetStringUTFRegion,
2887 JNI::GetPrimitiveArrayCritical,
2888 JNI::ReleasePrimitiveArrayCritical,
2889 JNI::GetStringCritical,
2890 JNI::ReleaseStringCritical,
2891 JNI::NewWeakGlobalRef,
2892 JNI::DeleteWeakGlobalRef,
2893 JNI::ExceptionCheck,
2894 JNI::NewDirectByteBuffer,
2895 JNI::GetDirectBufferAddress,
2896 JNI::GetDirectBufferCapacity,
2897 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002898};
2899
Elliott Hughes75770752011-08-24 17:52:38 -07002900JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002901 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002902 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002903 local_ref_cookie(IRT_FIRST_SEGMENT),
2904 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002905 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002906 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002907 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002908 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002909 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002910 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002911 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002912}
2913
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002914JNIEnvExt::~JNIEnvExt() {
2915}
2916
Mathieu Chartier590fee92013-09-13 13:46:47 -07002917jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2918 if (obj == nullptr) {
2919 return nullptr;
2920 }
2921 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2922}
2923
2924void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2925 if (obj != nullptr) {
2926 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2927 }
2928}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002929void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2930 check_jni = enabled;
2931 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002932}
2933
Elliott Hughes73e66f72012-05-09 09:34:45 -07002934void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2935 locals.Dump(os);
2936 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002937}
2938
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002939void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2940 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002941 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002942 stacked_local_ref_cookies.push_back(local_ref_cookie);
2943 local_ref_cookie = locals.GetSegmentState();
2944}
2945
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002946void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002947 locals.SetSegmentState(local_ref_cookie);
2948 local_ref_cookie = stacked_local_ref_cookies.back();
2949 stacked_local_ref_cookies.pop_back();
2950}
2951
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002952Offset JNIEnvExt::SegmentStateOffset() {
2953 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2954 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2955}
2956
Carl Shapiroea4dca82011-08-01 13:45:38 -07002957// JNI Invocation interface.
2958
Brian Carlstrombddf9762013-05-14 11:35:37 -07002959extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002960 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002961 if (IsBadJniVersion(args->version)) {
2962 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002963 return JNI_EVERSION;
2964 }
2965 Runtime::Options options;
2966 for (int i = 0; i < args->nOptions; ++i) {
2967 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002968 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002969 }
2970 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002971 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002972 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002973 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002974 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002975 bool started = runtime->Start();
2976 if (!started) {
2977 delete Thread::Current()->GetJniEnv();
2978 delete runtime->GetJavaVM();
2979 LOG(WARNING) << "CreateJavaVM failed";
2980 return JNI_ERR;
2981 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002982 *p_env = Thread::Current()->GetJniEnv();
2983 *p_vm = runtime->GetJavaVM();
2984 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002985}
2986
Elliott Hughesf2682d52011-08-15 16:37:04 -07002987extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002988 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002989 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002990 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002991 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002992 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002993 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002994 }
2995 return JNI_OK;
2996}
2997
2998// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002999extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003000 return JNI_ERR;
3001}
3002
Elliott Hughescdf53122011-08-19 15:46:09 -07003003class JII {
3004 public:
3005 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003006 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003007 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003008 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003009 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3010 delete raw_vm->runtime;
3011 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003012 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003013
Elliott Hughescdf53122011-08-19 15:46:09 -07003014 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003015 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003016 }
3017
3018 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003019 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003020 }
3021
3022 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003023 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003024 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003025 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003026 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3027 Runtime* runtime = raw_vm->runtime;
3028 runtime->DetachCurrentThread();
3029 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003030 }
3031
3032 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003033 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3034 // and unlike other calls that take a JNI version doesn't care if you supply
3035 // JNI_VERSION_1_1, which we don't otherwise support.
3036 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003037 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003038 return JNI_EVERSION;
3039 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003040 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003041 return JNI_ERR;
3042 }
3043 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003044 if (thread == nullptr) {
3045 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003046 return JNI_EDETACHED;
3047 }
3048 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003049 return JNI_OK;
3050 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003051};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003052
Elliott Hughes88c5c352012-03-15 18:49:48 -07003053const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003054 nullptr, // reserved0
3055 nullptr, // reserved1
3056 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003057 JII::DestroyJavaVM,
3058 JII::AttachCurrentThread,
3059 JII::DetachCurrentThread,
3060 JII::GetEnv,
3061 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003062};
3063
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003064JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003065 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003066 check_jni_abort_hook(nullptr),
3067 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003068 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003069 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003070 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003071 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003072 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003073 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003074 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003075 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003076 libraries(new Libraries),
3077 weak_globals_lock_("JNI weak global reference table lock"),
3078 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3079 allow_new_weak_globals_(true),
3080 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003081 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003082 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003083 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003084 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003085}
3086
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003087JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003088 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003089}
3090
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003091jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3092 if (obj == nullptr) {
3093 return nullptr;
3094 }
3095 MutexLock mu(self, weak_globals_lock_);
3096 while (UNLIKELY(!allow_new_weak_globals_)) {
3097 weak_globals_add_condition_.WaitHoldingLocks(self);
3098 }
3099 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3100 return reinterpret_cast<jweak>(ref);
3101}
3102
3103void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3104 MutexLock mu(self, weak_globals_lock_);
3105 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3106 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3107 << "failed to find entry";
3108 }
3109}
3110
Elliott Hughes88c5c352012-03-15 18:49:48 -07003111void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3112 check_jni = enabled;
3113 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003114}
3115
Elliott Hughesae80b492012-04-24 10:43:17 -07003116void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3117 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3118 if (force_copy) {
3119 os << " (with forcecopy)";
3120 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003121 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003122 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003123 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003124 os << "; pins=" << pin_table.Size();
3125 }
3126 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003127 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003128 os << "; globals=" << globals.Capacity();
3129 }
3130 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003131 MutexLock mu(self, weak_globals_lock_);
3132 if (weak_globals_.Capacity() > 0) {
3133 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003134 }
3135 }
3136 os << '\n';
3137
3138 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003139 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003140 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3141 }
3142}
3143
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003144void JavaVMExt::DisallowNewWeakGlobals() {
3145 MutexLock mu(Thread::Current(), weak_globals_lock_);
3146 allow_new_weak_globals_ = false;
3147}
3148
3149void JavaVMExt::AllowNewWeakGlobals() {
3150 Thread* self = Thread::Current();
3151 MutexLock mu(self, weak_globals_lock_);
3152 allow_new_weak_globals_ = true;
3153 weak_globals_add_condition_.Broadcast(self);
3154}
3155
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003156mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3157 MutexLock mu(self, weak_globals_lock_);
3158 while (UNLIKELY(!allow_new_weak_globals_)) {
3159 weak_globals_add_condition_.WaitHoldingLocks(self);
3160 }
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003161 // The weak globals do need a read barrier as they are weak roots.
3162 mirror::Object* obj = weak_globals_.Get<kWithReadBarrier>(ref);
3163 return obj;
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003164}
3165
Elliott Hughes73e66f72012-05-09 09:34:45 -07003166void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003167 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003168 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003169 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003170 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003171 }
3172 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003173 MutexLock mu(self, weak_globals_lock_);
3174 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003175 }
3176 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003177 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003178 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003179 }
3180}
3181
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003182bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003183 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003184 std::string* detail) {
3185 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003186
3187 // See if we've already loaded this library. If we have, and the class loader
3188 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003189 // TODO: for better results we should canonicalize the pathname (or even compare
3190 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003191 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003192 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003193 {
3194 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003195 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003196 library = libraries->Get(path);
3197 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003198 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003199 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003200 // The library will be associated with class_loader. The JNI
3201 // spec says we can't load the same library into more than one
3202 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003203 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003204 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003205 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003206 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003207 return false;
3208 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003209 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003210 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003211 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003212 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003213 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003214 return false;
3215 }
3216 return true;
3217 }
3218
3219 // Open the shared library. Because we're using a full path, the system
3220 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3221 // resolve this library's dependencies though.)
3222
3223 // Failures here are expected when java.library.path has several entries
3224 // and we have to hunt for the lib.
3225
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003226 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3227 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3228 // dlopen) becomes zero from dlclose.
3229
Elliott Hughescdf53122011-08-19 15:46:09 -07003230 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003231 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003232 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003233 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003234 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003235
Elliott Hughes84b2f142012-09-27 09:16:28 -07003236 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003237
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003238 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003239 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003240 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003241 return false;
3242 }
3243
3244 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003245 // TODO: move the locking (and more of this logic) into Libraries.
3246 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003247 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003248 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003249 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003250 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003251 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003252 libraries->Put(path, library);
3253 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003254 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003255 }
3256 if (!created_library) {
3257 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003258 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003259 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003260 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003261
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003262 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003263 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003264
Elliott Hughes79353722013-08-02 16:52:18 -07003265 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003266 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003267 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003268 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003269 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003270 } else {
3271 // Call JNI_OnLoad. We have to override the current class
3272 // loader, which will always be "null" since the stuff at the
3273 // top of the stack is around Runtime.loadLibrary(). (See
3274 // the comments in the JNI FindClass function.)
3275 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3276 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003277 StackHandleScope<1> hs(self);
3278 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3279 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003280
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003281 int version = 0;
3282 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003283 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003284 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003285 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003286 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003287
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003288 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003289
Elliott Hughes79353722013-08-02 16:52:18 -07003290 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003291 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003292 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003293 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003294 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003295 // It's unwise to call dlclose() here, but we can mark it
3296 // as bad and ensure that future load attempts will fail.
3297 // We don't know how far JNI_OnLoad got, so there could
3298 // be some partially-initialized stuff accessible through
3299 // newly-registered native method calls. We could try to
3300 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003301 } else {
3302 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003303 }
Elliott Hughes79353722013-08-02 16:52:18 -07003304 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003305 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003306 }
3307
Elliott Hughes79353722013-08-02 16:52:18 -07003308 library->SetResult(was_successful);
3309 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003310}
3311
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003312void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003313 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003314 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003315 // If this is a static method, it could be called before the class
3316 // has been initialized.
3317 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003318 c = EnsureInitialized(Thread::Current(), c);
3319 if (c == nullptr) {
3320 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003321 }
3322 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003323 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003324 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003325 std::string detail;
3326 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003327 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003328 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003329 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003330 native_method = libraries->FindNativeMethod(m, detail);
3331 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003332 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003333 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003334 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3335 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003336 }
3337 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003338}
3339
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003340void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003341 MutexLock mu(Thread::Current(), weak_globals_lock_);
3342 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003343 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003344 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003345 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003346 if (new_obj == nullptr) {
3347 new_obj = kClearedJniWeakGlobal;
3348 }
3349 *entry = new_obj;
3350 }
3351}
3352
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003353void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003354 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003355 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003356 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003357 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003358 }
3359 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003360 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003361 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003362 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003363 {
3364 MutexLock mu(self, libraries_lock);
3365 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003366 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003367 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003368 // The weak_globals table is visited by the GC itself (because it mutates the table).
3369}
3370
Elliott Hughesc8fece32013-01-02 11:27:23 -08003371void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003372 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003373 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003374 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003375 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3376 }
3377 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3378}
3379
Ian Rogersdf20fe02011-07-20 20:34:16 -07003380} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003381
3382std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3383 switch (rhs) {
3384 case JNIInvalidRefType:
3385 os << "JNIInvalidRefType";
3386 return os;
3387 case JNILocalRefType:
3388 os << "JNILocalRefType";
3389 return os;
3390 case JNIGlobalRefType:
3391 os << "JNIGlobalRefType";
3392 return os;
3393 case JNIWeakGlobalRefType:
3394 os << "JNIWeakGlobalRefType";
3395 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003396 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003397 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003398 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003399 }
3400}