blob: 845691d18277c3042be5cc950fe796ad25c04e62 [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
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200115static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
116 const char* kind, jint idx, bool return_errors)
117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
118 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
119 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
120 << ": " << kind << " is null at index " << idx;
121 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
122 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
123 "%s is null at index %d", kind, idx);
124}
125
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800126static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
128 if (LIKELY(klass->IsInitialized())) {
129 return klass;
130 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700131 StackHandleScope<1> hs(self);
132 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
133 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800134 return nullptr;
135 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700136 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800137}
138
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
140 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800142 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800143 if (c == nullptr) {
144 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700145 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800146 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700147 if (is_static) {
148 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700149 } else if (c->IsInterface()) {
150 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700151 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700152 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800153 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700154 // No virtual method matching the signature. Search declared
155 // private methods and constructors.
156 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700157 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700158 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800159 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700162 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700164}
165
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800166static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700169 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
170 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800171 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700172 }
Brian Carlstromce888532013-10-10 00:32:58 -0700173 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800174 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700175 return method->GetDeclaringClass()->GetClassLoader();
176 }
177 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800178 mirror::ClassLoader* class_loader =
179 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
180 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700181 return class_loader;
182 }
183 // See if the override ClassLoader is set for gtests.
184 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800185 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800186 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700187 CHECK(Runtime::Current()->UseCompileTimeClassPath());
188 return class_loader;
189 }
190 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800191 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700192}
193
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
195 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700197 StackHandleScope<2> hs(soa.Self());
198 Handle<mirror::Class> c(
199 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
200 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800201 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700202 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800203 mirror::ArtField* field = nullptr;
204 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
206 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800208 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700209 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700211 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800212 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700216 StackHandleScope<1> hs(soa.Self());
217 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800220 "no type \"%s\" found and so no field \"%s\" "
221 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700222 c->GetDescriptor().c_str());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700223 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800224 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 }
226 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700227 field = mirror::Class::FindStaticField(soa.Self(), c, name,
228 field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229 } else {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700230 field = c->FindInstanceField(name, field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800232 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
234 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
235 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700236 sig, name, c->GetDescriptor().c_str());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800237 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700238 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700240}
241
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800242static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700245 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700246 vm->pin_table.Add(array);
247}
248
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800249static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700252 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700253 vm->pin_table.Remove(array);
254}
255
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800256static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700259 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
261 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
262 "%s offset=%d length=%d %s.length=%d",
263 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700264}
Ian Rogers0571d352011-11-03 19:51:38 -0700265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
267 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
270 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
271 "offset=%d length=%d string.length()=%d", start, length,
272 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700273}
Elliott Hughes814e4032011-08-23 12:07:56 -0700274
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700276 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277 // Turn the const char* into a java.lang.String.
278 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800279 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700281 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700282
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 // Choose an appropriate constructor and set up the arguments.
284 jvalue args[2];
285 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800286 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800288 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 signature = "(Ljava/lang/String;)V";
290 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800291 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 signature = "(Ljava/lang/Throwable;)V";
293 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700294 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
296 args[0].l = s.get();
297 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700298 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800300 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800301 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800303 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 return JNI_ERR;
305 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700306
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800307 ScopedLocalRef<jthrowable> exception(
308 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
309 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310 return JNI_ERR;
311 }
Ian Rogersef28b142012-11-30 14:22:18 -0800312 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800313 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800314 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700316}
317
Elliott Hughes462c9442012-03-23 18:47:50 -0700318static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800319 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700320 return JNI_ERR;
321 }
322
Elliott Hughes462c9442012-03-23 18:47:50 -0700323 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700324 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800325 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700326 *p_env = self->GetJniEnv();
327 return JNI_OK;
328 }
329
330 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
331
332 // No threads allowed in zygote mode.
333 if (runtime->IsZygote()) {
334 LOG(ERROR) << "Attempt to attach a thread in the zygote";
335 return JNI_ERR;
336 }
337
Elliott Hughes462c9442012-03-23 18:47:50 -0700338 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800339 const char* thread_name = nullptr;
340 jobject thread_group = nullptr;
341 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700342 if (IsBadJniVersion(args->version)) {
343 LOG(ERROR) << "Bad JNI version passed to "
344 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
345 << args->version;
346 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700347 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700348 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700349 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700350 }
Elliott Hughes75770752011-08-24 17:52:38 -0700351
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800352 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800353 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700354 return JNI_ERR;
355 } else {
356 *p_env = Thread::Current()->GetJniEnv();
357 return JNI_OK;
358 }
Elliott Hughes75770752011-08-24 17:52:38 -0700359}
360
Elliott Hughes79082e32011-08-25 12:07:32 -0700361class SharedLibrary {
362 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800363 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700364 : path_(path),
365 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700366 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700368 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700369 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700370 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700371 }
372
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700373 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
374 mirror::Object** root = &class_loader_;
375 return ReadBarrier::BarrierForRoot<mirror::Object, kWithReadBarrier>(root);
Elliott Hughes79082e32011-08-25 12:07:32 -0700376 }
377
378 std::string GetPath() {
379 return path_;
380 }
381
382 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700383 * Check the result of an earlier call to JNI_OnLoad on this library.
384 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700385 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 bool CheckOnLoadResult()
387 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700388 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700389 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
391 bool okay;
392 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700393 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700394
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700395 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
397 // caller can continue.
398 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
399 okay = true;
400 } else {
401 while (jni_on_load_result_ == kPending) {
402 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700403 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700405
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700406 okay = (jni_on_load_result_ == kOkay);
407 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
408 << (okay ? "succeeded" : "failed") << "]";
409 }
410 }
411 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700412 return okay;
413 }
414
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700415 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700416 Thread* self = Thread::Current();
417 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700418
Elliott Hughes79082e32011-08-25 12:07:32 -0700419 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700420 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700421
422 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700423 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700424 }
425
426 void* FindSymbol(const std::string& symbol_name) {
427 return dlsym(handle_, symbol_name.c_str());
428 }
429
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800430 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800431 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800432 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800433 }
434 }
435
Elliott Hughes79082e32011-08-25 12:07:32 -0700436 private:
437 enum JNI_OnLoadState {
438 kPending,
439 kFailed,
440 kOkay,
441 };
442
443 // Path to library "/system/lib/libjni.so".
444 std::string path_;
445
446 // The void* returned by dlopen(3).
447 void* handle_;
448
449 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800450 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700451
452 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700453 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700454 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700455 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700457 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700459 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700460};
461
Elliott Hughes79082e32011-08-25 12:07:32 -0700462// This exists mainly to keep implementation details out of the header file.
463class Libraries {
464 public:
465 Libraries() {
466 }
467
468 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700469 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 }
471
Elliott Hughesae80b492012-04-24 10:43:17 -0700472 void Dump(std::ostream& os) const {
473 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700474 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700475 if (!first) {
476 os << ' ';
477 }
478 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700479 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700480 }
481 }
482
483 size_t size() const {
484 return libraries_.size();
485 }
486
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700488 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800489 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700490 }
491
492 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700493 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700494 }
495
496 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800497 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700498 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 std::string jni_short_name(JniShortName(m));
500 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800501 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700502 for (const auto& lib : libraries_) {
503 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 if (library->GetClassLoader() != declaring_class_loader) {
505 // We only search libraries loaded by the appropriate ClassLoader.
506 continue;
507 }
508 // Try the short name then the long name...
509 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800510 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700511 fn = library->FindSymbol(jni_long_name);
512 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800513 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800514 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
515 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 return fn;
517 }
518 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700519 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700520 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700521 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700522 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800523 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700524 }
525
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800526 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800527 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800528 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800529 }
530 }
531
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700533 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700534};
535
Ian Rogers2d10b202014-05-12 19:15:18 -0700536#define CHECK_NON_NULL_ARGUMENT(value) \
537 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700538
Ian Rogers2d10b202014-05-12 19:15:18 -0700539#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
540 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
541
542#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
543 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
544
545#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
546 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
547
548#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800549 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700550 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700551 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700552 }
553
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700554#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800555 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700556 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700557 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700558 }
559
Elliott Hughescdf53122011-08-19 15:46:09 -0700560class JNI {
561 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700562 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700563 return JNI_VERSION_1_6;
564 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700565
Ian Rogers25e8b912012-09-07 11:31:36 -0700566 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800568 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700569 }
570
Elliott Hughescdf53122011-08-19 15:46:09 -0700571 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700572 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700573 Runtime* runtime = Runtime::Current();
574 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700575 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800577 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700578 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700579 StackHandleScope<1> hs(soa.Self());
580 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800581 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700582 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800583 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700584 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700586 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700587
Ian Rogers62f05122014-03-21 11:21:29 -0700588 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700589 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700591 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700592 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700593
Ian Rogers62f05122014-03-21 11:21:29 -0700594 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700595 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700596 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700597 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700598 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700599
Elliott Hughescdf53122011-08-19 15:46:09 -0700600 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700601 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800603 mirror::ArtMethod* m = soa.DecodeMethod(mid);
604 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700605 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200606 jobject reflect_method;
607 if (m->IsConstructor()) {
608 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
609 } else {
610 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
611 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700612 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800613 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800615 SetObjectField(env, reflect_method,
616 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700617 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700621 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800623 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700624 jobject art_field = soa.AddLocalReference<jobject>(f);
625 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
626 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800627 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700628 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800629 SetObjectField(env, reflect_field,
630 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700631 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughes37f7a402011-08-22 18:56:01 -0700634 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700635 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800637 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700639 }
640
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700641 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700642 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800644 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700645 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughes37f7a402011-08-22 18:56:01 -0700648 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700649 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
650 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800652 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
653 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughese84278b2012-03-22 10:06:53 -0700657 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700658 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800659 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700660 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 return JNI_TRUE;
662 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700663 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800664 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
665 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700666 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700669
Elliott Hughes37f7a402011-08-22 18:56:01 -0700670 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800672 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
673 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 return JNI_ERR;
675 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800676 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
677 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 return JNI_OK;
679 }
680
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700681 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700682 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800683 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 }
685
686 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700687 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 }
689
690 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700691 ScopedObjectAccess soa(env);
692 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 }
694
695 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700697
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700698 // If we have no exception to describe, pass through.
699 if (!soa.Self()->GetException(nullptr)) {
700 return;
701 }
702
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700703 StackHandleScope<3> hs(soa.Self());
704 // TODO: Use nullptr instead of null handles?
705 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
706 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
707 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200709 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800710 {
711 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800712 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700713 old_throw_this_object.Assign(old_throw_location.GetThis());
714 old_throw_method.Assign(old_throw_location.GetMethod());
715 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200717 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800718 soa.Self()->ClearException();
719 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800720 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700721 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700722 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
723 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800724 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700725 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700726 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 } else {
728 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800729 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800730 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700731 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800732 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 }
734 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700735 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800736 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700737
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700738 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200739 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700741
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800744 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 }
747
Ian Rogers25e8b912012-09-07 11:31:36 -0700748 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 LOG(FATAL) << "JNI FatalError called: " << msg;
750 }
751
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700752 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700753 // TODO: SOA may not be necessary but I do it to please lock annotations.
754 ScopedObjectAccess soa(env);
755 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700756 return JNI_ERR;
757 }
Ian Rogersef28b142012-11-30 14:22:18 -0800758 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 return JNI_OK;
760 }
761
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700762 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800764 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700765 soa.Env()->PopFrame();
766 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 }
768
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700769 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700770 // TODO: SOA may not be necessary but I do it to please lock annotations.
771 ScopedObjectAccess soa(env);
772 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 }
774
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700776 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800777 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800778 // Check for null after decoding the object to handle cleared weak globals.
779 if (decoded_obj == nullptr) {
780 return nullptr;
781 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700784 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700785 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 return reinterpret_cast<jobject>(ref);
787 }
788
789 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800790 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 return;
792 }
Ian Rogersef28b142012-11-30 14:22:18 -0800793 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800795 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700796 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700797
798 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
799 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
800 << "failed to find entry";
801 }
802 }
803
804 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700805 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800806 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 }
808
809 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700810 if (obj != nullptr) {
811 ScopedObjectAccess soa(env);
812 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
814 }
815
816 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700817 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800818 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800819 // Check for null after decoding the object to handle cleared weak globals.
820 if (decoded_obj == nullptr) {
821 return nullptr;
822 }
823 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 }
825
826 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800827 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 return;
829 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700830 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800831 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700832
Ian Rogersef28b142012-11-30 14:22:18 -0800833 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 if (!locals.Remove(cookie, obj)) {
835 // Attempting to delete a local reference that is not in the
836 // topmost local reference frame is a no-op. DeleteLocalRef returns
837 // void and doesn't throw any exceptions, but we should probably
838 // complain about it so the user will notice that things aren't
839 // going quite the way they expect.
840 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
841 << "failed to find entry";
842 }
843 }
844
845 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700846 if (obj1 == obj2) {
847 return JNI_TRUE;
848 } else {
849 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800850 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
851 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700852 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 }
854
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700855 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700856 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700857 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800858 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800859 if (c == nullptr) {
860 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700861 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700862 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 }
864
Ian Rogersbc939662013-08-15 10:26:54 -0700865 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700868 CHECK_NON_NULL_ARGUMENT(java_class);
869 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700870 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 va_end(args);
872 return result;
873 }
874
Elliott Hughes72025e52011-08-23 17:50:30 -0700875 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700876 CHECK_NON_NULL_ARGUMENT(java_class);
877 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700878 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800879 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800880 if (c == nullptr) {
881 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700882 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800883 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800884 if (result == nullptr) {
885 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700886 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700887 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800889 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800890 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700891 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800892 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 }
894
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700896 CHECK_NON_NULL_ARGUMENT(java_class);
897 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800899 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800900 if (c == nullptr) {
901 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700902 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800903 mirror::Object* result = c->AllocObject(soa.Self());
904 if (result == nullptr) {
905 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700906 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700907 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800909 if (soa.Self()->IsExceptionPending()) {
910 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700911 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800912 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Ian Rogersbc939662013-08-15 10:26:54 -0700915 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700916 CHECK_NON_NULL_ARGUMENT(java_class);
917 CHECK_NON_NULL_ARGUMENT(name);
918 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700920 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Ian Rogersbc939662013-08-15 10:26:54 -0700923 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
924 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700925 CHECK_NON_NULL_ARGUMENT(java_class);
926 CHECK_NON_NULL_ARGUMENT(name);
927 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700929 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 va_list ap;
934 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700935 CHECK_NON_NULL_ARGUMENT(obj);
936 CHECK_NON_NULL_ARGUMENT(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);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700944 CHECK_NON_NULL_ARGUMENT(obj);
945 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 ScopedObjectAccess soa(env);
947 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
948 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700952 CHECK_NON_NULL_ARGUMENT(obj);
953 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700955 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
956 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_list ap;
962 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700963 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
964 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700965 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700968 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700972 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
973 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 ScopedObjectAccess soa(env);
975 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700979 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
980 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700981 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700982 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
983 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_list ap;
988 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700989 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
990 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700991 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700994 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700998 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
999 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001000 ScopedObjectAccess soa(env);
1001 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001005 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1006 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001008 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1009 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_list ap;
1014 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001015 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1016 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001017 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001020 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001024 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1025 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026 ScopedObjectAccess soa(env);
1027 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001031 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1032 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001034 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1035 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_list ap;
1040 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001041 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1042 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001043 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001046 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001050 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1051 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 ScopedObjectAccess soa(env);
1053 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001057 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1058 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001059 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001060 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1061 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_list ap;
1066 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001067 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001069 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001072 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001076 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1077 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
1079 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001083 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1084 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001086 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1087 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001093 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1094 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001095 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001098 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001102 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1103 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 ScopedObjectAccess soa(env);
1105 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001109 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1110 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001112 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1113 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001119 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1120 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001121 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001124 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001128 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1129 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001135 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1136 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001138 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1139 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_list ap;
1144 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001145 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1146 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001147 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001154 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1155 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001161 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1162 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001164 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1165 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 va_list ap;
1170 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001171 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1172 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001173 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001174 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001179 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1180 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 ScopedObjectAccess soa(env);
1182 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001186 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1187 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001189 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001192 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001195 CHECK_NON_NULL_ARGUMENT(obj);
1196 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001197 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1199 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 va_end(ap);
1201 return local_result;
1202 }
1203
Ian Rogersbc939662013-08-15 10:26:54 -07001204 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1205 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001206 CHECK_NON_NULL_ARGUMENT(obj);
1207 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 ScopedObjectAccess soa(env);
1209 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1210 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Ian Rogersbc939662013-08-15 10:26:54 -07001213 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1214 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001215 CHECK_NON_NULL_ARGUMENT(obj);
1216 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001218 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Ian Rogersbc939662013-08-15 10:26:54 -07001222 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1223 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001226 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1227 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001228 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001231 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Ian Rogersbc939662013-08-15 10:26:54 -07001234 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1235 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001236 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1237 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
1239 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Ian Rogersbc939662013-08-15 10:26:54 -07001242 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1243 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001244 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1245 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001247 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001253 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1254 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001255 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001258 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
Ian Rogersbc939662013-08-15 10:26:54 -07001261 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1262 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1264 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 ScopedObjectAccess soa(env);
1266 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Ian Rogersbc939662013-08-15 10:26:54 -07001269 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1270 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001271 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1272 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001274 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001277 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001280 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1281 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001282 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001283 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001285 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Ian Rogersbc939662013-08-15 10:26:54 -07001288 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1289 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001290 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1291 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001292 ScopedObjectAccess soa(env);
1293 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Ian Rogersbc939662013-08-15 10:26:54 -07001296 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1297 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001298 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1299 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001301 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001304 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001307 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1308 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001309 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001312 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Ian Rogersbc939662013-08-15 10:26:54 -07001315 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1316 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001317 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1318 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001319 ScopedObjectAccess soa(env);
1320 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Ian Rogersbc939662013-08-15 10:26:54 -07001323 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1324 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001325 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1326 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001328 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001334 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1335 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001336 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001339 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Ian Rogersbc939662013-08-15 10:26:54 -07001342 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1343 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001344 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1345 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001346 ScopedObjectAccess soa(env);
1347 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Ian Rogersbc939662013-08-15 10:26:54 -07001350 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1351 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001352 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1353 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001355 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001358 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001361 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1362 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001363 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001364 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001366 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
Ian Rogersbc939662013-08-15 10:26:54 -07001369 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1370 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001371 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1372 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 ScopedObjectAccess soa(env);
1374 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Ian Rogersbc939662013-08-15 10:26:54 -07001377 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1378 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001379 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1380 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001381 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001382 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001385 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001388 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1389 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001390 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001391 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001393 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Ian Rogersbc939662013-08-15 10:26:54 -07001396 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1397 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001398 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1399 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001400 ScopedObjectAccess soa(env);
1401 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Ian Rogersbc939662013-08-15 10:26:54 -07001404 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1405 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001406 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1407 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001409 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001412 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001414 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001415 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1416 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001417 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001418 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001420 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Ian Rogersbc939662013-08-15 10:26:54 -07001423 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1424 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001425 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1426 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001427 ScopedObjectAccess soa(env);
1428 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Ian Rogersbc939662013-08-15 10:26:54 -07001431 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1432 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001433 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1434 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001436 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001439 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001441 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001442 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1443 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001444 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 va_end(ap);
1447 }
1448
Brian Carlstromea46f952013-07-30 01:26:50 -07001449 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1450 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001451 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1452 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453 ScopedObjectAccess soa(env);
1454 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 }
1456
Ian Rogersbc939662013-08-15 10:26:54 -07001457 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1458 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001459 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1460 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001461 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001462 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
Ian Rogersbc939662013-08-15 10:26:54 -07001465 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001466 CHECK_NON_NULL_ARGUMENT(java_class);
1467 CHECK_NON_NULL_ARGUMENT(name);
1468 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001469 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001470 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001472
Ian Rogersbc939662013-08-15 10:26:54 -07001473 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1474 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001475 CHECK_NON_NULL_ARGUMENT(java_class);
1476 CHECK_NON_NULL_ARGUMENT(name);
1477 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001478 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001479 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001483 CHECK_NON_NULL_ARGUMENT(obj);
1484 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001485 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001486 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1487 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001488 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001492 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001493 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001494 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001495 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001499 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1500 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001502 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1503 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1504 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001505 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001509 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001510 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001511 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1512 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001513 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001517 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1518 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001519 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001520 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1521 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001522 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001524#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001525 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001526 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001527 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001528 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001531 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1532 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001534 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1535 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001536 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001538#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001539 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001540 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001541 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001542 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001549 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001561 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001576 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001580 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001588 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 }
1599
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001600 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 }
1607
1608 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 }
1611
1612 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 }
1615
1616 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 }
1619
1620 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 }
1623
1624 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001626 }
1627
1628 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001630 }
1631
1632 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 }
1635
1636 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001638 }
1639
1640 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001642 }
1643
1644 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 }
1647
1648 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001649 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001650 }
1651
1652 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001653 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 }
1655
1656 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001657 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001658 }
1659
1660 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001661 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001662 }
1663
1664 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001665 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001666 }
1667
1668 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001669 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001672 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001675 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001676 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001677 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001678 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 va_end(ap);
1680 return local_result;
1681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001684 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001686 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 }
1689
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001690 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001691 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001692 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001693 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001700 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001701 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001702 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001704 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001708 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001710 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001713 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001714 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001716 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001721 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001722 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001723 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001724 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001726 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001730 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001732 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001736 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001737 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001738 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001741 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001744 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001745 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001746 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001748 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001752 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001754 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001758 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001760 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001763 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001766 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001767 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001768 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001770 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001774 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001776 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001779 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001780 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001782 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 }
1784
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001785 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001787 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001788 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001789 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001790 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001792 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001796 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001798 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001801 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001802 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001804 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001807 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001809 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001811 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001812 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001814 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001820 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001823 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001824 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001826 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001829 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001831 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001832 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001833 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001834 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001836 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001840 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001842 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001845 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001846 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001848 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001851 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001853 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001854 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001855 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001858 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
1860
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001861 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001862 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001863 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001864 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001867 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001868 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001869 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001870 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001873 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001875 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001876 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001877 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001878 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 va_end(ap);
1880 }
1881
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001882 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001883 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001885 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001888 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001889 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001891 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes814e4032011-08-23 12:07:56 -07001894 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001895 if (UNLIKELY(char_count < 0)) {
1896 JniAbortF("NewString", "char_count < 0: %d", char_count);
1897 return nullptr;
1898 }
1899 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1900 JniAbortF("NewString", "chars == null && char_count > 0");
1901 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001902 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001904 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001905 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
1908 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001909 if (utf == nullptr) {
1910 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001913 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001914 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes814e4032011-08-23 12:07:56 -07001917 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001918 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001919 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001920 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001921 }
1922
1923 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001924 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001926 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001927 }
1928
Ian Rogersbc939662013-08-15 10:26:54 -07001929 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1930 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001931 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001932 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001933 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001934 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001935 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001936 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001937 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001938 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1939 memcpy(buf, chars + start, length * sizeof(jchar));
1940 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001941 }
1942
Ian Rogersbc939662013-08-15 10:26:54 -07001943 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1944 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001945 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001947 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001948 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001949 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001950 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001951 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001952 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1953 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1954 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001958 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001959 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001960 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1961 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001963 gc::Heap* heap = Runtime::Current()->GetHeap();
1964 if (heap->IsMovableObject(chars)) {
1965 if (is_copy != nullptr) {
1966 *is_copy = JNI_TRUE;
1967 }
1968 int32_t char_count = s->GetLength();
1969 int32_t offset = s->GetOffset();
1970 jchar* bytes = new jchar[char_count];
1971 for (int32_t i = 0; i < char_count; i++) {
1972 bytes[i] = chars->Get(i + offset);
1973 }
1974 return bytes;
1975 } else {
1976 if (is_copy != nullptr) {
1977 *is_copy = JNI_FALSE;
1978 }
1979 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001980 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001981 }
1982
Mathieu Chartier590fee92013-09-13 13:46:47 -07001983 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001984 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001986 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1987 mirror::CharArray* s_chars = s->GetCharArray();
1988 if (chars != (s_chars->GetData() + s->GetOffset())) {
1989 delete[] chars;
1990 }
1991 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001995 CHECK_NON_NULL_ARGUMENT(java_string);
1996 ScopedObjectAccess soa(env);
1997 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1998 mirror::CharArray* chars = s->GetCharArray();
1999 int32_t offset = s->GetOffset();
2000 PinPrimitiveArray(soa, chars);
2001 gc::Heap* heap = Runtime::Current()->GetHeap();
2002 if (heap->IsMovableObject(chars)) {
2003 StackHandleScope<1> hs(soa.Self());
2004 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
2005 heap->IncrementDisableMovingGC(soa.Self());
2006 }
2007 if (is_copy != nullptr) {
2008 *is_copy = JNI_FALSE;
2009 }
2010 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002014 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2015 ScopedObjectAccess soa(env);
2016 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
2017 gc::Heap* heap = Runtime::Current()->GetHeap();
2018 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2019 mirror::CharArray* s_chars = s->GetCharArray();
2020 if (heap->IsMovableObject(s_chars)) {
2021 heap->DecrementDisableMovingGC(soa.Self());
2022 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002026 if (java_string == nullptr) {
2027 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002028 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002029 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002030 *is_copy = JNI_TRUE;
2031 }
Ian Rogersef28b142012-11-30 14:22:18 -08002032 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002033 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002034 size_t byte_count = s->GetUtfLength();
2035 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002036 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002037 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2038 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2039 bytes[byte_count] = '\0';
2040 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002044 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002045 }
2046
Elliott Hughesbd935992011-08-22 11:59:34 -07002047 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002048 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002049 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002050 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002051 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002052 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2053 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002054 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002055 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002059 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002060 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002061 mirror::ObjectArray<mirror::Object>* array =
2062 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002063 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Ian Rogersbc939662013-08-15 10:26:54 -07002066 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2067 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002068 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002069 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002070 mirror::ObjectArray<mirror::Object>* array =
2071 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2072 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002073 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
2076 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002077 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
2080 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002081 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
2084 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002085 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
2088 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002089 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
2092 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002093 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
2096 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002097 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
2100 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002101 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Ian Rogers1d99e452014-01-02 17:36:41 -08002104 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2105 jobject initial_element) {
2106 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002107 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002108 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002109 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002110 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111
2112 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002113 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002114 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002115 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002116 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002117 if (UNLIKELY(element_class->IsPrimitive())) {
2118 JniAbortF("NewObjectArray", "not an object type: %s",
2119 PrettyDescriptor(element_class).c_str());
2120 return nullptr;
2121 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002122 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002123 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002124 if (UNLIKELY(array_class == nullptr)) {
2125 return nullptr;
2126 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes75770752011-08-24 17:52:38 -07002129 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002130 mirror::ObjectArray<mirror::Object>* result =
2131 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002132 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002133 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002134 if (initial_object != nullptr) {
2135 mirror::Class* element_class = result->GetClass()->GetComponentType();
2136 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2137 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2138 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2139 PrettyDescriptor(element_class).c_str());
2140
2141 } else {
2142 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002143 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002144 }
2145 }
Elliott Hughes75770752011-08-24 17:52:38 -07002146 }
2147 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002148 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
2151 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002152 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Ian Rogersa15e67d2012-02-28 13:51:55 -08002155 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002156 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002157 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002158 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002159 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2160 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2161 PrettyDescriptor(array->GetClass()).c_str());
2162 return nullptr;
2163 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002164 gc::Heap* heap = Runtime::Current()->GetHeap();
2165 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002166 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002167 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002168 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002169 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002170 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002171 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002172 *is_copy = JNI_FALSE;
2173 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002174 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002175 }
2176
Ian Rogers2d10b202014-05-12 19:15:18 -07002177 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2178 jint mode) {
2179 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2180 ScopedObjectAccess soa(env);
2181 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2182 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2183 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2184 PrettyDescriptor(array->GetClass()).c_str());
2185 return;
2186 }
2187 const size_t component_size = array->GetClass()->GetComponentSize();
2188 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002189 }
2190
Elliott Hughes75770752011-08-24 17:52:38 -07002191 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002192 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes75770752011-08-24 17:52:38 -07002195 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002196 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Elliott Hughes75770752011-08-24 17:52:38 -07002199 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002200 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes75770752011-08-24 17:52:38 -07002203 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002204 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes75770752011-08-24 17:52:38 -07002207 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002208 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughes75770752011-08-24 17:52:38 -07002211 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002212 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Elliott Hughes75770752011-08-24 17:52:38 -07002215 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002216 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughes75770752011-08-24 17:52:38 -07002219 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002220 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Mathieu Chartier590fee92013-09-13 13:46:47 -07002223 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2224 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002225 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2226 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 }
2228
Mathieu Chartier590fee92013-09-13 13:46:47 -07002229 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002230 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Mathieu Chartier590fee92013-09-13 13:46:47 -07002233 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002234 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Mathieu Chartier590fee92013-09-13 13:46:47 -07002237 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2238 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002239 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Mathieu Chartier590fee92013-09-13 13:46:47 -07002242 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2243 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002244 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Mathieu Chartier590fee92013-09-13 13:46:47 -07002247 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002248 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 }
2250
Mathieu Chartier590fee92013-09-13 13:46:47 -07002251 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002252 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Mathieu Chartier590fee92013-09-13 13:46:47 -07002255 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2256 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002257 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259
Ian Rogersbc939662013-08-15 10:26:54 -07002260 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2261 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002262 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002263 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
2265
Ian Rogersbc939662013-08-15 10:26:54 -07002266 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2267 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002268 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Ian Rogersbc939662013-08-15 10:26:54 -07002271 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2272 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002273 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
Ian Rogersbc939662013-08-15 10:26:54 -07002276 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2277 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002278 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002279 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2283 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002284 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002285 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 }
2287
Ian Rogersbc939662013-08-15 10:26:54 -07002288 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2289 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002290 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 }
2292
Ian Rogersbc939662013-08-15 10:26:54 -07002293 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2294 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002295 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Ian Rogersbc939662013-08-15 10:26:54 -07002298 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2299 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002300 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002301 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Ian Rogersbc939662013-08-15 10:26:54 -07002304 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2305 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002306 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002307 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Ian Rogersbc939662013-08-15 10:26:54 -07002310 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2311 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002312 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Ian Rogersbc939662013-08-15 10:26:54 -07002315 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2316 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002317 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319
Ian Rogersbc939662013-08-15 10:26:54 -07002320 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2321 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002322 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002323 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
2325
Ian Rogersbc939662013-08-15 10:26:54 -07002326 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2327 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002328 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002329 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002330 }
2331
Ian Rogersbc939662013-08-15 10:26:54 -07002332 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2333 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002334 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002335 }
2336
Ian Rogersbc939662013-08-15 10:26:54 -07002337 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2338 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002339 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002340 }
2341
Ian Rogersbc939662013-08-15 10:26:54 -07002342 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2343 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002344 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002345 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002346 }
2347
Ian Rogersbc939662013-08-15 10:26:54 -07002348 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2349 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002350 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2351 }
2352
Ian Rogersbc939662013-08-15 10:26:54 -07002353 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2354 jint method_count, bool return_errors) {
2355 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002356 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002357 return JNI_ERR; // Not reached.
2358 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002359 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002361 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002362 if (UNLIKELY(method_count == 0)) {
2363 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2364 << PrettyDescriptor(c);
2365 return JNI_OK;
2366 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002367 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002368 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 const char* name = methods[i].name;
2370 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002371 const void* fnPtr = methods[i].fnPtr;
2372 if (UNLIKELY(name == nullptr)) {
2373 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2374 return JNI_ERR;
2375 } else if (UNLIKELY(sig == nullptr)) {
2376 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2377 return JNI_ERR;
2378 } else if (UNLIKELY(fnPtr == nullptr)) {
2379 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2380 return JNI_ERR;
2381 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002382 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002383 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002384 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002385 ++sig;
2386 }
2387
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002388 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2389 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002390 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002391 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002392 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002393 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002394 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002395 << PrettyDescriptor(c) << "." << name << sig << " in "
2396 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002397 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002398 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002399 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002400 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002401 << PrettyDescriptor(c) << "." << name << sig
2402 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002403 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002404 return JNI_ERR;
2405 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002406
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002407 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002408
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002409 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002410 }
2411 return JNI_OK;
2412 }
2413
Elliott Hughes5174fe62011-08-23 15:12:35 -07002414 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002415 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002416 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002417 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002418
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002419 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002420
Ian Rogers2d10b202014-05-12 19:15:18 -07002421 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002422 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002423 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002424 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002425 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002426 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002427 }
2428 }
2429 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002430 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002431 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002432 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002433 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002434 }
2435 }
2436
Ian Rogers2d10b202014-05-12 19:15:18 -07002437 if (unregistered_count == 0) {
2438 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2439 << PrettyDescriptor(c) << "' that contains no native methods";
2440 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002441 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002442 }
2443
Ian Rogers719d1a32014-03-06 12:13:39 -08002444 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002445 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002446 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002447 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2448 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002449 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002450 return JNI_ERR;
2451 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002452 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002453 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002454 }
2455
Ian Rogers719d1a32014-03-06 12:13:39 -08002456 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002457 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002458 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002459 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002460 o->MonitorExit(soa.Self());
2461 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002462 return JNI_ERR;
2463 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002464 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002465 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002466 }
2467
2468 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002469 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002470 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002471 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002472 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002473 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002474 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002475 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002476 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002477 }
2478
Elliott Hughescdf53122011-08-19 15:46:09 -07002479 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002480 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002481 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002482 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002483 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002484 if (address == nullptr && capacity != 0) {
2485 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002486 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002487 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002488
Brian Carlstrom85a93362014-06-25 09:30:52 -07002489 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002490 if (capacity > INT_MAX) {
2491 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2492 return nullptr;
2493 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002494 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002495 jint capacity_arg = static_cast<jint>(capacity);
2496
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002497 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2498 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002499 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002500 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002501 }
2502
Elliott Hughesb465ab02011-08-24 11:21:21 -07002503 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002504 return reinterpret_cast<void*>(env->GetLongField(
2505 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002506 }
2507
Elliott Hughesb465ab02011-08-24 11:21:21 -07002508 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002509 return static_cast<jlong>(env->GetIntField(
2510 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002511 }
2512
Elliott Hughesb465ab02011-08-24 11:21:21 -07002513 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002514 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002515
2516 // Do we definitely know what kind of reference this is?
2517 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2518 IndirectRefKind kind = GetIndirectRefKind(ref);
2519 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002520 case kLocal: {
2521 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002522 // The local refs don't need a read barrier.
2523 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2524 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002525 return JNILocalRefType;
2526 }
2527 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002528 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002529 case kGlobal:
2530 return JNIGlobalRefType;
2531 case kWeakGlobal:
2532 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002533 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002534 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002535 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002536 return JNILocalRefType;
2537 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002538 return JNIInvalidRefType;
2539 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002540 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2541 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002542 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002543
2544 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002545 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2546 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002547 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002548 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002549 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2550 return JNI_ERR;
2551 }
2552 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002553 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002554 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2555 if (!okay) {
2556 soa.Self()->ThrowOutOfMemoryError(caller);
2557 }
2558 return okay ? JNI_OK : JNI_ERR;
2559 }
2560
2561 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002562 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002563 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002564 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002565 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002566 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002567 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002568 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002569 return soa.AddLocalReference<JniT>(result);
2570 }
2571
Ian Rogers2d10b202014-05-12 19:15:18 -07002572 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2573 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2574 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002577 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2578 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2579 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2580 PrettyDescriptor(array->GetClass()).c_str());
2581 return nullptr;
2582 }
2583 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2584 return array;
2585 }
2586
2587 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2588 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2589 CHECK_NON_NULL_ARGUMENT(java_array);
2590 ScopedObjectAccess soa(env);
2591 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2592 "GetArrayElements",
2593 "get");
2594 if (UNLIKELY(array == nullptr)) {
2595 return nullptr;
2596 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002597 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002598 // Only make a copy if necessary.
2599 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2600 if (is_copy != nullptr) {
2601 *is_copy = JNI_TRUE;
2602 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002603 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002604 size_t size = array->GetLength() * component_size;
2605 void* data = new uint64_t[RoundUp(size, 8) / 8];
2606 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002607 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002608 } else {
2609 if (is_copy != nullptr) {
2610 *is_copy = JNI_FALSE;
2611 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002612 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002613 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002614 }
2615
Ian Rogers2d10b202014-05-12 19:15:18 -07002616 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002617 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002618 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002619 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002620 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2621 "ReleaseArrayElements",
2622 "release");
2623 if (array == nullptr) {
2624 return;
2625 }
2626 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2627 }
2628
2629 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2630 size_t component_size, void* elements, jint mode)
2631 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002632 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002633 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002634 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002635 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002636 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2637 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002638 if (is_copy) {
2639 // Sanity check: If elements is not the same as the java array's data, it better not be a
2640 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2641 // copies we make?
2642 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2643 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2644 reinterpret_cast<void*>(elements), array_data);
2645 return;
2646 }
2647 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002648 // Don't need to copy if we had a direct pointer.
2649 if (mode != JNI_ABORT && is_copy) {
2650 memcpy(array_data, elements, bytes);
2651 }
2652 if (mode != JNI_COMMIT) {
2653 if (is_copy) {
2654 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002655 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002656 // Non copy to a movable object must means that we had disabled the moving GC.
2657 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002658 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002659 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002660 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002661 }
2662
Ian Rogers2d10b202014-05-12 19:15:18 -07002663 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2664 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2665 jsize start, jsize length, ElementT* buf) {
2666 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2667 ScopedObjectAccess soa(env);
2668 ArtArrayT* array =
2669 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2670 "GetPrimitiveArrayRegion",
2671 "get region of");
2672 if (array != nullptr) {
2673 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2674 ThrowAIOOBE(soa, array, start, length, "src");
2675 } else {
2676 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2677 ElementT* data = array->GetData();
2678 memcpy(buf, data + start, length * sizeof(ElementT));
2679 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002680 }
2681 }
2682
Ian Rogers2d10b202014-05-12 19:15:18 -07002683 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2684 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2685 jsize start, jsize length, const ElementT* buf) {
2686 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2687 ScopedObjectAccess soa(env);
2688 ArtArrayT* array =
2689 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2690 "SetPrimitiveArrayRegion",
2691 "set region of");
2692 if (array != nullptr) {
2693 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2694 ThrowAIOOBE(soa, array, start, length, "dst");
2695 } else {
2696 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2697 ElementT* data = array->GetData();
2698 memcpy(data + start, buf, length * sizeof(ElementT));
2699 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002700 }
2701 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002702};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002703
Elliott Hughes88c5c352012-03-15 18:49:48 -07002704const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002705 nullptr, // reserved0.
2706 nullptr, // reserved1.
2707 nullptr, // reserved2.
2708 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002709 JNI::GetVersion,
2710 JNI::DefineClass,
2711 JNI::FindClass,
2712 JNI::FromReflectedMethod,
2713 JNI::FromReflectedField,
2714 JNI::ToReflectedMethod,
2715 JNI::GetSuperclass,
2716 JNI::IsAssignableFrom,
2717 JNI::ToReflectedField,
2718 JNI::Throw,
2719 JNI::ThrowNew,
2720 JNI::ExceptionOccurred,
2721 JNI::ExceptionDescribe,
2722 JNI::ExceptionClear,
2723 JNI::FatalError,
2724 JNI::PushLocalFrame,
2725 JNI::PopLocalFrame,
2726 JNI::NewGlobalRef,
2727 JNI::DeleteGlobalRef,
2728 JNI::DeleteLocalRef,
2729 JNI::IsSameObject,
2730 JNI::NewLocalRef,
2731 JNI::EnsureLocalCapacity,
2732 JNI::AllocObject,
2733 JNI::NewObject,
2734 JNI::NewObjectV,
2735 JNI::NewObjectA,
2736 JNI::GetObjectClass,
2737 JNI::IsInstanceOf,
2738 JNI::GetMethodID,
2739 JNI::CallObjectMethod,
2740 JNI::CallObjectMethodV,
2741 JNI::CallObjectMethodA,
2742 JNI::CallBooleanMethod,
2743 JNI::CallBooleanMethodV,
2744 JNI::CallBooleanMethodA,
2745 JNI::CallByteMethod,
2746 JNI::CallByteMethodV,
2747 JNI::CallByteMethodA,
2748 JNI::CallCharMethod,
2749 JNI::CallCharMethodV,
2750 JNI::CallCharMethodA,
2751 JNI::CallShortMethod,
2752 JNI::CallShortMethodV,
2753 JNI::CallShortMethodA,
2754 JNI::CallIntMethod,
2755 JNI::CallIntMethodV,
2756 JNI::CallIntMethodA,
2757 JNI::CallLongMethod,
2758 JNI::CallLongMethodV,
2759 JNI::CallLongMethodA,
2760 JNI::CallFloatMethod,
2761 JNI::CallFloatMethodV,
2762 JNI::CallFloatMethodA,
2763 JNI::CallDoubleMethod,
2764 JNI::CallDoubleMethodV,
2765 JNI::CallDoubleMethodA,
2766 JNI::CallVoidMethod,
2767 JNI::CallVoidMethodV,
2768 JNI::CallVoidMethodA,
2769 JNI::CallNonvirtualObjectMethod,
2770 JNI::CallNonvirtualObjectMethodV,
2771 JNI::CallNonvirtualObjectMethodA,
2772 JNI::CallNonvirtualBooleanMethod,
2773 JNI::CallNonvirtualBooleanMethodV,
2774 JNI::CallNonvirtualBooleanMethodA,
2775 JNI::CallNonvirtualByteMethod,
2776 JNI::CallNonvirtualByteMethodV,
2777 JNI::CallNonvirtualByteMethodA,
2778 JNI::CallNonvirtualCharMethod,
2779 JNI::CallNonvirtualCharMethodV,
2780 JNI::CallNonvirtualCharMethodA,
2781 JNI::CallNonvirtualShortMethod,
2782 JNI::CallNonvirtualShortMethodV,
2783 JNI::CallNonvirtualShortMethodA,
2784 JNI::CallNonvirtualIntMethod,
2785 JNI::CallNonvirtualIntMethodV,
2786 JNI::CallNonvirtualIntMethodA,
2787 JNI::CallNonvirtualLongMethod,
2788 JNI::CallNonvirtualLongMethodV,
2789 JNI::CallNonvirtualLongMethodA,
2790 JNI::CallNonvirtualFloatMethod,
2791 JNI::CallNonvirtualFloatMethodV,
2792 JNI::CallNonvirtualFloatMethodA,
2793 JNI::CallNonvirtualDoubleMethod,
2794 JNI::CallNonvirtualDoubleMethodV,
2795 JNI::CallNonvirtualDoubleMethodA,
2796 JNI::CallNonvirtualVoidMethod,
2797 JNI::CallNonvirtualVoidMethodV,
2798 JNI::CallNonvirtualVoidMethodA,
2799 JNI::GetFieldID,
2800 JNI::GetObjectField,
2801 JNI::GetBooleanField,
2802 JNI::GetByteField,
2803 JNI::GetCharField,
2804 JNI::GetShortField,
2805 JNI::GetIntField,
2806 JNI::GetLongField,
2807 JNI::GetFloatField,
2808 JNI::GetDoubleField,
2809 JNI::SetObjectField,
2810 JNI::SetBooleanField,
2811 JNI::SetByteField,
2812 JNI::SetCharField,
2813 JNI::SetShortField,
2814 JNI::SetIntField,
2815 JNI::SetLongField,
2816 JNI::SetFloatField,
2817 JNI::SetDoubleField,
2818 JNI::GetStaticMethodID,
2819 JNI::CallStaticObjectMethod,
2820 JNI::CallStaticObjectMethodV,
2821 JNI::CallStaticObjectMethodA,
2822 JNI::CallStaticBooleanMethod,
2823 JNI::CallStaticBooleanMethodV,
2824 JNI::CallStaticBooleanMethodA,
2825 JNI::CallStaticByteMethod,
2826 JNI::CallStaticByteMethodV,
2827 JNI::CallStaticByteMethodA,
2828 JNI::CallStaticCharMethod,
2829 JNI::CallStaticCharMethodV,
2830 JNI::CallStaticCharMethodA,
2831 JNI::CallStaticShortMethod,
2832 JNI::CallStaticShortMethodV,
2833 JNI::CallStaticShortMethodA,
2834 JNI::CallStaticIntMethod,
2835 JNI::CallStaticIntMethodV,
2836 JNI::CallStaticIntMethodA,
2837 JNI::CallStaticLongMethod,
2838 JNI::CallStaticLongMethodV,
2839 JNI::CallStaticLongMethodA,
2840 JNI::CallStaticFloatMethod,
2841 JNI::CallStaticFloatMethodV,
2842 JNI::CallStaticFloatMethodA,
2843 JNI::CallStaticDoubleMethod,
2844 JNI::CallStaticDoubleMethodV,
2845 JNI::CallStaticDoubleMethodA,
2846 JNI::CallStaticVoidMethod,
2847 JNI::CallStaticVoidMethodV,
2848 JNI::CallStaticVoidMethodA,
2849 JNI::GetStaticFieldID,
2850 JNI::GetStaticObjectField,
2851 JNI::GetStaticBooleanField,
2852 JNI::GetStaticByteField,
2853 JNI::GetStaticCharField,
2854 JNI::GetStaticShortField,
2855 JNI::GetStaticIntField,
2856 JNI::GetStaticLongField,
2857 JNI::GetStaticFloatField,
2858 JNI::GetStaticDoubleField,
2859 JNI::SetStaticObjectField,
2860 JNI::SetStaticBooleanField,
2861 JNI::SetStaticByteField,
2862 JNI::SetStaticCharField,
2863 JNI::SetStaticShortField,
2864 JNI::SetStaticIntField,
2865 JNI::SetStaticLongField,
2866 JNI::SetStaticFloatField,
2867 JNI::SetStaticDoubleField,
2868 JNI::NewString,
2869 JNI::GetStringLength,
2870 JNI::GetStringChars,
2871 JNI::ReleaseStringChars,
2872 JNI::NewStringUTF,
2873 JNI::GetStringUTFLength,
2874 JNI::GetStringUTFChars,
2875 JNI::ReleaseStringUTFChars,
2876 JNI::GetArrayLength,
2877 JNI::NewObjectArray,
2878 JNI::GetObjectArrayElement,
2879 JNI::SetObjectArrayElement,
2880 JNI::NewBooleanArray,
2881 JNI::NewByteArray,
2882 JNI::NewCharArray,
2883 JNI::NewShortArray,
2884 JNI::NewIntArray,
2885 JNI::NewLongArray,
2886 JNI::NewFloatArray,
2887 JNI::NewDoubleArray,
2888 JNI::GetBooleanArrayElements,
2889 JNI::GetByteArrayElements,
2890 JNI::GetCharArrayElements,
2891 JNI::GetShortArrayElements,
2892 JNI::GetIntArrayElements,
2893 JNI::GetLongArrayElements,
2894 JNI::GetFloatArrayElements,
2895 JNI::GetDoubleArrayElements,
2896 JNI::ReleaseBooleanArrayElements,
2897 JNI::ReleaseByteArrayElements,
2898 JNI::ReleaseCharArrayElements,
2899 JNI::ReleaseShortArrayElements,
2900 JNI::ReleaseIntArrayElements,
2901 JNI::ReleaseLongArrayElements,
2902 JNI::ReleaseFloatArrayElements,
2903 JNI::ReleaseDoubleArrayElements,
2904 JNI::GetBooleanArrayRegion,
2905 JNI::GetByteArrayRegion,
2906 JNI::GetCharArrayRegion,
2907 JNI::GetShortArrayRegion,
2908 JNI::GetIntArrayRegion,
2909 JNI::GetLongArrayRegion,
2910 JNI::GetFloatArrayRegion,
2911 JNI::GetDoubleArrayRegion,
2912 JNI::SetBooleanArrayRegion,
2913 JNI::SetByteArrayRegion,
2914 JNI::SetCharArrayRegion,
2915 JNI::SetShortArrayRegion,
2916 JNI::SetIntArrayRegion,
2917 JNI::SetLongArrayRegion,
2918 JNI::SetFloatArrayRegion,
2919 JNI::SetDoubleArrayRegion,
2920 JNI::RegisterNatives,
2921 JNI::UnregisterNatives,
2922 JNI::MonitorEnter,
2923 JNI::MonitorExit,
2924 JNI::GetJavaVM,
2925 JNI::GetStringRegion,
2926 JNI::GetStringUTFRegion,
2927 JNI::GetPrimitiveArrayCritical,
2928 JNI::ReleasePrimitiveArrayCritical,
2929 JNI::GetStringCritical,
2930 JNI::ReleaseStringCritical,
2931 JNI::NewWeakGlobalRef,
2932 JNI::DeleteWeakGlobalRef,
2933 JNI::ExceptionCheck,
2934 JNI::NewDirectByteBuffer,
2935 JNI::GetDirectBufferAddress,
2936 JNI::GetDirectBufferCapacity,
2937 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002938};
2939
Elliott Hughes75770752011-08-24 17:52:38 -07002940JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002941 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002942 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002943 local_ref_cookie(IRT_FIRST_SEGMENT),
2944 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002945 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002946 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002947 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002948 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002949 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002950 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002951 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002952}
2953
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002954JNIEnvExt::~JNIEnvExt() {
2955}
2956
Mathieu Chartier590fee92013-09-13 13:46:47 -07002957jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2958 if (obj == nullptr) {
2959 return nullptr;
2960 }
2961 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2962}
2963
2964void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2965 if (obj != nullptr) {
2966 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2967 }
2968}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002969void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2970 check_jni = enabled;
2971 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002972}
2973
Elliott Hughes73e66f72012-05-09 09:34:45 -07002974void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2975 locals.Dump(os);
2976 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002977}
2978
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002979void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2980 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002981 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002982 stacked_local_ref_cookies.push_back(local_ref_cookie);
2983 local_ref_cookie = locals.GetSegmentState();
2984}
2985
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002986void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002987 locals.SetSegmentState(local_ref_cookie);
2988 local_ref_cookie = stacked_local_ref_cookies.back();
2989 stacked_local_ref_cookies.pop_back();
2990}
2991
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002992Offset JNIEnvExt::SegmentStateOffset() {
2993 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2994 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2995}
2996
Carl Shapiroea4dca82011-08-01 13:45:38 -07002997// JNI Invocation interface.
2998
Brian Carlstrombddf9762013-05-14 11:35:37 -07002999extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003000 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07003001 if (IsBadJniVersion(args->version)) {
3002 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003003 return JNI_EVERSION;
3004 }
3005 Runtime::Options options;
3006 for (int i = 0; i < args->nOptions; ++i) {
3007 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003008 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003009 }
3010 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003011 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003012 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003013 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003014 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003015 bool started = runtime->Start();
3016 if (!started) {
3017 delete Thread::Current()->GetJniEnv();
3018 delete runtime->GetJavaVM();
3019 LOG(WARNING) << "CreateJavaVM failed";
3020 return JNI_ERR;
3021 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003022 *p_env = Thread::Current()->GetJniEnv();
3023 *p_vm = runtime->GetJavaVM();
3024 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003025}
3026
Elliott Hughesf2682d52011-08-15 16:37:04 -07003027extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003028 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003029 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003030 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003031 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003032 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003033 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003034 }
3035 return JNI_OK;
3036}
3037
3038// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003039extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003040 return JNI_ERR;
3041}
3042
Elliott Hughescdf53122011-08-19 15:46:09 -07003043class JII {
3044 public:
3045 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003046 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003047 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003048 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003049 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3050 delete raw_vm->runtime;
3051 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003052 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003053
Elliott Hughescdf53122011-08-19 15:46:09 -07003054 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003055 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003056 }
3057
3058 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003059 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003060 }
3061
3062 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003063 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003064 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003065 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003066 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3067 Runtime* runtime = raw_vm->runtime;
3068 runtime->DetachCurrentThread();
3069 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003070 }
3071
3072 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003073 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3074 // and unlike other calls that take a JNI version doesn't care if you supply
3075 // JNI_VERSION_1_1, which we don't otherwise support.
3076 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003077 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003078 return JNI_EVERSION;
3079 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003080 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003081 return JNI_ERR;
3082 }
3083 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003084 if (thread == nullptr) {
3085 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003086 return JNI_EDETACHED;
3087 }
3088 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003089 return JNI_OK;
3090 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003091};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003092
Elliott Hughes88c5c352012-03-15 18:49:48 -07003093const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003094 nullptr, // reserved0
3095 nullptr, // reserved1
3096 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003097 JII::DestroyJavaVM,
3098 JII::AttachCurrentThread,
3099 JII::DetachCurrentThread,
3100 JII::GetEnv,
3101 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003102};
3103
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003104JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003105 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003106 check_jni_abort_hook(nullptr),
3107 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003108 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003109 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003110 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003111 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003112 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003113 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003114 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003115 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003116 libraries(new Libraries),
3117 weak_globals_lock_("JNI weak global reference table lock"),
3118 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3119 allow_new_weak_globals_(true),
3120 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003121 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003122 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003123 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003124 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003125}
3126
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003127JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003128 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003129}
3130
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003131jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3132 if (obj == nullptr) {
3133 return nullptr;
3134 }
3135 MutexLock mu(self, weak_globals_lock_);
3136 while (UNLIKELY(!allow_new_weak_globals_)) {
3137 weak_globals_add_condition_.WaitHoldingLocks(self);
3138 }
3139 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3140 return reinterpret_cast<jweak>(ref);
3141}
3142
3143void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3144 MutexLock mu(self, weak_globals_lock_);
3145 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3146 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3147 << "failed to find entry";
3148 }
3149}
3150
Elliott Hughes88c5c352012-03-15 18:49:48 -07003151void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3152 check_jni = enabled;
3153 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003154}
3155
Elliott Hughesae80b492012-04-24 10:43:17 -07003156void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3157 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3158 if (force_copy) {
3159 os << " (with forcecopy)";
3160 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003161 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003162 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003163 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003164 os << "; pins=" << pin_table.Size();
3165 }
3166 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003167 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003168 os << "; globals=" << globals.Capacity();
3169 }
3170 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003171 MutexLock mu(self, weak_globals_lock_);
3172 if (weak_globals_.Capacity() > 0) {
3173 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003174 }
3175 }
3176 os << '\n';
3177
3178 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003179 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003180 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3181 }
3182}
3183
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003184void JavaVMExt::DisallowNewWeakGlobals() {
3185 MutexLock mu(Thread::Current(), weak_globals_lock_);
3186 allow_new_weak_globals_ = false;
3187}
3188
3189void JavaVMExt::AllowNewWeakGlobals() {
3190 Thread* self = Thread::Current();
3191 MutexLock mu(self, weak_globals_lock_);
3192 allow_new_weak_globals_ = true;
3193 weak_globals_add_condition_.Broadcast(self);
3194}
3195
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003196mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3197 MutexLock mu(self, weak_globals_lock_);
3198 while (UNLIKELY(!allow_new_weak_globals_)) {
3199 weak_globals_add_condition_.WaitHoldingLocks(self);
3200 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003201 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003202}
3203
Elliott Hughes73e66f72012-05-09 09:34:45 -07003204void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003205 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003206 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003207 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003208 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003209 }
3210 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003211 MutexLock mu(self, weak_globals_lock_);
3212 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003213 }
3214 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003215 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003216 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003217 }
3218}
3219
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003220bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003221 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003222 std::string* detail) {
3223 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003224
3225 // See if we've already loaded this library. If we have, and the class loader
3226 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003227 // TODO: for better results we should canonicalize the pathname (or even compare
3228 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003229 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003230 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003231 {
3232 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003233 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003234 library = libraries->Get(path);
3235 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003236 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003237 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003238 // The library will be associated with class_loader. The JNI
3239 // spec says we can't load the same library into more than one
3240 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003241 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003242 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003243 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003244 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003245 return false;
3246 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003247 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003248 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003249 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003250 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003251 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003252 return false;
3253 }
3254 return true;
3255 }
3256
3257 // Open the shared library. Because we're using a full path, the system
3258 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3259 // resolve this library's dependencies though.)
3260
3261 // Failures here are expected when java.library.path has several entries
3262 // and we have to hunt for the lib.
3263
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003264 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3265 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3266 // dlopen) becomes zero from dlclose.
3267
Elliott Hughescdf53122011-08-19 15:46:09 -07003268 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003269 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003270 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003271 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003272 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003273
Elliott Hughes84b2f142012-09-27 09:16:28 -07003274 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003275
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003276 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003277 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003278 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003279 return false;
3280 }
3281
3282 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003283 // TODO: move the locking (and more of this logic) into Libraries.
3284 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003285 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003286 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003287 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003288 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003289 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003290 libraries->Put(path, library);
3291 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003292 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003293 }
3294 if (!created_library) {
3295 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003296 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003297 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003298 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003299
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003300 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003301 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003302
Elliott Hughes79353722013-08-02 16:52:18 -07003303 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003304 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003305 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003306 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003307 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003308 } else {
3309 // Call JNI_OnLoad. We have to override the current class
3310 // loader, which will always be "null" since the stuff at the
3311 // top of the stack is around Runtime.loadLibrary(). (See
3312 // the comments in the JNI FindClass function.)
3313 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3314 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003315 StackHandleScope<1> hs(self);
3316 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3317 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003318
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003319 int version = 0;
3320 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003321 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003322 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003323 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003324 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003325
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003326 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003327
Elliott Hughes79353722013-08-02 16:52:18 -07003328 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003329 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003330 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003331 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003332 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003333 // It's unwise to call dlclose() here, but we can mark it
3334 // as bad and ensure that future load attempts will fail.
3335 // We don't know how far JNI_OnLoad got, so there could
3336 // be some partially-initialized stuff accessible through
3337 // newly-registered native method calls. We could try to
3338 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003339 } else {
3340 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003341 }
Elliott Hughes79353722013-08-02 16:52:18 -07003342 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003343 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003344 }
3345
Elliott Hughes79353722013-08-02 16:52:18 -07003346 library->SetResult(was_successful);
3347 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003348}
3349
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003350void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003351 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003352 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003353 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003354 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003355 c = EnsureInitialized(Thread::Current(), c);
3356 if (c == nullptr) {
3357 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003358 }
3359 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003360 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003361 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003362 std::string detail;
3363 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003364 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003365 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003366 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003367 native_method = libraries->FindNativeMethod(m, detail);
3368 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003369 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003370 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003371 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3372 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003373 }
3374 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003375}
3376
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003377void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003378 MutexLock mu(Thread::Current(), weak_globals_lock_);
3379 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003380 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003381 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003382 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003383 if (new_obj == nullptr) {
3384 new_obj = kClearedJniWeakGlobal;
3385 }
3386 *entry = new_obj;
3387 }
3388}
3389
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003390void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003391 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003392 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003393 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003394 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003395 }
3396 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003397 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003398 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003399 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003400 {
3401 MutexLock mu(self, libraries_lock);
3402 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003403 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003404 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003405 // The weak_globals table is visited by the GC itself (because it mutates the table).
3406}
3407
Elliott Hughesc8fece32013-01-02 11:27:23 -08003408void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003409 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003410 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003411 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003412 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3413 }
3414 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3415}
3416
Ian Rogersdf20fe02011-07-20 20:34:16 -07003417} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003418
3419std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3420 switch (rhs) {
3421 case JNIInvalidRefType:
3422 os << "JNIInvalidRefType";
3423 return os;
3424 case JNILocalRefType:
3425 os << "JNILocalRefType";
3426 return os;
3427 case JNIGlobalRefType:
3428 os << "JNIGlobalRefType";
3429 return os;
3430 case JNIWeakGlobalRefType:
3431 os << "JNIWeakGlobalRefType";
3432 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003433 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003434 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003435 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003436 }
3437}