blob: b51e1d586cb7b65d650c840ca8de856286cbafff [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) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700672 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 }
674
675 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700677
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700678 StackHandleScope<3> hs(soa.Self());
679 // TODO: Use nullptr instead of null handles?
680 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
681 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
682 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800683 uint32_t old_throw_dex_pc;
684 {
685 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800686 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700687 old_throw_this_object.Assign(old_throw_location.GetThis());
688 old_throw_method.Assign(old_throw_location.GetMethod());
689 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800690 old_throw_dex_pc = old_throw_location.GetDexPc();
691 soa.Self()->ClearException();
692 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800693 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700694 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700695 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
696 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800697 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700698 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700699 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700700 } else {
701 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800702 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800703 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700704 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800705 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700706 }
707 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700708 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800709 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700710
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700711 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800716 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 }
719
Ian Rogers25e8b912012-09-07 11:31:36 -0700720 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 LOG(FATAL) << "JNI FatalError called: " << msg;
722 }
723
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700724 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700725 // TODO: SOA may not be necessary but I do it to please lock annotations.
726 ScopedObjectAccess soa(env);
727 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700728 return JNI_ERR;
729 }
Ian Rogersef28b142012-11-30 14:22:18 -0800730 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 return JNI_OK;
732 }
733
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700734 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800736 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 soa.Env()->PopFrame();
738 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 }
740
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700741 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700742 // TODO: SOA may not be necessary but I do it to please lock annotations.
743 ScopedObjectAccess soa(env);
744 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700745 }
746
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700748 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800749 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800750 // Check for null after decoding the object to handle cleared weak globals.
751 if (decoded_obj == nullptr) {
752 return nullptr;
753 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700756 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 return reinterpret_cast<jobject>(ref);
759 }
760
761 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800762 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 return;
764 }
Ian Rogersef28b142012-11-30 14:22:18 -0800765 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800767 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700768 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700769
770 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
771 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
772 << "failed to find entry";
773 }
774 }
775
776 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800778 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 }
780
781 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700782 if (obj != nullptr) {
783 ScopedObjectAccess soa(env);
784 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 }
786 }
787
788 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700789 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800790 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800791 // Check for null after decoding the object to handle cleared weak globals.
792 if (decoded_obj == nullptr) {
793 return nullptr;
794 }
795 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 }
797
798 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800799 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 return;
801 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700802 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800803 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700804
Ian Rogersef28b142012-11-30 14:22:18 -0800805 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 if (!locals.Remove(cookie, obj)) {
807 // Attempting to delete a local reference that is not in the
808 // topmost local reference frame is a no-op. DeleteLocalRef returns
809 // void and doesn't throw any exceptions, but we should probably
810 // complain about it so the user will notice that things aren't
811 // going quite the way they expect.
812 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
813 << "failed to find entry";
814 }
815 }
816
817 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700818 if (obj1 == obj2) {
819 return JNI_TRUE;
820 } else {
821 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800822 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
823 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700824 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 }
826
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700827 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700828 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700829 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800830 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800831 if (c == nullptr) {
832 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700833 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700834 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 }
836
Ian Rogersbc939662013-08-15 10:26:54 -0700837 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700840 CHECK_NON_NULL_ARGUMENT(java_class);
841 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700842 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 va_end(args);
844 return result;
845 }
846
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700848 CHECK_NON_NULL_ARGUMENT(java_class);
849 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800851 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800852 if (c == nullptr) {
853 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700854 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800855 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800856 if (result == nullptr) {
857 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700858 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700860 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800861 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800862 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700863 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800864 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700868 CHECK_NON_NULL_ARGUMENT(java_class);
869 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700870 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800871 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800872 if (c == nullptr) {
873 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700874 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800875 mirror::Object* result = c->AllocObject(soa.Self());
876 if (result == nullptr) {
877 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700878 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700879 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700880 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800881 if (soa.Self()->IsExceptionPending()) {
882 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700883 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800884 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 }
886
Ian Rogersbc939662013-08-15 10:26:54 -0700887 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700888 CHECK_NON_NULL_ARGUMENT(java_class);
889 CHECK_NON_NULL_ARGUMENT(name);
890 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700892 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 }
894
Ian Rogersbc939662013-08-15 10:26:54 -0700895 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
896 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700897 CHECK_NON_NULL_ARGUMENT(java_class);
898 CHECK_NON_NULL_ARGUMENT(name);
899 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700900 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700901 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 }
903
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 va_list ap;
906 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700907 CHECK_NON_NULL_ARGUMENT(obj);
908 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700909 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700916 CHECK_NON_NULL_ARGUMENT(obj);
917 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
919 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
920 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700924 CHECK_NON_NULL_ARGUMENT(obj);
925 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700927 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
928 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 va_list ap;
934 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700935 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
936 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700937 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700940 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700944 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
945 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 ScopedObjectAccess soa(env);
947 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700951 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
952 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700954 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
955 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 va_list ap;
960 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700961 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
962 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700963 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700966 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700970 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
971 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 ScopedObjectAccess soa(env);
973 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700977 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
978 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700980 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
981 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_list ap;
986 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700987 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
988 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700989 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700992 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700996 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
997 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ScopedObjectAccess soa(env);
999 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001003 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001006 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1007 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_list ap;
1012 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001013 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1014 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001015 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001018 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001022 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 ScopedObjectAccess soa(env);
1025 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001029 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1030 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001032 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1033 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_list ap;
1038 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001039 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1040 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001041 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001044 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001048 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1049 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 ScopedObjectAccess soa(env);
1051 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001055 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1056 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001058 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1059 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_list ap;
1064 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001065 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1066 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001067 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001070 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001074 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1075 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001081 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1082 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001084 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1085 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_list ap;
1090 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001091 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1092 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001093 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001096 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001100 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1101 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 ScopedObjectAccess soa(env);
1103 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001107 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1108 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001110 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1111 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001117 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1118 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001119 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001122 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001126 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1127 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 ScopedObjectAccess soa(env);
1129 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001133 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001136 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1137 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_list ap;
1142 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001143 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1144 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001145 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001146 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001151 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1152 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(env);
1154 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001158 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1159 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001161 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001164 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001167 CHECK_NON_NULL_ARGUMENT(obj);
1168 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001169 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1171 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 va_end(ap);
1173 return local_result;
1174 }
1175
Ian Rogersbc939662013-08-15 10:26:54 -07001176 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1177 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001178 CHECK_NON_NULL_ARGUMENT(obj);
1179 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1182 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Ian Rogersbc939662013-08-15 10:26:54 -07001185 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1186 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001187 CHECK_NON_NULL_ARGUMENT(obj);
1188 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001190 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001191 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Ian Rogersbc939662013-08-15 10:26:54 -07001194 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1195 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001198 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1199 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001200 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001203 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Ian Rogersbc939662013-08-15 10:26:54 -07001206 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1207 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001208 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1209 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 ScopedObjectAccess soa(env);
1211 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
Ian Rogersbc939662013-08-15 10:26:54 -07001214 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1215 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001216 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1217 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001219 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001222 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001225 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1226 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001227 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001230 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
Ian Rogersbc939662013-08-15 10:26:54 -07001233 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1234 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001235 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1236 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
1238 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Ian Rogersbc939662013-08-15 10:26:54 -07001241 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1242 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001243 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1244 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001246 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001249 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001252 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1253 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001254 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001257 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Ian Rogersbc939662013-08-15 10:26:54 -07001260 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1261 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001262 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 ScopedObjectAccess soa(env);
1265 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Ian Rogersbc939662013-08-15 10:26:54 -07001268 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1269 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001270 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1271 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001273 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001276 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001279 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1280 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001281 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001284 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Ian Rogersbc939662013-08-15 10:26:54 -07001287 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1288 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001289 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1290 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Ian Rogersbc939662013-08-15 10:26:54 -07001295 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1296 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001297 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1298 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001300 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001303 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001306 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1307 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001308 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001311 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Ian Rogersbc939662013-08-15 10:26:54 -07001314 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1315 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001316 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1317 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 ScopedObjectAccess soa(env);
1319 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Ian Rogersbc939662013-08-15 10:26:54 -07001322 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1323 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001324 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1325 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001327 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001333 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1334 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001335 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001338 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Ian Rogersbc939662013-08-15 10:26:54 -07001341 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1342 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001343 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1344 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001345 ScopedObjectAccess soa(env);
1346 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
Ian Rogersbc939662013-08-15 10:26:54 -07001349 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1350 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001351 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1352 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001354 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001357 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001360 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1361 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001362 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001363 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001365 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Ian Rogersbc939662013-08-15 10:26:54 -07001368 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1369 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001370 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1371 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372 ScopedObjectAccess soa(env);
1373 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
Ian Rogersbc939662013-08-15 10:26:54 -07001376 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1377 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001378 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1379 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001380 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001381 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001384 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001386 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001387 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1388 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001389 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001392 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Ian Rogersbc939662013-08-15 10:26:54 -07001395 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1396 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001397 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1398 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 ScopedObjectAccess soa(env);
1400 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Ian Rogersbc939662013-08-15 10:26:54 -07001403 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1404 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001405 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1406 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001407 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001408 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001411 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001414 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1415 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001416 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_end(ap);
1419 }
1420
Brian Carlstromea46f952013-07-30 01:26:50 -07001421 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1422 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001423 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1424 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001425 ScopedObjectAccess soa(env);
1426 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
Ian Rogersbc939662013-08-15 10:26:54 -07001429 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1430 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001431 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1432 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001434 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Ian Rogersbc939662013-08-15 10:26:54 -07001437 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001438 CHECK_NON_NULL_ARGUMENT(java_class);
1439 CHECK_NON_NULL_ARGUMENT(name);
1440 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001441 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001442 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001444
Ian Rogersbc939662013-08-15 10:26:54 -07001445 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1446 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001447 CHECK_NON_NULL_ARGUMENT(java_class);
1448 CHECK_NON_NULL_ARGUMENT(name);
1449 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001450 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001451 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001455 CHECK_NON_NULL_ARGUMENT(obj);
1456 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001458 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1459 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001460 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001464 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001465 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001466 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001467 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001471 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1472 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001474 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1475 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1476 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001477 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001481 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001483 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1484 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001485 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001489 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1490 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001491 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001492 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1493 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001494 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001496#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001497 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001498 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001499 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001500 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001503 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1504 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001505 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001506 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1507 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001508 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001510#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001511 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001512 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001513 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001514 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001517 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001521 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001525 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001533 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001537 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001549 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001560 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 }
1575
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001576 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 }
1579
1580 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 }
1583
1584 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 }
1587
1588 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 }
1591
1592 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 }
1595
1596 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 }
1599
1600 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 }
1603
1604 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 }
1607
1608 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 }
1611
1612 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 }
1615
1616 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 }
1619
1620 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 }
1623
1624 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001626 }
1627
1628 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001630 }
1631
1632 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 }
1635
1636 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001638 }
1639
1640 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001647 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001648 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001649 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 va_end(ap);
1652 return local_result;
1653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001656 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001658 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001662 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001663 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001665 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001666 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001671 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001672 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001673 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001674 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001676 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001680 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001682 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001686 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001688 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001694 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001695 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001696 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001698 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001702 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001704 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001708 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001710 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001713 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001716 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001717 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001718 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001720 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001723 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001724 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001725 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001726 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001730 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001732 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001738 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001739 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001740 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001742 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001746 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001748 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001752 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001754 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001759 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001760 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001761 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001762 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001764 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001768 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001769 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001774 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001776 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001779 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001781 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001782 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001783 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001784 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001786 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001790 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001792 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001796 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001798 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001801 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001803 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001804 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001805 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001806 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001808 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001812 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001814 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001820 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001823 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001825 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001827 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001828 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001830 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001836 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001840 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001842 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001845 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001847 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001848 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001849 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001850 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 va_end(ap);
1852 }
1853
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001854 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001855 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001860 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001861 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001863 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes814e4032011-08-23 12:07:56 -07001866 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001867 if (UNLIKELY(char_count < 0)) {
1868 JniAbortF("NewString", "char_count < 0: %d", char_count);
1869 return nullptr;
1870 }
1871 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1872 JniAbortF("NewString", "chars == null && char_count > 0");
1873 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001874 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001875 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001876 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001877 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
1880 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001881 if (utf == nullptr) {
1882 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001885 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
1888
Elliott Hughes814e4032011-08-23 12:07:56 -07001889 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001890 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001892 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001893 }
1894
1895 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001896 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001898 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001899 }
1900
Ian Rogersbc939662013-08-15 10:26:54 -07001901 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1902 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001903 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001904 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001905 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001906 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001909 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001910 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1911 memcpy(buf, chars + start, length * sizeof(jchar));
1912 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001913 }
1914
Ian Rogersbc939662013-08-15 10:26:54 -07001915 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1916 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001917 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001919 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001920 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001922 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001923 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001924 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1925 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1926 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001930 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001931 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001932 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1933 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001934 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001935 if (is_copy != nullptr) {
1936 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001937 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001938 int32_t char_count = s->GetLength();
1939 int32_t offset = s->GetOffset();
1940 jchar* bytes = new jchar[char_count + 1];
1941 for (int32_t i = 0; i < char_count; i++) {
1942 bytes[i] = chars->Get(i + offset);
1943 }
1944 bytes[char_count] = '\0';
1945 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07001946 }
1947
Mathieu Chartier590fee92013-09-13 13:46:47 -07001948 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001949 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001950 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001952 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001956 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001960 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001964 if (java_string == nullptr) {
1965 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001966 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001967 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001968 *is_copy = JNI_TRUE;
1969 }
Ian Rogersef28b142012-11-30 14:22:18 -08001970 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001971 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001972 size_t byte_count = s->GetUtfLength();
1973 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001974 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001975 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1976 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1977 bytes[byte_count] = '\0';
1978 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001982 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001983 }
1984
Elliott Hughesbd935992011-08-22 11:59:34 -07001985 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001986 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001988 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001989 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001990 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1991 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001992 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001993 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes814e4032011-08-23 12:07:56 -07001996 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001997 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001999 mirror::ObjectArray<mirror::Object>* array =
2000 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Ian Rogersbc939662013-08-15 10:26:54 -07002004 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2005 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002006 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002008 mirror::ObjectArray<mirror::Object>* array =
2009 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2010 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002011 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
2014 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002015 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
2018 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002019 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
2022 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002023 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
2026 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002027 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
2030 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002031 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
2034 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002035 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
2038 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002039 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Ian Rogers1d99e452014-01-02 17:36:41 -08002042 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2043 jobject initial_element) {
2044 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002045 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002046 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002047 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002048 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049
2050 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002051 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002052 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002053 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002054 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002055 if (UNLIKELY(element_class->IsPrimitive())) {
2056 JniAbortF("NewObjectArray", "not an object type: %s",
2057 PrettyDescriptor(element_class).c_str());
2058 return nullptr;
2059 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002061 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002062 if (UNLIKELY(array_class == nullptr)) {
2063 return nullptr;
2064 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes75770752011-08-24 17:52:38 -07002067 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002068 mirror::ObjectArray<mirror::Object>* result =
2069 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002070 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002071 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002072 if (initial_object != nullptr) {
2073 mirror::Class* element_class = result->GetClass()->GetComponentType();
2074 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2075 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2076 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2077 PrettyDescriptor(element_class).c_str());
2078
2079 } else {
2080 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002081 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002082 }
2083 }
Elliott Hughes75770752011-08-24 17:52:38 -07002084 }
2085 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002086 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
2089 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002090 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Ian Rogersa15e67d2012-02-28 13:51:55 -08002093 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002094 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002095 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002096 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002097 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2098 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2099 PrettyDescriptor(array->GetClass()).c_str());
2100 return nullptr;
2101 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002102 gc::Heap* heap = Runtime::Current()->GetHeap();
2103 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002104 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002105 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002106 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002107 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002109 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002110 *is_copy = JNI_FALSE;
2111 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002112 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002113 }
2114
Ian Rogers2d10b202014-05-12 19:15:18 -07002115 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2116 jint mode) {
2117 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2118 ScopedObjectAccess soa(env);
2119 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2120 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2121 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2122 PrettyDescriptor(array->GetClass()).c_str());
2123 return;
2124 }
2125 const size_t component_size = array->GetClass()->GetComponentSize();
2126 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002127 }
2128
Elliott Hughes75770752011-08-24 17:52:38 -07002129 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002130 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes75770752011-08-24 17:52:38 -07002133 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002134 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes75770752011-08-24 17:52:38 -07002137 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002138 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes75770752011-08-24 17:52:38 -07002141 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002142 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes75770752011-08-24 17:52:38 -07002145 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002146 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes75770752011-08-24 17:52:38 -07002149 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002150 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes75770752011-08-24 17:52:38 -07002153 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002154 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Elliott Hughes75770752011-08-24 17:52:38 -07002157 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002158 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Mathieu Chartier590fee92013-09-13 13:46:47 -07002161 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2162 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002163 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2164 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Mathieu Chartier590fee92013-09-13 13:46:47 -07002167 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002168 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Mathieu Chartier590fee92013-09-13 13:46:47 -07002171 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002172 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Mathieu Chartier590fee92013-09-13 13:46:47 -07002175 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2176 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002177 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 }
2179
Mathieu Chartier590fee92013-09-13 13:46:47 -07002180 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2181 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002182 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Mathieu Chartier590fee92013-09-13 13:46:47 -07002185 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002186 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Mathieu Chartier590fee92013-09-13 13:46:47 -07002189 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002190 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Mathieu Chartier590fee92013-09-13 13:46:47 -07002193 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2194 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002195 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Ian Rogersbc939662013-08-15 10:26:54 -07002198 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2199 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002200 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002201 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Ian Rogersbc939662013-08-15 10:26:54 -07002204 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2205 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002206 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Ian Rogersbc939662013-08-15 10:26:54 -07002209 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2210 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002211 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Ian Rogersbc939662013-08-15 10:26:54 -07002214 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2215 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002216 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002217 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Ian Rogersbc939662013-08-15 10:26:54 -07002220 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2221 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002222 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002223 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Ian Rogersbc939662013-08-15 10:26:54 -07002226 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2227 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002228 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Ian Rogersbc939662013-08-15 10:26:54 -07002231 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2232 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002233 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Ian Rogersbc939662013-08-15 10:26:54 -07002236 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2237 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002238 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002239 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Ian Rogersbc939662013-08-15 10:26:54 -07002242 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2243 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002244 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002245 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Ian Rogersbc939662013-08-15 10:26:54 -07002248 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2249 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002250 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Ian Rogersbc939662013-08-15 10:26:54 -07002253 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2254 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002255 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2259 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002260 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002261 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Ian Rogersbc939662013-08-15 10:26:54 -07002264 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2265 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002266 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002267 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Ian Rogersbc939662013-08-15 10:26:54 -07002270 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2271 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002272 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 }
2274
Ian Rogersbc939662013-08-15 10:26:54 -07002275 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2276 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002277 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Ian Rogersbc939662013-08-15 10:26:54 -07002280 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2281 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002282 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002283 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285
Ian Rogersbc939662013-08-15 10:26:54 -07002286 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2287 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002288 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2289 }
2290
Ian Rogersbc939662013-08-15 10:26:54 -07002291 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2292 jint method_count, bool return_errors) {
2293 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002294 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002295 return JNI_ERR; // Not reached.
2296 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002297 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002298 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002299 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002300 if (UNLIKELY(method_count == 0)) {
2301 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2302 << PrettyDescriptor(c);
2303 return JNI_OK;
2304 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002305 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002306 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 const char* name = methods[i].name;
2308 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002309 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002311 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 ++sig;
2313 }
2314
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002315 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2316 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002317 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002319 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002320 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002321 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002322 << PrettyDescriptor(c) << "." << name << sig << " in "
2323 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002326 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002327 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002328 << PrettyDescriptor(c) << "." << name << sig
2329 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 return JNI_ERR;
2332 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002333
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002334 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002335
Ian Rogers1eb512d2013-10-18 15:42:20 -07002336 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002337 }
2338 return JNI_OK;
2339 }
2340
Elliott Hughes5174fe62011-08-23 15:12:35 -07002341 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002342 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002343 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002344 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002345
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002346 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002347
Ian Rogers2d10b202014-05-12 19:15:18 -07002348 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002349 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002350 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002351 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002352 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002353 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002354 }
2355 }
2356 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002357 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002358 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002359 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002360 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002361 }
2362 }
2363
Ian Rogers2d10b202014-05-12 19:15:18 -07002364 if (unregistered_count == 0) {
2365 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2366 << PrettyDescriptor(c) << "' that contains no native methods";
2367 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002368 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 }
2370
Ian Rogers719d1a32014-03-06 12:13:39 -08002371 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002372 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002373 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002374 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2375 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002376 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002377 return JNI_ERR;
2378 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002379 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002380 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002381 }
2382
Ian Rogers719d1a32014-03-06 12:13:39 -08002383 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002384 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002385 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002386 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002387 o->MonitorExit(soa.Self());
2388 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002389 return JNI_ERR;
2390 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002391 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002392 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002393 }
2394
2395 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002396 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002397 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002398 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002399 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002400 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002401 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002402 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002403 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002404 }
2405
Elliott Hughescdf53122011-08-19 15:46:09 -07002406 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002407 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002408 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002409 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002410 if (address == nullptr && capacity != 0) {
2411 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002412 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002413
Ian Rogers936b37f2014-02-14 00:52:24 -08002414 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002415 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002416 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002417 jint capacity_arg = static_cast<jint>(capacity);
2418
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002419 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2420 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002421 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002422 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002423 }
2424
Elliott Hughesb465ab02011-08-24 11:21:21 -07002425 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002426 return reinterpret_cast<void*>(env->GetLongField(
2427 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002428 }
2429
Elliott Hughesb465ab02011-08-24 11:21:21 -07002430 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002431 return static_cast<jlong>(env->GetIntField(
2432 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002433 }
2434
Elliott Hughesb465ab02011-08-24 11:21:21 -07002435 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002436 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002437
2438 // Do we definitely know what kind of reference this is?
2439 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2440 IndirectRefKind kind = GetIndirectRefKind(ref);
2441 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002442 case kLocal: {
2443 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -08002444 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002445 return JNILocalRefType;
2446 }
2447 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002448 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002449 case kGlobal:
2450 return JNIGlobalRefType;
2451 case kWeakGlobal:
2452 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002453 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002454 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002455 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002456 return JNILocalRefType;
2457 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002458 return JNIInvalidRefType;
2459 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002460 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2461 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002462 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002463
2464 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002465 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2466 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002467 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002468 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2470 return JNI_ERR;
2471 }
2472 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002473 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002474 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2475 if (!okay) {
2476 soa.Self()->ThrowOutOfMemoryError(caller);
2477 }
2478 return okay ? JNI_OK : JNI_ERR;
2479 }
2480
2481 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002482 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002483 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002484 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002485 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002486 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002487 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002488 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002489 return soa.AddLocalReference<JniT>(result);
2490 }
2491
Ian Rogers2d10b202014-05-12 19:15:18 -07002492 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2493 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2494 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002496 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002497 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2498 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2499 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2500 PrettyDescriptor(array->GetClass()).c_str());
2501 return nullptr;
2502 }
2503 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2504 return array;
2505 }
2506
2507 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2508 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2509 CHECK_NON_NULL_ARGUMENT(java_array);
2510 ScopedObjectAccess soa(env);
2511 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2512 "GetArrayElements",
2513 "get");
2514 if (UNLIKELY(array == nullptr)) {
2515 return nullptr;
2516 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002517 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002518 // Only make a copy if necessary.
2519 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2520 if (is_copy != nullptr) {
2521 *is_copy = JNI_TRUE;
2522 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002523 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002524 size_t size = array->GetLength() * component_size;
2525 void* data = new uint64_t[RoundUp(size, 8) / 8];
2526 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002527 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002528 } else {
2529 if (is_copy != nullptr) {
2530 *is_copy = JNI_FALSE;
2531 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002532 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002533 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002534 }
2535
Ian Rogers2d10b202014-05-12 19:15:18 -07002536 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002537 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002538 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002539 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002540 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2541 "ReleaseArrayElements",
2542 "release");
2543 if (array == nullptr) {
2544 return;
2545 }
2546 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2547 }
2548
2549 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2550 size_t component_size, void* elements, jint mode)
2551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002552 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002553 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002554 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002555 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002556 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2557 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002558 if (is_copy) {
2559 // Sanity check: If elements is not the same as the java array's data, it better not be a
2560 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2561 // copies we make?
2562 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2563 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2564 reinterpret_cast<void*>(elements), array_data);
2565 return;
2566 }
2567 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002568 // Don't need to copy if we had a direct pointer.
2569 if (mode != JNI_ABORT && is_copy) {
2570 memcpy(array_data, elements, bytes);
2571 }
2572 if (mode != JNI_COMMIT) {
2573 if (is_copy) {
2574 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002575 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002576 // Non copy to a movable object must means that we had disabled the moving GC.
2577 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002578 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002579 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002580 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002581 }
2582
Ian Rogers2d10b202014-05-12 19:15:18 -07002583 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2584 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2585 jsize start, jsize length, ElementT* buf) {
2586 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2587 ScopedObjectAccess soa(env);
2588 ArtArrayT* array =
2589 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2590 "GetPrimitiveArrayRegion",
2591 "get region of");
2592 if (array != nullptr) {
2593 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2594 ThrowAIOOBE(soa, array, start, length, "src");
2595 } else {
2596 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2597 ElementT* data = array->GetData();
2598 memcpy(buf, data + start, length * sizeof(ElementT));
2599 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002600 }
2601 }
2602
Ian Rogers2d10b202014-05-12 19:15:18 -07002603 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2604 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2605 jsize start, jsize length, const ElementT* buf) {
2606 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2607 ScopedObjectAccess soa(env);
2608 ArtArrayT* array =
2609 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2610 "SetPrimitiveArrayRegion",
2611 "set region of");
2612 if (array != nullptr) {
2613 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2614 ThrowAIOOBE(soa, array, start, length, "dst");
2615 } else {
2616 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2617 ElementT* data = array->GetData();
2618 memcpy(data + start, buf, length * sizeof(ElementT));
2619 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002620 }
2621 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002622};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002623
Elliott Hughes88c5c352012-03-15 18:49:48 -07002624const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002625 nullptr, // reserved0.
2626 nullptr, // reserved1.
2627 nullptr, // reserved2.
2628 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002629 JNI::GetVersion,
2630 JNI::DefineClass,
2631 JNI::FindClass,
2632 JNI::FromReflectedMethod,
2633 JNI::FromReflectedField,
2634 JNI::ToReflectedMethod,
2635 JNI::GetSuperclass,
2636 JNI::IsAssignableFrom,
2637 JNI::ToReflectedField,
2638 JNI::Throw,
2639 JNI::ThrowNew,
2640 JNI::ExceptionOccurred,
2641 JNI::ExceptionDescribe,
2642 JNI::ExceptionClear,
2643 JNI::FatalError,
2644 JNI::PushLocalFrame,
2645 JNI::PopLocalFrame,
2646 JNI::NewGlobalRef,
2647 JNI::DeleteGlobalRef,
2648 JNI::DeleteLocalRef,
2649 JNI::IsSameObject,
2650 JNI::NewLocalRef,
2651 JNI::EnsureLocalCapacity,
2652 JNI::AllocObject,
2653 JNI::NewObject,
2654 JNI::NewObjectV,
2655 JNI::NewObjectA,
2656 JNI::GetObjectClass,
2657 JNI::IsInstanceOf,
2658 JNI::GetMethodID,
2659 JNI::CallObjectMethod,
2660 JNI::CallObjectMethodV,
2661 JNI::CallObjectMethodA,
2662 JNI::CallBooleanMethod,
2663 JNI::CallBooleanMethodV,
2664 JNI::CallBooleanMethodA,
2665 JNI::CallByteMethod,
2666 JNI::CallByteMethodV,
2667 JNI::CallByteMethodA,
2668 JNI::CallCharMethod,
2669 JNI::CallCharMethodV,
2670 JNI::CallCharMethodA,
2671 JNI::CallShortMethod,
2672 JNI::CallShortMethodV,
2673 JNI::CallShortMethodA,
2674 JNI::CallIntMethod,
2675 JNI::CallIntMethodV,
2676 JNI::CallIntMethodA,
2677 JNI::CallLongMethod,
2678 JNI::CallLongMethodV,
2679 JNI::CallLongMethodA,
2680 JNI::CallFloatMethod,
2681 JNI::CallFloatMethodV,
2682 JNI::CallFloatMethodA,
2683 JNI::CallDoubleMethod,
2684 JNI::CallDoubleMethodV,
2685 JNI::CallDoubleMethodA,
2686 JNI::CallVoidMethod,
2687 JNI::CallVoidMethodV,
2688 JNI::CallVoidMethodA,
2689 JNI::CallNonvirtualObjectMethod,
2690 JNI::CallNonvirtualObjectMethodV,
2691 JNI::CallNonvirtualObjectMethodA,
2692 JNI::CallNonvirtualBooleanMethod,
2693 JNI::CallNonvirtualBooleanMethodV,
2694 JNI::CallNonvirtualBooleanMethodA,
2695 JNI::CallNonvirtualByteMethod,
2696 JNI::CallNonvirtualByteMethodV,
2697 JNI::CallNonvirtualByteMethodA,
2698 JNI::CallNonvirtualCharMethod,
2699 JNI::CallNonvirtualCharMethodV,
2700 JNI::CallNonvirtualCharMethodA,
2701 JNI::CallNonvirtualShortMethod,
2702 JNI::CallNonvirtualShortMethodV,
2703 JNI::CallNonvirtualShortMethodA,
2704 JNI::CallNonvirtualIntMethod,
2705 JNI::CallNonvirtualIntMethodV,
2706 JNI::CallNonvirtualIntMethodA,
2707 JNI::CallNonvirtualLongMethod,
2708 JNI::CallNonvirtualLongMethodV,
2709 JNI::CallNonvirtualLongMethodA,
2710 JNI::CallNonvirtualFloatMethod,
2711 JNI::CallNonvirtualFloatMethodV,
2712 JNI::CallNonvirtualFloatMethodA,
2713 JNI::CallNonvirtualDoubleMethod,
2714 JNI::CallNonvirtualDoubleMethodV,
2715 JNI::CallNonvirtualDoubleMethodA,
2716 JNI::CallNonvirtualVoidMethod,
2717 JNI::CallNonvirtualVoidMethodV,
2718 JNI::CallNonvirtualVoidMethodA,
2719 JNI::GetFieldID,
2720 JNI::GetObjectField,
2721 JNI::GetBooleanField,
2722 JNI::GetByteField,
2723 JNI::GetCharField,
2724 JNI::GetShortField,
2725 JNI::GetIntField,
2726 JNI::GetLongField,
2727 JNI::GetFloatField,
2728 JNI::GetDoubleField,
2729 JNI::SetObjectField,
2730 JNI::SetBooleanField,
2731 JNI::SetByteField,
2732 JNI::SetCharField,
2733 JNI::SetShortField,
2734 JNI::SetIntField,
2735 JNI::SetLongField,
2736 JNI::SetFloatField,
2737 JNI::SetDoubleField,
2738 JNI::GetStaticMethodID,
2739 JNI::CallStaticObjectMethod,
2740 JNI::CallStaticObjectMethodV,
2741 JNI::CallStaticObjectMethodA,
2742 JNI::CallStaticBooleanMethod,
2743 JNI::CallStaticBooleanMethodV,
2744 JNI::CallStaticBooleanMethodA,
2745 JNI::CallStaticByteMethod,
2746 JNI::CallStaticByteMethodV,
2747 JNI::CallStaticByteMethodA,
2748 JNI::CallStaticCharMethod,
2749 JNI::CallStaticCharMethodV,
2750 JNI::CallStaticCharMethodA,
2751 JNI::CallStaticShortMethod,
2752 JNI::CallStaticShortMethodV,
2753 JNI::CallStaticShortMethodA,
2754 JNI::CallStaticIntMethod,
2755 JNI::CallStaticIntMethodV,
2756 JNI::CallStaticIntMethodA,
2757 JNI::CallStaticLongMethod,
2758 JNI::CallStaticLongMethodV,
2759 JNI::CallStaticLongMethodA,
2760 JNI::CallStaticFloatMethod,
2761 JNI::CallStaticFloatMethodV,
2762 JNI::CallStaticFloatMethodA,
2763 JNI::CallStaticDoubleMethod,
2764 JNI::CallStaticDoubleMethodV,
2765 JNI::CallStaticDoubleMethodA,
2766 JNI::CallStaticVoidMethod,
2767 JNI::CallStaticVoidMethodV,
2768 JNI::CallStaticVoidMethodA,
2769 JNI::GetStaticFieldID,
2770 JNI::GetStaticObjectField,
2771 JNI::GetStaticBooleanField,
2772 JNI::GetStaticByteField,
2773 JNI::GetStaticCharField,
2774 JNI::GetStaticShortField,
2775 JNI::GetStaticIntField,
2776 JNI::GetStaticLongField,
2777 JNI::GetStaticFloatField,
2778 JNI::GetStaticDoubleField,
2779 JNI::SetStaticObjectField,
2780 JNI::SetStaticBooleanField,
2781 JNI::SetStaticByteField,
2782 JNI::SetStaticCharField,
2783 JNI::SetStaticShortField,
2784 JNI::SetStaticIntField,
2785 JNI::SetStaticLongField,
2786 JNI::SetStaticFloatField,
2787 JNI::SetStaticDoubleField,
2788 JNI::NewString,
2789 JNI::GetStringLength,
2790 JNI::GetStringChars,
2791 JNI::ReleaseStringChars,
2792 JNI::NewStringUTF,
2793 JNI::GetStringUTFLength,
2794 JNI::GetStringUTFChars,
2795 JNI::ReleaseStringUTFChars,
2796 JNI::GetArrayLength,
2797 JNI::NewObjectArray,
2798 JNI::GetObjectArrayElement,
2799 JNI::SetObjectArrayElement,
2800 JNI::NewBooleanArray,
2801 JNI::NewByteArray,
2802 JNI::NewCharArray,
2803 JNI::NewShortArray,
2804 JNI::NewIntArray,
2805 JNI::NewLongArray,
2806 JNI::NewFloatArray,
2807 JNI::NewDoubleArray,
2808 JNI::GetBooleanArrayElements,
2809 JNI::GetByteArrayElements,
2810 JNI::GetCharArrayElements,
2811 JNI::GetShortArrayElements,
2812 JNI::GetIntArrayElements,
2813 JNI::GetLongArrayElements,
2814 JNI::GetFloatArrayElements,
2815 JNI::GetDoubleArrayElements,
2816 JNI::ReleaseBooleanArrayElements,
2817 JNI::ReleaseByteArrayElements,
2818 JNI::ReleaseCharArrayElements,
2819 JNI::ReleaseShortArrayElements,
2820 JNI::ReleaseIntArrayElements,
2821 JNI::ReleaseLongArrayElements,
2822 JNI::ReleaseFloatArrayElements,
2823 JNI::ReleaseDoubleArrayElements,
2824 JNI::GetBooleanArrayRegion,
2825 JNI::GetByteArrayRegion,
2826 JNI::GetCharArrayRegion,
2827 JNI::GetShortArrayRegion,
2828 JNI::GetIntArrayRegion,
2829 JNI::GetLongArrayRegion,
2830 JNI::GetFloatArrayRegion,
2831 JNI::GetDoubleArrayRegion,
2832 JNI::SetBooleanArrayRegion,
2833 JNI::SetByteArrayRegion,
2834 JNI::SetCharArrayRegion,
2835 JNI::SetShortArrayRegion,
2836 JNI::SetIntArrayRegion,
2837 JNI::SetLongArrayRegion,
2838 JNI::SetFloatArrayRegion,
2839 JNI::SetDoubleArrayRegion,
2840 JNI::RegisterNatives,
2841 JNI::UnregisterNatives,
2842 JNI::MonitorEnter,
2843 JNI::MonitorExit,
2844 JNI::GetJavaVM,
2845 JNI::GetStringRegion,
2846 JNI::GetStringUTFRegion,
2847 JNI::GetPrimitiveArrayCritical,
2848 JNI::ReleasePrimitiveArrayCritical,
2849 JNI::GetStringCritical,
2850 JNI::ReleaseStringCritical,
2851 JNI::NewWeakGlobalRef,
2852 JNI::DeleteWeakGlobalRef,
2853 JNI::ExceptionCheck,
2854 JNI::NewDirectByteBuffer,
2855 JNI::GetDirectBufferAddress,
2856 JNI::GetDirectBufferCapacity,
2857 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002858};
2859
Elliott Hughes75770752011-08-24 17:52:38 -07002860JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002861 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002862 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002863 local_ref_cookie(IRT_FIRST_SEGMENT),
2864 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002865 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002866 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002867 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002868 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002869 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002870 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002871 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002872}
2873
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002874JNIEnvExt::~JNIEnvExt() {
2875}
2876
Mathieu Chartier590fee92013-09-13 13:46:47 -07002877jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2878 if (obj == nullptr) {
2879 return nullptr;
2880 }
2881 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2882}
2883
2884void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2885 if (obj != nullptr) {
2886 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2887 }
2888}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002889void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2890 check_jni = enabled;
2891 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002892}
2893
Elliott Hughes73e66f72012-05-09 09:34:45 -07002894void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2895 locals.Dump(os);
2896 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002897}
2898
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002899void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2900 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002901 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002902 stacked_local_ref_cookies.push_back(local_ref_cookie);
2903 local_ref_cookie = locals.GetSegmentState();
2904}
2905
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002906void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002907 locals.SetSegmentState(local_ref_cookie);
2908 local_ref_cookie = stacked_local_ref_cookies.back();
2909 stacked_local_ref_cookies.pop_back();
2910}
2911
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002912Offset JNIEnvExt::SegmentStateOffset() {
2913 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2914 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2915}
2916
Carl Shapiroea4dca82011-08-01 13:45:38 -07002917// JNI Invocation interface.
2918
Brian Carlstrombddf9762013-05-14 11:35:37 -07002919extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002920 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002921 if (IsBadJniVersion(args->version)) {
2922 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002923 return JNI_EVERSION;
2924 }
2925 Runtime::Options options;
2926 for (int i = 0; i < args->nOptions; ++i) {
2927 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002928 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002929 }
2930 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002931 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002932 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002933 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002934 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002935 bool started = runtime->Start();
2936 if (!started) {
2937 delete Thread::Current()->GetJniEnv();
2938 delete runtime->GetJavaVM();
2939 LOG(WARNING) << "CreateJavaVM failed";
2940 return JNI_ERR;
2941 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002942 *p_env = Thread::Current()->GetJniEnv();
2943 *p_vm = runtime->GetJavaVM();
2944 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002945}
2946
Elliott Hughesf2682d52011-08-15 16:37:04 -07002947extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002948 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002949 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002950 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002951 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002952 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002953 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002954 }
2955 return JNI_OK;
2956}
2957
2958// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002959extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002960 return JNI_ERR;
2961}
2962
Elliott Hughescdf53122011-08-19 15:46:09 -07002963class JII {
2964 public:
2965 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002966 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002967 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002968 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002969 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2970 delete raw_vm->runtime;
2971 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002972 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002973
Elliott Hughescdf53122011-08-19 15:46:09 -07002974 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002975 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002976 }
2977
2978 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002979 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002980 }
2981
2982 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002983 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002984 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002985 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002986 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2987 Runtime* runtime = raw_vm->runtime;
2988 runtime->DetachCurrentThread();
2989 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002990 }
2991
2992 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002993 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2994 // and unlike other calls that take a JNI version doesn't care if you supply
2995 // JNI_VERSION_1_1, which we don't otherwise support.
2996 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002997 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002998 return JNI_EVERSION;
2999 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003000 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003001 return JNI_ERR;
3002 }
3003 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003004 if (thread == nullptr) {
3005 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003006 return JNI_EDETACHED;
3007 }
3008 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003009 return JNI_OK;
3010 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003011};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003012
Elliott Hughes88c5c352012-03-15 18:49:48 -07003013const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003014 nullptr, // reserved0
3015 nullptr, // reserved1
3016 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003017 JII::DestroyJavaVM,
3018 JII::AttachCurrentThread,
3019 JII::DetachCurrentThread,
3020 JII::GetEnv,
3021 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003022};
3023
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003024JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003025 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003026 check_jni_abort_hook(nullptr),
3027 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003028 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003029 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003030 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003031 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003032 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003033 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003034 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003035 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003036 libraries(new Libraries),
3037 weak_globals_lock_("JNI weak global reference table lock"),
3038 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3039 allow_new_weak_globals_(true),
3040 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003041 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003042 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003043 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003044 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003045}
3046
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003047JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003048 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003049}
3050
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003051jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3052 if (obj == nullptr) {
3053 return nullptr;
3054 }
3055 MutexLock mu(self, weak_globals_lock_);
3056 while (UNLIKELY(!allow_new_weak_globals_)) {
3057 weak_globals_add_condition_.WaitHoldingLocks(self);
3058 }
3059 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3060 return reinterpret_cast<jweak>(ref);
3061}
3062
3063void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3064 MutexLock mu(self, weak_globals_lock_);
3065 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3066 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3067 << "failed to find entry";
3068 }
3069}
3070
Elliott Hughes88c5c352012-03-15 18:49:48 -07003071void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3072 check_jni = enabled;
3073 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003074}
3075
Elliott Hughesae80b492012-04-24 10:43:17 -07003076void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3077 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3078 if (force_copy) {
3079 os << " (with forcecopy)";
3080 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003081 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003082 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003083 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003084 os << "; pins=" << pin_table.Size();
3085 }
3086 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003087 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003088 os << "; globals=" << globals.Capacity();
3089 }
3090 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003091 MutexLock mu(self, weak_globals_lock_);
3092 if (weak_globals_.Capacity() > 0) {
3093 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003094 }
3095 }
3096 os << '\n';
3097
3098 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003099 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003100 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3101 }
3102}
3103
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003104void JavaVMExt::DisallowNewWeakGlobals() {
3105 MutexLock mu(Thread::Current(), weak_globals_lock_);
3106 allow_new_weak_globals_ = false;
3107}
3108
3109void JavaVMExt::AllowNewWeakGlobals() {
3110 Thread* self = Thread::Current();
3111 MutexLock mu(self, weak_globals_lock_);
3112 allow_new_weak_globals_ = true;
3113 weak_globals_add_condition_.Broadcast(self);
3114}
3115
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003116mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3117 MutexLock mu(self, weak_globals_lock_);
3118 while (UNLIKELY(!allow_new_weak_globals_)) {
3119 weak_globals_add_condition_.WaitHoldingLocks(self);
3120 }
Mathieu Chartier5647d182014-03-07 15:00:39 -08003121 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003122}
3123
Elliott Hughes73e66f72012-05-09 09:34:45 -07003124void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003125 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003126 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003127 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003128 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003129 }
3130 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003131 MutexLock mu(self, weak_globals_lock_);
3132 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003133 }
3134 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003135 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003136 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003137 }
3138}
3139
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003140bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003141 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003142 std::string* detail) {
3143 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003144
3145 // See if we've already loaded this library. If we have, and the class loader
3146 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003147 // TODO: for better results we should canonicalize the pathname (or even compare
3148 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003149 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003150 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003151 {
3152 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003153 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003154 library = libraries->Get(path);
3155 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003156 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003157 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003158 // The library will be associated with class_loader. The JNI
3159 // spec says we can't load the same library into more than one
3160 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003161 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003162 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003163 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003164 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003165 return false;
3166 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003167 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003168 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003169 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003170 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003171 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003172 return false;
3173 }
3174 return true;
3175 }
3176
3177 // Open the shared library. Because we're using a full path, the system
3178 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3179 // resolve this library's dependencies though.)
3180
3181 // Failures here are expected when java.library.path has several entries
3182 // and we have to hunt for the lib.
3183
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003184 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3185 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3186 // dlopen) becomes zero from dlclose.
3187
Elliott Hughescdf53122011-08-19 15:46:09 -07003188 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003189 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003190 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003191 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003192 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003193
Elliott Hughes84b2f142012-09-27 09:16:28 -07003194 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003195
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003196 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003197 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003198 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003199 return false;
3200 }
3201
3202 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003203 // TODO: move the locking (and more of this logic) into Libraries.
3204 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003205 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003206 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003207 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003208 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003209 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003210 libraries->Put(path, library);
3211 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003212 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003213 }
3214 if (!created_library) {
3215 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003216 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003217 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003218 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003219
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003220 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003221 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003222
Elliott Hughes79353722013-08-02 16:52:18 -07003223 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003224 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003225 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003226 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003227 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003228 } else {
3229 // Call JNI_OnLoad. We have to override the current class
3230 // loader, which will always be "null" since the stuff at the
3231 // top of the stack is around Runtime.loadLibrary(). (See
3232 // the comments in the JNI FindClass function.)
3233 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3234 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003235 StackHandleScope<1> hs(self);
3236 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3237 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003238
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003239 int version = 0;
3240 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003241 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003242 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003243 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003244 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003245
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003246 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003247
Elliott Hughes79353722013-08-02 16:52:18 -07003248 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003249 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003250 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003251 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003252 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003253 // It's unwise to call dlclose() here, but we can mark it
3254 // as bad and ensure that future load attempts will fail.
3255 // We don't know how far JNI_OnLoad got, so there could
3256 // be some partially-initialized stuff accessible through
3257 // newly-registered native method calls. We could try to
3258 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003259 } else {
3260 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003261 }
Elliott Hughes79353722013-08-02 16:52:18 -07003262 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003263 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003264 }
3265
Elliott Hughes79353722013-08-02 16:52:18 -07003266 library->SetResult(was_successful);
3267 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003268}
3269
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003270void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003271 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003272 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003273 // If this is a static method, it could be called before the class
3274 // has been initialized.
3275 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003276 c = EnsureInitialized(Thread::Current(), c);
3277 if (c == nullptr) {
3278 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003279 }
3280 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003281 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003282 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003283 std::string detail;
3284 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003285 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003286 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003287 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003288 native_method = libraries->FindNativeMethod(m, detail);
3289 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003290 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003291 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003292 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3293 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003294 }
3295 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003296}
3297
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003298void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003299 MutexLock mu(Thread::Current(), weak_globals_lock_);
3300 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003301 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003302 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003303 if (new_obj == nullptr) {
3304 new_obj = kClearedJniWeakGlobal;
3305 }
3306 *entry = new_obj;
3307 }
3308}
3309
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003310void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003311 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003312 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003313 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003314 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003315 }
3316 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003317 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003318 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003319 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003320 {
3321 MutexLock mu(self, libraries_lock);
3322 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003323 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003324 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003325 // The weak_globals table is visited by the GC itself (because it mutates the table).
3326}
3327
Elliott Hughesc8fece32013-01-02 11:27:23 -08003328void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003329 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003330 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003331 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003332 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3333 }
3334 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3335}
3336
Ian Rogersdf20fe02011-07-20 20:34:16 -07003337} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003338
3339std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3340 switch (rhs) {
3341 case JNIInvalidRefType:
3342 os << "JNIInvalidRefType";
3343 return os;
3344 case JNILocalRefType:
3345 os << "JNILocalRefType";
3346 return os;
3347 case JNIGlobalRefType:
3348 os << "JNIGlobalRefType";
3349 return os;
3350 case JNIWeakGlobalRefType:
3351 os << "JNIWeakGlobalRefType";
3352 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003353 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003354 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003355 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003356 }
3357}