blob: f9c7ec692c6f40c7b6ff0c54fefb3b5196b916e9 [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"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080044#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070045#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070046#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070052#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070053
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070054namespace art {
55
Brian Carlstrom7934ac22013-07-26 10:54:15 -070056static const size_t kMonitorsInitial = 32; // Arbitrary.
57static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070058
Brian Carlstrom7934ac22013-07-26 10:54:15 -070059static const size_t kLocalsInitial = 64; // Arbitrary.
60static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070061
Brian Carlstrom7934ac22013-07-26 10:54:15 -070062static const size_t kPinTableInitial = 16; // Arbitrary.
63static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070064
Brian Carlstrom7934ac22013-07-26 10:54:15 -070065static size_t gGlobalsInitial = 512; // Arbitrary.
66static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070067
Brian Carlstrom7934ac22013-07-26 10:54:15 -070068static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
69static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070070
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080071static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070073 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070074}
75
Jeff Hao19c5d372013-03-15 14:33:43 -070076static bool IsBadJniVersion(int version) {
77 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
78 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
79}
80
Elliott Hughes6b436852011-08-12 10:16:44 -070081// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
82// separated with slashes but aren't wrapped with "L;" like regular descriptors
83// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
84// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
85// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070086static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070087 std::string result;
88 // Add the missing "L;" if necessary.
89 if (name[0] == '[') {
90 result = name;
91 } else {
92 result += 'L';
93 result += name;
94 result += ';';
95 }
96 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070097 if (result.find('.') != std::string::npos) {
98 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
99 << "\"" << name << "\"";
100 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700101 }
102 return result;
103}
104
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800105static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
109 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
110 "no %s method \"%s.%s%s\"",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700111 kind, c->GetDescriptor().c_str(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700112}
113
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200114static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
115 const char* kind, jint idx, bool return_errors)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
118 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
119 << ": " << kind << " is null at index " << idx;
120 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
121 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
122 "%s is null at index %d", kind, idx);
123}
124
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800125static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
127 if (LIKELY(klass->IsInitialized())) {
128 return klass;
129 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 StackHandleScope<1> hs(self);
131 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
132 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800133 return nullptr;
134 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800136}
137
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
139 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800141 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800142 if (c == nullptr) {
143 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700144 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700146 if (is_static) {
147 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700148 } else if (c->IsInterface()) {
149 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700150 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700151 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800152 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700153 // No virtual method matching the signature. Search declared
154 // private methods and constructors.
155 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700156 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700157 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800158 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800160 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700161 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700163}
164
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800165static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800167 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700168 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
169 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800170 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700171 }
Brian Carlstromce888532013-10-10 00:32:58 -0700172 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800173 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700174 return method->GetDeclaringClass()->GetClassLoader();
175 }
176 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800177 mirror::ClassLoader* class_loader =
178 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
179 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700180 return class_loader;
181 }
182 // See if the override ClassLoader is set for gtests.
183 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800184 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800185 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700186 CHECK(Runtime::Current()->UseCompileTimeClassPath());
187 return class_loader;
188 }
189 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800190 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700191}
192
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
194 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700196 StackHandleScope<2> hs(soa.Self());
197 Handle<mirror::Class> c(
198 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
199 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800200 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700201 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800202 mirror::ArtField* field = nullptr;
203 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
205 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700206 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800207 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700208 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700210 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800211 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700215 StackHandleScope<1> hs(soa.Self());
216 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800218 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800219 "no type \"%s\" found and so no field \"%s\" "
220 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700221 c->GetDescriptor().c_str());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700222 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800223 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 }
225 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700226 field = mirror::Class::FindStaticField(soa.Self(), c, name,
227 field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 } else {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 field = c->FindInstanceField(name, field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800231 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
233 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
234 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700235 sig, name, c->GetDescriptor().c_str());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800236 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700237 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700239}
240
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800241static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700244 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700245 vm->pin_table.Add(array);
246}
247
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800248static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700251 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700252 vm->pin_table.Remove(array);
253}
254
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800255static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700258 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
260 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
261 "%s offset=%d length=%d %s.length=%d",
262 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700263}
Ian Rogers0571d352011-11-03 19:51:38 -0700264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
266 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
269 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
270 "offset=%d length=%d string.length()=%d", start, length,
271 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700272}
Elliott Hughes814e4032011-08-23 12:07:56 -0700273
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 // Turn the const char* into a java.lang.String.
277 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800278 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700280 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700281
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 // Choose an appropriate constructor and set up the arguments.
283 jvalue args[2];
284 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800285 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800287 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288 signature = "(Ljava/lang/String;)V";
289 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800290 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 signature = "(Ljava/lang/Throwable;)V";
292 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700293 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
295 args[0].l = s.get();
296 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700297 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800299 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800300 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800302 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 return JNI_ERR;
304 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700305
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800306 ScopedLocalRef<jthrowable> exception(
307 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
308 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700309 return JNI_ERR;
310 }
Ian Rogersef28b142012-11-30 14:22:18 -0800311 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800313 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700315}
316
Elliott Hughes462c9442012-03-23 18:47:50 -0700317static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800318 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700319 return JNI_ERR;
320 }
321
Elliott Hughes462c9442012-03-23 18:47:50 -0700322 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700323 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800324 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700325 *p_env = self->GetJniEnv();
326 return JNI_OK;
327 }
328
329 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
330
331 // No threads allowed in zygote mode.
332 if (runtime->IsZygote()) {
333 LOG(ERROR) << "Attempt to attach a thread in the zygote";
334 return JNI_ERR;
335 }
336
Elliott Hughes462c9442012-03-23 18:47:50 -0700337 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800338 const char* thread_name = nullptr;
339 jobject thread_group = nullptr;
340 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700341 if (IsBadJniVersion(args->version)) {
342 LOG(ERROR) << "Bad JNI version passed to "
343 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
344 << args->version;
345 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700346 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700347 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700348 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700349 }
Elliott Hughes75770752011-08-24 17:52:38 -0700350
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800351 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800352 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700353 return JNI_ERR;
354 } else {
355 *p_env = Thread::Current()->GetJniEnv();
356 return JNI_OK;
357 }
Elliott Hughes75770752011-08-24 17:52:38 -0700358}
359
Elliott Hughes79082e32011-08-25 12:07:32 -0700360class SharedLibrary {
361 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800362 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700363 : path_(path),
364 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700365 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700366 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700367 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700368 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700369 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700370 }
371
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700372 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
373 mirror::Object** root = &class_loader_;
374 return ReadBarrier::BarrierForRoot<mirror::Object, kWithReadBarrier>(root);
Elliott Hughes79082e32011-08-25 12:07:32 -0700375 }
376
377 std::string GetPath() {
378 return path_;
379 }
380
381 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700382 * Check the result of an earlier call to JNI_OnLoad on this library.
383 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700384 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 bool CheckOnLoadResult()
386 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700387 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700388 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
390 bool okay;
391 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700392 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700393
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700394 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
396 // caller can continue.
397 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
398 okay = true;
399 } else {
400 while (jni_on_load_result_ == kPending) {
401 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700402 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700404
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405 okay = (jni_on_load_result_ == kOkay);
406 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
407 << (okay ? "succeeded" : "failed") << "]";
408 }
409 }
410 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700411 return okay;
412 }
413
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700415 Thread* self = Thread::Current();
416 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700417
Elliott Hughes79082e32011-08-25 12:07:32 -0700418 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700419 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700420
421 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700422 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700423 }
424
425 void* FindSymbol(const std::string& symbol_name) {
426 return dlsym(handle_, symbol_name.c_str());
427 }
428
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800429 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800430 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800431 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800432 }
433 }
434
Elliott Hughes79082e32011-08-25 12:07:32 -0700435 private:
436 enum JNI_OnLoadState {
437 kPending,
438 kFailed,
439 kOkay,
440 };
441
442 // Path to library "/system/lib/libjni.so".
443 std::string path_;
444
445 // The void* returned by dlopen(3).
446 void* handle_;
447
448 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800449 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700450
451 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700453 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700454 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700455 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700456 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700457 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700458 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700459};
460
Elliott Hughes79082e32011-08-25 12:07:32 -0700461// This exists mainly to keep implementation details out of the header file.
462class Libraries {
463 public:
464 Libraries() {
465 }
466
467 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700468 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 }
470
Elliott Hughesae80b492012-04-24 10:43:17 -0700471 void Dump(std::ostream& os) const {
472 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700473 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700474 if (!first) {
475 os << ' ';
476 }
477 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700478 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700479 }
480 }
481
482 size_t size() const {
483 return libraries_.size();
484 }
485
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700487 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800488 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700489 }
490
491 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700492 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700493 }
494
495 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800496 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700498 std::string jni_short_name(JniShortName(m));
499 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800500 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700501 for (const auto& lib : libraries_) {
502 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700503 if (library->GetClassLoader() != declaring_class_loader) {
504 // We only search libraries loaded by the appropriate ClassLoader.
505 continue;
506 }
507 // Try the short name then the long name...
508 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800509 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700510 fn = library->FindSymbol(jni_long_name);
511 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800512 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800513 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
514 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700515 return fn;
516 }
517 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700519 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700520 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800522 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 }
524
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800525 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800526 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800527 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800528 }
529 }
530
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700532 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700533};
534
Ian Rogers2d10b202014-05-12 19:15:18 -0700535#define CHECK_NON_NULL_ARGUMENT(value) \
536 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700537
Ian Rogers2d10b202014-05-12 19:15:18 -0700538#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
539 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
540
541#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
542 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
543
544#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
545 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
546
547#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800548 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700549 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700550 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700551 }
552
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700553#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800554 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700555 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700556 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700557 }
558
Elliott Hughescdf53122011-08-19 15:46:09 -0700559class JNI {
560 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700561 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700562 return JNI_VERSION_1_6;
563 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700564
Ian Rogers25e8b912012-09-07 11:31:36 -0700565 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700566 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800567 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700568 }
569
Elliott Hughescdf53122011-08-19 15:46:09 -0700570 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700571 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700572 Runtime* runtime = Runtime::Current();
573 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700574 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700575 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800576 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700577 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700578 StackHandleScope<1> hs(soa.Self());
579 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800580 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700581 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800582 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700583 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700584 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700585 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700586
Ian Rogers62f05122014-03-21 11:21:29 -0700587 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700588 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700590 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700591 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700592
Ian Rogers62f05122014-03-21 11:21:29 -0700593 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700594 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700595 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700596 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700597 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700598
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700600 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700601 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800602 mirror::ArtMethod* m = soa.DecodeMethod(mid);
603 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700604 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200605 jobject reflect_method;
606 if (m->IsConstructor()) {
607 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
608 } else {
609 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
610 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700613 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800614 SetObjectField(env, reflect_method,
615 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700616 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700620 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800622 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700623 jobject art_field = soa.AddLocalReference<jobject>(f);
624 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
625 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800626 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700627 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800628 SetObjectField(env, reflect_field,
629 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700630 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700632
Elliott Hughes37f7a402011-08-22 18:56:01 -0700633 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700634 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800636 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 }
639
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700640 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700641 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800643 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700646
Elliott Hughes37f7a402011-08-22 18:56:01 -0700647 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700648 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
649 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800651 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
652 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700655
Elliott Hughese84278b2012-03-22 10:06:53 -0700656 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700657 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800658 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700659 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700660 return JNI_TRUE;
661 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700662 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
664 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700665 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700666 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700668
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800671 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
672 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 return JNI_ERR;
674 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800675 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
676 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 return JNI_OK;
678 }
679
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700680 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700681 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800682 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 }
684
685 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700686 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 }
688
689 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700690 ScopedObjectAccess soa(env);
691 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 }
693
694 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700696
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700697 // If we have no exception to describe, pass through.
698 if (!soa.Self()->GetException(nullptr)) {
699 return;
700 }
701
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700702 StackHandleScope<3> hs(soa.Self());
703 // TODO: Use nullptr instead of null handles?
704 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
705 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
706 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800707 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200708 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800709 {
710 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800711 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700712 old_throw_this_object.Assign(old_throw_location.GetThis());
713 old_throw_method.Assign(old_throw_location.GetMethod());
714 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800715 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200716 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800717 soa.Self()->ClearException();
718 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800719 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700720 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
722 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800723 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700724 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700725 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700726 } else {
727 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800728 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800729 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700730 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800731 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700732 }
733 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700734 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800735 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700736
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700737 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200738 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700740
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800743 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700744 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
746
Ian Rogers25e8b912012-09-07 11:31:36 -0700747 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 LOG(FATAL) << "JNI FatalError called: " << msg;
749 }
750
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700751 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700752 // TODO: SOA may not be necessary but I do it to please lock annotations.
753 ScopedObjectAccess soa(env);
754 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700755 return JNI_ERR;
756 }
Ian Rogersef28b142012-11-30 14:22:18 -0800757 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 return JNI_OK;
759 }
760
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700761 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700762 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800763 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 soa.Env()->PopFrame();
765 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
767
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700768 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700769 // TODO: SOA may not be necessary but I do it to please lock annotations.
770 ScopedObjectAccess soa(env);
771 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700772 }
773
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700775 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800776 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800777 // Check for null after decoding the object to handle cleared weak globals.
778 if (decoded_obj == nullptr) {
779 return nullptr;
780 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700781 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700783 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700784 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 return reinterpret_cast<jobject>(ref);
786 }
787
788 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800789 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 return;
791 }
Ian Rogersef28b142012-11-30 14:22:18 -0800792 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800794 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700795 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700796
797 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
798 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
799 << "failed to find entry";
800 }
801 }
802
803 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800805 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 }
807
808 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700809 if (obj != nullptr) {
810 ScopedObjectAccess soa(env);
811 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 }
813 }
814
815 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700816 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800817 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800818 // Check for null after decoding the object to handle cleared weak globals.
819 if (decoded_obj == nullptr) {
820 return nullptr;
821 }
822 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 }
824
825 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800826 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 return;
828 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700829 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800830 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700831
Ian Rogersef28b142012-11-30 14:22:18 -0800832 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 if (!locals.Remove(cookie, obj)) {
834 // Attempting to delete a local reference that is not in the
835 // topmost local reference frame is a no-op. DeleteLocalRef returns
836 // void and doesn't throw any exceptions, but we should probably
837 // complain about it so the user will notice that things aren't
838 // going quite the way they expect.
839 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
840 << "failed to find entry";
841 }
842 }
843
844 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700845 if (obj1 == obj2) {
846 return JNI_TRUE;
847 } else {
848 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800849 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
850 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700851 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 }
853
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700854 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700855 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800857 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800858 if (c == nullptr) {
859 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700860 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700861 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 }
863
Ian Rogersbc939662013-08-15 10:26:54 -0700864 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700866 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700867 CHECK_NON_NULL_ARGUMENT(java_class);
868 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700869 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 va_end(args);
871 return result;
872 }
873
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700875 CHECK_NON_NULL_ARGUMENT(java_class);
876 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800878 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800879 if (c == nullptr) {
880 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700881 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800882 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800883 if (result == nullptr) {
884 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700885 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800888 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800889 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700890 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800891 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 }
893
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700895 CHECK_NON_NULL_ARGUMENT(java_class);
896 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700897 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800898 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800899 if (c == nullptr) {
900 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700901 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800902 mirror::Object* result = c->AllocObject(soa.Self());
903 if (result == nullptr) {
904 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700905 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800908 if (soa.Self()->IsExceptionPending()) {
909 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700910 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800911 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 }
913
Ian Rogersbc939662013-08-15 10:26:54 -0700914 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700915 CHECK_NON_NULL_ARGUMENT(java_class);
916 CHECK_NON_NULL_ARGUMENT(name);
917 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700919 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Ian Rogersbc939662013-08-15 10:26:54 -0700922 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
923 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700924 CHECK_NON_NULL_ARGUMENT(java_class);
925 CHECK_NON_NULL_ARGUMENT(name);
926 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700928 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 va_list ap;
933 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700934 CHECK_NON_NULL_ARGUMENT(obj);
935 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700936 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700937 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 }
941
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700943 CHECK_NON_NULL_ARGUMENT(obj);
944 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 ScopedObjectAccess soa(env);
946 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
947 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700951 CHECK_NON_NULL_ARGUMENT(obj);
952 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700954 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
955 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_list ap;
961 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700962 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
963 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700964 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700967 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700971 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
972 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 ScopedObjectAccess soa(env);
974 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700978 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
979 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700981 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
982 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 va_list ap;
987 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700988 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
989 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700990 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700993 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700997 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
998 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 ScopedObjectAccess soa(env);
1000 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1005 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001007 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1008 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_list ap;
1013 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001014 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1015 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001016 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001017 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001019 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1024 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
1026 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001030 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1031 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001033 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1034 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_list ap;
1039 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001040 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1041 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001042 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001045 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001049 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1050 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
1052 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001056 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1057 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001058 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001059 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1060 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 va_list ap;
1065 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001066 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1067 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001068 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001071 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001075 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1076 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 ScopedObjectAccess soa(env);
1078 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001082 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1083 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001085 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1086 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_list ap;
1091 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001092 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1093 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001094 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001097 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001101 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1102 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
1104 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001108 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1109 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001111 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1112 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001118 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1119 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001120 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001123 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001127 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1128 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 ScopedObjectAccess soa(env);
1130 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1135 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001137 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1138 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_list ap;
1143 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001144 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1145 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001146 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001149 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001153 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1154 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001155 ScopedObjectAccess soa(env);
1156 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001160 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1161 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001163 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1164 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_list ap;
1169 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001170 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1171 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001172 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001173 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001178 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1179 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001185 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1186 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001188 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001191 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001194 CHECK_NON_NULL_ARGUMENT(obj);
1195 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001196 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001197 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1198 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 va_end(ap);
1200 return local_result;
1201 }
1202
Ian Rogersbc939662013-08-15 10:26:54 -07001203 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1204 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001205 CHECK_NON_NULL_ARGUMENT(obj);
1206 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 ScopedObjectAccess soa(env);
1208 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1209 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Ian Rogersbc939662013-08-15 10:26:54 -07001212 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1213 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001214 CHECK_NON_NULL_ARGUMENT(obj);
1215 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001216 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001217 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Ian Rogersbc939662013-08-15 10:26:54 -07001221 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1222 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001225 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1226 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001227 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001230 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
Ian Rogersbc939662013-08-15 10:26:54 -07001233 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1234 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001235 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1236 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
1238 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Ian Rogersbc939662013-08-15 10:26:54 -07001241 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1242 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001243 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1244 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001246 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001249 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001252 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1253 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001254 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001257 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Ian Rogersbc939662013-08-15 10:26:54 -07001260 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1261 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001262 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 ScopedObjectAccess soa(env);
1265 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Ian Rogersbc939662013-08-15 10:26:54 -07001268 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1269 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001270 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1271 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001273 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001276 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001279 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1280 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001281 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001284 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Ian Rogersbc939662013-08-15 10:26:54 -07001287 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1288 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001289 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1290 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Ian Rogersbc939662013-08-15 10:26:54 -07001295 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1296 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001297 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1298 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001300 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001303 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001306 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1307 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001308 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001311 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Ian Rogersbc939662013-08-15 10:26:54 -07001314 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1315 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001316 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1317 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 ScopedObjectAccess soa(env);
1319 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Ian Rogersbc939662013-08-15 10:26:54 -07001322 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1323 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001324 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1325 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001327 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001333 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1334 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001335 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001338 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Ian Rogersbc939662013-08-15 10:26:54 -07001341 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1342 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001343 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1344 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001345 ScopedObjectAccess soa(env);
1346 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
Ian Rogersbc939662013-08-15 10:26:54 -07001349 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1350 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001351 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1352 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001354 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001357 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001360 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1361 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001362 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001363 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001365 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Ian Rogersbc939662013-08-15 10:26:54 -07001368 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1369 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001370 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1371 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372 ScopedObjectAccess soa(env);
1373 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
Ian Rogersbc939662013-08-15 10:26:54 -07001376 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1377 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001378 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1379 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001380 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001381 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001384 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001386 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001387 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1388 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001389 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001392 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Ian Rogersbc939662013-08-15 10:26:54 -07001395 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1396 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001397 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1398 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 ScopedObjectAccess soa(env);
1400 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Ian Rogersbc939662013-08-15 10:26:54 -07001403 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1404 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001405 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1406 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001407 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001408 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001411 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001414 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1415 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001416 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001419 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Ian Rogersbc939662013-08-15 10:26:54 -07001422 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1423 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001424 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1425 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001426 ScopedObjectAccess soa(env);
1427 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Ian Rogersbc939662013-08-15 10:26:54 -07001430 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1431 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001432 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1433 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001434 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001435 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001438 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001440 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001441 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1442 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001443 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001444 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 va_end(ap);
1446 }
1447
Brian Carlstromea46f952013-07-30 01:26:50 -07001448 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1449 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001450 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1451 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001452 ScopedObjectAccess soa(env);
1453 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Ian Rogersbc939662013-08-15 10:26:54 -07001456 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1457 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001458 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1459 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001460 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001461 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Ian Rogersbc939662013-08-15 10:26:54 -07001464 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001465 CHECK_NON_NULL_ARGUMENT(java_class);
1466 CHECK_NON_NULL_ARGUMENT(name);
1467 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001468 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001469 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001471
Ian Rogersbc939662013-08-15 10:26:54 -07001472 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1473 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001474 CHECK_NON_NULL_ARGUMENT(java_class);
1475 CHECK_NON_NULL_ARGUMENT(name);
1476 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001478 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001480
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001481 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001482 CHECK_NON_NULL_ARGUMENT(obj);
1483 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001484 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001485 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1486 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001487 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001491 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001493 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001494 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001497 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001498 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1499 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001500 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001501 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1502 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1503 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001504 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001508 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001510 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1511 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001512 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001516 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1517 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001519 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1520 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001521 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001523#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001524 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001525 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001526 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001527 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001528
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001530 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1531 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001532 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001533 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1534 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001535 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001537#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001538 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001539 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001540 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001541 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001542
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001544 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001548 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001552 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001556 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001560 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001563 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001564 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001568 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001572 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001576 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001579 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001580 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001583 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001584 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001588 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001592 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001595 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001596 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001600 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001604 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 }
1606
1607 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001608 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 }
1610
1611 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001612 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001613 }
1614
1615 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001616 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001617 }
1618
1619 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001620 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001621 }
1622
1623 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001624 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001625 }
1626
1627 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001628 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001629 }
1630
1631 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001632 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001633 }
1634
1635 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001636 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001637 }
1638
1639 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001640 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001641 }
1642
1643 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001644 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001645 }
1646
1647 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001648 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001649 }
1650
1651 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001652 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001653 }
1654
1655 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001656 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001657 }
1658
1659 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001660 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001661 }
1662
1663 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001664 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001665 }
1666
1667 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001668 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001671 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001674 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001675 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001676 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 va_end(ap);
1679 return local_result;
1680 }
1681
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001682 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001683 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001685 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001686 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001690 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001692 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001693 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001696 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001699 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001700 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001701 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001703 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001706 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001707 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001709 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001712 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001713 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001715 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001718 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001721 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001722 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001723 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001725 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001728 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001729 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001730 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001731 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001735 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001737 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001740 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001742 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001743 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001744 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001745 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001747 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001751 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001752 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001753 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001756 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001757 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001759 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001765 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001766 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001767 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001769 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001772 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001773 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001774 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001775 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001778 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001779 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001781 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001784 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001786 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001787 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001788 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001789 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001791 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001794 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001795 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001797 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001801 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001803 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001808 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001809 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001810 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001811 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001813 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001816 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001819 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001822 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001823 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001825 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001828 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001830 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001831 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001832 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001833 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001835 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001838 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001839 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001840 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001841 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001844 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001845 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001846 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001847 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001850 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001852 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001853 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001854 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001855 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001857 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001860 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001863 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001866 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001867 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001869 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001872 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001874 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001875 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001876 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001877 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 va_end(ap);
1879 }
1880
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001881 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001882 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001883 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001884 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001887 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001888 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001890 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Elliott Hughes814e4032011-08-23 12:07:56 -07001893 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001894 if (UNLIKELY(char_count < 0)) {
1895 JniAbortF("NewString", "char_count < 0: %d", char_count);
1896 return nullptr;
1897 }
1898 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1899 JniAbortF("NewString", "chars == null && char_count > 0");
1900 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001901 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001902 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001903 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001904 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
1907 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001908 if (utf == nullptr) {
1909 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001912 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
Elliott Hughes814e4032011-08-23 12:07:56 -07001916 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001917 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001919 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001920 }
1921
1922 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001923 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001925 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 }
1927
Ian Rogersbc939662013-08-15 10:26:54 -07001928 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1929 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001930 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001931 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001932 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001933 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001934 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001935 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001936 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001937 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1938 memcpy(buf, chars + start, length * sizeof(jchar));
1939 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001940 }
1941
Ian Rogersbc939662013-08-15 10:26:54 -07001942 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1943 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001944 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001945 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001946 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001947 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001949 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001950 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001951 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1952 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1953 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001957 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001958 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001959 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1960 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001962 gc::Heap* heap = Runtime::Current()->GetHeap();
1963 if (heap->IsMovableObject(chars)) {
1964 if (is_copy != nullptr) {
1965 *is_copy = JNI_TRUE;
1966 }
1967 int32_t char_count = s->GetLength();
1968 int32_t offset = s->GetOffset();
1969 jchar* bytes = new jchar[char_count];
1970 for (int32_t i = 0; i < char_count; i++) {
1971 bytes[i] = chars->Get(i + offset);
1972 }
1973 return bytes;
1974 } else {
1975 if (is_copy != nullptr) {
1976 *is_copy = JNI_FALSE;
1977 }
1978 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001979 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 }
1981
Mathieu Chartier590fee92013-09-13 13:46:47 -07001982 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001983 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001984 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001985 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1986 mirror::CharArray* s_chars = s->GetCharArray();
1987 if (chars != (s_chars->GetData() + s->GetOffset())) {
1988 delete[] chars;
1989 }
1990 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001994 CHECK_NON_NULL_ARGUMENT(java_string);
1995 ScopedObjectAccess soa(env);
1996 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1997 mirror::CharArray* chars = s->GetCharArray();
1998 int32_t offset = s->GetOffset();
1999 PinPrimitiveArray(soa, chars);
2000 gc::Heap* heap = Runtime::Current()->GetHeap();
2001 if (heap->IsMovableObject(chars)) {
2002 StackHandleScope<1> hs(soa.Self());
2003 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
2004 heap->IncrementDisableMovingGC(soa.Self());
2005 }
2006 if (is_copy != nullptr) {
2007 *is_copy = JNI_FALSE;
2008 }
2009 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes75770752011-08-24 17:52:38 -07002012 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002013 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2014 ScopedObjectAccess soa(env);
2015 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
2016 gc::Heap* heap = Runtime::Current()->GetHeap();
2017 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2018 mirror::CharArray* s_chars = s->GetCharArray();
2019 if (heap->IsMovableObject(s_chars)) {
2020 heap->DecrementDisableMovingGC(soa.Self());
2021 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002025 if (java_string == nullptr) {
2026 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002027 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002028 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002029 *is_copy = JNI_TRUE;
2030 }
Ian Rogersef28b142012-11-30 14:22:18 -08002031 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002032 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 size_t byte_count = s->GetUtfLength();
2034 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002035 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002036 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2037 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2038 bytes[byte_count] = '\0';
2039 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002043 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002044 }
2045
Elliott Hughesbd935992011-08-22 11:59:34 -07002046 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002047 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002048 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002049 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002050 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002051 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2052 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002053 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002054 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002058 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002060 mirror::ObjectArray<mirror::Object>* array =
2061 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002062 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Ian Rogersbc939662013-08-15 10:26:54 -07002065 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2066 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002067 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002069 mirror::ObjectArray<mirror::Object>* array =
2070 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2071 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002072 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
2075 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002076 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
2079 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002080 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
2083 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002084 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
2087 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002088 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
2091 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002092 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
2095 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002096 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
2099 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002100 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Ian Rogers1d99e452014-01-02 17:36:41 -08002103 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2104 jobject initial_element) {
2105 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002106 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002107 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002108 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002109 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110
2111 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002112 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002113 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002114 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002115 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002116 if (UNLIKELY(element_class->IsPrimitive())) {
2117 JniAbortF("NewObjectArray", "not an object type: %s",
2118 PrettyDescriptor(element_class).c_str());
2119 return nullptr;
2120 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002121 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002122 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002123 if (UNLIKELY(array_class == nullptr)) {
2124 return nullptr;
2125 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes75770752011-08-24 17:52:38 -07002128 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002129 mirror::ObjectArray<mirror::Object>* result =
2130 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002131 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002132 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002133 if (initial_object != nullptr) {
2134 mirror::Class* element_class = result->GetClass()->GetComponentType();
2135 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2136 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2137 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2138 PrettyDescriptor(element_class).c_str());
2139
2140 } else {
2141 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002142 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002143 }
2144 }
Elliott Hughes75770752011-08-24 17:52:38 -07002145 }
2146 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002147 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
2150 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002151 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Ian Rogersa15e67d2012-02-28 13:51:55 -08002154 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002155 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002156 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002157 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002158 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2159 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2160 PrettyDescriptor(array->GetClass()).c_str());
2161 return nullptr;
2162 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002163 gc::Heap* heap = Runtime::Current()->GetHeap();
2164 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002165 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002166 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002167 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002168 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002170 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002171 *is_copy = JNI_FALSE;
2172 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002173 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002174 }
2175
Ian Rogers2d10b202014-05-12 19:15:18 -07002176 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2177 jint mode) {
2178 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2179 ScopedObjectAccess soa(env);
2180 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2181 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2182 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2183 PrettyDescriptor(array->GetClass()).c_str());
2184 return;
2185 }
2186 const size_t component_size = array->GetClass()->GetComponentSize();
2187 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002188 }
2189
Elliott Hughes75770752011-08-24 17:52:38 -07002190 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002191 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughes75770752011-08-24 17:52:38 -07002194 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002195 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes75770752011-08-24 17:52:38 -07002198 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002199 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes75770752011-08-24 17:52:38 -07002202 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002203 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
Elliott Hughes75770752011-08-24 17:52:38 -07002206 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002207 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
Elliott Hughes75770752011-08-24 17:52:38 -07002210 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002211 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Elliott Hughes75770752011-08-24 17:52:38 -07002214 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002215 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Elliott Hughes75770752011-08-24 17:52:38 -07002218 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002219 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Mathieu Chartier590fee92013-09-13 13:46:47 -07002222 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2223 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002224 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2225 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Mathieu Chartier590fee92013-09-13 13:46:47 -07002228 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002229 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Mathieu Chartier590fee92013-09-13 13:46:47 -07002232 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002233 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Mathieu Chartier590fee92013-09-13 13:46:47 -07002236 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2237 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002238 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Mathieu Chartier590fee92013-09-13 13:46:47 -07002241 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2242 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002243 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Mathieu Chartier590fee92013-09-13 13:46:47 -07002246 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002247 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Mathieu Chartier590fee92013-09-13 13:46:47 -07002250 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002251 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253
Mathieu Chartier590fee92013-09-13 13:46:47 -07002254 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2255 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002256 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Ian Rogersbc939662013-08-15 10:26:54 -07002259 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2260 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002261 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002262 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Ian Rogersbc939662013-08-15 10:26:54 -07002265 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2266 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002267 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Ian Rogersbc939662013-08-15 10:26:54 -07002270 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2271 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002272 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 }
2274
Ian Rogersbc939662013-08-15 10:26:54 -07002275 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2276 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002277 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002278 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 }
2280
Ian Rogersbc939662013-08-15 10:26:54 -07002281 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2282 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002283 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002284 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Ian Rogersbc939662013-08-15 10:26:54 -07002287 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2288 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002289 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Ian Rogersbc939662013-08-15 10:26:54 -07002292 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2293 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002294 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Ian Rogersbc939662013-08-15 10:26:54 -07002297 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2298 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002299 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002300 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002301 }
2302
Ian Rogersbc939662013-08-15 10:26:54 -07002303 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2304 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002305 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002306 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Ian Rogersbc939662013-08-15 10:26:54 -07002309 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2310 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002311 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313
Ian Rogersbc939662013-08-15 10:26:54 -07002314 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2315 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002316 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
2318
Ian Rogersbc939662013-08-15 10:26:54 -07002319 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2320 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002321 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002322 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 }
2324
Ian Rogersbc939662013-08-15 10:26:54 -07002325 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2326 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002327 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002328 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 }
2330
Ian Rogersbc939662013-08-15 10:26:54 -07002331 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2332 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002333 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002334 }
2335
Ian Rogersbc939662013-08-15 10:26:54 -07002336 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2337 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002338 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002339 }
2340
Ian Rogersbc939662013-08-15 10:26:54 -07002341 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2342 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002343 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002344 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002345 }
2346
Ian Rogersbc939662013-08-15 10:26:54 -07002347 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2348 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002349 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2353 jint method_count, bool return_errors) {
2354 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002355 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002356 return JNI_ERR; // Not reached.
2357 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002358 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002359 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002360 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002361 if (UNLIKELY(method_count == 0)) {
2362 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2363 << PrettyDescriptor(c);
2364 return JNI_OK;
2365 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002366 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002367 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 const char* name = methods[i].name;
2369 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002370 const void* fnPtr = methods[i].fnPtr;
2371 if (UNLIKELY(name == nullptr)) {
2372 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2373 return JNI_ERR;
2374 } else if (UNLIKELY(sig == nullptr)) {
2375 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2376 return JNI_ERR;
2377 } else if (UNLIKELY(fnPtr == nullptr)) {
2378 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2379 return JNI_ERR;
2380 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002381 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002382 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002383 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002384 ++sig;
2385 }
2386
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002387 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2388 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002389 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002390 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002391 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002392 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002393 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002394 << PrettyDescriptor(c) << "." << name << sig << " in "
2395 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002396 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002397 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002398 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002399 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002400 << PrettyDescriptor(c) << "." << name << sig
2401 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002402 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002403 return JNI_ERR;
2404 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002405
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002406 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002407
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002408 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002409 }
2410 return JNI_OK;
2411 }
2412
Elliott Hughes5174fe62011-08-23 15:12:35 -07002413 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002414 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002415 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002416 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002417
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002418 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002419
Ian Rogers2d10b202014-05-12 19:15:18 -07002420 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002421 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002422 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002423 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002424 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002425 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002426 }
2427 }
2428 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002429 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002430 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002431 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002432 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002433 }
2434 }
2435
Ian Rogers2d10b202014-05-12 19:15:18 -07002436 if (unregistered_count == 0) {
2437 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2438 << PrettyDescriptor(c) << "' that contains no native methods";
2439 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002440 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002441 }
2442
Ian Rogers719d1a32014-03-06 12:13:39 -08002443 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002444 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002445 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002446 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2447 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002449 return JNI_ERR;
2450 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002451 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002452 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002453 }
2454
Ian Rogers719d1a32014-03-06 12:13:39 -08002455 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002456 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002457 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002458 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002459 o->MonitorExit(soa.Self());
2460 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002461 return JNI_ERR;
2462 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002463 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002464 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002465 }
2466
2467 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002468 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002469 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002470 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002471 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002472 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002473 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002474 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002475 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002476 }
2477
Elliott Hughescdf53122011-08-19 15:46:09 -07002478 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002479 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002480 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002481 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002482 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002483 if (address == nullptr && capacity != 0) {
2484 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002485 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002486 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002487
Brian Carlstrom85a93362014-06-25 09:30:52 -07002488 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002489 if (capacity > INT_MAX) {
2490 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2491 return nullptr;
2492 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002493 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002494 jint capacity_arg = static_cast<jint>(capacity);
2495
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002496 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2497 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002498 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002499 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002500 }
2501
Elliott Hughesb465ab02011-08-24 11:21:21 -07002502 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002503 return reinterpret_cast<void*>(env->GetLongField(
2504 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002505 }
2506
Elliott Hughesb465ab02011-08-24 11:21:21 -07002507 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002508 return static_cast<jlong>(env->GetIntField(
2509 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002510 }
2511
Elliott Hughesb465ab02011-08-24 11:21:21 -07002512 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002513 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002514
2515 // Do we definitely know what kind of reference this is?
2516 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2517 IndirectRefKind kind = GetIndirectRefKind(ref);
2518 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002519 case kLocal: {
2520 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002521 // The local refs don't need a read barrier.
2522 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2523 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002524 return JNILocalRefType;
2525 }
2526 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002527 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002528 case kGlobal:
2529 return JNIGlobalRefType;
2530 case kWeakGlobal:
2531 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002532 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002533 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002534 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002535 return JNILocalRefType;
2536 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002537 return JNIInvalidRefType;
2538 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002539 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2540 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002541 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002542
2543 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002544 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2545 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002546 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002547 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002548 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2549 return JNI_ERR;
2550 }
2551 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002552 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002553 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2554 if (!okay) {
2555 soa.Self()->ThrowOutOfMemoryError(caller);
2556 }
2557 return okay ? JNI_OK : JNI_ERR;
2558 }
2559
2560 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002561 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002562 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002563 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002564 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002565 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002566 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002567 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002568 return soa.AddLocalReference<JniT>(result);
2569 }
2570
Ian Rogers2d10b202014-05-12 19:15:18 -07002571 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2572 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2573 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002575 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002576 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2577 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2578 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2579 PrettyDescriptor(array->GetClass()).c_str());
2580 return nullptr;
2581 }
2582 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2583 return array;
2584 }
2585
2586 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2587 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2588 CHECK_NON_NULL_ARGUMENT(java_array);
2589 ScopedObjectAccess soa(env);
2590 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2591 "GetArrayElements",
2592 "get");
2593 if (UNLIKELY(array == nullptr)) {
2594 return nullptr;
2595 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002596 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002597 // Only make a copy if necessary.
2598 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2599 if (is_copy != nullptr) {
2600 *is_copy = JNI_TRUE;
2601 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002602 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002603 size_t size = array->GetLength() * component_size;
2604 void* data = new uint64_t[RoundUp(size, 8) / 8];
2605 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002606 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002607 } else {
2608 if (is_copy != nullptr) {
2609 *is_copy = JNI_FALSE;
2610 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002611 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002612 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002613 }
2614
Ian Rogers2d10b202014-05-12 19:15:18 -07002615 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002616 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002617 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002618 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002619 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2620 "ReleaseArrayElements",
2621 "release");
2622 if (array == nullptr) {
2623 return;
2624 }
2625 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2626 }
2627
2628 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2629 size_t component_size, void* elements, jint mode)
2630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002631 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002632 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002633 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002634 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002635 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2636 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002637 if (is_copy) {
2638 // Sanity check: If elements is not the same as the java array's data, it better not be a
2639 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2640 // copies we make?
2641 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2642 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2643 reinterpret_cast<void*>(elements), array_data);
2644 return;
2645 }
2646 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002647 // Don't need to copy if we had a direct pointer.
2648 if (mode != JNI_ABORT && is_copy) {
2649 memcpy(array_data, elements, bytes);
2650 }
2651 if (mode != JNI_COMMIT) {
2652 if (is_copy) {
2653 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002654 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002655 // Non copy to a movable object must means that we had disabled the moving GC.
2656 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002657 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002658 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002659 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002660 }
2661
Ian Rogers2d10b202014-05-12 19:15:18 -07002662 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2663 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2664 jsize start, jsize length, ElementT* buf) {
2665 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2666 ScopedObjectAccess soa(env);
2667 ArtArrayT* array =
2668 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2669 "GetPrimitiveArrayRegion",
2670 "get region of");
2671 if (array != nullptr) {
2672 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2673 ThrowAIOOBE(soa, array, start, length, "src");
2674 } else {
2675 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2676 ElementT* data = array->GetData();
2677 memcpy(buf, data + start, length * sizeof(ElementT));
2678 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002679 }
2680 }
2681
Ian Rogers2d10b202014-05-12 19:15:18 -07002682 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2683 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2684 jsize start, jsize length, const ElementT* buf) {
2685 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2686 ScopedObjectAccess soa(env);
2687 ArtArrayT* array =
2688 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2689 "SetPrimitiveArrayRegion",
2690 "set region of");
2691 if (array != nullptr) {
2692 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2693 ThrowAIOOBE(soa, array, start, length, "dst");
2694 } else {
2695 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2696 ElementT* data = array->GetData();
2697 memcpy(data + start, buf, length * sizeof(ElementT));
2698 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002699 }
2700 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002701};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002702
Elliott Hughes88c5c352012-03-15 18:49:48 -07002703const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002704 nullptr, // reserved0.
2705 nullptr, // reserved1.
2706 nullptr, // reserved2.
2707 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002708 JNI::GetVersion,
2709 JNI::DefineClass,
2710 JNI::FindClass,
2711 JNI::FromReflectedMethod,
2712 JNI::FromReflectedField,
2713 JNI::ToReflectedMethod,
2714 JNI::GetSuperclass,
2715 JNI::IsAssignableFrom,
2716 JNI::ToReflectedField,
2717 JNI::Throw,
2718 JNI::ThrowNew,
2719 JNI::ExceptionOccurred,
2720 JNI::ExceptionDescribe,
2721 JNI::ExceptionClear,
2722 JNI::FatalError,
2723 JNI::PushLocalFrame,
2724 JNI::PopLocalFrame,
2725 JNI::NewGlobalRef,
2726 JNI::DeleteGlobalRef,
2727 JNI::DeleteLocalRef,
2728 JNI::IsSameObject,
2729 JNI::NewLocalRef,
2730 JNI::EnsureLocalCapacity,
2731 JNI::AllocObject,
2732 JNI::NewObject,
2733 JNI::NewObjectV,
2734 JNI::NewObjectA,
2735 JNI::GetObjectClass,
2736 JNI::IsInstanceOf,
2737 JNI::GetMethodID,
2738 JNI::CallObjectMethod,
2739 JNI::CallObjectMethodV,
2740 JNI::CallObjectMethodA,
2741 JNI::CallBooleanMethod,
2742 JNI::CallBooleanMethodV,
2743 JNI::CallBooleanMethodA,
2744 JNI::CallByteMethod,
2745 JNI::CallByteMethodV,
2746 JNI::CallByteMethodA,
2747 JNI::CallCharMethod,
2748 JNI::CallCharMethodV,
2749 JNI::CallCharMethodA,
2750 JNI::CallShortMethod,
2751 JNI::CallShortMethodV,
2752 JNI::CallShortMethodA,
2753 JNI::CallIntMethod,
2754 JNI::CallIntMethodV,
2755 JNI::CallIntMethodA,
2756 JNI::CallLongMethod,
2757 JNI::CallLongMethodV,
2758 JNI::CallLongMethodA,
2759 JNI::CallFloatMethod,
2760 JNI::CallFloatMethodV,
2761 JNI::CallFloatMethodA,
2762 JNI::CallDoubleMethod,
2763 JNI::CallDoubleMethodV,
2764 JNI::CallDoubleMethodA,
2765 JNI::CallVoidMethod,
2766 JNI::CallVoidMethodV,
2767 JNI::CallVoidMethodA,
2768 JNI::CallNonvirtualObjectMethod,
2769 JNI::CallNonvirtualObjectMethodV,
2770 JNI::CallNonvirtualObjectMethodA,
2771 JNI::CallNonvirtualBooleanMethod,
2772 JNI::CallNonvirtualBooleanMethodV,
2773 JNI::CallNonvirtualBooleanMethodA,
2774 JNI::CallNonvirtualByteMethod,
2775 JNI::CallNonvirtualByteMethodV,
2776 JNI::CallNonvirtualByteMethodA,
2777 JNI::CallNonvirtualCharMethod,
2778 JNI::CallNonvirtualCharMethodV,
2779 JNI::CallNonvirtualCharMethodA,
2780 JNI::CallNonvirtualShortMethod,
2781 JNI::CallNonvirtualShortMethodV,
2782 JNI::CallNonvirtualShortMethodA,
2783 JNI::CallNonvirtualIntMethod,
2784 JNI::CallNonvirtualIntMethodV,
2785 JNI::CallNonvirtualIntMethodA,
2786 JNI::CallNonvirtualLongMethod,
2787 JNI::CallNonvirtualLongMethodV,
2788 JNI::CallNonvirtualLongMethodA,
2789 JNI::CallNonvirtualFloatMethod,
2790 JNI::CallNonvirtualFloatMethodV,
2791 JNI::CallNonvirtualFloatMethodA,
2792 JNI::CallNonvirtualDoubleMethod,
2793 JNI::CallNonvirtualDoubleMethodV,
2794 JNI::CallNonvirtualDoubleMethodA,
2795 JNI::CallNonvirtualVoidMethod,
2796 JNI::CallNonvirtualVoidMethodV,
2797 JNI::CallNonvirtualVoidMethodA,
2798 JNI::GetFieldID,
2799 JNI::GetObjectField,
2800 JNI::GetBooleanField,
2801 JNI::GetByteField,
2802 JNI::GetCharField,
2803 JNI::GetShortField,
2804 JNI::GetIntField,
2805 JNI::GetLongField,
2806 JNI::GetFloatField,
2807 JNI::GetDoubleField,
2808 JNI::SetObjectField,
2809 JNI::SetBooleanField,
2810 JNI::SetByteField,
2811 JNI::SetCharField,
2812 JNI::SetShortField,
2813 JNI::SetIntField,
2814 JNI::SetLongField,
2815 JNI::SetFloatField,
2816 JNI::SetDoubleField,
2817 JNI::GetStaticMethodID,
2818 JNI::CallStaticObjectMethod,
2819 JNI::CallStaticObjectMethodV,
2820 JNI::CallStaticObjectMethodA,
2821 JNI::CallStaticBooleanMethod,
2822 JNI::CallStaticBooleanMethodV,
2823 JNI::CallStaticBooleanMethodA,
2824 JNI::CallStaticByteMethod,
2825 JNI::CallStaticByteMethodV,
2826 JNI::CallStaticByteMethodA,
2827 JNI::CallStaticCharMethod,
2828 JNI::CallStaticCharMethodV,
2829 JNI::CallStaticCharMethodA,
2830 JNI::CallStaticShortMethod,
2831 JNI::CallStaticShortMethodV,
2832 JNI::CallStaticShortMethodA,
2833 JNI::CallStaticIntMethod,
2834 JNI::CallStaticIntMethodV,
2835 JNI::CallStaticIntMethodA,
2836 JNI::CallStaticLongMethod,
2837 JNI::CallStaticLongMethodV,
2838 JNI::CallStaticLongMethodA,
2839 JNI::CallStaticFloatMethod,
2840 JNI::CallStaticFloatMethodV,
2841 JNI::CallStaticFloatMethodA,
2842 JNI::CallStaticDoubleMethod,
2843 JNI::CallStaticDoubleMethodV,
2844 JNI::CallStaticDoubleMethodA,
2845 JNI::CallStaticVoidMethod,
2846 JNI::CallStaticVoidMethodV,
2847 JNI::CallStaticVoidMethodA,
2848 JNI::GetStaticFieldID,
2849 JNI::GetStaticObjectField,
2850 JNI::GetStaticBooleanField,
2851 JNI::GetStaticByteField,
2852 JNI::GetStaticCharField,
2853 JNI::GetStaticShortField,
2854 JNI::GetStaticIntField,
2855 JNI::GetStaticLongField,
2856 JNI::GetStaticFloatField,
2857 JNI::GetStaticDoubleField,
2858 JNI::SetStaticObjectField,
2859 JNI::SetStaticBooleanField,
2860 JNI::SetStaticByteField,
2861 JNI::SetStaticCharField,
2862 JNI::SetStaticShortField,
2863 JNI::SetStaticIntField,
2864 JNI::SetStaticLongField,
2865 JNI::SetStaticFloatField,
2866 JNI::SetStaticDoubleField,
2867 JNI::NewString,
2868 JNI::GetStringLength,
2869 JNI::GetStringChars,
2870 JNI::ReleaseStringChars,
2871 JNI::NewStringUTF,
2872 JNI::GetStringUTFLength,
2873 JNI::GetStringUTFChars,
2874 JNI::ReleaseStringUTFChars,
2875 JNI::GetArrayLength,
2876 JNI::NewObjectArray,
2877 JNI::GetObjectArrayElement,
2878 JNI::SetObjectArrayElement,
2879 JNI::NewBooleanArray,
2880 JNI::NewByteArray,
2881 JNI::NewCharArray,
2882 JNI::NewShortArray,
2883 JNI::NewIntArray,
2884 JNI::NewLongArray,
2885 JNI::NewFloatArray,
2886 JNI::NewDoubleArray,
2887 JNI::GetBooleanArrayElements,
2888 JNI::GetByteArrayElements,
2889 JNI::GetCharArrayElements,
2890 JNI::GetShortArrayElements,
2891 JNI::GetIntArrayElements,
2892 JNI::GetLongArrayElements,
2893 JNI::GetFloatArrayElements,
2894 JNI::GetDoubleArrayElements,
2895 JNI::ReleaseBooleanArrayElements,
2896 JNI::ReleaseByteArrayElements,
2897 JNI::ReleaseCharArrayElements,
2898 JNI::ReleaseShortArrayElements,
2899 JNI::ReleaseIntArrayElements,
2900 JNI::ReleaseLongArrayElements,
2901 JNI::ReleaseFloatArrayElements,
2902 JNI::ReleaseDoubleArrayElements,
2903 JNI::GetBooleanArrayRegion,
2904 JNI::GetByteArrayRegion,
2905 JNI::GetCharArrayRegion,
2906 JNI::GetShortArrayRegion,
2907 JNI::GetIntArrayRegion,
2908 JNI::GetLongArrayRegion,
2909 JNI::GetFloatArrayRegion,
2910 JNI::GetDoubleArrayRegion,
2911 JNI::SetBooleanArrayRegion,
2912 JNI::SetByteArrayRegion,
2913 JNI::SetCharArrayRegion,
2914 JNI::SetShortArrayRegion,
2915 JNI::SetIntArrayRegion,
2916 JNI::SetLongArrayRegion,
2917 JNI::SetFloatArrayRegion,
2918 JNI::SetDoubleArrayRegion,
2919 JNI::RegisterNatives,
2920 JNI::UnregisterNatives,
2921 JNI::MonitorEnter,
2922 JNI::MonitorExit,
2923 JNI::GetJavaVM,
2924 JNI::GetStringRegion,
2925 JNI::GetStringUTFRegion,
2926 JNI::GetPrimitiveArrayCritical,
2927 JNI::ReleasePrimitiveArrayCritical,
2928 JNI::GetStringCritical,
2929 JNI::ReleaseStringCritical,
2930 JNI::NewWeakGlobalRef,
2931 JNI::DeleteWeakGlobalRef,
2932 JNI::ExceptionCheck,
2933 JNI::NewDirectByteBuffer,
2934 JNI::GetDirectBufferAddress,
2935 JNI::GetDirectBufferCapacity,
2936 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002937};
2938
Elliott Hughes75770752011-08-24 17:52:38 -07002939JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002940 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002941 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002942 local_ref_cookie(IRT_FIRST_SEGMENT),
2943 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002944 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002945 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002946 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002947 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002948 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002949 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002950 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002951}
2952
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002953JNIEnvExt::~JNIEnvExt() {
2954}
2955
Mathieu Chartier590fee92013-09-13 13:46:47 -07002956jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2957 if (obj == nullptr) {
2958 return nullptr;
2959 }
2960 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2961}
2962
2963void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2964 if (obj != nullptr) {
2965 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2966 }
2967}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002968void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2969 check_jni = enabled;
2970 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002971}
2972
Elliott Hughes73e66f72012-05-09 09:34:45 -07002973void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2974 locals.Dump(os);
2975 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002976}
2977
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002978void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2979 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002980 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002981 stacked_local_ref_cookies.push_back(local_ref_cookie);
2982 local_ref_cookie = locals.GetSegmentState();
2983}
2984
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002985void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002986 locals.SetSegmentState(local_ref_cookie);
2987 local_ref_cookie = stacked_local_ref_cookies.back();
2988 stacked_local_ref_cookies.pop_back();
2989}
2990
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002991Offset JNIEnvExt::SegmentStateOffset() {
2992 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2993 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2994}
2995
Carl Shapiroea4dca82011-08-01 13:45:38 -07002996// JNI Invocation interface.
2997
Brian Carlstrombddf9762013-05-14 11:35:37 -07002998extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002999 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07003000 if (IsBadJniVersion(args->version)) {
3001 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003002 return JNI_EVERSION;
3003 }
Ian Rogerse63db272014-07-15 15:36:11 -07003004 RuntimeOptions options;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003005 for (int i = 0; i < args->nOptions; ++i) {
3006 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003007 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003008 }
3009 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003010 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003011 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003012 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003013 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003014 bool started = runtime->Start();
3015 if (!started) {
3016 delete Thread::Current()->GetJniEnv();
3017 delete runtime->GetJavaVM();
3018 LOG(WARNING) << "CreateJavaVM failed";
3019 return JNI_ERR;
3020 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003021 *p_env = Thread::Current()->GetJniEnv();
3022 *p_vm = runtime->GetJavaVM();
3023 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003024}
3025
Elliott Hughesf2682d52011-08-15 16:37:04 -07003026extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003027 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003028 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003029 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003030 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003031 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003032 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003033 }
3034 return JNI_OK;
3035}
3036
3037// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003038extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003039 return JNI_ERR;
3040}
3041
Elliott Hughescdf53122011-08-19 15:46:09 -07003042class JII {
3043 public:
3044 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003045 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003046 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003047 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003048 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3049 delete raw_vm->runtime;
3050 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003051 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003052
Elliott Hughescdf53122011-08-19 15:46:09 -07003053 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003054 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003055 }
3056
3057 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003058 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003059 }
3060
3061 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003062 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003063 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003064 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003065 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3066 Runtime* runtime = raw_vm->runtime;
3067 runtime->DetachCurrentThread();
3068 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003069 }
3070
3071 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003072 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3073 // and unlike other calls that take a JNI version doesn't care if you supply
3074 // JNI_VERSION_1_1, which we don't otherwise support.
3075 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003076 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003077 return JNI_EVERSION;
3078 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003079 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003080 return JNI_ERR;
3081 }
3082 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003083 if (thread == nullptr) {
3084 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003085 return JNI_EDETACHED;
3086 }
3087 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003088 return JNI_OK;
3089 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003090};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003091
Elliott Hughes88c5c352012-03-15 18:49:48 -07003092const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003093 nullptr, // reserved0
3094 nullptr, // reserved1
3095 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003096 JII::DestroyJavaVM,
3097 JII::AttachCurrentThread,
3098 JII::DetachCurrentThread,
3099 JII::GetEnv,
3100 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003101};
3102
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003103JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003104 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003105 check_jni_abort_hook(nullptr),
3106 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003107 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003108 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003109 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003110 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003111 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003112 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003113 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003114 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003115 libraries(new Libraries),
3116 weak_globals_lock_("JNI weak global reference table lock"),
3117 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3118 allow_new_weak_globals_(true),
3119 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003120 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003121 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003122 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003123 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003124}
3125
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003126JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003127 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003128}
3129
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003130jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3131 if (obj == nullptr) {
3132 return nullptr;
3133 }
3134 MutexLock mu(self, weak_globals_lock_);
3135 while (UNLIKELY(!allow_new_weak_globals_)) {
3136 weak_globals_add_condition_.WaitHoldingLocks(self);
3137 }
3138 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3139 return reinterpret_cast<jweak>(ref);
3140}
3141
3142void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3143 MutexLock mu(self, weak_globals_lock_);
3144 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3145 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3146 << "failed to find entry";
3147 }
3148}
3149
Elliott Hughes88c5c352012-03-15 18:49:48 -07003150void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3151 check_jni = enabled;
3152 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003153}
3154
Elliott Hughesae80b492012-04-24 10:43:17 -07003155void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3156 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3157 if (force_copy) {
3158 os << " (with forcecopy)";
3159 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003160 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003161 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003162 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003163 os << "; pins=" << pin_table.Size();
3164 }
3165 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003166 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003167 os << "; globals=" << globals.Capacity();
3168 }
3169 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003170 MutexLock mu(self, weak_globals_lock_);
3171 if (weak_globals_.Capacity() > 0) {
3172 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003173 }
3174 }
3175 os << '\n';
3176
3177 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003178 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003179 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3180 }
3181}
3182
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003183void JavaVMExt::DisallowNewWeakGlobals() {
3184 MutexLock mu(Thread::Current(), weak_globals_lock_);
3185 allow_new_weak_globals_ = false;
3186}
3187
3188void JavaVMExt::AllowNewWeakGlobals() {
3189 Thread* self = Thread::Current();
3190 MutexLock mu(self, weak_globals_lock_);
3191 allow_new_weak_globals_ = true;
3192 weak_globals_add_condition_.Broadcast(self);
3193}
3194
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003195mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3196 MutexLock mu(self, weak_globals_lock_);
3197 while (UNLIKELY(!allow_new_weak_globals_)) {
3198 weak_globals_add_condition_.WaitHoldingLocks(self);
3199 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003200 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003201}
3202
Elliott Hughes73e66f72012-05-09 09:34:45 -07003203void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003204 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003205 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003206 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003207 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003208 }
3209 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003210 MutexLock mu(self, weak_globals_lock_);
3211 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003212 }
3213 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003214 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003215 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003216 }
3217}
3218
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003219bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003220 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003221 std::string* detail) {
3222 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003223
3224 // See if we've already loaded this library. If we have, and the class loader
3225 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003226 // TODO: for better results we should canonicalize the pathname (or even compare
3227 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003228 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003229 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003230 {
3231 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003232 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003233 library = libraries->Get(path);
3234 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003235 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003236 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003237 // The library will be associated with class_loader. The JNI
3238 // spec says we can't load the same library into more than one
3239 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003240 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003241 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003242 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003243 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003244 return false;
3245 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003246 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003247 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003248 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003249 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003250 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003251 return false;
3252 }
3253 return true;
3254 }
3255
3256 // Open the shared library. Because we're using a full path, the system
3257 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3258 // resolve this library's dependencies though.)
3259
3260 // Failures here are expected when java.library.path has several entries
3261 // and we have to hunt for the lib.
3262
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003263 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3264 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3265 // dlopen) becomes zero from dlclose.
3266
Elliott Hughescdf53122011-08-19 15:46:09 -07003267 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003268 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003269 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003270 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003271 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003272
Elliott Hughes84b2f142012-09-27 09:16:28 -07003273 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003274
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003275 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003276 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003277 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003278 return false;
3279 }
3280
3281 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003282 // TODO: move the locking (and more of this logic) into Libraries.
3283 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003284 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003285 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003286 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003287 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003288 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003289 libraries->Put(path, library);
3290 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003291 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003292 }
3293 if (!created_library) {
3294 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003295 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003296 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003297 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003298
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003299 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003300 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003301
Elliott Hughes79353722013-08-02 16:52:18 -07003302 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003303 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003304 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003305 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003306 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003307 } else {
3308 // Call JNI_OnLoad. We have to override the current class
3309 // loader, which will always be "null" since the stuff at the
3310 // top of the stack is around Runtime.loadLibrary(). (See
3311 // the comments in the JNI FindClass function.)
3312 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3313 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003314 StackHandleScope<1> hs(self);
3315 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3316 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003317
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003318 int version = 0;
3319 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003320 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003321 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003322 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003323 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003324
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003325 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003326
Elliott Hughes79353722013-08-02 16:52:18 -07003327 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003328 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003329 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003330 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003331 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003332 // It's unwise to call dlclose() here, but we can mark it
3333 // as bad and ensure that future load attempts will fail.
3334 // We don't know how far JNI_OnLoad got, so there could
3335 // be some partially-initialized stuff accessible through
3336 // newly-registered native method calls. We could try to
3337 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003338 } else {
3339 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003340 }
Elliott Hughes79353722013-08-02 16:52:18 -07003341 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003342 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003343 }
3344
Elliott Hughes79353722013-08-02 16:52:18 -07003345 library->SetResult(was_successful);
3346 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003347}
3348
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003349void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003350 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003351 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003352 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003353 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003354 c = EnsureInitialized(Thread::Current(), c);
3355 if (c == nullptr) {
3356 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003357 }
3358 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003359 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003360 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003361 std::string detail;
3362 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003363 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003364 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003365 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003366 native_method = libraries->FindNativeMethod(m, detail);
3367 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003368 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003369 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003370 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3371 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003372 }
3373 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003374}
3375
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003376void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003377 MutexLock mu(Thread::Current(), weak_globals_lock_);
3378 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003379 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003380 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003381 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003382 if (new_obj == nullptr) {
3383 new_obj = kClearedJniWeakGlobal;
3384 }
3385 *entry = new_obj;
3386 }
3387}
3388
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003389void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003390 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003391 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003392 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003393 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003394 }
3395 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003396 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003397 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003398 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003399 {
3400 MutexLock mu(self, libraries_lock);
3401 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003402 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003403 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003404 // The weak_globals table is visited by the GC itself (because it mutates the table).
3405}
3406
Elliott Hughesc8fece32013-01-02 11:27:23 -08003407void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003408 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003409 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003410 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003411 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3412 }
3413 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3414}
3415
Ian Rogersdf20fe02011-07-20 20:34:16 -07003416} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003417
3418std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3419 switch (rhs) {
3420 case JNIInvalidRefType:
3421 os << "JNIInvalidRefType";
3422 return os;
3423 case JNILocalRefType:
3424 os << "JNILocalRefType";
3425 return os;
3426 case JNIGlobalRefType:
3427 os << "JNIGlobalRefType";
3428 return os;
3429 case JNIWeakGlobalRefType:
3430 os << "JNIWeakGlobalRefType";
3431 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003432 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003433 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003434 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003435 }
3436}