blob: 938bf2c20e8d39384d17ee7d23a7c62efafbb825 [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"
Mathieu Chartier5369c402014-08-25 13:08:22 -070027#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080029#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080030#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080031#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "dex_file-inl.h"
Mathieu Chartier1f242962014-10-15 16:59:47 -070033#include "fault_handler.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070036#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070037#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070038#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070039#include "mirror/art_field-inl.h"
40#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class-inl.h"
42#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070045#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/throwable.h"
jgu216d031042014-09-10 06:57:17 -040047#include "nativebridge/native_bridge.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080048#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070049#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070051#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070056#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070057
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070058namespace art {
59
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060static const size_t kMonitorsInitial = 32; // Arbitrary.
61static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070062
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063static const size_t kLocalsInitial = 64; // Arbitrary.
64static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070065
Brian Carlstrom7934ac22013-07-26 10:54:15 -070066static size_t gGlobalsInitial = 512; // Arbitrary.
67static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070068
Brian Carlstrom7934ac22013-07-26 10:54:15 -070069static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
70static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070071
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080072static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070074 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070075}
76
Jeff Hao19c5d372013-03-15 14:33:43 -070077static bool IsBadJniVersion(int version) {
78 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
79 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
80}
81
Elliott Hughes6b436852011-08-12 10:16:44 -070082// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
83// separated with slashes but aren't wrapped with "L;" like regular descriptors
84// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
85// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
86// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070087static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070088 std::string result;
89 // Add the missing "L;" if necessary.
90 if (name[0] == '[') {
91 result = name;
92 } else {
93 result += 'L';
94 result += name;
95 result += ';';
96 }
97 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070098 if (result.find('.') != std::string::npos) {
99 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
100 << "\"" << name << "\"";
101 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700102 }
103 return result;
104}
105
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800106static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700110 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
112 "no %s method \"%s.%s%s\"",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700113 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700114}
115
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200116static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
117 const char* kind, jint idx, bool return_errors)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
119 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
120 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
121 << ": " << kind << " is null at index " << idx;
122 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
123 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
124 "%s is null at index %d", kind, idx);
125}
126
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800127static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
129 if (LIKELY(klass->IsInitialized())) {
130 return klass;
131 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132 StackHandleScope<1> hs(self);
133 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
134 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800135 return nullptr;
136 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700137 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800138}
139
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
141 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800143 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800144 if (c == nullptr) {
145 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700146 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800147 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700148 if (is_static) {
149 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700150 } else if (c->IsInterface()) {
151 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700152 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700153 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800154 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700155 // No virtual method matching the signature. Search declared
156 // private methods and constructors.
157 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700158 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700159 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800160 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800162 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700163 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700165}
166
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800167static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800169 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700170 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
171 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800172 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700173 }
Brian Carlstromce888532013-10-10 00:32:58 -0700174 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800175 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700176 return method->GetDeclaringClass()->GetClassLoader();
177 }
178 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800179 mirror::ClassLoader* class_loader =
180 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
181 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700182 return class_loader;
183 }
184 // See if the override ClassLoader is set for gtests.
185 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800186 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800187 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700188 CHECK(Runtime::Current()->UseCompileTimeClassPath());
189 return class_loader;
190 }
191 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800192 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700193}
194
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
196 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700198 StackHandleScope<2> hs(soa.Self());
199 Handle<mirror::Class> c(
200 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
201 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800202 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700203 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800204 mirror::ArtField* field = nullptr;
205 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
207 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700208 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800209 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700210 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700212 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800213 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700217 StackHandleScope<1> hs(soa.Self());
218 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 soa.Self()->ClearException();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700220 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800222 "no type \"%s\" found and so no field \"%s\" "
223 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700224 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700225 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800226 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 }
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700228 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700230 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700231 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700232 } else {
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700233 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800235 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
237 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
238 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700239 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800240 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700241 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700243}
244
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800245static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700248 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
250 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
251 "%s offset=%d length=%d %s.length=%d",
252 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700253}
Ian Rogers0571d352011-11-03 19:51:38 -0700254
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
256 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
259 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
260 "offset=%d length=%d string.length()=%d", start, length,
261 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700262}
Elliott Hughes814e4032011-08-23 12:07:56 -0700263
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 // Turn the const char* into a java.lang.String.
267 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800268 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700270 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 // Choose an appropriate constructor and set up the arguments.
273 jvalue args[2];
274 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800275 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 signature = "(Ljava/lang/String;)V";
279 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800280 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 signature = "(Ljava/lang/Throwable;)V";
282 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700283 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
285 args[0].l = s.get();
286 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700287 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800289 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800290 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800292 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 return JNI_ERR;
294 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700295
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800296 ScopedLocalRef<jthrowable> exception(
297 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
298 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299 return JNI_ERR;
300 }
Ian Rogersef28b142012-11-30 14:22:18 -0800301 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800303 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700305}
306
Elliott Hughes462c9442012-03-23 18:47:50 -0700307static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800308 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700309 return JNI_ERR;
310 }
311
Elliott Hughes462c9442012-03-23 18:47:50 -0700312 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700313 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800314 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700315 *p_env = self->GetJniEnv();
316 return JNI_OK;
317 }
318
319 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
320
321 // No threads allowed in zygote mode.
322 if (runtime->IsZygote()) {
323 LOG(ERROR) << "Attempt to attach a thread in the zygote";
324 return JNI_ERR;
325 }
326
Elliott Hughes462c9442012-03-23 18:47:50 -0700327 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800328 const char* thread_name = nullptr;
329 jobject thread_group = nullptr;
330 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700331 if (IsBadJniVersion(args->version)) {
332 LOG(ERROR) << "Bad JNI version passed to "
333 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
334 << args->version;
335 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700336 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700337 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700338 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700339 }
Elliott Hughes75770752011-08-24 17:52:38 -0700340
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800341 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800342 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700343 return JNI_ERR;
344 } else {
345 *p_env = Thread::Current()->GetJniEnv();
346 return JNI_OK;
347 }
Elliott Hughes75770752011-08-24 17:52:38 -0700348}
349
Elliott Hughes79082e32011-08-25 12:07:32 -0700350class SharedLibrary {
351 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800352 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700353 : path_(path),
354 handle_(handle),
Yong WU355383f2014-07-24 21:32:15 +0800355 needs_native_bridge_(false),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700356 class_loader_(GcRoot<mirror::Object>(class_loader)),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700357 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700358 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700359 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700360 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700361 }
362
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700363 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700364 return class_loader_.Read();
Elliott Hughes79082e32011-08-25 12:07:32 -0700365 }
366
367 std::string GetPath() {
368 return path_;
369 }
370
371 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700372 * Check the result of an earlier call to JNI_OnLoad on this library.
373 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700374 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 bool CheckOnLoadResult()
376 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700378 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
380 bool okay;
381 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700382 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700383
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700384 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
386 // caller can continue.
387 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
388 okay = true;
389 } else {
390 while (jni_on_load_result_ == kPending) {
391 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700392 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700394
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 okay = (jni_on_load_result_ == kOkay);
396 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
397 << (okay ? "succeeded" : "failed") << "]";
398 }
399 }
400 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700401 return okay;
402 }
403
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700405 Thread* self = Thread::Current();
406 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700407
Elliott Hughes79082e32011-08-25 12:07:32 -0700408 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700409 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700410
411 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700412 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700413 }
414
Yong WU355383f2014-07-24 21:32:15 +0800415 void SetNeedsNativeBridge() {
416 needs_native_bridge_ = true;
417 }
418
419 bool NeedsNativeBridge() const {
420 return needs_native_bridge_;
421 }
422
Elliott Hughes79082e32011-08-25 12:07:32 -0700423 void* FindSymbol(const std::string& symbol_name) {
424 return dlsym(handle_, symbol_name.c_str());
425 }
426
Yong WU355383f2014-07-24 21:32:15 +0800427 void* FindSymbolWithNativeBridge(const std::string& symbol_name, mirror::ArtMethod* m)
428 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
429 CHECK(NeedsNativeBridge());
430
431 uint32_t len = 0;
432 const char* shorty = nullptr;
433 if (m != nullptr) {
434 shorty = m->GetShorty(&len);
435 }
Calin Juravle93de4272014-08-12 20:55:20 +0100436 return android::NativeBridgeGetTrampoline(handle_, symbol_name.c_str(), shorty, len);
Yong WU355383f2014-07-24 21:32:15 +0800437 }
438
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800439 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier12f74232015-01-14 14:55:47 -0800440 class_loader_.VisitRootIfNonNull(visitor, arg, RootInfo(kRootVMInternal));
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800441 }
442
Elliott Hughes79082e32011-08-25 12:07:32 -0700443 private:
444 enum JNI_OnLoadState {
445 kPending,
446 kFailed,
447 kOkay,
448 };
449
450 // Path to library "/system/lib/libjni.so".
451 std::string path_;
452
453 // The void* returned by dlopen(3).
454 void* handle_;
455
Yong WU355383f2014-07-24 21:32:15 +0800456 // True if a native bridge is required.
457 bool needs_native_bridge_;
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459 // The ClassLoader this library is associated with.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700460 GcRoot<mirror::Object> class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700461
462 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700464 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700465 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700466 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700467 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700469 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700470};
471
Elliott Hughes79082e32011-08-25 12:07:32 -0700472// This exists mainly to keep implementation details out of the header file.
473class Libraries {
474 public:
475 Libraries() {
476 }
477
478 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700479 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700480 }
481
Elliott Hughesae80b492012-04-24 10:43:17 -0700482 void Dump(std::ostream& os) const {
483 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700484 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700485 if (!first) {
486 os << ' ';
487 }
488 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700489 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700490 }
491 }
492
493 size_t size() const {
494 return libraries_.size();
495 }
496
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700498 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800499 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 }
501
502 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700503 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 }
505
506 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800507 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700508 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700509 std::string jni_short_name(JniShortName(m));
510 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800511 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700512 for (const auto& lib : libraries_) {
513 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 if (library->GetClassLoader() != declaring_class_loader) {
515 // We only search libraries loaded by the appropriate ClassLoader.
516 continue;
517 }
518 // Try the short name then the long name...
Yong WU355383f2014-07-24 21:32:15 +0800519 void* fn = nullptr;
520 if (UNLIKELY(library->NeedsNativeBridge())) {
521 fn = library->FindSymbolWithNativeBridge(jni_short_name, m);
522 if (fn == nullptr) {
523 fn = library->FindSymbolWithNativeBridge(jni_long_name, m);
524 }
525 } else {
526 fn = library->FindSymbol(jni_short_name);
527 if (fn == nullptr) {
528 fn = library->FindSymbol(jni_long_name);
529 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800531 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800532 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
533 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 return fn;
535 }
536 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700538 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700539 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700540 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800541 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 }
543
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800544 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800545 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800546 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800547 }
548 }
549
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 private:
Mathieu Chartier5369c402014-08-25 13:08:22 -0700551 AllocationTrackingSafeMap<std::string, SharedLibrary*, kAllocatorTagJNILibrarires> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700552};
553
Ian Rogers2d10b202014-05-12 19:15:18 -0700554#define CHECK_NON_NULL_ARGUMENT(value) \
555 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700556
Ian Rogers2d10b202014-05-12 19:15:18 -0700557#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
558 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
559
560#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
561 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
562
563#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
564 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
565
566#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800567 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700568 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700569 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700570 }
571
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700572#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800573 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700574 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700575 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700576 }
577
Elliott Hughescdf53122011-08-19 15:46:09 -0700578class JNI {
579 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700580 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700581 return JNI_VERSION_1_6;
582 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700583
Ian Rogers25e8b912012-09-07 11:31:36 -0700584 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700585 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800586 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700587 }
588
Elliott Hughescdf53122011-08-19 15:46:09 -0700589 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700590 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700591 Runtime* runtime = Runtime::Current();
592 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700593 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800595 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700596 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700597 StackHandleScope<1> hs(soa.Self());
598 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800599 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700600 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800601 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700602 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700604 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700605
Ian Rogers62f05122014-03-21 11:21:29 -0700606 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700607 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700608 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700609 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700610 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700611
Ian Rogers62f05122014-03-21 11:21:29 -0700612 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700613 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700615 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700617
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700619 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800621 mirror::ArtMethod* m = soa.DecodeMethod(mid);
622 CHECK(!kMovingMethods);
Mathieu Chartier8c417532014-11-15 13:07:39 -0800623 ScopedLocalRef<jobject> art_method(env, soa.AddLocalReference<jobject>(m));
Sebastien Hertzd3333762014-06-26 14:45:07 +0200624 jobject reflect_method;
625 if (m->IsConstructor()) {
626 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
627 } else {
628 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
629 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700630 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800631 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700632 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800633 SetObjectField(env, reflect_method,
Mathieu Chartier8c417532014-11-15 13:07:39 -0800634 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method.get());
Brian Carlstromea46f952013-07-30 01:26:50 -0700635 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700637
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700639 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800641 mirror::ArtField* f = soa.DecodeField(fid);
Mathieu Chartier8c417532014-11-15 13:07:39 -0800642 ScopedLocalRef<jobject> art_field(env, soa.AddLocalReference<jobject>(f));
Brian Carlstromea46f952013-07-30 01:26:50 -0700643 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
644 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800645 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800647 SetObjectField(env, reflect_field,
Mathieu Chartier8c417532014-11-15 13:07:39 -0800648 WellKnownClasses::java_lang_reflect_Field_artField, art_field.get());
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700650 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughes37f7a402011-08-22 18:56:01 -0700652 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700653 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800655 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700657 }
658
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700659 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700660 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800662 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Narayan Kamath1268b742014-07-11 19:15:11 +0100666 // Note: java_class1 should be safely castable to java_class2, and
667 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700669 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
670 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800672 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
673 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100674 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Elliott Hughese84278b2012-03-22 10:06:53 -0700677 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700678 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800679 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700680 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700681 return JNI_TRUE;
682 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800684 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
685 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700686 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700689
Elliott Hughes37f7a402011-08-22 18:56:01 -0700690 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700691 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800692 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
693 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700694 return JNI_ERR;
695 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
697 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 return JNI_OK;
699 }
700
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700701 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700702 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800703 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700704 }
705
706 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700707 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 }
709
710 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700711 ScopedObjectAccess soa(env);
712 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 }
714
715 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700717
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700718 // If we have no exception to describe, pass through.
719 if (!soa.Self()->GetException(nullptr)) {
720 return;
721 }
722
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700723 StackHandleScope<3> hs(soa.Self());
724 // TODO: Use nullptr instead of null handles?
725 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
726 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
727 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800728 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200729 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800730 {
731 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800732 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700733 old_throw_this_object.Assign(old_throw_location.GetThis());
734 old_throw_method.Assign(old_throw_location.GetMethod());
735 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800736 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200737 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800738 soa.Self()->ClearException();
739 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800740 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700741 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700742 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
743 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800744 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700745 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700746 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700747 } else {
748 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800749 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800750 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700751 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700753 }
754 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700755 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800756 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700757
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700758 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200759 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700761
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800764 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700765 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
767
Ian Rogers25e8b912012-09-07 11:31:36 -0700768 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 LOG(FATAL) << "JNI FatalError called: " << msg;
770 }
771
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700772 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700773 // TODO: SOA may not be necessary but I do it to please lock annotations.
774 ScopedObjectAccess soa(env);
775 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700776 return JNI_ERR;
777 }
Ian Rogersef28b142012-11-30 14:22:18 -0800778 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 return JNI_OK;
780 }
781
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700782 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800784 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700785 soa.Env()->PopFrame();
786 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 }
788
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700789 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700790 // TODO: SOA may not be necessary but I do it to please lock annotations.
791 ScopedObjectAccess soa(env);
792 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700793 }
794
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700796 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800797 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800798 // Check for null after decoding the object to handle cleared weak globals.
799 if (decoded_obj == nullptr) {
800 return nullptr;
801 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700804 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700805 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 return reinterpret_cast<jobject>(ref);
807 }
808
809 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800810 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 return;
812 }
Ian Rogersef28b142012-11-30 14:22:18 -0800813 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800815 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700816 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700817
818 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
819 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
820 << "failed to find entry";
821 }
822 }
823
824 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800826 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
828
829 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700830 if (obj != nullptr) {
831 ScopedObjectAccess soa(env);
832 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 }
834 }
835
836 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700837 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800838 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800839 // Check for null after decoding the object to handle cleared weak globals.
840 if (decoded_obj == nullptr) {
841 return nullptr;
842 }
843 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 }
845
846 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800847 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 return;
849 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700850 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800851 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700852
Ian Rogersef28b142012-11-30 14:22:18 -0800853 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 if (!locals.Remove(cookie, obj)) {
855 // Attempting to delete a local reference that is not in the
856 // topmost local reference frame is a no-op. DeleteLocalRef returns
857 // void and doesn't throw any exceptions, but we should probably
858 // complain about it so the user will notice that things aren't
859 // going quite the way they expect.
860 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
861 << "failed to find entry";
862 }
863 }
864
865 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700866 if (obj1 == obj2) {
867 return JNI_TRUE;
868 } else {
869 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800870 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
871 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700872 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700875 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700876 CHECK_NON_NULL_ARGUMENT(java_class);
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 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700882 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 }
884
Ian Rogersbc939662013-08-15 10:26:54 -0700885 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700888 CHECK_NON_NULL_ARGUMENT(java_class);
889 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700890 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 va_end(args);
892 return result;
893 }
894
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700896 CHECK_NON_NULL_ARGUMENT(java_class);
897 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800899 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800900 if (c == nullptr) {
901 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700902 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800903 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800904 if (result == nullptr) {
905 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700906 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700907 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800909 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800910 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700911 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800912 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700916 CHECK_NON_NULL_ARGUMENT(java_class);
917 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800919 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800920 if (c == nullptr) {
921 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700922 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800923 mirror::Object* result = c->AllocObject(soa.Self());
924 if (result == nullptr) {
925 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700926 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800929 if (soa.Self()->IsExceptionPending()) {
930 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700931 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800932 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 }
934
Ian Rogersbc939662013-08-15 10:26:54 -0700935 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700936 CHECK_NON_NULL_ARGUMENT(java_class);
937 CHECK_NON_NULL_ARGUMENT(name);
938 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700940 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Ian Rogersbc939662013-08-15 10:26:54 -0700943 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
944 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700945 CHECK_NON_NULL_ARGUMENT(java_class);
946 CHECK_NON_NULL_ARGUMENT(name);
947 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700948 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700949 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_list ap;
954 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700955 CHECK_NON_NULL_ARGUMENT(obj);
956 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700957 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700964 CHECK_NON_NULL_ARGUMENT(obj);
965 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 ScopedObjectAccess soa(env);
967 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
968 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700972 CHECK_NON_NULL_ARGUMENT(obj);
973 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700975 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
976 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_list ap;
982 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700983 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700985 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700988 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700992 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
993 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 ScopedObjectAccess soa(env);
995 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700999 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1000 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001002 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1003 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 va_list ap;
1008 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001009 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001011 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001014 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001018 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1019 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001020 ScopedObjectAccess soa(env);
1021 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001025 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001028 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1029 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_list ap;
1034 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001035 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001037 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001040 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001044 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1045 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001051 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1052 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001054 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1055 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 va_list ap;
1060 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001061 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001063 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001066 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001070 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1071 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 ScopedObjectAccess soa(env);
1073 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001077 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1078 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001080 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1081 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_list ap;
1086 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001087 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001089 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001090 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001092 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001096 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1097 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 ScopedObjectAccess soa(env);
1099 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001103 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001106 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1107 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 va_list ap;
1112 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001113 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001115 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001118 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001122 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1123 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 ScopedObjectAccess soa(env);
1125 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001129 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001132 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1133 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_list ap;
1138 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001139 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001141 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001144 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001148 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1149 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001150 ScopedObjectAccess soa(env);
1151 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001155 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001158 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1159 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_list ap;
1164 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001165 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1166 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001167 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001170 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001174 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1175 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 ScopedObjectAccess soa(env);
1177 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001181 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1182 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001184 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1185 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_list ap;
1190 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001191 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1192 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001193 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001194 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001199 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1200 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 ScopedObjectAccess soa(env);
1202 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001206 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1207 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001209 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001212 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001215 CHECK_NON_NULL_ARGUMENT(obj);
1216 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001217 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1219 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 va_end(ap);
1221 return local_result;
1222 }
1223
Ian Rogersbc939662013-08-15 10:26:54 -07001224 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1225 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001226 CHECK_NON_NULL_ARGUMENT(obj);
1227 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 ScopedObjectAccess soa(env);
1229 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1230 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
Ian Rogersbc939662013-08-15 10:26:54 -07001233 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1234 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001235 CHECK_NON_NULL_ARGUMENT(obj);
1236 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001238 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Ian Rogersbc939662013-08-15 10:26:54 -07001242 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1243 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001246 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1247 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001248 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001251 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Ian Rogersbc939662013-08-15 10:26:54 -07001254 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1255 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001256 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1257 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001258 ScopedObjectAccess soa(env);
1259 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Ian Rogersbc939662013-08-15 10:26:54 -07001262 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1263 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001264 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1265 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001267 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001270 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001273 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1274 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001275 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001278 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
Ian Rogersbc939662013-08-15 10:26:54 -07001281 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1282 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001283 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1284 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 ScopedObjectAccess soa(env);
1286 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Ian Rogersbc939662013-08-15 10:26:54 -07001289 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1290 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001291 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1292 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001294 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001297 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001300 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1301 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001302 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001305 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Ian Rogersbc939662013-08-15 10:26:54 -07001308 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1309 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001310 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1311 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(env);
1313 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Ian Rogersbc939662013-08-15 10:26:54 -07001316 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1317 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001318 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1319 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001321 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001327 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1328 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001329 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001332 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Ian Rogersbc939662013-08-15 10:26:54 -07001335 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1336 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001337 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1338 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env);
1340 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Ian Rogersbc939662013-08-15 10:26:54 -07001343 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1344 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001345 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1346 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001348 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001354 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1355 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001356 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001359 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Ian Rogersbc939662013-08-15 10:26:54 -07001362 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1363 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001364 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1365 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Ian Rogersbc939662013-08-15 10:26:54 -07001370 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1371 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001372 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1373 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001375 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001381 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1382 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001383 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001386 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Ian Rogersbc939662013-08-15 10:26:54 -07001389 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1390 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001391 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1392 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 ScopedObjectAccess soa(env);
1394 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Ian Rogersbc939662013-08-15 10:26:54 -07001397 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1398 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001399 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1400 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001401 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001402 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001408 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1409 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001410 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001413 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Ian Rogersbc939662013-08-15 10:26:54 -07001416 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1417 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001418 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1419 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001420 ScopedObjectAccess soa(env);
1421 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Ian Rogersbc939662013-08-15 10:26:54 -07001424 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1425 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001426 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1427 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001429 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001434 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001435 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1436 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001437 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001440 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Ian Rogersbc939662013-08-15 10:26:54 -07001443 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1444 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001445 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1446 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
1448 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Ian Rogersbc939662013-08-15 10:26:54 -07001451 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1452 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001453 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1454 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001456 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001459 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001461 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001462 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1463 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001464 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001465 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 va_end(ap);
1467 }
1468
Brian Carlstromea46f952013-07-30 01:26:50 -07001469 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1470 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001471 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1472 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 ScopedObjectAccess soa(env);
1474 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
Ian Rogersbc939662013-08-15 10:26:54 -07001477 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1478 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001479 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1480 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001481 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001482 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 }
1484
Ian Rogersbc939662013-08-15 10:26:54 -07001485 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001486 CHECK_NON_NULL_ARGUMENT(java_class);
1487 CHECK_NON_NULL_ARGUMENT(name);
1488 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001490 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001492
Ian Rogersbc939662013-08-15 10:26:54 -07001493 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1494 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001495 CHECK_NON_NULL_ARGUMENT(java_class);
1496 CHECK_NON_NULL_ARGUMENT(name);
1497 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001499 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001503 CHECK_NON_NULL_ARGUMENT(obj);
1504 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001505 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001506 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1507 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001508 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001512 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001513 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001514 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001515 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001519 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1520 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001521 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001522 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1523 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1524 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001525 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001529 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001530 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001531 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1532 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001533 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001537 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1538 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001540 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1541 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001542 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001544#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001545 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001546 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001547 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001548 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001549
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001551 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1552 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001553 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001554 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1555 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001556 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001558#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001559 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001560 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001561 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001562 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001563
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001565 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001569 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001573 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001600 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001608 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001626 }
1627
1628 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001630 }
1631
1632 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 }
1635
1636 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001638 }
1639
1640 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001642 }
1643
1644 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 }
1647
1648 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001649 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001650 }
1651
1652 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001653 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 }
1655
1656 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001657 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001658 }
1659
1660 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001661 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001662 }
1663
1664 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001665 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001666 }
1667
1668 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001669 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001670 }
1671
1672 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001673 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001674 }
1675
1676 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001677 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001678 }
1679
1680 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001681 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001682 }
1683
1684 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001685 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001686 }
1687
1688 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001689 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001692 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001695 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001696 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001697 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001698 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_end(ap);
1700 return local_result;
1701 }
1702
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001703 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001704 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001705 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001706 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001710 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001711 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001712 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001713 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001720 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001722 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001724 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001728 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001730 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001734 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001736 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001742 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001743 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001744 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001746 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001750 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001752 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001756 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001764 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001765 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001766 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001768 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001772 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001774 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001778 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001780 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001786 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001787 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001788 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001790 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001793 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001794 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001802 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001807 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001808 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001809 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001810 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001812 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001816 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001817 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001818 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001822 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001824 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001829 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001830 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001831 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001832 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001834 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001837 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001838 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001839 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001840 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001845 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001846 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001849 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001851 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001852 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001853 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001854 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001856 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001859 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001861 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001862 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001865 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001866 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001868 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001871 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001873 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001874 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001875 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001876 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001878 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001881 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001882 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001883 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001884 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001887 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001888 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001890 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001893 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001895 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001896 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001897 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001898 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 va_end(ap);
1900 }
1901
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001902 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001903 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001904 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001905 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001908 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001909 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001910 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001911 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 }
1913
Elliott Hughes814e4032011-08-23 12:07:56 -07001914 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001915 if (UNLIKELY(char_count < 0)) {
1916 JniAbortF("NewString", "char_count < 0: %d", char_count);
1917 return nullptr;
1918 }
1919 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1920 JniAbortF("NewString", "chars == null && char_count > 0");
1921 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001922 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001923 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001924 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
1928 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001929 if (utf == nullptr) {
1930 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001932 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001933 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001934 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes814e4032011-08-23 12:07:56 -07001937 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001938 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001940 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001941 }
1942
1943 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001944 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001945 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001946 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001947 }
1948
Ian Rogersbc939662013-08-15 10:26:54 -07001949 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1950 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001951 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001953 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001955 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001956 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001957 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001958 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1959 memcpy(buf, chars + start, length * sizeof(jchar));
1960 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001961 }
1962
Ian Rogersbc939662013-08-15 10:26:54 -07001963 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1964 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001965 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001966 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001967 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001968 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001969 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001970 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001971 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1973 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1974 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001975 }
1976
Elliott Hughes75770752011-08-24 17:52:38 -07001977 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001978 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001979 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001980 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1981 mirror::CharArray* chars = s->GetCharArray();
Fred Shih56890e22014-06-02 11:11:52 -07001982 gc::Heap* heap = Runtime::Current()->GetHeap();
1983 if (heap->IsMovableObject(chars)) {
1984 if (is_copy != nullptr) {
1985 *is_copy = JNI_TRUE;
1986 }
1987 int32_t char_count = s->GetLength();
1988 int32_t offset = s->GetOffset();
1989 jchar* bytes = new jchar[char_count];
1990 for (int32_t i = 0; i < char_count; i++) {
1991 bytes[i] = chars->Get(i + offset);
1992 }
1993 return bytes;
1994 } else {
1995 if (is_copy != nullptr) {
1996 *is_copy = JNI_FALSE;
1997 }
1998 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001999 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002000 }
2001
Mathieu Chartier590fee92013-09-13 13:46:47 -07002002 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002003 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002004 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07002005 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2006 mirror::CharArray* s_chars = s->GetCharArray();
2007 if (chars != (s_chars->GetData() + s->GetOffset())) {
2008 delete[] chars;
2009 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes75770752011-08-24 17:52:38 -07002012 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07002013 CHECK_NON_NULL_ARGUMENT(java_string);
2014 ScopedObjectAccess soa(env);
2015 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2016 mirror::CharArray* chars = s->GetCharArray();
2017 int32_t offset = s->GetOffset();
Fred Shih56890e22014-06-02 11:11:52 -07002018 gc::Heap* heap = Runtime::Current()->GetHeap();
2019 if (heap->IsMovableObject(chars)) {
2020 StackHandleScope<1> hs(soa.Self());
2021 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
2022 heap->IncrementDisableMovingGC(soa.Self());
2023 }
2024 if (is_copy != nullptr) {
2025 *is_copy = JNI_FALSE;
2026 }
2027 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002031 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2032 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07002033 gc::Heap* heap = Runtime::Current()->GetHeap();
2034 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2035 mirror::CharArray* s_chars = s->GetCharArray();
2036 if (heap->IsMovableObject(s_chars)) {
2037 heap->DecrementDisableMovingGC(soa.Self());
2038 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes75770752011-08-24 17:52:38 -07002041 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002042 if (java_string == nullptr) {
2043 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002044 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002045 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002046 *is_copy = JNI_TRUE;
2047 }
Ian Rogersef28b142012-11-30 14:22:18 -08002048 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002049 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 size_t byte_count = s->GetUtfLength();
2051 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002052 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002053 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2054 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2055 bytes[byte_count] = '\0';
2056 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002057 }
2058
Elliott Hughes75770752011-08-24 17:52:38 -07002059 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002060 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002061 }
2062
Elliott Hughesbd935992011-08-22 11:59:34 -07002063 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002064 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002065 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002066 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002067 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002068 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2069 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002070 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002071 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002075 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002076 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002077 mirror::ObjectArray<mirror::Object>* array =
2078 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002079 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Ian Rogersbc939662013-08-15 10:26:54 -07002082 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2083 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002084 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002086 mirror::ObjectArray<mirror::Object>* array =
2087 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2088 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002089 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
2092 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002093 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
2096 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002097 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
2100 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002101 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
2104 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002105 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
2108 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002109 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
2112 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002113 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
2116 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002117 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Ian Rogers1d99e452014-01-02 17:36:41 -08002120 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2121 jobject initial_element) {
2122 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002123 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002124 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002125 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002126 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127
2128 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002129 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002130 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002131 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002132 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002133 if (UNLIKELY(element_class->IsPrimitive())) {
2134 JniAbortF("NewObjectArray", "not an object type: %s",
2135 PrettyDescriptor(element_class).c_str());
2136 return nullptr;
2137 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002138 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002139 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002140 if (UNLIKELY(array_class == nullptr)) {
2141 return nullptr;
2142 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes75770752011-08-24 17:52:38 -07002145 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002146 mirror::ObjectArray<mirror::Object>* result =
2147 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002148 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002149 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002150 if (initial_object != nullptr) {
2151 mirror::Class* element_class = result->GetClass()->GetComponentType();
2152 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2153 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2154 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2155 PrettyDescriptor(element_class).c_str());
2156
2157 } else {
2158 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002159 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002160 }
2161 }
Elliott Hughes75770752011-08-24 17:52:38 -07002162 }
2163 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002164 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
2167 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002168 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Ian Rogersa15e67d2012-02-28 13:51:55 -08002171 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002172 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002173 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002174 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002175 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2176 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2177 PrettyDescriptor(array->GetClass()).c_str());
2178 return nullptr;
2179 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002180 gc::Heap* heap = Runtime::Current()->GetHeap();
2181 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002182 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002183 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002184 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002185 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002186 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002187 *is_copy = JNI_FALSE;
2188 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002189 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002190 }
2191
Ian Rogers2d10b202014-05-12 19:15:18 -07002192 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2193 jint mode) {
2194 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2195 ScopedObjectAccess soa(env);
2196 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2197 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2198 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2199 PrettyDescriptor(array->GetClass()).c_str());
2200 return;
2201 }
2202 const size_t component_size = array->GetClass()->GetComponentSize();
2203 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002204 }
2205
Elliott Hughes75770752011-08-24 17:52:38 -07002206 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002207 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
Elliott Hughes75770752011-08-24 17:52:38 -07002210 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002211 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Elliott Hughes75770752011-08-24 17:52:38 -07002214 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002215 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Elliott Hughes75770752011-08-24 17:52:38 -07002218 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002219 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughes75770752011-08-24 17:52:38 -07002222 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002223 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Elliott Hughes75770752011-08-24 17:52:38 -07002226 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002227 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Elliott Hughes75770752011-08-24 17:52:38 -07002230 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002231 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Elliott Hughes75770752011-08-24 17:52:38 -07002234 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002235 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Mathieu Chartier590fee92013-09-13 13:46:47 -07002238 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2239 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002240 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2241 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Mathieu Chartier590fee92013-09-13 13:46:47 -07002244 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002245 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Mathieu Chartier590fee92013-09-13 13:46:47 -07002248 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002249 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Mathieu Chartier590fee92013-09-13 13:46:47 -07002252 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2253 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002254 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 }
2256
Mathieu Chartier590fee92013-09-13 13:46:47 -07002257 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2258 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002259 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
Mathieu Chartier590fee92013-09-13 13:46:47 -07002262 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002263 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
2265
Mathieu Chartier590fee92013-09-13 13:46:47 -07002266 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002267 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Mathieu Chartier590fee92013-09-13 13:46:47 -07002270 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2271 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002272 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 }
2274
Ian Rogersbc939662013-08-15 10:26:54 -07002275 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2276 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002277 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002278 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 }
2280
Ian Rogersbc939662013-08-15 10:26:54 -07002281 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2282 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002283 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285
Ian Rogersbc939662013-08-15 10:26:54 -07002286 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2287 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002288 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
2290
Ian Rogersbc939662013-08-15 10:26:54 -07002291 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2292 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002293 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002294 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Ian Rogersbc939662013-08-15 10:26:54 -07002297 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2298 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002299 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(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 GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2304 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002305 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Ian Rogersbc939662013-08-15 10:26:54 -07002308 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2309 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002310 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312
Ian Rogersbc939662013-08-15 10:26:54 -07002313 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2314 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002315 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002316 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
2318
Ian Rogersbc939662013-08-15 10:26:54 -07002319 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2320 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002321 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002322 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 }
2324
Ian Rogersbc939662013-08-15 10:26:54 -07002325 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2326 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002327 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002328 }
2329
Ian Rogersbc939662013-08-15 10:26:54 -07002330 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2331 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002332 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002333 }
2334
Ian Rogersbc939662013-08-15 10:26:54 -07002335 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2336 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002337 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002338 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002339 }
2340
Ian Rogersbc939662013-08-15 10:26:54 -07002341 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2342 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002343 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002344 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002345 }
2346
Ian Rogersbc939662013-08-15 10:26:54 -07002347 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2348 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002349 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2353 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002355 }
2356
Ian Rogersbc939662013-08-15 10:26:54 -07002357 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2358 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002359 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002360 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002361 }
2362
Ian Rogersbc939662013-08-15 10:26:54 -07002363 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2364 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002365 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2366 }
2367
Ian Rogersbc939662013-08-15 10:26:54 -07002368 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2369 jint method_count, bool return_errors) {
2370 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002371 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002372 return JNI_ERR; // Not reached.
2373 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002374 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002375 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002376 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002377 if (UNLIKELY(method_count == 0)) {
2378 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2379 << PrettyDescriptor(c);
2380 return JNI_OK;
2381 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002382 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002383 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002384 const char* name = methods[i].name;
2385 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002386 const void* fnPtr = methods[i].fnPtr;
2387 if (UNLIKELY(name == nullptr)) {
2388 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2389 return JNI_ERR;
2390 } else if (UNLIKELY(sig == nullptr)) {
2391 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2392 return JNI_ERR;
2393 } else if (UNLIKELY(fnPtr == nullptr)) {
2394 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2395 return JNI_ERR;
2396 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002397 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002398 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002399 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002400 ++sig;
2401 }
2402
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002403 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2404 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002405 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002406 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002407 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002408 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002409 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002410 << PrettyDescriptor(c) << "." << name << sig << " in "
2411 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002412 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002413 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002414 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002415 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002416 << PrettyDescriptor(c) << "." << name << sig
2417 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002418 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002419 return JNI_ERR;
2420 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002421
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002422 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002423
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002424 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002425 }
2426 return JNI_OK;
2427 }
2428
Elliott Hughes5174fe62011-08-23 15:12:35 -07002429 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002430 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002431 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002432 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002433
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002434 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002435
Ian Rogers2d10b202014-05-12 19:15:18 -07002436 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002437 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002438 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002439 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002440 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002441 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002442 }
2443 }
2444 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002445 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002446 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002447 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002448 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002449 }
2450 }
2451
Ian Rogers2d10b202014-05-12 19:15:18 -07002452 if (unregistered_count == 0) {
2453 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2454 << PrettyDescriptor(c) << "' that contains no native methods";
2455 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002456 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002457 }
2458
Ian Rogers719d1a32014-03-06 12:13:39 -08002459 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002460 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002461 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002462 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2463 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002464 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002465 return JNI_ERR;
2466 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002467 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002468 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002469 }
2470
Ian Rogers719d1a32014-03-06 12:13:39 -08002471 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002472 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002473 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002474 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002475 o->MonitorExit(soa.Self());
2476 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002477 return JNI_ERR;
2478 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002479 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002480 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002481 }
2482
2483 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002484 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002485 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002486 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002487 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002488 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002489 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002490 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002491 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002492 }
2493
Elliott Hughescdf53122011-08-19 15:46:09 -07002494 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002495 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002496 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002497 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002498 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002499 if (address == nullptr && capacity != 0) {
2500 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002501 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002502 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002503
Brian Carlstrom85a93362014-06-25 09:30:52 -07002504 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002505 if (capacity > INT_MAX) {
2506 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2507 return nullptr;
2508 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002509 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002510 jint capacity_arg = static_cast<jint>(capacity);
2511
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002512 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2513 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002514 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002515 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002516 }
2517
Elliott Hughesb465ab02011-08-24 11:21:21 -07002518 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002519 return reinterpret_cast<void*>(env->GetLongField(
2520 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002521 }
2522
Elliott Hughesb465ab02011-08-24 11:21:21 -07002523 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002524 return static_cast<jlong>(env->GetIntField(
2525 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002526 }
2527
Elliott Hughesb465ab02011-08-24 11:21:21 -07002528 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Andreas Gampe246a0132014-12-20 00:08:35 -08002529 if (java_object == nullptr) {
2530 return JNIInvalidRefType;
2531 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002532
2533 // Do we definitely know what kind of reference this is?
2534 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2535 IndirectRefKind kind = GetIndirectRefKind(ref);
2536 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002537 case kLocal: {
2538 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002539 // The local refs don't need a read barrier.
2540 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2541 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002542 return JNILocalRefType;
2543 }
2544 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002545 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002546 case kGlobal:
2547 return JNIGlobalRefType;
2548 case kWeakGlobal:
2549 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002550 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002551 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002552 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002553 return JNILocalRefType;
2554 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002555 return JNIInvalidRefType;
2556 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002557 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2558 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002559 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002560
2561 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002562 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2563 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002564 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002565 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002566 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2567 return JNI_ERR;
2568 }
2569 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002570 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002571 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2572 if (!okay) {
2573 soa.Self()->ThrowOutOfMemoryError(caller);
2574 }
2575 return okay ? JNI_OK : JNI_ERR;
2576 }
2577
2578 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002579 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002580 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002581 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002582 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002583 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002584 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002585 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002586 return soa.AddLocalReference<JniT>(result);
2587 }
2588
Ian Rogers2d10b202014-05-12 19:15:18 -07002589 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2590 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2591 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002592 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002593 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002594 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2595 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2596 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2597 PrettyDescriptor(array->GetClass()).c_str());
2598 return nullptr;
2599 }
2600 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2601 return array;
2602 }
2603
2604 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2605 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2606 CHECK_NON_NULL_ARGUMENT(java_array);
2607 ScopedObjectAccess soa(env);
2608 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2609 "GetArrayElements",
2610 "get");
2611 if (UNLIKELY(array == nullptr)) {
2612 return nullptr;
2613 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002614 // Only make a copy if necessary.
2615 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2616 if (is_copy != nullptr) {
2617 *is_copy = JNI_TRUE;
2618 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002619 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002620 size_t size = array->GetLength() * component_size;
2621 void* data = new uint64_t[RoundUp(size, 8) / 8];
2622 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002623 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002624 } else {
2625 if (is_copy != nullptr) {
2626 *is_copy = JNI_FALSE;
2627 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002628 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002629 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002630 }
2631
Ian Rogers2d10b202014-05-12 19:15:18 -07002632 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002633 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002634 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002635 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002636 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2637 "ReleaseArrayElements",
2638 "release");
2639 if (array == nullptr) {
2640 return;
2641 }
2642 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2643 }
2644
2645 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2646 size_t component_size, void* elements, jint mode)
2647 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002648 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002649 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002650 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002651 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002652 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2653 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002654 if (is_copy) {
2655 // Sanity check: If elements is not the same as the java array's data, it better not be a
2656 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2657 // copies we make?
2658 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2659 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2660 reinterpret_cast<void*>(elements), array_data);
2661 return;
2662 }
2663 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002664 // Don't need to copy if we had a direct pointer.
2665 if (mode != JNI_ABORT && is_copy) {
2666 memcpy(array_data, elements, bytes);
2667 }
2668 if (mode != JNI_COMMIT) {
2669 if (is_copy) {
2670 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002671 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002672 // Non copy to a movable object must means that we had disabled the moving GC.
2673 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002674 }
2675 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002676 }
2677
Ian Rogers2d10b202014-05-12 19:15:18 -07002678 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2679 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2680 jsize start, jsize length, ElementT* buf) {
2681 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2682 ScopedObjectAccess soa(env);
2683 ArtArrayT* array =
2684 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2685 "GetPrimitiveArrayRegion",
2686 "get region of");
2687 if (array != nullptr) {
2688 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2689 ThrowAIOOBE(soa, array, start, length, "src");
2690 } else {
2691 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2692 ElementT* data = array->GetData();
2693 memcpy(buf, data + start, length * sizeof(ElementT));
2694 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002695 }
2696 }
2697
Ian Rogers2d10b202014-05-12 19:15:18 -07002698 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2699 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2700 jsize start, jsize length, const ElementT* buf) {
2701 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2702 ScopedObjectAccess soa(env);
2703 ArtArrayT* array =
2704 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2705 "SetPrimitiveArrayRegion",
2706 "set region of");
2707 if (array != nullptr) {
2708 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2709 ThrowAIOOBE(soa, array, start, length, "dst");
2710 } else {
2711 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2712 ElementT* data = array->GetData();
2713 memcpy(data + start, buf, length * sizeof(ElementT));
2714 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002715 }
2716 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002717};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002718
Elliott Hughes88c5c352012-03-15 18:49:48 -07002719const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002720 nullptr, // reserved0.
2721 nullptr, // reserved1.
2722 nullptr, // reserved2.
2723 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002724 JNI::GetVersion,
2725 JNI::DefineClass,
2726 JNI::FindClass,
2727 JNI::FromReflectedMethod,
2728 JNI::FromReflectedField,
2729 JNI::ToReflectedMethod,
2730 JNI::GetSuperclass,
2731 JNI::IsAssignableFrom,
2732 JNI::ToReflectedField,
2733 JNI::Throw,
2734 JNI::ThrowNew,
2735 JNI::ExceptionOccurred,
2736 JNI::ExceptionDescribe,
2737 JNI::ExceptionClear,
2738 JNI::FatalError,
2739 JNI::PushLocalFrame,
2740 JNI::PopLocalFrame,
2741 JNI::NewGlobalRef,
2742 JNI::DeleteGlobalRef,
2743 JNI::DeleteLocalRef,
2744 JNI::IsSameObject,
2745 JNI::NewLocalRef,
2746 JNI::EnsureLocalCapacity,
2747 JNI::AllocObject,
2748 JNI::NewObject,
2749 JNI::NewObjectV,
2750 JNI::NewObjectA,
2751 JNI::GetObjectClass,
2752 JNI::IsInstanceOf,
2753 JNI::GetMethodID,
2754 JNI::CallObjectMethod,
2755 JNI::CallObjectMethodV,
2756 JNI::CallObjectMethodA,
2757 JNI::CallBooleanMethod,
2758 JNI::CallBooleanMethodV,
2759 JNI::CallBooleanMethodA,
2760 JNI::CallByteMethod,
2761 JNI::CallByteMethodV,
2762 JNI::CallByteMethodA,
2763 JNI::CallCharMethod,
2764 JNI::CallCharMethodV,
2765 JNI::CallCharMethodA,
2766 JNI::CallShortMethod,
2767 JNI::CallShortMethodV,
2768 JNI::CallShortMethodA,
2769 JNI::CallIntMethod,
2770 JNI::CallIntMethodV,
2771 JNI::CallIntMethodA,
2772 JNI::CallLongMethod,
2773 JNI::CallLongMethodV,
2774 JNI::CallLongMethodA,
2775 JNI::CallFloatMethod,
2776 JNI::CallFloatMethodV,
2777 JNI::CallFloatMethodA,
2778 JNI::CallDoubleMethod,
2779 JNI::CallDoubleMethodV,
2780 JNI::CallDoubleMethodA,
2781 JNI::CallVoidMethod,
2782 JNI::CallVoidMethodV,
2783 JNI::CallVoidMethodA,
2784 JNI::CallNonvirtualObjectMethod,
2785 JNI::CallNonvirtualObjectMethodV,
2786 JNI::CallNonvirtualObjectMethodA,
2787 JNI::CallNonvirtualBooleanMethod,
2788 JNI::CallNonvirtualBooleanMethodV,
2789 JNI::CallNonvirtualBooleanMethodA,
2790 JNI::CallNonvirtualByteMethod,
2791 JNI::CallNonvirtualByteMethodV,
2792 JNI::CallNonvirtualByteMethodA,
2793 JNI::CallNonvirtualCharMethod,
2794 JNI::CallNonvirtualCharMethodV,
2795 JNI::CallNonvirtualCharMethodA,
2796 JNI::CallNonvirtualShortMethod,
2797 JNI::CallNonvirtualShortMethodV,
2798 JNI::CallNonvirtualShortMethodA,
2799 JNI::CallNonvirtualIntMethod,
2800 JNI::CallNonvirtualIntMethodV,
2801 JNI::CallNonvirtualIntMethodA,
2802 JNI::CallNonvirtualLongMethod,
2803 JNI::CallNonvirtualLongMethodV,
2804 JNI::CallNonvirtualLongMethodA,
2805 JNI::CallNonvirtualFloatMethod,
2806 JNI::CallNonvirtualFloatMethodV,
2807 JNI::CallNonvirtualFloatMethodA,
2808 JNI::CallNonvirtualDoubleMethod,
2809 JNI::CallNonvirtualDoubleMethodV,
2810 JNI::CallNonvirtualDoubleMethodA,
2811 JNI::CallNonvirtualVoidMethod,
2812 JNI::CallNonvirtualVoidMethodV,
2813 JNI::CallNonvirtualVoidMethodA,
2814 JNI::GetFieldID,
2815 JNI::GetObjectField,
2816 JNI::GetBooleanField,
2817 JNI::GetByteField,
2818 JNI::GetCharField,
2819 JNI::GetShortField,
2820 JNI::GetIntField,
2821 JNI::GetLongField,
2822 JNI::GetFloatField,
2823 JNI::GetDoubleField,
2824 JNI::SetObjectField,
2825 JNI::SetBooleanField,
2826 JNI::SetByteField,
2827 JNI::SetCharField,
2828 JNI::SetShortField,
2829 JNI::SetIntField,
2830 JNI::SetLongField,
2831 JNI::SetFloatField,
2832 JNI::SetDoubleField,
2833 JNI::GetStaticMethodID,
2834 JNI::CallStaticObjectMethod,
2835 JNI::CallStaticObjectMethodV,
2836 JNI::CallStaticObjectMethodA,
2837 JNI::CallStaticBooleanMethod,
2838 JNI::CallStaticBooleanMethodV,
2839 JNI::CallStaticBooleanMethodA,
2840 JNI::CallStaticByteMethod,
2841 JNI::CallStaticByteMethodV,
2842 JNI::CallStaticByteMethodA,
2843 JNI::CallStaticCharMethod,
2844 JNI::CallStaticCharMethodV,
2845 JNI::CallStaticCharMethodA,
2846 JNI::CallStaticShortMethod,
2847 JNI::CallStaticShortMethodV,
2848 JNI::CallStaticShortMethodA,
2849 JNI::CallStaticIntMethod,
2850 JNI::CallStaticIntMethodV,
2851 JNI::CallStaticIntMethodA,
2852 JNI::CallStaticLongMethod,
2853 JNI::CallStaticLongMethodV,
2854 JNI::CallStaticLongMethodA,
2855 JNI::CallStaticFloatMethod,
2856 JNI::CallStaticFloatMethodV,
2857 JNI::CallStaticFloatMethodA,
2858 JNI::CallStaticDoubleMethod,
2859 JNI::CallStaticDoubleMethodV,
2860 JNI::CallStaticDoubleMethodA,
2861 JNI::CallStaticVoidMethod,
2862 JNI::CallStaticVoidMethodV,
2863 JNI::CallStaticVoidMethodA,
2864 JNI::GetStaticFieldID,
2865 JNI::GetStaticObjectField,
2866 JNI::GetStaticBooleanField,
2867 JNI::GetStaticByteField,
2868 JNI::GetStaticCharField,
2869 JNI::GetStaticShortField,
2870 JNI::GetStaticIntField,
2871 JNI::GetStaticLongField,
2872 JNI::GetStaticFloatField,
2873 JNI::GetStaticDoubleField,
2874 JNI::SetStaticObjectField,
2875 JNI::SetStaticBooleanField,
2876 JNI::SetStaticByteField,
2877 JNI::SetStaticCharField,
2878 JNI::SetStaticShortField,
2879 JNI::SetStaticIntField,
2880 JNI::SetStaticLongField,
2881 JNI::SetStaticFloatField,
2882 JNI::SetStaticDoubleField,
2883 JNI::NewString,
2884 JNI::GetStringLength,
2885 JNI::GetStringChars,
2886 JNI::ReleaseStringChars,
2887 JNI::NewStringUTF,
2888 JNI::GetStringUTFLength,
2889 JNI::GetStringUTFChars,
2890 JNI::ReleaseStringUTFChars,
2891 JNI::GetArrayLength,
2892 JNI::NewObjectArray,
2893 JNI::GetObjectArrayElement,
2894 JNI::SetObjectArrayElement,
2895 JNI::NewBooleanArray,
2896 JNI::NewByteArray,
2897 JNI::NewCharArray,
2898 JNI::NewShortArray,
2899 JNI::NewIntArray,
2900 JNI::NewLongArray,
2901 JNI::NewFloatArray,
2902 JNI::NewDoubleArray,
2903 JNI::GetBooleanArrayElements,
2904 JNI::GetByteArrayElements,
2905 JNI::GetCharArrayElements,
2906 JNI::GetShortArrayElements,
2907 JNI::GetIntArrayElements,
2908 JNI::GetLongArrayElements,
2909 JNI::GetFloatArrayElements,
2910 JNI::GetDoubleArrayElements,
2911 JNI::ReleaseBooleanArrayElements,
2912 JNI::ReleaseByteArrayElements,
2913 JNI::ReleaseCharArrayElements,
2914 JNI::ReleaseShortArrayElements,
2915 JNI::ReleaseIntArrayElements,
2916 JNI::ReleaseLongArrayElements,
2917 JNI::ReleaseFloatArrayElements,
2918 JNI::ReleaseDoubleArrayElements,
2919 JNI::GetBooleanArrayRegion,
2920 JNI::GetByteArrayRegion,
2921 JNI::GetCharArrayRegion,
2922 JNI::GetShortArrayRegion,
2923 JNI::GetIntArrayRegion,
2924 JNI::GetLongArrayRegion,
2925 JNI::GetFloatArrayRegion,
2926 JNI::GetDoubleArrayRegion,
2927 JNI::SetBooleanArrayRegion,
2928 JNI::SetByteArrayRegion,
2929 JNI::SetCharArrayRegion,
2930 JNI::SetShortArrayRegion,
2931 JNI::SetIntArrayRegion,
2932 JNI::SetLongArrayRegion,
2933 JNI::SetFloatArrayRegion,
2934 JNI::SetDoubleArrayRegion,
2935 JNI::RegisterNatives,
2936 JNI::UnregisterNatives,
2937 JNI::MonitorEnter,
2938 JNI::MonitorExit,
2939 JNI::GetJavaVM,
2940 JNI::GetStringRegion,
2941 JNI::GetStringUTFRegion,
2942 JNI::GetPrimitiveArrayCritical,
2943 JNI::ReleasePrimitiveArrayCritical,
2944 JNI::GetStringCritical,
2945 JNI::ReleaseStringCritical,
2946 JNI::NewWeakGlobalRef,
2947 JNI::DeleteWeakGlobalRef,
2948 JNI::ExceptionCheck,
2949 JNI::NewDirectByteBuffer,
2950 JNI::GetDirectBufferAddress,
2951 JNI::GetDirectBufferCapacity,
2952 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002953};
2954
Elliott Hughes75770752011-08-24 17:52:38 -07002955JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002956 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002957 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002958 local_ref_cookie(IRT_FIRST_SEGMENT),
2959 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002960 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002961 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002962 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002963 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002964 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002965 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002966 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002967}
2968
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002969JNIEnvExt::~JNIEnvExt() {
2970}
2971
Mathieu Chartier590fee92013-09-13 13:46:47 -07002972jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2973 if (obj == nullptr) {
2974 return nullptr;
2975 }
2976 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2977}
2978
2979void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2980 if (obj != nullptr) {
2981 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2982 }
2983}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002984void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2985 check_jni = enabled;
2986 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002987}
2988
Elliott Hughes73e66f72012-05-09 09:34:45 -07002989void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2990 locals.Dump(os);
2991 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002992}
2993
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002994void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2995 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002996 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002997 stacked_local_ref_cookies.push_back(local_ref_cookie);
2998 local_ref_cookie = locals.GetSegmentState();
2999}
3000
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003001void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003002 locals.SetSegmentState(local_ref_cookie);
3003 local_ref_cookie = stacked_local_ref_cookies.back();
3004 stacked_local_ref_cookies.pop_back();
3005}
3006
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003007Offset JNIEnvExt::SegmentStateOffset() {
3008 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
3009 IndirectReferenceTable::SegmentStateOffset().Int32Value());
3010}
3011
Carl Shapiroea4dca82011-08-01 13:45:38 -07003012// JNI Invocation interface.
3013
Brian Carlstrombddf9762013-05-14 11:35:37 -07003014extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003015 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07003016 if (IsBadJniVersion(args->version)) {
3017 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003018 return JNI_EVERSION;
3019 }
Ian Rogerse63db272014-07-15 15:36:11 -07003020 RuntimeOptions options;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003021 for (int i = 0; i < args->nOptions; ++i) {
3022 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003023 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003024 }
3025 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003026 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003027 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003028 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003029 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003030 bool started = runtime->Start();
3031 if (!started) {
3032 delete Thread::Current()->GetJniEnv();
3033 delete runtime->GetJavaVM();
3034 LOG(WARNING) << "CreateJavaVM failed";
3035 return JNI_ERR;
3036 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003037 *p_env = Thread::Current()->GetJniEnv();
3038 *p_vm = runtime->GetJavaVM();
3039 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003040}
3041
Elliott Hughesf2682d52011-08-15 16:37:04 -07003042extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003043 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003044 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003045 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003046 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003047 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003048 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003049 }
3050 return JNI_OK;
3051}
3052
3053// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003054extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003055 return JNI_ERR;
3056}
3057
Elliott Hughescdf53122011-08-19 15:46:09 -07003058class JII {
3059 public:
3060 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003061 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003062 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003063 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003064 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3065 delete raw_vm->runtime;
3066 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003067 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003068
Elliott Hughescdf53122011-08-19 15:46:09 -07003069 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003070 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003071 }
3072
3073 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003074 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003075 }
3076
3077 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003078 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003079 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003080 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003081 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3082 Runtime* runtime = raw_vm->runtime;
3083 runtime->DetachCurrentThread();
3084 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003085 }
3086
3087 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003088 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3089 // and unlike other calls that take a JNI version doesn't care if you supply
3090 // JNI_VERSION_1_1, which we don't otherwise support.
3091 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003092 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003093 return JNI_EVERSION;
3094 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003095 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003096 return JNI_ERR;
3097 }
3098 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003099 if (thread == nullptr) {
3100 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003101 return JNI_EDETACHED;
3102 }
3103 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003104 return JNI_OK;
3105 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003106};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003107
Elliott Hughes88c5c352012-03-15 18:49:48 -07003108const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003109 nullptr, // reserved0
3110 nullptr, // reserved1
3111 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003112 JII::DestroyJavaVM,
3113 JII::AttachCurrentThread,
3114 JII::DetachCurrentThread,
3115 JII::GetEnv,
3116 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003117};
3118
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003119JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003120 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003121 check_jni_abort_hook(nullptr),
3122 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003123 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003124 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003125 trace(options->jni_trace_),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003126 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003127 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003128 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003129 libraries(new Libraries),
3130 weak_globals_lock_("JNI weak global reference table lock"),
3131 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3132 allow_new_weak_globals_(true),
3133 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003134 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003135 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003136 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003137 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003138}
3139
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003140JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003141 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003142}
3143
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003144jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3145 if (obj == nullptr) {
3146 return nullptr;
3147 }
3148 MutexLock mu(self, weak_globals_lock_);
3149 while (UNLIKELY(!allow_new_weak_globals_)) {
3150 weak_globals_add_condition_.WaitHoldingLocks(self);
3151 }
3152 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3153 return reinterpret_cast<jweak>(ref);
3154}
3155
3156void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3157 MutexLock mu(self, weak_globals_lock_);
3158 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3159 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3160 << "failed to find entry";
3161 }
3162}
3163
Elliott Hughes88c5c352012-03-15 18:49:48 -07003164void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3165 check_jni = enabled;
3166 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003167}
3168
Elliott Hughesae80b492012-04-24 10:43:17 -07003169void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3170 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3171 if (force_copy) {
3172 os << " (with forcecopy)";
3173 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003174 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003175 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003176 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003177 os << "; globals=" << globals.Capacity();
3178 }
3179 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003180 MutexLock mu(self, weak_globals_lock_);
3181 if (weak_globals_.Capacity() > 0) {
3182 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003183 }
3184 }
3185 os << '\n';
3186
3187 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003188 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003189 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3190 }
3191}
3192
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003193void JavaVMExt::DisallowNewWeakGlobals() {
3194 MutexLock mu(Thread::Current(), weak_globals_lock_);
3195 allow_new_weak_globals_ = false;
3196}
3197
3198void JavaVMExt::AllowNewWeakGlobals() {
3199 Thread* self = Thread::Current();
3200 MutexLock mu(self, weak_globals_lock_);
3201 allow_new_weak_globals_ = true;
3202 weak_globals_add_condition_.Broadcast(self);
3203}
3204
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003205mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3206 MutexLock mu(self, weak_globals_lock_);
3207 while (UNLIKELY(!allow_new_weak_globals_)) {
3208 weak_globals_add_condition_.WaitHoldingLocks(self);
3209 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003210 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003211}
3212
Elliott Hughes73e66f72012-05-09 09:34:45 -07003213void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003214 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003215 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003216 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003217 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003218 }
3219 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003220 MutexLock mu(self, weak_globals_lock_);
3221 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003222 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003223}
3224
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003225bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003226 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003227 std::string* detail) {
3228 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003229
3230 // See if we've already loaded this library. If we have, and the class loader
3231 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003232 // TODO: for better results we should canonicalize the pathname (or even compare
3233 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003234 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003235 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003236 {
3237 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003238 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003239 library = libraries->Get(path);
3240 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003241 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003242 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003243 // The library will be associated with class_loader. The JNI
3244 // spec says we can't load the same library into more than one
3245 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003246 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003247 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003248 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003249 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003250 return false;
3251 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003252 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003253 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003254 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003255 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003256 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003257 return false;
3258 }
3259 return true;
3260 }
3261
3262 // Open the shared library. Because we're using a full path, the system
3263 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3264 // resolve this library's dependencies though.)
3265
3266 // Failures here are expected when java.library.path has several entries
3267 // and we have to hunt for the lib.
3268
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003269 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3270 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3271 // dlopen) becomes zero from dlclose.
3272
Elliott Hughescdf53122011-08-19 15:46:09 -07003273 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003274 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003275 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Yong WU355383f2014-07-24 21:32:15 +08003276 const char* path_str = path.empty() ? nullptr : path.c_str();
3277 void* handle = dlopen(path_str, RTLD_LAZY);
3278 bool needs_native_bridge = false;
3279 if (handle == nullptr) {
Calin Juravle93de4272014-08-12 20:55:20 +01003280 if (android::NativeBridgeIsSupported(path_str)) {
3281 handle = android::NativeBridgeLoadLibrary(path_str, RTLD_LAZY);
Yong WU355383f2014-07-24 21:32:15 +08003282 needs_native_bridge = true;
3283 }
3284 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003285 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003286
Elliott Hughes84b2f142012-09-27 09:16:28 -07003287 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003288
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003289 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003290 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003291 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003292 return false;
3293 }
3294
3295 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003296 // TODO: move the locking (and more of this logic) into Libraries.
3297 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003298 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003299 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003300 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003301 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003302 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003303 libraries->Put(path, library);
3304 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003305 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003306 }
3307 if (!created_library) {
3308 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003309 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003310 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003311 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003312
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003313 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003314 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003315
Elliott Hughes79353722013-08-02 16:52:18 -07003316 bool was_successful = false;
Yong WU355383f2014-07-24 21:32:15 +08003317 void* sym = nullptr;
3318 if (UNLIKELY(needs_native_bridge)) {
3319 library->SetNeedsNativeBridge();
3320 sym = library->FindSymbolWithNativeBridge("JNI_OnLoad", nullptr);
3321 } else {
3322 sym = dlsym(handle, "JNI_OnLoad");
3323 }
3324
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003325 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003326 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003327 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003328 } else {
3329 // Call JNI_OnLoad. We have to override the current class
3330 // loader, which will always be "null" since the stuff at the
3331 // top of the stack is around Runtime.loadLibrary(). (See
3332 // the comments in the JNI FindClass function.)
3333 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3334 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003335 StackHandleScope<1> hs(self);
3336 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3337 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003338
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003339 int version = 0;
3340 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003341 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003342 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003343 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003344 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003345
Mathieu Chartier1f242962014-10-15 16:59:47 -07003346 if (runtime->GetTargetSdkVersion() != 0 && runtime->GetTargetSdkVersion() <= 21) {
3347 fault_manager.EnsureArtActionInFrontOfSignalChain();
3348 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003349 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003350
Elliott Hughes79353722013-08-02 16:52:18 -07003351 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003352 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003353 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003354 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003355 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003356 // It's unwise to call dlclose() here, but we can mark it
3357 // as bad and ensure that future load attempts will fail.
3358 // We don't know how far JNI_OnLoad got, so there could
3359 // be some partially-initialized stuff accessible through
3360 // newly-registered native method calls. We could try to
3361 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003362 } else {
3363 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003364 }
Elliott Hughes79353722013-08-02 16:52:18 -07003365 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003366 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003367 }
3368
Elliott Hughes79353722013-08-02 16:52:18 -07003369 library->SetResult(was_successful);
3370 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003371}
3372
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003373void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003374 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003375 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003376 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003377 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003378 c = EnsureInitialized(Thread::Current(), c);
3379 if (c == nullptr) {
3380 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003381 }
3382 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003383 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003384 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003385 std::string detail;
3386 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003387 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003388 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003389 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003390 native_method = libraries->FindNativeMethod(m, detail);
3391 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003392 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003393 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003394 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3395 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003396 }
3397 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003398}
3399
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003400void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003401 MutexLock mu(Thread::Current(), weak_globals_lock_);
3402 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003403 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003404 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003405 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003406 if (new_obj == nullptr) {
3407 new_obj = kClearedJniWeakGlobal;
3408 }
3409 *entry = new_obj;
3410 }
3411}
3412
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003413void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003414 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003415 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003416 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier12f74232015-01-14 14:55:47 -08003417 globals.VisitRoots(callback, arg, RootInfo(kRootJNIGlobal));
Elliott Hughes410c0c82011-09-01 17:58:25 -07003418 }
3419 {
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003420 MutexLock mu(self, libraries_lock);
3421 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003422 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003423 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003424 // The weak_globals table is visited by the GC itself (because it mutates the table).
3425}
3426
Elliott Hughesc8fece32013-01-02 11:27:23 -08003427void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003428 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003429 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003430 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003431 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3432 }
3433 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3434}
3435
Ian Rogersdf20fe02011-07-20 20:34:16 -07003436} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003437
3438std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3439 switch (rhs) {
3440 case JNIInvalidRefType:
3441 os << "JNIInvalidRefType";
3442 return os;
3443 case JNILocalRefType:
3444 os << "JNILocalRefType";
3445 return os;
3446 case JNIGlobalRefType:
3447 os << "JNIGlobalRefType";
3448 return os;
3449 case JNIWeakGlobalRefType:
3450 os << "JNIWeakGlobalRefType";
3451 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003452 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003453 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003454 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003455 }
3456}